Module: Symbolize::ActiveRecord
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/symbolize/active_record.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#read_and_symbolize_attribute(attr_name) ⇒ Object
Return an attribute’s value as a symbol or nil.
-
#read_i18n_attribute(attr_name) ⇒ Object
Return an attribute’s i18n.
-
#symbolize_attribute(value) ⇒ Object
String becomes symbol, booleans string and nil nil.
-
#write_symbolized_attribute(attr_name, value) ⇒ Object
Write a symbolized value.
Instance Method Details
#read_and_symbolize_attribute(attr_name) ⇒ Object
Return an attribute’s value as a symbol or nil
193 194 195 |
# File 'lib/symbolize/active_record.rb', line 193 def read_and_symbolize_attribute attr_name symbolize_attribute self[attr_name] end |
#read_i18n_attribute(attr_name) ⇒ Object
Return an attribute’s i18n
198 199 200 201 202 |
# File 'lib/symbolize/active_record.rb', line 198 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(value) ⇒ Object
String becomes symbol, booleans string and nil nil.
188 189 190 |
# File 'lib/symbolize/active_record.rb', line 188 def symbolize_attribute value self.class.symbolize_attribute value end |
#write_symbolized_attribute(attr_name, value) ⇒ Object
Write a symbolized value. Watch out for booleans.
205 206 207 208 209 210 |
# File 'lib/symbolize/active_record.rb', line 205 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 |