|
103 | 103 | 'name' => ['Alex', 'Bob'], |
104 | 104 | 'updated_at' => ['2019-07-20 00:53:32'.to_datetime, '2019-07-20 20:11:01'.to_datetime] |
105 | 105 | ) |
| 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']) |
106 | 110 | end |
107 | 111 |
|
108 | 112 | it 'returns {} when there were no changes made before saving' do |
|
153 | 157 | end |
154 | 158 | end |
155 | 159 |
|
| 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 | + |
156 | 202 | describe '<attribute>_will_change!' do |
157 | 203 | it 'marks that the attribute is changing' do |
158 | 204 | obj = model.create(name: 'Alex') |
|
0 commit comments