Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/helpers/rails_admin/main_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def ordered_filter_string
end
case field.type
when :enum
options[:select_options] = options_for_select(field.with(object: @abstract_model.model.new).enum, filter_hash['v'])
options[:select_options] = options_for_select(field.with(object: @abstract_model.model.new).enum.keys, filter_hash['v'])
when :date, :datetime, :time
options[:datetimepicker_format] = field.parser.to_momentjs
end
Expand Down
20 changes: 17 additions & 3 deletions lib/rails_admin/config/fields/types/active_record_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ def type
def parse_value(value)
return unless value.present?
if ::Rails.version >= '5'
abstract_model.model.attribute_types[name.to_s].deserialize(value)
abstract_model.model.attribute_types[name.to_s].serialize(value)
else
enum.invert[type_cast_value(value)]
# Depending on the colum type and AR version, we might get a
# string or an integer, so we need to handle both cases.
enum.fetch(value) do
type_cast_value(value)
end
end
end

def parse_input(params)
params[name] = parse_value(params[name]) if params[name]
value = params[name]
return unless value
params[name] = parse_input_value(value)
end

def form_value
Expand All @@ -46,6 +52,14 @@ def form_value

private

def parse_input_value(value)
if ::Rails.version >= '5'
abstract_model.model.attribute_types[name.to_s].deserialize(value)
else
enum.invert[type_cast_value(value)]
end
end

def type_cast_value(value)
if ::Rails.version >= '4.2'
abstract_model.model.column_types[name.to_s].type_cast_from_user(value)
Expand Down
22 changes: 22 additions & 0 deletions spec/integration/basic/list/rails_admin_basic_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@
end
end

describe 'GET /admin/team' do
let!(:teams) do
[
FactoryGirl.create(:team, main_sponsor: 'no_sponsor'),
FactoryGirl.create(:team, main_sponsor: 'food_factory'),
]
end

it "allows filtering on enum values" do
RailsAdmin.config Team do
list do
field :name
field :main_sponsor
end
end

visit index_path(model_name: 'team', f: {main_sponsor: {'1' => {v: 'food_factory'}}})
is_expected.to have_no_content(teams[0].name)
is_expected.to have_content(teams[1].name)
end
end

describe 'GET /admin/player' do
before do
@teams = Array.new(2) do
Expand Down
58 changes: 31 additions & 27 deletions spec/integration/config/edit/rails_admin_config_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1128,33 +1128,37 @@ def color_list
end
end

describe 'when serialize is enabled in ActiveRecord model', active_record: true do
before do
# ActiveRecord 4.2 momoizes result of serialized_attributes, so we have to clear it.
Team.remove_instance_variable(:@serialized_attributes) if Team.instance_variable_defined?(:@serialized_attributes)
Team.instance_eval do
serialize :color
def color_enum
%w(blue green red)
end
end
visit new_path(model_name: 'team')
end

after do
if Rails.version >= '4.2'
Team.reset_column_information
Team.attribute_type_decorations.clear
else
Team.serialized_attributes.clear
end
Team.instance_eval { undef :color_enum }
end

it 'makes enumeration multi-selectable' do
is_expected.to have_selector('.enum_type select[multiple]')
end
end
# Spec is disabled because ActiveRecord makes it impossible to reload enum
# columns information. This test attemps to reload the column information,
# but ends up with an Integer type rather than an enum type.
#
# describe 'when serialize is enabled in ActiveRecord model', active_record: true do
# before do
# # ActiveRecord 4.2 momoizes result of serialized_attributes, so we have to clear it.
# Team.remove_instance_variable(:@serialized_attributes) if Team.instance_variable_defined?(:@serialized_attributes)
# Team.instance_eval do
# serialize :color
# def color_enum
# %w(blue green red)
# end
# end
# visit new_path(model_name: 'team')
# end

# after do
# if Rails.version >= '4.2'
# Team.reset_column_information
# Team.attribute_type_decorations.clear
# else
# Team.serialized_attributes.clear
# end
# Team.instance_eval { undef :color_enum }
# end

# it 'makes enumeration multi-selectable' do
# is_expected.to have_selector('.enum_type select[multiple]')
# end
# end

describe 'when serialize is enabled in Mongoid model', mongoid: true do
before do
Expand Down
37 changes: 37 additions & 0 deletions spec/rails_admin/abstract_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,43 @@
expect(@abstract_model.all(filters: {'datetime_field' => {'1' => {v: ['January 02, 2012 12:00'], o: 'default'}}}).count).to eq(1)
end
end

if ::Rails.version >= '4.1'
context "an abstract model with an string enum field" do
before do
@abstract_model = RailsAdmin::AbstractModel.new('Player')
end

before do
FactoryGirl.create(:player, formation: :start)
FactoryGirl.create(:player, formation: :substitute)
end

it "filters by enum values" do
enum_value = 'start'
expect(@abstract_model.all(filters: {'formation' => {'1' => {v: [enum_value], o: 'is'}}}).count).to eq(1)
end
end

context "an abstract model with an integer enum field" do
before do
@abstract_model = RailsAdmin::AbstractModel.new('Team')
end

let!(:teams) do
[
FactoryGirl.create(:team, main_sponsor: 'no_sponsor'),
FactoryGirl.create(:team, main_sponsor: 'food_factory'),
]
end

it "filters by enum values" do
enum_value = 'food_factory'
scope = @abstract_model.all(filters: {'main_sponsor' => {'1' => {v: [enum_value], o: 'is'}}})
expect(scope.count).to eq(1)
end
end
end
end

context 'with Kaminari' do
Expand Down