Skip to content

Commit

Permalink
Merge pull request #3318 from mitchkettzend/default_filter_operator
Browse files Browse the repository at this point in the history
Enhancement: Default filter operator for fields
  • Loading branch information
mshibuya committed Feb 28, 2021
2 parents 54fb4f2 + cd955e5 commit 3d8cb28
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/helpers/rails_admin/main_helper.rb
Original file line number Diff line number Diff line change
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[:required] = field.required
options
end if ordered_filters
Expand Down
2 changes: 1 addition & 1 deletion app/views/rails_admin/main/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
- else
- ''
%li
%a{href: '#', :"data-field-label" => field.label, :"data-field-name" => field.name, :"data-field-options" => field_options.html_safe, :"data-field-required" => field.required.to_s, :"data-field-type" => field.type, :"data-field-value" => "", :"data-field-datetimepicker-format" => (field.try(:parser) && field.parser.to_momentjs)}= capitalize_first_letter(field.label)
%a{href: '#', :"data-field-label" => field.label, :"data-field-name" => field.name, :"data-field-operator" => field.default_filter_operator, :"data-field-options" => field_options.html_safe, :"data-field-required" => field.required.to_s, :"data-field-type" => field.type, :"data-field-value" => "", :"data-field-datetimepicker-format" => (field.try(:parser) && field.parser.to_momentjs)}= capitalize_first_letter(field.label)
%style
- properties.select{ |p| p.column_width.present? }.each do |property|
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_admin/config/fields/base.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,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 @@ -314,7 +320,7 @@
name: 'name',
type: 'string',
value: '',
operator: nil,
operator: 'is',
required: true,
},
{
Expand Down
17 changes: 17 additions & 0 deletions spec/rails_admin/config/fields/base_spec.rb
Original file line number Diff line number Diff line change
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 3d8cb28

Please sign in to comment.