| 1 |
module ActiveRecord |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
module Dirty |
|---|
| 37 |
def self.included(base) |
|---|
| 38 |
base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was' |
|---|
| 39 |
base.alias_method_chain :write_attribute, :dirty |
|---|
| 40 |
base.alias_method_chain :save, :dirty |
|---|
| 41 |
base.alias_method_chain :save!, :dirty |
|---|
| 42 |
base.alias_method_chain :update, :dirty |
|---|
| 43 |
|
|---|
| 44 |
base.superclass_delegating_accessor :partial_updates |
|---|
| 45 |
base.partial_updates = false |
|---|
| 46 |
end |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
def changed? |
|---|
| 53 |
!changed_attributes.empty? |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
def changed |
|---|
| 61 |
changed_attributes.keys |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
def changes |
|---|
| 69 |
changed.inject({}) { |h, attr| h[attr] = attribute_change(attr); h } |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
def save_with_dirty(*args) |
|---|
| 75 |
save_without_dirty(*args) |
|---|
| 76 |
ensure |
|---|
| 77 |
changed_attributes.clear |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
def save_with_dirty!(*args) |
|---|
| 82 |
save_without_dirty!(*args) |
|---|
| 83 |
ensure |
|---|
| 84 |
changed_attributes.clear |
|---|
| 85 |
end |
|---|
| 86 |
|
|---|
| 87 |
private |
|---|
| 88 |
|
|---|
| 89 |
def changed_attributes |
|---|
| 90 |
@changed_attributes ||= {} |
|---|
| 91 |
end |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
def attribute_changed?(attr) |
|---|
| 95 |
changed_attributes.include?(attr) |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
def attribute_change(attr) |
|---|
| 100 |
[changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) |
|---|
| 101 |
end |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
def attribute_was(attr) |
|---|
| 105 |
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) |
|---|
| 106 |
end |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
def attribute_will_change!(attr) |
|---|
| 110 |
changed_attributes[attr] = clone_attribute_value(:read_attribute, attr) |
|---|
| 111 |
end |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
def write_attribute_with_dirty(attr, value) |
|---|
| 115 |
attr = attr.to_s |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
unless changed_attributes.include?(attr) |
|---|
| 119 |
old = clone_attribute_value(:read_attribute, attr) |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
typecasted = if column = column_for_attribute(attr) |
|---|
| 123 |
column.type_cast(value) |
|---|
| 124 |
else |
|---|
| 125 |
value |
|---|
| 126 |
end |
|---|
| 127 |
changed_attributes[attr] = old unless old == typecasted |
|---|
| 128 |
end |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
write_attribute_without_dirty(attr, value) |
|---|
| 132 |
end |
|---|
| 133 |
|
|---|
| 134 |
def update_with_dirty |
|---|
| 135 |
if partial_updates? |
|---|
| 136 |
update_without_dirty(changed) |
|---|
| 137 |
else |
|---|
| 138 |
update_without_dirty |
|---|
| 139 |
end |
|---|
| 140 |
end |
|---|
| 141 |
end |
|---|
| 142 |
end |
|---|