Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1053 from adrianmacneil/class-enum-methods

Support enumeration class methods
  • Loading branch information...
commit 473c0a5b54df7ecb651d20f388e7d4b9d5872dff 2 parents 5057507 + df41eab
@bbenezech bbenezech authored
View
2  lib/rails_admin/config/fields/factories/enum.rb
@@ -2,7 +2,7 @@
require 'rails_admin/config/fields/types/enum'
RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
- if parent.abstract_model.model.instance_methods.map(&:to_s).include?("#{properties[:name]}_enum")
+ if parent.abstract_model.model.respond_to?("#{properties[:name]}_enum") || parent.abstract_model.model.method_defined?("#{properties[:name]}_enum")
fields << RailsAdmin::Config::Fields::Types::Enum.new(parent, properties[:name], properties)
true
else
View
4 lib/rails_admin/config/fields/types/enum.rb
@@ -12,11 +12,11 @@ class Enum < RailsAdmin::Config::Fields::Base
end
register_instance_option(:enum_method) do
- @enum_method ||= bindings[:object].respond_to?("#{name}_enum") ? "#{name}_enum" : name
+ @enum_method ||= (bindings[:object].class.respond_to?("#{name}_enum") || bindings[:object].respond_to?("#{name}_enum")) ? "#{name}_enum" : name
end
register_instance_option(:enum) do
- bindings[:object].send(self.enum_method)
+ bindings[:object].class.respond_to?(enum_method) ? bindings[:object].class.send(enum_method) : bindings[:object].send(enum_method)
end
register_instance_option(:pretty_value) do
View
36 spec/integration/config/edit/rails_admin_config_edit_spec.rb
@@ -949,6 +949,23 @@ def color_enum
Team.send(:remove_method, :color_enum) # Reset
end
+ it "should auto-detect enumeration when class responds to '::{method}_enum'" do
+ Team.instance_eval do
+ def color_enum
+ ["blue", "green", "red"]
+ end
+ end
+ RailsAdmin.config Team do
+ edit do
+ field :color
+ end
+ end
+ visit new_path(:model_name => "team")
+ should have_selector(".enum_type select")
+ should have_content("green")
+ Team.instance_eval { undef :color_enum } # Reset
+ end
+
it "should allow configuration of the enum method" do
Team.class_eval do
def color_list
@@ -968,6 +985,25 @@ def color_list
Team.send(:remove_method, :color_list) # Reset
end
+ it "should allow configuration of the enum class method" do
+ Team.instance_eval do
+ def color_list
+ ["blue", "green", "red"]
+ end
+ end
+ RailsAdmin.config Team do
+ edit do
+ field :color, :enum do
+ enum_method :color_list
+ end
+ end
+ end
+ visit new_path(:model_name => "team")
+ should have_selector(".enum_type select")
+ should have_content("green")
+ Team.instance_eval { undef :color_list } # Reset
+ end
+
it "should allow direct listing of enumeration options and override enum method" do
Team.class_eval do
def color_list
Please sign in to comment.
Something went wrong with that request. Please try again.