Skip to content

Commit 8040966

Browse files
committed
Fix calls to Fields::Types::Enum#enum_method when no bindings provided
Similar to `Fields::Types::Enum#enum` the code path using `abstract_model` was untested and the implementation did not account for the possibility that the `bindings` may be nil.
1 parent ee60311 commit 8040966

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/rails_admin/config/fields/types/enum.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Enum < RailsAdmin::Config::Fields::Base
2121
end
2222

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

2727
register_instance_option :enum do

spec/rails_admin/config/fields/types/enum_spec.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717
end
1818
end
1919

20-
it 'auto-detects enumeration' do
20+
it 'auto-detects enumeration when bindings provide object' do
2121
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
2222
is_expected.not_to be_multiple
2323
expect(subject.with(object: Team.new).enum).to eq %w[blue green red]
2424
end
25+
26+
it 'auto-detects enumeration when no bindings provided' do
27+
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
28+
is_expected.not_to be_multiple
29+
expect(subject.enum).to eq %w[blue green red]
30+
end
2531
end
2632

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

42-
it 'auto-detects enumeration' do
48+
it 'auto-detects enumeration when bindings provide object' do
4349
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
4450
expect(subject.with(object: Team.new).enum).to eq %w[blue green red]
4551
end
52+
53+
it 'auto-detects enumeration when no bindings provided' do
54+
is_expected.to be_a(RailsAdmin::Config::Fields::Types::Enum)
55+
expect(subject.enum).to eq %w[blue green red]
56+
end
4657
end
4758

4859
describe 'the enum instance method' do

0 commit comments

Comments
 (0)