Module: SingleTableGlobalize3::ActiveRecord::AttributeReadWrite

Included in:
InstanceMethods
Defined in:
lib/single_table_globalize3/active_record/attribute_read_write.rb

Instance Method Summary collapse

Instance Method Details

#attribute_namesObject



48
49
50
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 48

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

#attributesObject



5
6
7
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 5

def attributes
  super.merge(translated_attributes)
end

#deprecated_options(options) ⇒ Object

Deprecate old use of locale



10
11
12
13
14
15
16
17
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 10

def deprecated_options(options)
  unless options.is_a?(Hash)
    warn "[DEPRECATION] passing 'locale' as #{options.inspect} is deprecated. Please use {:locale => #{options.inspect}} instead."
    {:locale => options}
  else
    options
  end
end

#read_attribute(name, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 39

def read_attribute(name, options = {})
  options = {:translated => true, :locale => nil}.merge(deprecated_options(options))
  if (self.class.translated?(name) && options[:translated]) && (value = globalize.fetch(options[:locale] || SingleTableGlobalize3.locale, name))
    value
  else
    super(name)
  end
end

#translated?(name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 52

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

#translated_attributesObject



56
57
58
59
60
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 56

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

#untranslated_attributesObject

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



64
65
66
67
68
69
70
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 64

def untranslated_attributes
  attrs = {}
  attribute_names.each do |name|
    attrs[name] = read_attribute(name, {:translated => false})
  end
  attrs
end

#write_attribute(name, value, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/single_table_globalize3/active_record/attribute_read_write.rb', line 19

def write_attribute(name, value, options = {})
  if translated?(name)
    options = {:locale => SingleTableGlobalize3.locale}.merge(deprecated_options(options))

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

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