Skip to content

Commit

Permalink
Limit children for deletion notice
Browse files Browse the repository at this point in the history
  • Loading branch information
jibidus committed Nov 27, 2015
1 parent 2a79811 commit 5a93245
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
23 changes: 14 additions & 9 deletions app/views/rails_admin/main/_delete_notice.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
- else
= wording
%ul
- @abstract_model.each_associated_children(object) do |association, child|
%li
- child_config = RailsAdmin.config(child)
= @abstract_model.model.human_attribute_name association.name
- wording = child.send(child_config.object_label_method)
- if child.id && (show_action = action(:show, child_config.abstract_model, child))
= link_to(wording, url_for(action: show_action.action_name, model_name: child_config.abstract_model.to_param, id: child.id), class: 'pjax')
- else
= wording
- @abstract_model.each_associated_children(object) do |association, children|
- humanized_association = @abstract_model.model.human_attribute_name association.name
- limit = children.count > 12 ? 10 : children.count
- children.first(limit).each do |child|
= content_tag_for :li, child do
- child_config = RailsAdmin.config(child)
= humanized_association.singularize
- wording = child.send(child_config.object_label_method)
- if child.id && (show_action = action(:show, child_config.abstract_model, child))
= link_to(wording, url_for(action: show_action.action_name, model_name: child_config.abstract_model.to_param, id: child.id), class: 'pjax')
- else
= wording
- if children.count > limit
%li= t('admin.misc.more', count: children.count - limit, models_name: humanized_association)
1 change: 1 addition & 0 deletions config/locales/rails_admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ en:
navigation_static_label: "Links"
log_out: "Log out"
ago: "ago"
more: "Plus %{count} more %{models_name}"
flash:
successful: "%{name} successfully %{action}"
error: "%{name} failed to be %{action}"
Expand Down
7 changes: 3 additions & 4 deletions lib/rails_admin/abstract_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ def each_associated_children(object)
case association.type
when :has_one
if child = object.send(association.name)
yield(association, child)
yield(association, [child])
end
when :has_many
object.send(association.name).each do |child| # rubocop:disable ShadowingOuterLocalVariable
yield(association, child)
end
children = object.send(association.name)
yield(association, children)
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/integration/basic/delete/rails_admin_basic_delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,18 @@
is_expected.to have_link(@player.name, href: "/admin/player/#{@player.id}")
end
end

describe 'delete an object which has many associated item' do
before do
comments = FactoryGirl.create_list :comment, 20
@player = FactoryGirl.create :player, comments: comments
visit delete_path(model_name: 'player', id: @player.id)
end

it "shows only ten first plus x mores" do
is_expected.to have_selector('.comment', count: 10)
is_expected.to have_content('Plus 10 more Comments')
end

end
end
21 changes: 21 additions & 0 deletions spec/rails_admin/abstract_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,25 @@
@abstract_model.all(sort: PK_COLUMN, page: 1, per: 2)
end
end

describe 'each_associated_children' do
before do
@abstract_model = RailsAdmin::AbstractModel.new('Player')
@draft = FactoryGirl.build :draft
@comments = FactoryGirl.build_list :comment, 2
@player = FactoryGirl.build :player, draft: @draft, comments: @comments
end

it 'should return has_one and has_many associations with its children' do
@abstract_model.each_associated_children(@player) do |association, children|
expect(children).to eq case association.name
when :draft
[@draft]
when :comments
@comments
end
end
end

end
end

0 comments on commit 5a93245

Please sign in to comment.