Module: Globalize::ActiveRecord::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#attribute_namesObject



42
43
44
# File 'lib/globalize/active_record/instance_methods.rb', line 42

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

#globalizeObject



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

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

#read_attribute(name, locale = nil) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/globalize/active_record/instance_methods.rb', line 34

def read_attribute(name, locale = nil)
  if self.class.translated?(name)
    globalize.fetch(locale || Globalize.locale, name)
  else
    super(name)
  end
end

#reload(options = nil) ⇒ Object



64
65
66
67
68
# File 'lib/globalize/active_record/instance_methods.rb', line 64

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

#set_translations(options) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/globalize/active_record/instance_methods.rb', line 56

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?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/globalize/active_record/instance_methods.rb', line 46

def translated?(name)
  self.class.translated?(name)
end

#translated_attributesObject



50
51
52
53
54
# File 'lib/globalize/active_record/instance_methods.rb', line 50

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

#update_attributes(attributes, *args) ⇒ Object



22
23
24
# File 'lib/globalize/active_record/instance_methods.rb', line 22

def update_attributes(attributes, *args)
  with_given_locale(attributes) { super }
end

#update_attributes!(attributes, *args) ⇒ Object



18
19
20
# File 'lib/globalize/active_record/instance_methods.rb', line 18

def update_attributes!(attributes, *args)
  with_given_locale(attributes) { super }
end

#write_attribute(name, value, locale = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/globalize/active_record/instance_methods.rb', line 26

def write_attribute(name, value, locale = nil)
  # Make sure that we return some value as some methods might 
  # rely on the data
  return_value = super(name, value)
  return_value = globalize.write(locale || Globalize.locale, name, value) if translated?(name)
  return_value
end