Skip to content

Commit cc2f2a8

Browse files
committed
Support methods in Dirty API added in Rails 5.2 - name_previously_changed? and name_previous_change
1 parent faeb555 commit cc2f2a8

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

lib/dynamoid/dirty.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Dirty
1818

1919
included do
2020
attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
21+
attribute_method_suffix '_previously_changed?', '_previous_change'
2122
attribute_method_affix prefix: 'restore_', suffix: '!'
2223
end
2324

@@ -140,6 +141,16 @@ def restore_attributes(attributes = changed)
140141
attributes.each { |attr| restore_attribute! attr }
141142
end
142143

144+
# Handles <tt>*_previously_changed?</tt> for +method_missing+.
145+
def attribute_previously_changed?(attr) #:nodoc:
146+
previous_changes_include?(attr)
147+
end
148+
149+
# Handles <tt>*_previous_change</tt> for +method_missing+.
150+
def attribute_previous_change(attr)
151+
previous_changes[attr] if attribute_previously_changed?(attr)
152+
end
153+
143154
private
144155

145156
def changes_include?(attr_name)
@@ -185,6 +196,12 @@ def restore_attribute!(attr)
185196
end
186197
end
187198

199+
# Returns +true+ if attr_name were changed before the model was saved,
200+
# +false+ otherwise.
201+
def previous_changes_include?(attr_name)
202+
previous_changes.include?(attr_name)
203+
end
204+
188205
# This is necessary because `changed_attributes` might be overridden in
189206
# other implemntations (e.g. in `ActiveRecord`)
190207
alias_method :attributes_changed_by_setter, :changed_attributes # :nodoc:

spec/dynamoid/dirty_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@
103103
'name' => ['Alex', 'Bob'],
104104
'updated_at' => ['2019-07-20 00:53:32'.to_datetime, '2019-07-20 20:11:01'.to_datetime]
105105
)
106+
107+
obj = model.create(name: 'Alex')
108+
# there are also changes for `created_at` and `updated_at` - just don't check them
109+
expect(obj.previous_changes).to include('id' => [nil, obj.id], 'name' => [nil, 'Alex'])
106110
end
107111

108112
it 'returns {} when there were no changes made before saving' do
@@ -153,6 +157,48 @@
153157
end
154158
end
155159

160+
describe '<attribute>_previously_changed?' do
161+
it 'returns true if attribute was changed before model was saved' do
162+
obj = model.create(name: 'Alex')
163+
obj.name = 'Bob'
164+
obj.save
165+
expect(obj.name_previously_changed?).to eq(true)
166+
167+
obj = model.create(name: 'Alex')
168+
expect(obj.name_previously_changed?).to eq(true)
169+
end
170+
171+
it 'returns false otherwise' do
172+
obj = model.create(name: 'Alex')
173+
obj = model.find(obj.id)
174+
expect(obj.name_previously_changed?).to eq(false)
175+
176+
obj = model.new(name: 'Alex')
177+
expect(obj.name_previously_changed?).to eq(false)
178+
end
179+
end
180+
181+
describe '<attribute>_previous_change' do
182+
it 'returns an array of old and changed attribute value before the model was saved' do
183+
obj = model.create(name: 'Alex')
184+
obj.name = 'Bob'
185+
obj.save
186+
expect(obj.name_previous_change).to eq(['Alex', 'Bob'])
187+
188+
obj = model.create(name: 'Alex')
189+
expect(obj.name_previous_change).to eq([nil, 'Alex'])
190+
end
191+
192+
it 'returns nil when there were no changes made before saving' do
193+
obj = model.create(name: 'Alex')
194+
obj = model.find(obj.id)
195+
expect(obj.name_previous_change).to eq(nil)
196+
197+
obj = model.new(name: 'Alex')
198+
expect(obj.name_previous_change).to eq(nil)
199+
end
200+
end
201+
156202
describe '<attribute>_will_change!' do
157203
it 'marks that the attribute is changing' do
158204
obj = model.create(name: 'Alex')

0 commit comments

Comments
 (0)