Skip to content

Commit

Permalink
Fix export action didn't use export section for eager loading
Browse files Browse the repository at this point in the history
Fixes #1954
  • Loading branch information
mshibuya committed Aug 14, 2022
1 parent e8cb8ed commit 4cc3f30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/controllers/rails_admin/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def check_for_cancel
end

def get_collection(model_config, scope, pagination)
eager_loads = model_config.list.fields.flat_map(&:eager_load_values)
section = @action.key == :export ? model_config.export : model_config.list
eager_loads = section.fields.flat_map(&:eager_load_values)
options = {}
options = options.merge(page: (params[Kaminari.config.param_name] || 1).to_i, per: (params[:per] || model_config.list.items_per_page)) if pagination
options = options.merge(include: eager_loads) unless eager_loads.blank?
Expand Down
22 changes: 22 additions & 0 deletions spec/controllers/rails_admin/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def get(action, params)
super action, params: params
end

before do
controller.instance_variable_set :@action, RailsAdmin::Config::Actions.find(:index)
end

describe '#check_for_cancel' do
before do
allow(controller).to receive(:back_or_index) { raise StandardError.new('redirected back') }
Expand Down Expand Up @@ -266,6 +270,24 @@ def get(action, params)
expect(abstract_model).to receive(:all).with(hash_including(include: [{players: :draft}]), nil).once.and_call_original
controller.send(:get_collection, model_config, nil, false).to_a
end

context 'on export' do
before do
controller.instance_variable_set :@action, RailsAdmin::Config::Actions.find(:export)
end

it 'uses the export section' do
RailsAdmin.config Team do
export do
field :players do
eager_load true
end
end
end
expect(abstract_model).to receive(:all).with(hash_including(include: [:players]), nil).once.and_call_original
controller.send(:get_collection, model_config, nil, false).to_a
end
end
end

describe 'index' do
Expand Down

0 comments on commit 4cc3f30

Please sign in to comment.