Module: Globalize::ActiveRecord::InstanceMethods

Defined in:
lib/globalize/active_record.rb

Instance Method Summary collapse

Instance Method Details

#attribute_namesObject



173
174
175
# File 'lib/globalize/active_record.rb', line 173

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

#attributesObject



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

def attributes
  self.attribute_names.inject({}) do |attrs, name|
    attrs[name] = read_attribute(name) ||
      (globalize.fetch(I18n.locale, name) rescue nil)
    attrs
  end
end

#attributes=(attributes, *args) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/globalize/active_record.rb', line 165

def attributes=(attributes, *args)
  if attributes.respond_to?(:delete) && locale = attributes.delete(:locale)
    self.class.with_locale(locale) { super }
  else
    super
  end
end

#available_localesObject



177
178
179
# File 'lib/globalize/active_record.rb', line 177

def available_locales
  translations.scoped(:select => 'DISTINCT locale').map(&:locale)
end

#globalizeObject



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

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

#reload(options = nil) ⇒ Object



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

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

#set_translations(options) ⇒ Object



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

def set_translations(options)
  options.keys.each do |locale|
    translation = translations.find_by_locale(locale.to_s) ||
      translations.build(:locale => locale.to_s)
    translation.update_attributes!(options[locale])
  end
end

#translated_attributesObject



185
186
187
188
189
# File 'lib/globalize/active_record.rb', line 185

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

#translated_localesObject



181
182
183
# File 'lib/globalize/active_record.rb', line 181

def translated_locales
  translations.map(&:locale)
end