diff --git a/lib/rails_admin/config/fields/factories/enum.rb b/lib/rails_admin/config/fields/factories/enum.rb index 87f45b6ea9..a92a2fed39 100644 --- a/lib/rails_admin/config/fields/factories/enum.rb +++ b/lib/rails_admin/config/fields/factories/enum.rb @@ -1,5 +1,6 @@ require 'rails_admin/config/fields' require 'rails_admin/config/fields/types/enum' +require 'rails_admin/config/fields/types/active_record_enum' RailsAdmin::Config::Fields.register_factory do |parent, properties, fields| model = parent.abstract_model.model @@ -11,6 +12,9 @@ model.method_defined?(method_name)) fields << RailsAdmin::Config::Fields::Types::Enum.new(parent, properties.name, properties) true + elsif model.respond_to?(:defined_enums) && model.defined_enums[properties.name.to_s] + fields << RailsAdmin::Config::Fields::Types::ActiveRecordEnum.new(parent, properties.name, properties) + true else false end diff --git a/lib/rails_admin/config/fields/types/active_record_enum.rb b/lib/rails_admin/config/fields/types/active_record_enum.rb new file mode 100644 index 0000000000..4d67ce10f9 --- /dev/null +++ b/lib/rails_admin/config/fields/types/active_record_enum.rb @@ -0,0 +1,41 @@ +require 'rails_admin/config/fields/types/enum' + +module RailsAdmin + module Config + module Fields + module Types + class ActiveRecordEnum < Enum + RailsAdmin::Config::Fields::Types.register(self) + + def type + :enum + end + + register_instance_option :enum do + abstract_model.model.defined_enums[name.to_s] + end + + register_instance_option :pretty_value do + bindings[:object].send(name).presence || ' - ' + end + + register_instance_option :multiple? do + false + end + + register_instance_option :queryable do + false + end + + def parse_input(params) + if params[name].present? + params[name] = enum.invert[params[name].to_i] + elsif params[name] + params[name] = nil + end + end + end + end + end + end +end diff --git a/lib/rails_admin/config/fields/types/all.rb b/lib/rails_admin/config/fields/types/all.rb index 9ca0e19293..771232aa0c 100644 --- a/lib/rails_admin/config/fields/types/all.rb +++ b/lib/rails_admin/config/fields/types/all.rb @@ -1,3 +1,4 @@ +require 'rails_admin/config/fields/types/active_record_enum' require 'rails_admin/config/fields/types/belongs_to_association' require 'rails_admin/config/fields/types/boolean' require 'rails_admin/config/fields/types/bson_object_id' diff --git a/spec/integration/config/edit/rails_admin_config_edit_spec.rb b/spec/integration/config/edit/rails_admin_config_edit_spec.rb index 9f8b91841e..275c05c868 100644 --- a/spec/integration/config/edit/rails_admin_config_edit_spec.rb +++ b/spec/integration/config/edit/rails_admin_config_edit_spec.rb @@ -963,7 +963,7 @@ def color_enum it 'auto-detects enumeration' do is_expected.to have_selector('.enum_type select') is_expected.not_to have_selector('.enum_type select[multiple]') - is_expected.to have_content('green') + expect(all('.enum_type option').map(&:text).select(&:present?)).to eq %w(blue green red) end end @@ -1129,6 +1129,39 @@ def color_enum end end + if defined?(ActiveRecord) && ActiveRecord::VERSION::STRING >= '4.1' + describe 'ActiveRecord::Enum support', active_record: true do + before do + class FieldTestWithEnum < FieldTest + self.table_name = 'field_tests' + enum integer_field: %w(foo bar) + end + RailsAdmin.config.included_models = [FieldTestWithEnum] + RailsAdmin.config FieldTestWithEnum do + edit do + field :integer_field + end + end + end + + after do + Object.send :remove_const, :FieldTestWithEnum + end + + it 'auto-detects enumeration' do + visit new_path(model_name: 'field_test_with_enum') + is_expected.to have_selector('.enum_type select') + is_expected.not_to have_selector('.enum_type select[multiple]') + expect(all('.enum_type option').map(&:text).select(&:present?)).to eq %w(foo bar) + end + + it 'shows current value as selected' do + visit edit_path(model_name: 'field_test_with_enum', id: FieldTestWithEnum.create(integer_field: 'bar')) + expect(find('.enum_type select').value).to eq '1' + end + end + end + describe 'ColorPicker Support' do it 'shows input with class color' do RailsAdmin.config Team do