Skip to content

Commit

Permalink
Merge pull request #2680 from tiii/support-active-record-enum-string-…
Browse files Browse the repository at this point in the history
…columns

Support for ActiveRecord:Enum using string columns
  • Loading branch information
mshibuya authored Aug 13, 2016
2 parents df3b30c + 91bc566 commit 36d5085
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 5 deletions.
14 changes: 13 additions & 1 deletion lib/rails_admin/config/fields/types/active_record_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def type
end

def parse_value(value)
value.present? ? enum.invert[value.to_i] : nil
value.present? ? enum.invert[type_cast_value(value)] : nil
end

def parse_input(params)
Expand All @@ -38,6 +38,18 @@ def parse_input(params)
def form_value
::Rails.version >= '5' ? enum[super] : super
end

private

def type_cast_value(value)
if ::Rails.version >= '5'
abstract_model.model.attribute_types[name].cast(value)
elsif ::Rails.version >= '4.2'
abstract_model.model.column_types[name].type_cast_from_user(value)
else
abstract_model.model.column_types[name.to_s].type_cast(value)
end
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy_app/app/active_record/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Player < ActiveRecord::Base
record.errors.add(:base, 'Player is cheating') if value.to_s =~ /on steroids/
end

if ::Rails.version >= '4.1'
enum formation: {start: 'start', substitute: 'substitute'}
end

before_destroy :destroy_hook

def destroy_hook; end
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy_app/app/active_record/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Team < ActiveRecord::Base
validates_numericality_of :revenue, allow_nil: true
belongs_to :division

if ::Rails.version >= '4.1'
enum main_sponsor: [:no_sponsor, :food_factory, :transportation_company, :bank, :energy_producer]
end

def player_names_truncated
players.collect(&:name).join(', ')[0..32]
end
Expand Down
1 change: 1 addition & 0 deletions spec/dummy_app/app/locales/models.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ en:
name: Their Name
team:
manager: Team Manager
main_sponsor: Main Sponsor
fans: Some Fans
mongoid:
*en_attributes
Expand Down
1 change: 1 addition & 0 deletions spec/dummy_app/app/mongoid/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Player
field :born_on, type: Date
field :notes, type: String
field :suspended, type: Boolean, default: false
field :formation, type: String

validates_presence_of(:name)
validates_numericality_of(:number, only_integer: true)
Expand Down
1 change: 1 addition & 0 deletions spec/dummy_app/app/mongoid/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Team
field :revenue, type: BigDecimal
field :color, type: String
field :custom_field, type: String
field :main_sponsor, type: Integer

has_many :players, inverse_of: :team, order: :_id.asc
has_and_belongs_to_many :fans
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMainSponsorToTeams < MigrationBase
def change
add_column :teams, :main_sponsor, :integer, default: 0, null: false
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFormationToPlayers < MigrationBase
def change
add_column :players, :formation, :string, default: 'substitute', null: false
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
click_button 'Export to csv'
csv = CSV.parse page.driver.response.body.force_encoding('utf-8') # comes through as us-ascii on some platforms
expect(csv[0]).to match_array ['Id', 'Created at', 'Updated at', 'Deleted at', 'Name', 'Position',
'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Id [Team]', 'Created at [Team]',
'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Id [Team]', 'Created at [Team]',
'Updated at [Team]', 'Name [Team]', 'Logo url [Team]', 'Team Manager [Team]', 'Ballpark [Team]',
'Mascot [Team]', 'Founded [Team]', 'Wins [Team]', 'Losses [Team]', 'Win percentage [Team]',
'Revenue [Team]', 'Color [Team]', 'Custom field [Team]', 'Id [Draft]', 'Created at [Draft]',
'Revenue [Team]', 'Color [Team]', 'Custom field [Team]', 'Main Sponsor [Team]', 'Id [Draft]', 'Created at [Draft]',
'Updated at [Draft]', 'Date [Draft]', 'Round [Draft]', 'Pick [Draft]', 'Overall [Draft]',
'College [Draft]', 'Notes [Draft]', 'Id [Comments]', 'Content [Comments]', 'Created at [Comments]',
'Updated at [Comments]']
Expand Down
4 changes: 2 additions & 2 deletions spec/rails_admin/config/sections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@
expect(RailsAdmin.config(Team).edit.visible_groups.collect { |g| g.visible_fields.collect(&:name) }).to eq([[:name], [:founded, :wins]])
expect(RailsAdmin.config(Team).create.visible_groups.collect { |g| g.visible_fields.collect(&:name) }).to eq([[:name], [:founded, :wins]])
expect(RailsAdmin.config(Team).update.visible_groups.collect { |g| g.visible_fields.collect(&:name) }).to eq([[:name], [:founded], [:wins], [:losses]])
expect(RailsAdmin.config(Team).visible_groups.collect { |g| g.visible_fields.collect(&:name) }.flatten.count).to eq(19)
expect(RailsAdmin.config(Team).export.visible_groups.collect { |g| g.visible_fields.collect(&:name) }.flatten.count).to eq(19)
expect(RailsAdmin.config(Team).visible_groups.collect { |g| g.visible_fields.collect(&:name) }.flatten.count).to eq(20)
expect(RailsAdmin.config(Team).export.visible_groups.collect { |g| g.visible_fields.collect(&:name) }.flatten.count).to eq(20)
end
end
end

0 comments on commit 36d5085

Please sign in to comment.