Module: Globalize::ActiveRecord::InstanceMethods
- Defined in:
- lib/globalize/active_record/instance_methods.rb
Instance Method Summary collapse
- #attribute_names ⇒ Object
- #attributes ⇒ Object
- #attributes=(attributes, *args) ⇒ Object
- #clone ⇒ Object
- #globalize ⇒ Object
- #read_attribute(name, options = {}) ⇒ Object
- #reload(options = nil) ⇒ Object
- #rollback ⇒ Object
- #set_translations(options) ⇒ Object
- #translated?(name) ⇒ Boolean
- #translated_attributes ⇒ Object
- #translation ⇒ Object
- #translation_for(locale) ⇒ Object
-
#untranslated_attributes ⇒ Object
This method is basically the method built into Rails but we have to pass => false.
- #update_attributes(attributes, *args) ⇒ Object
- #update_attributes!(attributes, *args) ⇒ Object
- #write_attribute(name, value, options = {}) ⇒ Object
Instance Method Details
#attribute_names ⇒ Object
58 59 60 |
# File 'lib/globalize/active_record/instance_methods.rb', line 58 def attribute_names translated_attribute_names.map(&:to_s) + super end |
#attributes ⇒ Object
10 11 12 |
# File 'lib/globalize/active_record/instance_methods.rb', line 10 def attributes super.merge(translated_attributes) end |
#attributes=(attributes, *args) ⇒ Object
14 15 16 |
# File 'lib/globalize/active_record/instance_methods.rb', line 14 def attributes=(attributes, *args) with_given_locale(attributes) { super } end |
#clone ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/globalize/active_record/instance_methods.rb', line 96 def clone obj = super return obj unless respond_to?(:translated_attribute_names) obj.instance_variable_set(:@translations, nil) if new_record? # Reset the collection because of rails bug: http://pastie.org/1521874 obj.instance_variable_set(:@globalize, nil ) each_locale_and_translated_attribute do |locale, name| obj.globalize.write(locale, name, globalize.fetch(locale, name) ) end return obj end |
#globalize ⇒ Object
6 7 8 |
# File 'lib/globalize/active_record/instance_methods.rb', line 6 def globalize @globalize ||= Adapter.new(self) end |
#read_attribute(name, options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/globalize/active_record/instance_methods.rb', line 43 def read_attribute(name, = {}) # Deprecate old use of locale unless .is_a?(Hash) warn "[DEPRECATION] passing 'locale' as #{.inspect} is deprecated. Please use {:locale => #{.inspect}} instead." = {:locale => } end = {:translated => true, :locale => nil}.merge() if self.class.translated?(name) and [:translated] globalize.fetch([:locale] || Globalize.locale, name) else super(name) end end |
#reload(options = nil) ⇒ Object
90 91 92 93 94 |
# File 'lib/globalize/active_record/instance_methods.rb', line 90 def reload( = nil) translated_attribute_names.each { |name| @attributes.delete(name.to_s) } globalize.reset super() end |
#rollback ⇒ Object
125 126 127 |
# File 'lib/globalize/active_record/instance_methods.rb', line 125 def rollback @translation_caches[::Globalize.locale] = translation.previous_version end |
#set_translations(options) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/globalize/active_record/instance_methods.rb', line 82 def set_translations() .keys.each do |locale| translation = translation_for(locale) || translations.build(:locale => locale.to_s) translation.update_attributes!([locale]) end end |
#translated?(name) ⇒ Boolean
62 63 64 |
# File 'lib/globalize/active_record/instance_methods.rb', line 62 def translated?(name) self.class.translated?(name) end |
#translated_attributes ⇒ Object
66 67 68 69 70 |
# File 'lib/globalize/active_record/instance_methods.rb', line 66 def translated_attributes translated_attribute_names.inject({}) do |attributes, name| attributes.merge(name.to_s => translation.send(name)) end end |
#translation ⇒ Object
109 110 111 |
# File 'lib/globalize/active_record/instance_methods.rb', line 109 def translation translation_for(::Globalize.locale) end |
#translation_for(locale) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/globalize/active_record/instance_methods.rb', line 113 def translation_for(locale) @translation_caches ||= {} unless @translation_caches[locale] # Enumberable#detect is better since we have the translations collection (already) loaded # using either Model.includes(:translations) or Model.with_translations _translation = translations.detect{|t| t.locale.to_s == locale.to_s} _translation ||= translations.build(:locale => locale) @translation_caches[locale] = _translation end @translation_caches[locale] end |
#untranslated_attributes ⇒ Object
This method is basically the method built into Rails but we have to pass => false
74 75 76 77 78 79 80 |
# File 'lib/globalize/active_record/instance_methods.rb', line 74 def untranslated_attributes attrs = {} attribute_names.each do |name| attrs[name] = read_attribute(name, {:translated => false}) end attrs end |
#update_attributes(attributes, *args) ⇒ Object
22 23 24 |
# File 'lib/globalize/active_record/instance_methods.rb', line 22 def update_attributes(attributes, *args) with_given_locale(attributes) { super } end |
#update_attributes!(attributes, *args) ⇒ Object
18 19 20 |
# File 'lib/globalize/active_record/instance_methods.rb', line 18 def update_attributes!(attributes, *args) with_given_locale(attributes) { super } end |
#write_attribute(name, value, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/globalize/active_record/instance_methods.rb', line 26 def write_attribute(name, value, = {}) # raise 'y' if value.nil? # TODO. if translated?(name) # Deprecate old use of locale unless .is_a?(Hash) warn "[DEPRECATION] passing 'locale' as #{.inspect} is deprecated. Please use {:locale => #{.inspect}} instead." = {:locale => } end = {:locale => nil}.merge() attribute_will_change! name.to_s globalize.write([:locale] || Globalize.locale, name, value) else super(name, value) end end |