Module: Globalize::ActiveRecord::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#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 }
end

#attribute_namesObject



61
62
63
# File 'lib/globalize/active_record/instance_methods.rb', line 61

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=(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 }
end

#available_localesObject

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



140
141
142
# File 'lib/globalize/active_record/instance_methods.rb', line 140

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

#column_for_attribute(name) ⇒ Object



158
159
160
161
162
# File 'lib/globalize/active_record/instance_methods.rb', line 158

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



144
145
146
# File 'lib/globalize/active_record/instance_methods.rb', line 144

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

#initialize_dup(other) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/globalize/active_record/instance_methods.rb', line 101

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/globalize/active_record/instance_methods.rb', line 44

def read_attribute(name, options = {})
  options = {:translated => true, :locale => nil}.merge(options)
  return super(name) unless options[:translated]

  if name == :locale
    self.try(:locale).presence || self.translation.locale
  elsif self.class.translated?(name)
    if (value = globalize.fetch(options[:locale] || Globalize.locale, name))
      value
    else
      super(name)
    end
  else
    super(name)
  end
end

#reload(options = nil) ⇒ Object



94
95
96
97
98
99
# File 'lib/globalize/active_record/instance_methods.rb', line 94

def reload(options = nil)
  translation_caches.clear
  translated_attribute_names.each { |name| @attributes.delete(name.to_s) }
  globalize.reset
  super(options)
end

#rollbackObject



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

def rollback
  translation_caches[::Globalize.locale] = translation.previous_version
end

#saveObject



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

def save(*)
  Globalize.with_locale(read_attribute(:locale) || I18n.default_locale) do
    super
  end
end

#set_translations(options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/globalize/active_record/instance_methods.rb', line 81

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
    end
    translation.save
  end
  globalize.reset
end

#translated_attribute_by_locale(name) ⇒ Object



135
136
137
# File 'lib/globalize/active_record/instance_methods.rb', line 135

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

#translated_attributesObject



67
68
69
70
71
# File 'lib/globalize/active_record/instance_methods.rb', line 67

def translated_attributes
  translated_attribute_names.inject({}) do |attributes, name|
    attributes.merge(name.to_s => self.send(name))
  end
end

#translationObject



110
111
112
# File 'lib/globalize/active_record/instance_methods.rb', line 110

def translation
  translation_for(::Globalize.locale)
end

#translation_cachesObject



125
126
127
# File 'lib/globalize/active_record/instance_methods.rb', line 125

def translation_caches
  @translation_caches ||= {}
end

#translation_for(locale, build_if_missing = true) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/globalize/active_record/instance_methods.rb', line 114

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



129
130
131
132
133
# File 'lib/globalize/active_record/instance_methods.rb', line 129

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



75
76
77
78
79
# File 'lib/globalize/active_record/instance_methods.rb', line 75

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/globalize/active_record/instance_methods.rb', line 22

def write_attribute(name, value, options = {})
  return super(name, value) unless translated?(name)

  options = {:locale => Globalize.locale}.merge(options)

  # Dirty tracking, paraphrased from
  # ActiveRecord::AttributeMethods::Dirty#write_attribute.
  name_str = name.to_s
  if attribute_changed?(name_str)
    # If there's already a change, delete it if this undoes the change.
    old = changed_attributes[name_str]
    changed_attributes.delete(name_str) if value == old
  else
    # If there's not a change yet, record it.
    old = globalize.fetch(options[:locale], name)
    old = old.dup if old.duplicable?
    changed_attributes[name_str] = old if value != old
  end

  globalize.write(options[:locale], name, value)
end