Module: Globalize::ActiveRecord::InstanceMethods
- Defined in:
- lib/globalize/active_record/instance_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
- #attribute_names ⇒ Object
- #attributes ⇒ 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.
- #write_attribute(name, value, options = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/globalize/active_record/instance_methods.rb', line 14 def self.included(base) # Maintain Rails 3.0.x compatibility while adding Rails 3.1.x compatibility if base.method_defined?(:assign_attributes) base.class_eval %{ def assign_attributes(attributes, options = {}) with_given_locale(attributes) { super } end } else base.class_eval %{ def attributes=(attributes, *args) with_given_locale(attributes) { super } end def update_attributes!(attributes, *args) with_given_locale(attributes) { super } end def update_attributes(attributes, *args) with_given_locale(attributes) { super } end } end end |
Instance Method Details
#attribute_names ⇒ Object
73 74 75 |
# File 'lib/globalize/active_record/instance_methods.rb', line 73 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 |
#clone ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/globalize/active_record/instance_methods.rb', line 111 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/globalize/active_record/instance_methods.rb', line 58 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
105 106 107 108 109 |
# File 'lib/globalize/active_record/instance_methods.rb', line 105 def reload( = nil) translated_attribute_names.each { |name| @attributes.delete(name.to_s) } globalize.reset super() end |
#rollback ⇒ Object
140 141 142 |
# File 'lib/globalize/active_record/instance_methods.rb', line 140 def rollback @translation_caches[::Globalize.locale] = translation.previous_version end |
#set_translations(options) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/globalize/active_record/instance_methods.rb', line 97 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
77 78 79 |
# File 'lib/globalize/active_record/instance_methods.rb', line 77 def translated?(name) self.class.translated?(name) end |
#translated_attributes ⇒ Object
81 82 83 84 85 |
# File 'lib/globalize/active_record/instance_methods.rb', line 81 def translated_attributes translated_attribute_names.inject({}) do |attributes, name| attributes.merge(name.to_s => translation.send(name)) end end |
#translation ⇒ Object
124 125 126 |
# File 'lib/globalize/active_record/instance_methods.rb', line 124 def translation translation_for(::Globalize.locale) end |
#translation_for(locale) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/globalize/active_record/instance_methods.rb', line 128 def translation_for(locale) @translation_caches ||= {} unless @translation_caches[locale] # Fetch translations from database as those in the translation collection may be incomplete _translation = translations.detect{|t| t.locale.to_s == locale.to_s} _translation ||= translations.with_locale(locale).first _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
89 90 91 92 93 94 95 |
# File 'lib/globalize/active_record/instance_methods.rb', line 89 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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/globalize/active_record/instance_methods.rb', line 39 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 the_locale = [:locale] || Globalize.locale self.translations.reject!{|t| t.new_record? && t.locale != the_locale} globalize.write(the_locale, name, value) else super(name, value) end end |