module Globalize::ActiveRecord::InstanceMethods
Public Instance Methods
[](attr_name)
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 48 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(new_attributes)
click to toggle source
In Rails 5.2 we need to override _assign_attributes as it's called earlier in the stack (before assign_attributes) See github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_assignment.rb#L11
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 25 def _assign_attributes(new_attributes) attributes = new_attributes.stringify_keys with_given_locale(attributes) { super(attributes.except("locale")) } end
_read_attribute(attr_name, options = {}, &block)
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 68 def _read_attribute(attr_name, options = {}, &block) translated_value = read_translated_attribute(attr_name, options, &block) translated_value.nil? ? super(attr_name, &block) : translated_value end
assign_attributes(new_attributes, *options)
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 32 def assign_attributes(new_attributes, *options) super unless new_attributes.respond_to?(:stringify_keys) && new_attributes.present? attributes = new_attributes.stringify_keys with_given_locale(attributes) { super(attributes.except("locale"), *options) } end
attribute_names()
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 73 def attribute_names translated_attribute_names.map(&:to_s) + super end
attributes()
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 10 def attributes super.merge(translated_attributes) end
attributes=(new_attributes, *options)
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 14 def attributes=(new_attributes, *options) super unless new_attributes.respond_to?(:stringify_keys) && new_attributes.present? attributes = new_attributes.stringify_keys with_given_locale(attributes) { super(attributes.except("locale"), *options) } end
available_locales()
click to toggle source
Get available locales from translations association, without a separate distinct query
# File lib/globalize/active_record/instance_methods.rb, line 153 def available_locales translations.map(&:locale).uniq end
cache_key()
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 180 def cache_key [super, translation.cache_key].join("/") end
changed?()
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 184 def changed? changed_attributes.present? || translations.any?(&:changed?) end
column_for_attribute(name)
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 174 def column_for_attribute name return super if translated_attribute_names.exclude?(name) globalize.send(:column_for_attribute, name) end
globalize()
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 6 def globalize @globalize ||= Adapter.new(self) end
globalize_fallbacks(locale)
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 157 def globalize_fallbacks(locale) Globalize.fallbacks(locale) end
initialize_dup(other)
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 114 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
original_changed_attributes()
click to toggle source
need to access instance variable directly since changed_attributes is frozen as of Rails 4.2
# File lib/globalize/active_record/instance_methods.rb, line 190 def original_changed_attributes @changed_attributes end
read_attribute(attr_name, options = {}, &block)
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 56 def read_attribute(attr_name, options = {}, &block) name = if self.class.attribute_alias?(attr_name) self.class.attribute_alias(attr_name).to_s else attr_name.to_s end name = self.class.primary_key if name == "id".freeze && self.class.primary_key _read_attribute(name, options, &block) end
reload(options = nil)
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 107 def reload(options = nil) translation_caches.clear translated_attribute_names.each { |name| @attributes.reset(name.to_s) } globalize.reset super(options) end
save(*)
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 161 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)
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 93 def set_translations(options) options.keys.each do |locale| translation = translation_for(locale) || translations.build(:locale => locale.to_s) options[locale].each do |key, value| translation.send :"#{key}=", value translation.globalized_model.send :"#{key}=", value end translation.save if persisted? end globalize.reset end
translated_attribute_by_locale(name)
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 148 def translated_attribute_by_locale(name) translations_by_locale(&:"#{name}") end
translated_attributes()
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 79 def translated_attributes translated_attribute_names.inject({}) do |attributes, name| attributes.merge(name.to_s => send(name)) end end
translation()
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 123 def translation translation_for(::Globalize.locale) end
translation_caches()
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 138 def translation_caches @translation_caches ||= {} end
translation_for(locale, build_if_missing = true)
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 127 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() { |t| ... }
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 142 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()
click to toggle source
This method is basically the method built into Rails but we have to pass {:translated => false}
# File lib/globalize/active_record/instance_methods.rb, line 87 def untranslated_attributes attribute_names.inject({}) do |attrs, name| attrs[name] = read_attribute(name, {:translated => false}); attrs end end
write_attribute(name, value, *args, &block)
click to toggle source
Calls superclass method
# File lib/globalize/active_record/instance_methods.rb, line 40 def write_attribute(name, value, *args, &block) return super(name, value, *args, &block) unless translated?(name) options = {:locale => Globalize.locale}.merge(args.first || {}) globalize.write(options[:locale], name, value) end
Protected Instance Methods
each_locale_and_translated_attribute() { |locale, name| ... }
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 196 def each_locale_and_translated_attribute used_locales.each do |locale| translated_attribute_names.each do |name| yield locale, name end end end
read_translated_attribute(name, options) { |value| ... }
click to toggle source
nil or value
# File lib/globalize/active_record/instance_methods.rb, line 234 def read_translated_attribute(name, options) options = {:translated => true, :locale => nil}.merge(options) return nil unless options[:translated] return nil unless translated?(name) value = globalize.fetch(options[:locale] || Globalize.locale, name) return nil if value.nil? block_given? ? yield(value) : value end
save_translations!()
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 210 def save_translations! globalize.save_translations! translation_caches.clear end
used_locales()
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 204 def used_locales locales = globalize.stash.keys.concat(globalize.stash.keys).concat(translations.translated_locales) locales.uniq! locales end
with_given_locale(_attributes) { || ... }
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 215 def with_given_locale(_attributes, &block) attributes = _attributes.stringify_keys if locale = attributes.try(:delete, "locale") Globalize.with_locale(locale, &block) else yield end end
without_fallbacks() { || ... }
click to toggle source
# File lib/globalize/active_record/instance_methods.rb, line 225 def without_fallbacks before = self.fallbacks_for_empty_translations self.fallbacks_for_empty_translations = false yield ensure self.fallbacks_for_empty_translations = before end