Skip to content

Commit

Permalink
Add ActiveRecord::Enum support. Closes #1993
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Aug 15, 2015
1 parent 5d84252 commit 309a2d1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/rails_admin/config/fields/factories/enum.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions lib/rails_admin/config/fields/types/active_record_enum.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/rails_admin/config/fields/types/all.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
35 changes: 34 additions & 1 deletion spec/integration/config/edit/rails_admin_config_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 309a2d1

Please sign in to comment.