Module: Globalize::ActiveRecord::AdapterDirty

Included in:
Adapter
Defined in:
lib/globalize/active_record/adapter_dirty.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dirtyObject



27
28
29
# File 'lib/globalize/active_record/adapter_dirty.rb', line 27

def dirty
  @dirty ||= {}
end

Instance Method Details

#_reset_attribute(name) ⇒ Object



45
46
47
# File 'lib/globalize/active_record/adapter_dirty.rb', line 45

def _reset_attribute name
  record.send(:clear_attribute_changes, [name])
end

#changedObject



66
67
68
# File 'lib/globalize/active_record/adapter_dirty.rb', line 66

def changed
  stash.keys.flat_map { |locale| changes(locale).keys }.uniq
end

#changed_attributes(locale) ⇒ Object



62
63
64
# File 'lib/globalize/active_record/adapter_dirty.rb', line 62

def changed_attributes(locale)
  Hash[changes(locale).map { |name, change| [name, change.first] }]
end

#changes(locale) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/globalize/active_record/adapter_dirty.rb', line 70

def changes(locale)
  stash[locale].keys.inject({}) do |hash, name|
    next hash unless dirty[name].is_a?(Hash)
    next hash unless dirty[name].key?(locale)

    new_value = stash[locale][name]
    old_value = dirty[name][locale]

    unless new_value == old_value
      hash[name] = [old_value, new_value]
    end

    hash
  end
end

#clear_dirtyObject



40
41
42
# File 'lib/globalize/active_record/adapter_dirty.rb', line 40

def clear_dirty
  self.dirty = {}
end

#resetObject



57
58
59
60
# File 'lib/globalize/active_record/adapter_dirty.rb', line 57

def reset
  clear_dirty
  super
end

#store_old_value(name, locale) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/globalize/active_record/adapter_dirty.rb', line 31

def store_old_value name, locale
  dirty[name] ||= {}
  unless dirty[name].key? locale
    old = fetch(locale, name)
    old = old.dup if old.duplicable?
    dirty[name][locale] = old
  end
end

#write(locale, name, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/globalize/active_record/adapter_dirty.rb', line 4

def write locale, name, value
  # Dirty tracking, paraphrased from
  # ActiveRecord::AttributeMethods::Dirty#write_attribute.
  name = name.to_s
  store_old_value name, locale
  old_values = dirty[name]
  old_value = old_values[locale]
  is_changed = record.send :attribute_changed?, name
  if is_changed && value == old_value
    # If there's already a change, delete it if this undoes the change.
    old_values.delete locale
    if old_values.empty?
      _reset_attribute name
    end
  elsif !is_changed
    # If there's not a change yet, record it.
    record.send(:attribute_will_change!, name) if old_value != value
  end

  super locale, name, value
end