Module: TranslatedAttr::ActiveRecordExtensions::InstanceMethods
- Defined in:
- lib/translated_attr/active_record_extensions.rb
Instance Method Summary collapse
- #all_translations ⇒ Object
- #find_or_create_translation(locale) ⇒ Object
- #find_translation(locale) ⇒ Object
-
#localized_attributes ⇒ Object
attributes are stored in @localized_attributes instance variable via setter.
- #set_attribute(attribute, value, locale = I18n.locale) ⇒ Object
- #translations_default_locale ⇒ Object
-
#update_translations! ⇒ Object
called before validations.
Instance Method Details
#all_translations ⇒ Object
187 188 189 190 191 192 |
# File 'lib/translated_attr/active_record_extensions.rb', line 187 def all_translations t = I18n.available_locales.map do |locale| [locale, find_or_create_translation(locale)] end ActiveSupport::OrderedHash[t] end |
#find_or_create_translation(locale) ⇒ Object
180 181 182 183 184 185 |
# File 'lib/translated_attr/active_record_extensions.rb', line 180 def find_or_create_translation(locale) locale = locale.to_s (find_translation(locale) || self.translations.new).tap do |t| t.locale = locale end end |
#find_translation(locale) ⇒ Object
194 195 196 197 |
# File 'lib/translated_attr/active_record_extensions.rb', line 194 def find_translation(locale) locale = locale.to_s translations.detect { |t| t.locale == locale } end |
#localized_attributes ⇒ Object
attributes are stored in @localized_attributes instance variable via setter
206 207 208 |
# File 'lib/translated_attr/active_record_extensions.rb', line 206 def localized_attributes @localized_attributes ||= Hash.new { |hash, key| hash[key] = {} } end |
#set_attribute(attribute, value, locale = I18n.locale) ⇒ Object
176 177 178 |
# File 'lib/translated_attr/active_record_extensions.rb', line 176 def set_attribute(attribute, value, locale = I18n.locale) localized_attributes[locale][attribute] = value end |
#translations_default_locale ⇒ Object
199 200 201 202 203 |
# File 'lib/translated_attr/active_record_extensions.rb', line 199 def translations_default_locale return default_locale.to_sym if respond_to?(:default_locale) return self.class.default_locale.to_sym if self.class.respond_to?(:default_locale) I18n.default_locale end |
#update_translations! ⇒ Object
called before validations
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/translated_attr/active_record_extensions.rb', line 211 def update_translations! return if @localized_attributes.blank? @localized_attributes.each do |locale, attributes| translation = find_translation(locale) if translation attributes.each { |attribute, value| translation.send("#{attribute}=", value) } else translations.build(attributes.merge(:locale => locale.to_s)) end end end |