Module: EnumAttribute::ClassMethods

Defined in:
lib/enum_attribute.rb

Instance Method Summary collapse

Instance Method Details

#enum_attribute(name, values, opts = {:number=>false,:colum_name=>false,:i18n=>true}) ⇒ Object



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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/enum_attribute.rb', line 58

def enum_attribute name, values, opts={:number=>false,:colum_name=>false,:i18n=>true}
  raise "Values should be given as Enumerable" unless values.is_a?(Enumerable)
  values = values.map(&:to_s)
  #validates_inclusion_of __send__(name), :in=>values unless opts[:skip_validation]

  pluralized = name.to_s.pluralize

  extend Module.new{
    define_method pluralized do
      values
    end
    define_method "#{name}_names" do
      if opts[:i18n] == false
        values
      else
        values.map{|n|self.i18nt(n,pluralized)}
      end
    end
    define_method "#{name}_pairs" do
      if opts[:number]
        values.each_with_index.map{|n,i|[ self.i18nt(n,pluralized), i + 1]}
      else
        values.map{|n|[ self.i18nt(n,pluralized), n]}
      end
    end
    define_method "#{name}_name" do |col|
      return "" unless col
      if col.to_i==0
        [self.i18nt(col,pluralized)]
      else
        [self.i18nt(values[col - 1],pluralized)]
      end
    end
    def i18nt(value,pluralized) 
      return "" unless value
      #I18n.t("models.#{self.model_name.to_s.underscore}.#{pluralized}.#{value}")
      i18n_scope = "activerecord"
      i18n_scope = self.i18n_scope.to_s if defined?(self.i18n_scope) #rails2 support i18n_scope
      I18n.t("#{i18n_scope}.attributes.#{self.model_name.to_s.underscore}.#{pluralized}.#{value}")
    end
  }

  # foo.val_seed
  define_method "#{name}_seed" do
    column_name = name
    column_name = opts[:column_name] if opts[:column_name]
    if opts[:number]
      if __send__(column_name) #reject nil in column
        values[__send__(column_name) - 1]
      else
        ""
      end
    else
      column_name
    end
  end


  # if locale is just variable,it'll cause to error
  # So array(*locale) can make parameter nothing.
  # ex. foo.val_name
  #
  define_method "#{name}_name" do |*locale|
    column_name = name
    column_name = opts[:column_name] if opts[:column_name]

    return "" unless __send__(column_name) #reject nil in column

    if opts[:number]
      return "" if __send__(column_name).to_i - 1 < 0
      value = values[__send__(column_name).to_i - 1]
    else
      value = __send__(column_name) 
    end

    return value if opts[:i18n] == false

    l = :en
    l = I18n.locale if defined?(I18n.locale)
    l = locale[0] if locale

    i18n_scope = "activerecord"
    i18n_scope = self.i18n_scope.to_s if defined?(self.i18n_scope) #rails2 support i18n_scope
    I18n.t("#{i18n_scope}.attributes.#{self.model_name.to_s.underscore}.#{pluralized}.#{value}",:locale=>l)

  end

  # I want to use for checkbox type ,but indeed its hard to manage to store&read with Mysql
  # so,it'll be dupricated in near future.
  #
  # foo.val_names
  #
  # :assumption data is array
  #  ex @aaa.test_names => ['a','b','c']
  #
  #define_method "#{name}_names" do
    #array =[]
    #return false if self.send(name).blank?
    #if self.class.to_s.index("::")
      #self.send(name).map{|n|array << I18n.t("models.#{self.class.superclass.to_s.underscore}.#{pluralized}.#{n}")}
    #else
      #self.send(name).map{|n|array << I18n.t("models.#{self.class.to_s.underscore}.#{pluralized}.#{n}")}
    #end
    #return array.join("<br />")
  #end

end