From b489a2f33ce36a9c36d236173a2cc2f436e6189d Mon Sep 17 00:00:00 2001 From: _tiii <_@tiii.de> Date: Thu, 28 Jul 2016 15:35:45 +0200 Subject: [PATCH 1/2] type cast enum values before getting their key. This adds support for string-based enum columns. --- .../config/fields/types/active_record_enum.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/rails_admin/config/fields/types/active_record_enum.rb b/lib/rails_admin/config/fields/types/active_record_enum.rb index 250e1e509b..5f14ac3375 100644 --- a/lib/rails_admin/config/fields/types/active_record_enum.rb +++ b/lib/rails_admin/config/fields/types/active_record_enum.rb @@ -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) @@ -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 From 91bc56646cd903574f14f9f524cad344cb15ed25 Mon Sep 17 00:00:00 2001 From: _tiii <_@tiii.de> Date: Thu, 28 Jul 2016 15:57:51 +0200 Subject: [PATCH 2/2] Add ActiveRecord::Enum columns to teams and players There are no tests for the active_record_enum type. Now the integration tests at least test them implicitly --- spec/dummy_app/app/active_record/player.rb | 4 ++++ spec/dummy_app/app/active_record/team.rb | 4 ++++ spec/dummy_app/app/locales/models.en.yml | 1 + spec/dummy_app/app/mongoid/player.rb | 1 + spec/dummy_app/app/mongoid/team.rb | 1 + .../db/migrate/20160728152942_add_main_sponsor_to_teams.rb | 5 +++++ .../db/migrate/20160728153058_add_formation_to_players.rb | 5 +++++ .../basic/export/rails_admin_basic_export_spec.rb | 4 ++-- spec/rails_admin/config/sections_spec.rb | 4 ++-- 9 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 spec/dummy_app/db/migrate/20160728152942_add_main_sponsor_to_teams.rb create mode 100644 spec/dummy_app/db/migrate/20160728153058_add_formation_to_players.rb diff --git a/spec/dummy_app/app/active_record/player.rb b/spec/dummy_app/app/active_record/player.rb index b3ac3cd49c..16dc16ffa0 100644 --- a/spec/dummy_app/app/active_record/player.rb +++ b/spec/dummy_app/app/active_record/player.rb @@ -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 diff --git a/spec/dummy_app/app/active_record/team.rb b/spec/dummy_app/app/active_record/team.rb index a5508331ed..e66e9ec551 100644 --- a/spec/dummy_app/app/active_record/team.rb +++ b/spec/dummy_app/app/active_record/team.rb @@ -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 diff --git a/spec/dummy_app/app/locales/models.en.yml b/spec/dummy_app/app/locales/models.en.yml index d1fb51d710..24d042ebd6 100644 --- a/spec/dummy_app/app/locales/models.en.yml +++ b/spec/dummy_app/app/locales/models.en.yml @@ -5,6 +5,7 @@ en: name: Their Name team: manager: Team Manager + main_sponsor: Main Sponsor fans: Some Fans mongoid: *en_attributes diff --git a/spec/dummy_app/app/mongoid/player.rb b/spec/dummy_app/app/mongoid/player.rb index 2b10f466cc..0d26dbc79b 100644 --- a/spec/dummy_app/app/mongoid/player.rb +++ b/spec/dummy_app/app/mongoid/player.rb @@ -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) diff --git a/spec/dummy_app/app/mongoid/team.rb b/spec/dummy_app/app/mongoid/team.rb index a82bcf0a53..8f36e7ef99 100644 --- a/spec/dummy_app/app/mongoid/team.rb +++ b/spec/dummy_app/app/mongoid/team.rb @@ -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 diff --git a/spec/dummy_app/db/migrate/20160728152942_add_main_sponsor_to_teams.rb b/spec/dummy_app/db/migrate/20160728152942_add_main_sponsor_to_teams.rb new file mode 100644 index 0000000000..a43da9d0f4 --- /dev/null +++ b/spec/dummy_app/db/migrate/20160728152942_add_main_sponsor_to_teams.rb @@ -0,0 +1,5 @@ +class AddMainSponsorToTeams < MigrationBase + def change + add_column :teams, :main_sponsor, :integer, default: 0, null: false + end +end diff --git a/spec/dummy_app/db/migrate/20160728153058_add_formation_to_players.rb b/spec/dummy_app/db/migrate/20160728153058_add_formation_to_players.rb new file mode 100644 index 0000000000..d416388bce --- /dev/null +++ b/spec/dummy_app/db/migrate/20160728153058_add_formation_to_players.rb @@ -0,0 +1,5 @@ +class AddFormationToPlayers < MigrationBase + def change + add_column :players, :formation, :string, default: 'substitute', null: false + end +end diff --git a/spec/integration/basic/export/rails_admin_basic_export_spec.rb b/spec/integration/basic/export/rails_admin_basic_export_spec.rb index 8ce7f06f75..8b42c683e3 100644 --- a/spec/integration/basic/export/rails_admin_basic_export_spec.rb +++ b/spec/integration/basic/export/rails_admin_basic_export_spec.rb @@ -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]'] diff --git a/spec/rails_admin/config/sections_spec.rb b/spec/rails_admin/config/sections_spec.rb index 39079dc7ac..fa5fc5b089 100644 --- a/spec/rails_admin/config/sections_spec.rb +++ b/spec/rails_admin/config/sections_spec.rb @@ -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