From d62f604cc8d7d1434f7dfe0c5aca3aaf3dc2547b Mon Sep 17 00:00:00 2001 From: Mitsuhiro Shibuya Date: Sat, 8 Jun 2024 18:45:50 +0900 Subject: [PATCH] Fix enum filter breaking when pre-populated Fixes #3651 --- app/helpers/rails_admin/main_helper.rb | 2 +- spec/integration/widgets/filter_box_spec.rb | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/helpers/rails_admin/main_helper.rb b/app/helpers/rails_admin/main_helper.rb index d80f4e4b09..3f7540b872 100644 --- a/app/helpers/rails_admin/main_helper.rb +++ b/app/helpers/rails_admin/main_helper.rb @@ -45,7 +45,7 @@ def ordered_filter_options filter_for_field = duplet[1] filter_name = filter_for_field.keys.first filter_hash = filter_for_field.values.first - unless (field = filterable_fields.find { |f| f.name == filter_name.to_sym }) + unless (field = filterable_fields.find { |f| f.name == filter_name.to_sym }&.with({view: self})) raise "#{filter_name} is not currently filterable; filterable fields are #{filterable_fields.map(&:name).join(', ')}" end diff --git a/spec/integration/widgets/filter_box_spec.rb b/spec/integration/widgets/filter_box_spec.rb index 8f66e8da7f..27a2a89a35 100644 --- a/spec/integration/widgets/filter_box_spec.rb +++ b/spec/integration/widgets/filter_box_spec.rb @@ -157,7 +157,7 @@ describe 'for enum field' do before do RailsAdmin.config Team do - field :color + field :color, :enum end end @@ -170,6 +170,15 @@ expect(find('#filters_box select')['multiple']).to be true expect(find('#filters_box select')['name']).to match(/\[\]$/) end + + context 'with the filter pre-populated' do + it 'does not break' do + visit index_path(model_name: 'team', f: {color: {'1' => {v: 'red'}}}) + is_expected.to have_css('.filter select[name^="f[color]"]') + expect(find('.filter select[name^="f[color]"]').value).to eq 'red' + expect(all('#filters_box option').map(&:text)).to include 'white', 'black', 'red', 'green', 'blué' + end + end end describe 'for time field', active_record: true do