Module: Globalize::ActiveRecord::InstanceMethods
- Defined in:
- lib/globalize/active_record/instance_methods.rb
Instance Method Summary collapse
- #[](attr_name) ⇒ Object
- #assign_attributes(attributes, *args) ⇒ Object
- #attribute_names ⇒ Object
- #attributes ⇒ Object
- #attributes=(attributes, *args) ⇒ Object
-
#available_locales ⇒ Object
Get available locales from translations association, without a separate distinct query.
- #cache_key ⇒ Object
- #changed? ⇒ Boolean
- #column_for_attribute(name) ⇒ Object
- #globalize ⇒ Object
- #globalize_fallbacks(locale) ⇒ Object
- #initialize_dup(other) ⇒ Object
- #read_attribute(name, options = {}) ⇒ Object
- #reload(options = nil) ⇒ Object
- #rollback ⇒ Object
- #save ⇒ Object
- #set_translations(options) ⇒ Object
- #translated_attribute_by_locale(name) ⇒ Object
- #translated_attributes ⇒ Object
- #translation ⇒ Object
- #translation_caches ⇒ Object
- #translation_for(locale, build_if_missing = true) ⇒ Object
- #translations_by_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
Instance Method Details
#[](attr_name) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/globalize/active_record/instance_methods.rb', line 32 def [](attr_name) if translated?(attr_name) read_attribute(attr_name) else read_attribute(attr_name) { |n| missing_attribute(n, caller) } end end |
#assign_attributes(attributes, *args) ⇒ Object
18 19 20 |
# File 'lib/globalize/active_record/instance_methods.rb', line 18 def assign_attributes(attributes, *args) with_given_locale(attributes) { super(attributes_without_locale(attributes)) } end |
#attribute_names ⇒ Object
55 56 57 |
# File 'lib/globalize/active_record/instance_methods.rb', line 55 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(attributes_without_locale(attributes)) } end |
#available_locales ⇒ Object
Get available locales from translations association, without a separate distinct query
134 135 136 |
# File 'lib/globalize/active_record/instance_methods.rb', line 134 def available_locales translations.map(&:locale).uniq end |
#cache_key ⇒ Object
165 166 167 |
# File 'lib/globalize/active_record/instance_methods.rb', line 165 def cache_key [super, translation.cache_key].join("/") end |
#changed? ⇒ Boolean
169 170 171 |
# File 'lib/globalize/active_record/instance_methods.rb', line 169 def changed? changed_attributes.present? || translations.any?(&:changed?) end |
#column_for_attribute(name) ⇒ Object
159 160 161 162 163 |
# File 'lib/globalize/active_record/instance_methods.rb', line 159 def column_for_attribute name return super if translated_attribute_names.exclude?(name) globalize.send(:column_for_attribute, name) end |
#globalize ⇒ Object
6 7 8 |
# File 'lib/globalize/active_record/instance_methods.rb', line 6 def globalize @globalize ||= Adapter.new(self) end |
#globalize_fallbacks(locale) ⇒ Object
138 139 140 |
# File 'lib/globalize/active_record/instance_methods.rb', line 138 def globalize_fallbacks(locale) Globalize.fallbacks(locale) end |
#initialize_dup(other) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/globalize/active_record/instance_methods.rb', line 95 def initialize_dup(other) @globalize = nil @translation_caches = nil super other.each_locale_and_translated_attribute do |locale, name| globalize.write(locale, name, other.globalize.fetch(locale, name) ) end end |
#read_attribute(name, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/globalize/active_record/instance_methods.rb', line 40 def read_attribute(name, = {}) = {:translated => true, :locale => nil}.merge() return super(name) unless [:translated] if translated?(name) if !(value = globalize.fetch([:locale] || Globalize.locale, name)).nil? value else super(name) end else super(name) end end |
#reload(options = nil) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/globalize/active_record/instance_methods.rb', line 88 def reload( = nil) translation_caches.clear translated_attribute_names.each { |name| @attributes.reset(name.to_s) } globalize.reset super() end |
#rollback ⇒ Object
142 143 144 |
# File 'lib/globalize/active_record/instance_methods.rb', line 142 def rollback translation_caches[::Globalize.locale] = translation.previous_version end |
#save ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/globalize/active_record/instance_methods.rb', line 146 def save(*) result = Globalize.with_locale(translation.locale || I18n.default_locale) do without_fallbacks do super end end if result globalize.clear_dirty end result end |
#set_translations(options) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/globalize/active_record/instance_methods.rb', line 75 def set_translations() .keys.each do |locale| translation = translation_for(locale) || translations.build(:locale => locale.to_s) [locale].each do |key, value| translation.send :"#{key}=", value end translation.save end globalize.reset end |
#translated_attribute_by_locale(name) ⇒ Object
129 130 131 |
# File 'lib/globalize/active_record/instance_methods.rb', line 129 def translated_attribute_by_locale(name) translations_by_locale(&:"#{name}") end |
#translated_attributes ⇒ Object
61 62 63 64 65 |
# File 'lib/globalize/active_record/instance_methods.rb', line 61 def translated_attributes translated_attribute_names.inject({}) do |attributes, name| attributes.merge(name.to_s => send(name)) end end |
#translation ⇒ Object
104 105 106 |
# File 'lib/globalize/active_record/instance_methods.rb', line 104 def translation translation_for(::Globalize.locale) end |
#translation_caches ⇒ Object
119 120 121 |
# File 'lib/globalize/active_record/instance_methods.rb', line 119 def translation_caches @translation_caches ||= {} end |
#translation_for(locale, build_if_missing = true) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/globalize/active_record/instance_methods.rb', line 108 def translation_for(locale, build_if_missing = true) 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 unless translations.loaded? _translation ||= translations.build(:locale => locale) if build_if_missing translation_caches[locale] = _translation if _translation end translation_caches[locale] end |
#translations_by_locale ⇒ Object
123 124 125 126 127 |
# File 'lib/globalize/active_record/instance_methods.rb', line 123 def translations_by_locale translations.each_with_object(HashWithIndifferentAccess.new) do |t, hash| hash[t.locale] = block_given? ? yield(t) : t end end |
#untranslated_attributes ⇒ Object
This method is basically the method built into Rails but we have to pass => false
69 70 71 72 73 |
# File 'lib/globalize/active_record/instance_methods.rb', line 69 def untranslated_attributes attribute_names.inject({}) do |attrs, name| attrs[name] = read_attribute(name, {:translated => false}); attrs end end |
#write_attribute(name, value, options = {}) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/globalize/active_record/instance_methods.rb', line 24 def write_attribute(name, value, = {}) return super(name, value) unless translated?(name) = {:locale => Globalize.locale}.merge() globalize.write([:locale], name, value) end |