diff --git a/lib/rails_admin/config/fields/types/enum.rb b/lib/rails_admin/config/fields/types/enum.rb index 961e6ff48..5f05af7cc 100644 --- a/lib/rails_admin/config/fields/types/enum.rb +++ b/lib/rails_admin/config/fields/types/enum.rb @@ -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 diff --git a/spec/rails_admin/config/fields/types/enum_spec.rb b/spec/rails_admin/config/fields/types/enum_spec.rb index 6bee0de9b..3ea169c8a 100644 --- a/spec/rails_admin/config/fields/types/enum_spec.rb +++ b/spec/rails_admin/config/fields/types/enum_spec.rb @@ -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 @@ -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 @@ -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 @@ -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