Permalink
Browse files

Add DSL for association eager-loading

Closes #1325, Closes #1342, Closes #1434, Closes #2649, Refs. #2682
1 parent b2bae25 commit b92a4d1a30b706d08df2aee4d0a59dad698a0552 @mshibuya mshibuya committed Sep 24, 2016
View
3 CHANGELOG.md
@@ -4,6 +4,9 @@
[Full Changelog](https://github.com/sferik/rails_admin/compare/v1.0.0...HEAD)
+### Added
+- DSL for association eager-loading([#1325](https://github.com/sferik/rails_admin/issues/1325), [#1342](https://github.com/sferik/rails_admin/issues/1342))
+
## [1.0.0](https://github.com/sferik/rails_admin/tree/v1.0.0) - 2016-09-19
View
2 app/controllers/rails_admin/main_controller.rb
@@ -120,7 +120,7 @@ def check_for_cancel
end
def get_collection(model_config, scope, pagination)
- associations = model_config.list.fields.select { |f| f.type == :belongs_to_association && !f.polymorphic? }.collect { |f| f.association.name }
+ associations = model_config.list.fields.select { |f| f.try(:eager_load?) }.collect { |f| f.association.name }
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: associations) unless associations.blank?
View
4 lib/rails_admin/config/fields/association.rb
@@ -62,6 +62,10 @@ def association
association.foreign_key_nullable?
end
+ register_instance_option :eager_load? do
+ !!searchable
+ end
+
# Reader for the association's child model's configuration
def associated_model_config
@associated_model_config ||= RailsAdmin.config(association.klass)
View
4 lib/rails_admin/config/fields/types/belongs_to_association.rb
@@ -31,6 +31,10 @@ class BelongsToAssociation < RailsAdmin::Config::Fields::Association
true
end
+ register_instance_option :eager_load? do
+ true
+ end
+
def selected_id
bindings[:object].send(foreign_key)
end
View
4 lib/rails_admin/config/fields/types/polymorphic_association.rb
@@ -45,6 +45,10 @@ class PolymorphicAssociation < RailsAdmin::Config::Fields::Types::BelongsToAssoc
[children_fields]
end
+ register_instance_option :eager_load? do
+ false
+ end
+
def associated_collection(type)
return [] if type.blank?
config = RailsAdmin.config(type)
View
21 spec/controllers/rails_admin/main_controller_spec.rb
@@ -233,6 +233,27 @@ def get(action, params)
end
end
+ describe '#get_collection' do
+ before do
+ @team = FactoryGirl.create(:team)
+ controller.params = {model_name: 'teams'}
+ RailsAdmin.config Team do
+ field :players do
+ eager_load true
+ end
+ end
+ @model_config = RailsAdmin.config(Team)
+ end
+
+ it 'performs eager-loading for an association field with `eagar_load true`' do
+ scope = double('scope')
+ abstract_model = @model_config.abstract_model
+ allow(@model_config).to receive(:abstract_model).and_return(abstract_model)
+ expect(abstract_model).to receive(:all).with(hash_including(include: [:players]), scope).once
+ controller.send(:get_collection, @model_config, scope, false)
+ end
+ end
+
describe 'index' do
it "uses source association's primary key with :compact, not target model's default primary key", skip_mongoid: true do
class TeamWithNumberedPlayers < Team
View
18 spec/integration/basic/list/rails_admin_basic_list_spec.rb
@@ -64,6 +64,7 @@
FactoryGirl.create(:player, retired: false, injured: true, team: @teams[1]),
FactoryGirl.create(:player, retired: false, injured: false, team: @teams[1]),
]
+ @comment = FactoryGirl.create(:comment, commentable: @players[2])
end
it 'allows to query on any attribute' do
@@ -270,6 +271,23 @@
is_expected.to have_no_content(@players[3].name)
end
+ it 'allows to search a has_many attribute over the target table' do
+ RailsAdmin.config Player do
+ list do
+ field PK_COLUMN
+ field :name
+ field :comments do
+ searchable :content
+ end
+ end
+ end
+ visit index_path(model_name: 'player', f: {comments: {'1' => {v: @comment.content}}})
+ is_expected.to have_no_content(@players[0].name)
+ is_expected.to have_no_content(@players[1].name)
+ is_expected.to have_content(@players[2].name)
+ is_expected.to have_no_content(@players[3].name)
+ end
+
it 'displays base filters when no filters are present in the params' do
RailsAdmin.config Player do
list { filters([:name, :team]) }

0 comments on commit b92a4d1

Please sign in to comment.