Module: SingleTableGlobalize3::ActiveRecord::AttributeReadWrite
- Included in:
- InstanceMethods
- Defined in:
- lib/single_table_globalize3/active_record/attribute_read_write.rb
Instance Method Summary collapse
- #attribute_names ⇒ Object
- #attributes ⇒ Object
-
#deprecated_options(options) ⇒ Object
Deprecate old use of locale.
- #read_attribute(name, options = {}) ⇒ Object
- #translated?(name) ⇒ Boolean
- #translated_attributes ⇒ Object
-
#untranslated_attributes ⇒ Object
This method is basically the method built into Rails but we have to pass => false.
- #write_attribute(name, value, options = {}) ⇒ Object
Instance Method Details
#attribute_names ⇒ Object
48 49 50 |
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 48 def attribute_names translated_attribute_names.map(&:to_s) + super end |
#attributes ⇒ Object
5 6 7 |
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 5 def attributes super.merge(translated_attributes) end |
#deprecated_options(options) ⇒ Object
Deprecate old use of locale
10 11 12 13 14 15 16 17 |
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 10 def () unless .is_a?(Hash) warn "[DEPRECATION] passing 'locale' as #{.inspect} is deprecated. Please use {:locale => #{.inspect}} instead." {:locale => } else end end |
#read_attribute(name, options = {}) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 39 def read_attribute(name, = {}) = {:translated => true, :locale => nil}.merge(()) if (self.class.translated?(name) && [:translated]) && (value = globalize.fetch([:locale] || SingleTableGlobalize3.locale, name)) value else super(name) end end |
#translated?(name) ⇒ Boolean
52 53 54 |
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 52 def translated?(name) self.class.translated?(name) end |
#translated_attributes ⇒ Object
56 57 58 59 60 |
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 56 def translated_attributes translated_attribute_names.inject({}) do |attributes, name| attributes.merge(name.to_s => read_attribute(name)) end end |
#untranslated_attributes ⇒ Object
This method is basically the method built into Rails but we have to pass => false
64 65 66 67 68 69 70 |
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 64 def untranslated_attributes attrs = {} attribute_names.each do |name| attrs[name] = read_attribute(name, {:translated => false}) end attrs end |
#write_attribute(name, value, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 19 def write_attribute(name, value, = {}) if translated?(name) = {:locale => SingleTableGlobalize3.locale}.merge(()) # Dirty tracking, paraphrased from # ActiveRecord::AttributeMethods::Dirty#write_attribute. name_str = name.to_s # If there's already a change, delete it if this undoes the change. if attribute_changed?(name_str) && value == changed_attributes[name_str] changed_attributes.delete(name_str) elsif !attribute_changed?(name_str) && value != (old = globalize.fetch([:locale], name)) changed_attributes[name_str] = old end globalize.write([:locale], name, value) else super(name, value) end end |