Module: Globalize::ActiveRecord::InstanceMethods

Defined in:
lib/globalize/active_record/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#[](attr_name) ⇒ Object



48
49
50
51
52
53
54
# 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) ⇒ Object

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/5-2-stable/activerecord/lib/active_record/attribute_assignment.rb#L12



25
26
27
28
# 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) ⇒ Object



68
69
70
71
# 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)
  translated_value.nil? ? super(attr_name, &block) : translated_value
end

#assign_attributes(new_attributes, *options) ⇒ Object



32
33
34
35
36
# 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_namesObject



73
74
75
# File 'lib/globalize/active_record/instance_methods.rb', line 73

def attribute_names
  translated_attribute_names.map(&:to_s) + super
end

#attributesObject



10
11
12
# File 'lib/globalize/active_record/instance_methods.rb', line 10

def attributes
  super.merge(translated_attributes)
end

#attributes=(new_attributes, *options) ⇒ Object



14
15
16
17
18
# 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_localesObject

Get available locales from translations association, without a separate distinct query



153
154
155
# File 'lib/globalize/active_record/instance_methods.rb', line 153

def available_locales
  translations.map(&:locale).uniq
end

#cache_keyObject



197
198
199
# File 'lib/globalize/active_record/instance_methods.rb', line 197

def cache_key
  [super, translation.cache_key].join("/")
end

#changedObject



226
227
228
# File 'lib/globalize/active_record/instance_methods.rb', line 226

def changed
  super.concat(globalize.changed).uniq
end

#changed?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/globalize/active_record/instance_methods.rb', line 201

def changed?
  changed_attributes.present? || translations.any?(&:changed?)
end

#changed_attributesObject



218
219
220
# File 'lib/globalize/active_record/instance_methods.rb', line 218

def changed_attributes
  super.merge(globalize.changed_attributes(::Globalize.locale))
end

#changesObject



222
223
224
# File 'lib/globalize/active_record/instance_methods.rb', line 222

def changes
  super.merge(globalize.changes(::Globalize.locale))
end

#column_for_attribute(name) ⇒ Object



191
192
193
194
195
# File 'lib/globalize/active_record/instance_methods.rb', line 191

def column_for_attribute name
  return super if translated_attribute_names.exclude?(name)

  globalize.send(:column_for_attribute, name)
end

#globalizeObject



6
7
8
# File 'lib/globalize/active_record/instance_methods.rb', line 6

def globalize
  @globalize ||= Adapter.new(self)
end

#globalize_fallbacks(locale) ⇒ Object



157
158
159
# File 'lib/globalize/active_record/instance_methods.rb', line 157

def globalize_fallbacks(locale)
  Globalize.fallbacks(locale)
end

#initialize_dup(other) ⇒ Object



114
115
116
117
118
119
120
121
# 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_attributesObject

need to access instance variable directly since changed_attributes is frozen as of Rails 4.2



233
234
235
# File 'lib/globalize/active_record/instance_methods.rb', line 233

def original_changed_attributes
  @changed_attributes
end

#read_attribute(attr_name, options = {}, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



107
108
109
110
111
112
# 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

#saveObject



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/globalize/active_record/instance_methods.rb', line 177

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

#saved_changesObject



206
207
208
209
210
211
212
213
214
# File 'lib/globalize/active_record/instance_methods.rb', line 206

def saved_changes
  super.tap do |changes|
    translation = translation_for(::Globalize.locale, false)
    if translation
      translation_changes = translation.saved_changes.select { |name| translated?(name) }
      changes.merge!(translation_changes) if translation_changes.any?
    end
  end
end

#set_translations(options) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Object



148
149
150
# File 'lib/globalize/active_record/instance_methods.rb', line 148

def translated_attribute_by_locale(name)
  translations_by_locale(&:"#{name}")
end

#translated_attributesObject



79
80
81
82
83
# 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

#translationObject



123
124
125
# File 'lib/globalize/active_record/instance_methods.rb', line 123

def translation
  translation_for(::Globalize.locale)
end

#translation_cachesObject



138
139
140
# File 'lib/globalize/active_record/instance_methods.rb', line 138

def translation_caches
  @translation_caches ||= {}
end

#translation_for(locale, build_if_missing = true) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# 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_localeObject



142
143
144
145
146
# 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_attributesObject

This method is basically the method built into Rails but we have to pass => false



87
88
89
90
91
# 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) ⇒ Object



40
41
42
43
44
45
46
# 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