Skip to content

Commit

Permalink
Merge pull request #2058 from quainjn/config-association-limits
Browse files Browse the repository at this point in the history
Be able to configure default association limit
  • Loading branch information
bbenezech committed Feb 4, 2016
2 parents d7cc0fe + d4897ff commit 12aeaf3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/rails_admin/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class << self
# been configured
attr_accessor :default_items_per_page

# Default association limit
attr_accessor :default_associated_collection_limit

attr_reader :default_search_operator

# Configuration option to specify which method names will be searched for
Expand Down Expand Up @@ -268,6 +271,7 @@ def reset
@default_hidden_fields[:edit] = [:id, :_id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on]
@default_hidden_fields[:show] = [:id, :_id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on]
@default_items_per_page = 20
@default_associated_collection_limit = 100
@default_search_operator = 'default'
@excluded_models = []
@included_models = []
Expand Down
6 changes: 5 additions & 1 deletion lib/rails_admin/config/fields/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def association # rubocop:disable TrivialAccessors
# preload entire associated collection (per associated_collection_scope) on load
# Be sure to set limit in associated_collection_scope if set is large
register_instance_option :associated_collection_cache_all do
@associated_collection_cache_all ||= (associated_model_config.abstract_model.count < 100)
@associated_collection_cache_all ||= (associated_model_config.abstract_model.count < associated_model_limit)
end

# determines whether association's elements can be removed
Expand Down Expand Up @@ -105,6 +105,10 @@ def multiple?
def virtual?
true
end

def associated_model_limit
RailsAdmin.config.default_associated_collection_limit
end
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/rails_admin/config/fields/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ class CommentReversed < Tableless
end
expect(RailsAdmin.config(Team).edit.fields.detect { |f| f.name == :players }.associated_collection_cache_all).to be_falsey
end

context "with custom configuration" do
before do
RailsAdmin.config.default_associated_collection_limit = 5
end
it 'defaults to true if associated collection count less than than limit' do
@players = 4.times.collect do
FactoryGirl.create :player
end
expect(RailsAdmin.config(Team).edit.fields.detect { |f| f.name == :players }.associated_collection_cache_all).to be_truthy
end

it 'defaults to false if associated collection count >= that limit' do
@players = 5.times.collect do
FactoryGirl.create :player
end
expect(RailsAdmin.config(Team).edit.fields.detect { |f| f.name == :players }.associated_collection_cache_all).to be_falsey
end
end
end

describe '#searchable_columns' do
Expand Down

0 comments on commit 12aeaf3

Please sign in to comment.