Module: ActsAsEnum::ClassMethods

Defined in:
lib/acts_as_enum.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.translate_enum_symbol(klass, attr_name, enum_symbol) ⇒ Object



135
136
137
# File 'lib/acts_as_enum.rb', line 135

def self.translate_enum_symbol(klass, attr_name, enum_symbol)
  ::I18n.t("activerecord.attributes.#{klass.to_s.underscore.gsub('/', '.')}.#{attr_name.to_s.pluralize}.#{enum_symbol}", default: enum_symbol.humanize)
end

Instance Method Details

#acts_as_enum(attr, options = { :in => [], :prefix => false }) ⇒ Object Also known as: enum_attr



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/acts_as_enum.rb', line 56

def acts_as_enum(attr, options = { :in => [], :prefix => false })
  attr = attr.to_s
  plural_upcase_attr = attr.pluralize.upcase
  enum = options[:in]

  raise "Can not load Rails." unless defined?(Rails)
  raise "Options :in can not be empty." if enum.blank?
  raise "Options :in must be an object of Array or Hash." unless enum.is_a?(Array) or enum.is_a?(Hash)

  if enum.is_a?(Hash)
    enum = enum.to_a
  elsif enum.is_a?(Array) and enum.first.is_a?(String)
    enum = enum.inject([]) { |arr, obj| arr << [obj] * 2 }
  end

  attr_options = enum.each_with_object({}) do |arr, hash|
    if arr.count == 2
      hash[arr.last.to_s] = arr.last.to_s
    else
      hash[arr[-2]] = arr.last.to_s
    end
  end
  const_set(plural_upcase_attr, attr_options)

  enum.each do |arr|
    enum_name = arr.first.to_s.downcase
    attr_value = arr[-2]
    method_name = options[:prefix] ? "#{attr}_#{enum_name}" : enum_name

    const_set(method_name.upcase, attr_value)

    if Rails.version.to_i > 2
      scope method_name.to_sym, -> { where(attr => attr_value) }
    else
      named_scope method_name.to_sym, :conditions => { attr.to_sym => attr_value }
    end

    class_eval do
      define_method "#{method_name}?" do
        public_send(attr) == attr_value
      end

      define_method "#{method_name}!" do
        update_attribute(attr, attr_value) # use update_attribute method to skip validations

      end
    end
  end

  class_eval(%Q{
    def self.#{attr}_options
      #{plural_upcase_attr}.inject([]){ |arr, obj| arr << obj.reverse }
    end

    def self.#{attr}_options_i18n
      #{plural_upcase_attr}.inject([]) do |arr, obj|
        #{enum}.each do |a|
          obj[1] = ::ActsAsEnum::ClassMethods.translate_enum_symbol("#{self}", "#{attr}", a[0]) if a[1] == obj[0]
        end
        arr << obj.reverse
      end
    end

    def #{attr}_name
      return #{plural_upcase_attr}[#{attr}] if #{attr}.is_a?(FalseClass)
      #{plural_upcase_attr}[#{attr}] unless #{attr}.blank?
    end

    def #{attr}_name_i18n
      #{plural_upcase_attr}.each do |k, v|
        #{enum}.each do |a|
          #{plural_upcase_attr}[k] = ::ActsAsEnum::ClassMethods.translate_enum_symbol("#{self}", "#{attr}", a[0]) if a[1] == k
        end
      end
      return #{plural_upcase_attr}[#{attr}] if #{attr}.is_a?(FalseClass)
      #{plural_upcase_attr}[#{attr}] unless #{attr}.blank?
    end
  })
end