Skip to content

Commit

Permalink
Be able to configure default association limit
Browse files Browse the repository at this point in the history
  • Loading branch information
quainjn committed Aug 29, 2014
1 parent a72340f commit d4897ff
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 @@ -261,6 +264,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

# Reader for the association's child model's configuration
Expand Down Expand Up @@ -100,6 +100,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 @@ -160,6 +160,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 d4897ff

Please sign in to comment.