Skip to content

Commit 5a93245

Browse files
committed
Limit children for deletion notice
1 parent 2a79811 commit 5a93245

5 files changed

Lines changed: 53 additions & 13 deletions

File tree

app/views/rails_admin/main/_delete_notice.html.haml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
- else
99
= wording
1010
%ul
11-
- @abstract_model.each_associated_children(object) do |association, child|
12-
%li
13-
- child_config = RailsAdmin.config(child)
14-
= @abstract_model.model.human_attribute_name association.name
15-
- wording = child.send(child_config.object_label_method)
16-
- if child.id && (show_action = action(:show, child_config.abstract_model, child))
17-
= link_to(wording, url_for(action: show_action.action_name, model_name: child_config.abstract_model.to_param, id: child.id), class: 'pjax')
18-
- else
19-
= wording
11+
- @abstract_model.each_associated_children(object) do |association, children|
12+
- humanized_association = @abstract_model.model.human_attribute_name association.name
13+
- limit = children.count > 12 ? 10 : children.count
14+
- children.first(limit).each do |child|
15+
= content_tag_for :li, child do
16+
- child_config = RailsAdmin.config(child)
17+
= humanized_association.singularize
18+
- wording = child.send(child_config.object_label_method)
19+
- if child.id && (show_action = action(:show, child_config.abstract_model, child))
20+
= link_to(wording, url_for(action: show_action.action_name, model_name: child_config.abstract_model.to_param, id: child.id), class: 'pjax')
21+
- else
22+
= wording
23+
- if children.count > limit
24+
%li= t('admin.misc.more', count: children.count - limit, models_name: humanized_association)

config/locales/rails_admin.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ en:
4444
navigation_static_label: "Links"
4545
log_out: "Log out"
4646
ago: "ago"
47+
more: "Plus %{count} more %{models_name}"
4748
flash:
4849
successful: "%{name} successfully %{action}"
4950
error: "%{name} failed to be %{action}"

lib/rails_admin/abstract_model.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ def each_associated_children(object)
8787
case association.type
8888
when :has_one
8989
if child = object.send(association.name)
90-
yield(association, child)
90+
yield(association, [child])
9191
end
9292
when :has_many
93-
object.send(association.name).each do |child| # rubocop:disable ShadowingOuterLocalVariable
94-
yield(association, child)
95-
end
93+
children = object.send(association.name)
94+
yield(association, children)
9695
end
9796
end
9897
end

spec/integration/basic/delete/rails_admin_basic_delete_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,18 @@
6060
is_expected.to have_link(@player.name, href: "/admin/player/#{@player.id}")
6161
end
6262
end
63+
64+
describe 'delete an object which has many associated item' do
65+
before do
66+
comments = FactoryGirl.create_list :comment, 20
67+
@player = FactoryGirl.create :player, comments: comments
68+
visit delete_path(model_name: 'player', id: @player.id)
69+
end
70+
71+
it "shows only ten first plus x mores" do
72+
is_expected.to have_selector('.comment', count: 10)
73+
is_expected.to have_content('Plus 10 more Comments')
74+
end
75+
76+
end
6377
end

spec/rails_admin/abstract_model_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,25 @@
7575
@abstract_model.all(sort: PK_COLUMN, page: 1, per: 2)
7676
end
7777
end
78+
79+
describe 'each_associated_children' do
80+
before do
81+
@abstract_model = RailsAdmin::AbstractModel.new('Player')
82+
@draft = FactoryGirl.build :draft
83+
@comments = FactoryGirl.build_list :comment, 2
84+
@player = FactoryGirl.build :player, draft: @draft, comments: @comments
85+
end
86+
87+
it 'should return has_one and has_many associations with its children' do
88+
@abstract_model.each_associated_children(@player) do |association, children|
89+
expect(children).to eq case association.name
90+
when :draft
91+
[@draft]
92+
when :comments
93+
@comments
94+
end
95+
end
96+
end
97+
98+
end
7899
end

0 commit comments

Comments
 (0)