Skip to content

Commit

Permalink
Merge 604fb72 into a2fd16b
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchkettzend committed Nov 12, 2020
2 parents a2fd16b + 604fb72 commit 85446b6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/helpers/rails_admin/main_helper.rb
Expand Up @@ -76,7 +76,7 @@ def ordered_filter_options
options[:type] = field.type
options[:value] = filter_hash['v']
options[:label] = field.label
options[:operator] = filter_hash['o']
options[:operator] = filter_hash['o'] || field.default_filter_operator
options
end if ordered_filters
end
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_admin/config/fields/base.rb
Expand Up @@ -227,6 +227,10 @@ def virtual?
bindings[:view].render partial: "rails_admin/main/#{partial}", locals: {field: self, form: bindings[:form]}
end

register_instance_option :default_filter_operator do
nil
end

def editable?
!(@properties && @properties.read_only?)
end
Expand Down
8 changes: 7 additions & 1 deletion spec/integration/actions/index_spec.rb
Expand Up @@ -283,6 +283,12 @@
it 'displays base filters when no filters are present in the params' do
RailsAdmin.config Player do
list { filters([:name, :team]) }
field :name do
default_filter_operator 'is'
end
field :team do
filterable true
end
end
visit index_path(model_name: 'player')

Expand All @@ -293,7 +299,7 @@
name: 'name',
type: 'string',
value: '',
operator: nil,
operator: 'is',
},
{
index: 2,
Expand Down
17 changes: 17 additions & 0 deletions spec/rails_admin/config/fields/base_spec.rb
Expand Up @@ -556,4 +556,21 @@ class FieldVisibilityTest < Tableless
expect(RailsAdmin.config(Team).field(:name).allowed_methods).to eq [:name]
end
end

describe '#default_filter_operator' do
it 'has a default and be user customizable' do
RailsAdmin.config Team do
list do
field :division
field :name do
default_filter_operator 'is'
end
end
end
name_field = RailsAdmin.config('Team').list.fields.detect { |f| f.name == :name }
expect(name_field.default_filter_operator).to eq('is') # custom via user specification
division_field = RailsAdmin.config('Team').list.fields.detect { |f| f.name == :division }
expect(division_field.default_filter_operator).to be nil # rails_admin generic fallback
end
end
end

0 comments on commit 85446b6

Please sign in to comment.