Module: RailsAdminEnumConfigurable::ClassMethods

Defined in:
lib/rails_admin_enum_configurable.rb

Constant Summary collapse

@@enum_attribtues =
{}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_admin_enum_configurable.rb', line 24

def method_missing(name)
  if enum_method?(name)
    attribute = name.to_s.match(/(.+)_enum/)[1]
    if self.send(attribute.pluralize).is_a?(::Hash)
      self.send(attribute.pluralize).keys.map{|k| [I18n.t("enums.#{self.to_s.underscore}.#{attribute}.#{k}"), k]}.to_h
    elsif self.send(attribute.pluralize).is_a?(::Array)
      self.send(attribute.pluralize).map{|k| I18n.t("enums.#{self.to_s.underscore}.#{attribute}.#{k}")}
    else
      raise "Unknown enum type. attribute: #{attribute}, type: #{self.send(attribute.pluralize).class}"
    end
  else
    super
  end
end

Instance Method Details

#enum(definitions) ⇒ Object



9
10
11
12
13
14
# File 'lib/rails_admin_enum_configurable.rb', line 9

def enum(definitions)
  # call super first because super deletes :_prefix and :_suffix (these are unnecessary) from definitions.
  super
  @@enum_attribtues[self.to_s.underscore] ||= []
  @@enum_attribtues[self.to_s.underscore].push(definitions.keys.first)
end

#enum_attributesObject



16
17
18
# File 'lib/rails_admin_enum_configurable.rb', line 16

def enum_attributes
  @@enum_attribtues[self.to_s.underscore] || []
end

#enum_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rails_admin_enum_configurable.rb', line 20

def enum_method?(name)
  name.to_s.end_with?('_enum') && enum_attributes.include?(name.to_s.match(/(.+)_enum/)[1].to_sym)
end

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/rails_admin_enum_configurable.rb', line 39

def respond_to?(name, include_all = false)
  enum_method?(name) || super
end

#respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rails_admin_enum_configurable.rb', line 43

def respond_to_missing?(name, include_all = false)
  enum_method?(name) || super
end