Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filter on enum value in list view v2 #3684

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/rails_admin/config/fields/types/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class Enum < RailsAdmin::Config::Fields::Base
end

register_instance_option :enum_method do
@enum_method ||= bindings[:object].class.respond_to?("#{name}_enum") || (bindings[:object] || abstract_model.model.new).respond_to?("#{name}_enum") ? "#{name}_enum" : name
@enum_method ||= bindings&.[](:object).class.respond_to?("#{name}_enum") || (bindings&.[](:object) || abstract_model.model.new).respond_to?("#{name}_enum") ? "#{name}_enum" : name
end

register_instance_option :enum do
if abstract_model.model.respond_to?(enum_method)
abstract_model.model.send(enum_method)
else
(bindings[:object] || abstract_model.model.new).send(enum_method)
(bindings&.[](:object) || abstract_model.model.new).send(enum_method)
end
end

Expand Down
29 changes: 25 additions & 4 deletions spec/rails_admin/config/fields/types/enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
end
end

it 'auto-detects enumeration' do
it 'auto-detects enumeration when bindings provide object' do
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
is_expected.not_to be_multiple
expect(subject.with(object: Team.new).enum).to eq %w[blue green red]
end

it 'auto-detects enumeration when no bindings provided' do
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
is_expected.not_to be_multiple
expect(subject.enum).to eq %w[blue green red]
end
end

describe "when class responds to '\#{method}_enum'" do
Expand All @@ -39,10 +45,15 @@ def color_enum
end
end

it 'auto-detects enumeration' do
it 'auto-detects enumeration when bindings provide object' do
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
expect(subject.with(object: Team.new).enum).to eq %w[blue green red]
end

it 'auto-detects enumeration when no bindings provided' do
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
expect(subject.enum).to eq %w[blue green red]
end
end

describe 'the enum instance method' do
Expand All @@ -63,10 +74,15 @@ def color_list
Team.send(:remove_method, :color_list)
end

it 'allows configuration' do
it 'allows configuration by enum_method when bindings provide object' do
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
expect(subject.with(object: Team.new).enum).to eq %w[blue green red]
end

it 'allows configuration by enum_method when no bindings provided' do
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
expect(subject.enum).to eq %w[blue green red]
end
end

describe 'the enum class method' do
Expand All @@ -87,10 +103,15 @@ def color_list
Team.instance_eval { undef :color_list }
end

it 'allows configuration' do
it 'allows configuration by enum_method when bindings provide object' do
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
expect(subject.with(object: Team.new).enum).to eq %w[blue green red]
end

it 'allows configuration by enum_method when no bindings provided' do
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
expect(subject.enum).to eq %w[blue green red]
end
end

describe 'when overriding enum configuration' do
Expand Down
Loading