Module: Symbolize::ActiveRecord

Extended by:
ActiveSupport::Concern
Defined in:
lib/symbolize/active_record.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#read_and_symbolize_attribute(attr_name) ⇒ Object

Return an attribute’s value as a symbol or nil



165
166
167
# File 'lib/symbolize/active_record.rb', line 165

def read_and_symbolize_attribute attr_name
  symbolize_attribute self[attr_name]
end

#read_i18n_attribute(attr_name) ⇒ Object

Return an attribute’s i18n



170
171
172
173
174
# File 'lib/symbolize/active_record.rb', line 170

def read_i18n_attribute attr_name
  attr = read_attribute(attr_name)
  t = I18n.translate("activerecord.symbolizes.#{self.class.model_name.to_s.underscore}.#{attr_name}.#{attr}") #.to_sym rescue nila
  t.is_a?(Hash) ? nil : t
end

#symbolize_attribute(attr) ⇒ Object

String becomes symbol, booleans string and nil nil.



156
157
158
159
160
161
162
# File 'lib/symbolize/active_record.rb', line 156

def symbolize_attribute attr
  case attr
    when String then attr.empty? ? nil : attr.to_sym
    when Symbol, TrueClass, FalseClass, Numeric then attr
    else nil
  end
end

#write_symbolized_attribute(attr_name, value) ⇒ Object

Write a symbolized value. Watch out for booleans.



177
178
179
180
181
182
# File 'lib/symbolize/active_record.rb', line 177

def write_symbolized_attribute attr_name, value
  val = { "true" => true, "false" => false }[value]
  val = symbolize_attribute(value) if val.nil?

  self[attr_name] = val #.to_s
end