couchrest_model 2.1.0.rc1 → 2.2.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.travis.yml +15 -4
- data/Gemfile.activesupport-4.x +4 -0
- data/Gemfile.activesupport-5.x +4 -0
- data/README.md +2 -0
- data/VERSION +1 -1
- data/couchrest_model.gemspec +3 -2
- data/history.md +14 -1
- data/lib/couchrest/model/associations.rb +3 -8
- data/lib/couchrest/model/base.rb +15 -7
- data/lib/couchrest/model/casted_array.rb +22 -34
- data/lib/couchrest/model/configuration.rb +2 -0
- data/lib/couchrest/model/design.rb +4 -3
- data/lib/couchrest/model/designs/view.rb +37 -32
- data/lib/couchrest/model/dirty.rb +93 -19
- data/lib/couchrest/model/embeddable.rb +2 -14
- data/lib/couchrest/model/extended_attachments.rb +2 -4
- data/lib/couchrest/model/persistence.rb +14 -17
- data/lib/couchrest/model/properties.rb +46 -54
- data/lib/couchrest/model/property.rb +0 -3
- data/lib/couchrest/model/proxyable.rb +20 -4
- data/lib/couchrest/model/validations/uniqueness.rb +4 -1
- data/lib/couchrest_model.rb +2 -2
- data/spec/fixtures/models/article.rb +1 -1
- data/spec/fixtures/models/card.rb +2 -1
- data/spec/fixtures/models/person.rb +1 -0
- data/spec/fixtures/models/project.rb +3 -0
- data/spec/unit/assocations_spec.rb +73 -73
- data/spec/unit/attachment_spec.rb +34 -34
- data/spec/unit/base_spec.rb +102 -102
- data/spec/unit/casted_array_spec.rb +7 -7
- data/spec/unit/casted_spec.rb +7 -7
- data/spec/unit/configuration_spec.rb +11 -11
- data/spec/unit/connection_spec.rb +30 -30
- data/spec/unit/core_extensions/{time_parsing.rb → time_parsing_spec.rb} +21 -21
- data/spec/unit/design_spec.rb +38 -38
- data/spec/unit/designs/design_mapper_spec.rb +26 -26
- data/spec/unit/designs/migrations_spec.rb +13 -13
- data/spec/unit/designs/view_spec.rb +319 -274
- data/spec/unit/designs_spec.rb +39 -39
- data/spec/unit/dirty_spec.rb +188 -103
- data/spec/unit/embeddable_spec.rb +119 -117
- data/spec/unit/inherited_spec.rb +4 -4
- data/spec/unit/persistence_spec.rb +122 -122
- data/spec/unit/properties_spec.rb +466 -16
- data/spec/unit/property_protection_spec.rb +32 -32
- data/spec/unit/property_spec.rb +45 -436
- data/spec/unit/proxyable_spec.rb +140 -82
- data/spec/unit/subclass_spec.rb +14 -14
- data/spec/unit/translations_spec.rb +5 -5
- data/spec/unit/typecast_spec.rb +131 -131
- data/spec/unit/utils/migrate_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +31 -31
- metadata +27 -12
- data/lib/couchrest/model/casted_hash.rb +0 -84
data/spec/unit/designs_spec.rb
CHANGED
@@ -4,7 +4,7 @@ require "spec_helper"
|
|
4
4
|
describe CouchRest::Model::Designs do
|
5
5
|
|
6
6
|
it "should accessable from model" do
|
7
|
-
DesignModel.respond_to?(:design).
|
7
|
+
expect(DesignModel.respond_to?(:design)).to be_truthy
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "class methods" do
|
@@ -18,35 +18,35 @@ describe CouchRest::Model::Designs do
|
|
18
18
|
describe "without block" do
|
19
19
|
it "should create design_doc and all methods" do
|
20
20
|
@klass.design
|
21
|
-
@klass.
|
22
|
-
@klass.
|
21
|
+
expect(@klass).to respond_to(:design_doc)
|
22
|
+
expect(@klass).to respond_to(:all)
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should created named design_doc method and not all" do
|
26
26
|
@klass.design :stats
|
27
|
-
@klass.
|
28
|
-
@klass.
|
27
|
+
expect(@klass).to respond_to(:stats_design_doc)
|
28
|
+
expect(@klass).not_to respond_to(:all)
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should have added itself to a design_blocks array" do
|
32
32
|
@klass.design
|
33
33
|
blocks = @klass.instance_variable_get(:@_design_blocks)
|
34
|
-
blocks.length.
|
35
|
-
blocks.first.
|
34
|
+
expect(blocks.length).to eql(1)
|
35
|
+
expect(blocks.first).to eql({:args => [], :block => nil})
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should have added itself to a design_blocks array" do
|
39
39
|
@klass.design
|
40
40
|
blocks = @klass.instance_variable_get(:@_design_blocks)
|
41
|
-
blocks.length.
|
42
|
-
blocks.first.
|
41
|
+
expect(blocks.length).to eql(1)
|
42
|
+
expect(blocks.first).to eql({:args => [], :block => nil})
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should have added itself to a design_blocks array with prefix" do
|
46
46
|
@klass.design :stats
|
47
47
|
blocks = @klass.instance_variable_get(:@_design_blocks)
|
48
|
-
blocks.length.
|
49
|
-
blocks.first.
|
48
|
+
expect(blocks.length).to eql(1)
|
49
|
+
expect(blocks.first).to eql({:args => [:stats], :block => nil})
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -59,13 +59,13 @@ describe CouchRest::Model::Designs do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should pass calls to mapper" do
|
62
|
-
@klass.design_doc.auto_update.
|
62
|
+
expect(@klass.design_doc.auto_update).to be_falsey
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should have added itself to a design_blocks array" do
|
66
66
|
blocks = @klass.instance_variable_get(:@_design_blocks)
|
67
|
-
blocks.length.
|
68
|
-
blocks.first.
|
67
|
+
expect(blocks.length).to eql(1)
|
68
|
+
expect(blocks.first).to eql({:args => [], :block => @block})
|
69
69
|
end
|
70
70
|
|
71
71
|
it "should handle multiple designs" do
|
@@ -74,9 +74,9 @@ describe CouchRest::Model::Designs do
|
|
74
74
|
end
|
75
75
|
@klass.design :stats, &@block2
|
76
76
|
blocks = @klass.instance_variable_get(:@_design_blocks)
|
77
|
-
blocks.length.
|
78
|
-
blocks.first.
|
79
|
-
blocks.last.
|
77
|
+
expect(blocks.length).to eql(2)
|
78
|
+
expect(blocks.first).to eql({:args => [], :block => @block})
|
79
|
+
expect(blocks.last).to eql({:args => [:stats], :block => @block2})
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
@@ -92,21 +92,21 @@ describe CouchRest::Model::Designs do
|
|
92
92
|
end
|
93
93
|
|
94
94
|
it "should add designs to sub module" do
|
95
|
-
@klass.
|
95
|
+
expect(@klass).to respond_to(:design_doc)
|
96
96
|
end
|
97
97
|
|
98
98
|
end
|
99
99
|
|
100
100
|
describe "default_per_page" do
|
101
101
|
it "should return 25 default" do
|
102
|
-
DesignModel.default_per_page.
|
102
|
+
expect(DesignModel.default_per_page).to eql(25)
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
106
|
describe ".paginates_per" do
|
107
107
|
it "should set the default per page value" do
|
108
108
|
DesignModel.paginates_per(21)
|
109
|
-
DesignModel.default_per_page.
|
109
|
+
expect(DesignModel.default_per_page).to eql(21)
|
110
110
|
end
|
111
111
|
end
|
112
112
|
end
|
@@ -136,12 +136,12 @@ describe CouchRest::Model::Designs do
|
|
136
136
|
|
137
137
|
it "will fail if reduce is not specific in view" do
|
138
138
|
@mod.create(:title => 'This is a test')
|
139
|
-
|
139
|
+
expect { @mod.by_title_fail.first }.to raise_error(CouchRest::NotFound)
|
140
140
|
end
|
141
141
|
|
142
142
|
it "will perform view request" do
|
143
143
|
@mod.create(:title => 'This is a test')
|
144
|
-
@mod.by_title.first.title.
|
144
|
+
expect(@mod.by_title.first.title).to eql("This is a test")
|
145
145
|
end
|
146
146
|
|
147
147
|
end
|
@@ -159,28 +159,28 @@ describe CouchRest::Model::Designs do
|
|
159
159
|
|
160
160
|
it "should return single matched record with find helper" do
|
161
161
|
course = Course.find_by_title('bbb')
|
162
|
-
course.
|
163
|
-
course.title.
|
162
|
+
expect(course).not_to be_nil
|
163
|
+
expect(course.title).to eql('bbb') # Ensure really is a Course!
|
164
164
|
end
|
165
165
|
|
166
166
|
it "should return nil if not found" do
|
167
167
|
course = Course.find_by_title('fff')
|
168
|
-
course.
|
168
|
+
expect(course).to be_nil
|
169
169
|
end
|
170
170
|
|
171
171
|
it "should peform search on view with two properties" do
|
172
172
|
course = Course.find_by_title_and_active(['bbb', true])
|
173
|
-
course.
|
174
|
-
course.title.
|
173
|
+
expect(course).not_to be_nil
|
174
|
+
expect(course.title).to eql('bbb') # Ensure really is a Course!
|
175
175
|
end
|
176
176
|
|
177
177
|
it "should return nil if not found" do
|
178
178
|
course = Course.find_by_title_and_active(['bbb', false])
|
179
|
-
course.
|
179
|
+
expect(course).to be_nil
|
180
180
|
end
|
181
181
|
|
182
182
|
it "should raise exception if view not present" do
|
183
|
-
|
183
|
+
expect { Course.find_by_foobar('123') }.to raise_error(NoMethodError)
|
184
184
|
end
|
185
185
|
|
186
186
|
end
|
@@ -226,15 +226,15 @@ describe CouchRest::Model::Designs do
|
|
226
226
|
it "should make the design doc upon first query" do
|
227
227
|
Unattached.by_title.database(@db)
|
228
228
|
doc = Unattached.design_doc
|
229
|
-
doc['views']['all']['map'].
|
229
|
+
expect(doc['views']['all']['map']).to include('Unattached')
|
230
230
|
end
|
231
231
|
it "should merge query params" do
|
232
232
|
rs = Unattached.by_title.database(@db).startkey("bbb").endkey("eee")
|
233
|
-
rs.length.
|
233
|
+
expect(rs.length).to eq(3)
|
234
234
|
end
|
235
235
|
it "should get from specific database" do
|
236
236
|
u = Unattached.get(@first_id, @db)
|
237
|
-
u.title.
|
237
|
+
expect(u.title).to eq("aaa")
|
238
238
|
end
|
239
239
|
it "should barf on first if no database given" do
|
240
240
|
expect{ Unattached.first }.to raise_error(CouchRest::Model::DatabaseNotDefined)
|
@@ -246,7 +246,7 @@ describe CouchRest::Model::Designs do
|
|
246
246
|
end
|
247
247
|
it "should get last" do
|
248
248
|
u = Unattached.all.database(@db).last
|
249
|
-
u.title.
|
249
|
+
expect(u.title).to eq("aaa")
|
250
250
|
end
|
251
251
|
|
252
252
|
end
|
@@ -268,18 +268,18 @@ describe CouchRest::Model::Designs do
|
|
268
268
|
it "should create the design doc" do
|
269
269
|
Article.by_user_id_and_date rescue nil
|
270
270
|
doc = Article.design_doc
|
271
|
-
doc['views']['by_date'].
|
271
|
+
expect(doc['views']['by_date']).not_to be_nil
|
272
272
|
end
|
273
273
|
it "should sort correctly" do
|
274
274
|
articles = Article.by_user_id_and_date.all
|
275
|
-
articles.collect{|a|a['user_id']}.
|
276
|
-
'quentin']
|
277
|
-
articles[1].title.
|
275
|
+
expect(articles.collect{|a|a['user_id']}).to eq(['aaron', 'aaron', 'quentin',
|
276
|
+
'quentin'])
|
277
|
+
expect(articles[1].title).to eq('not junk')
|
278
278
|
end
|
279
279
|
it "should be queryable with couchrest options" do
|
280
280
|
articles = Article.by_user_id_and_date(:limit => 1, :startkey => 'quentin').all
|
281
|
-
articles.length.
|
282
|
-
articles[0].title.
|
281
|
+
expect(articles.length).to eq(1)
|
282
|
+
expect(articles[0].title).to eq("even more interesting")
|
283
283
|
end
|
284
284
|
end
|
285
285
|
|
data/spec/unit/dirty_spec.rb
CHANGED
@@ -37,12 +37,8 @@ describe "Dirty" do
|
|
37
37
|
it "should return changes on an attribute" do
|
38
38
|
@card = Card.new(:first_name => "matt")
|
39
39
|
@card.first_name = "andrew"
|
40
|
-
@card.first_name_changed
|
41
|
-
|
42
|
-
@card.changes.should == { "first_name" => [nil, "andrew"] }
|
43
|
-
else
|
44
|
-
@card.changes.should == { "first_name" => ["matt", "andrew"] }
|
45
|
-
end
|
40
|
+
expect(@card.first_name_changed?).to be_truthy
|
41
|
+
expect(@card.changes).to eq([ ["+", "first_name", "andrew"] ])
|
46
42
|
end
|
47
43
|
end
|
48
44
|
|
@@ -50,8 +46,8 @@ describe "Dirty" do
|
|
50
46
|
it "should return changes on an attribute" do
|
51
47
|
@card = Card.create!(:first_name => "matt")
|
52
48
|
@card.first_name = "andrew"
|
53
|
-
@card.first_name_changed
|
54
|
-
@card.changes.
|
49
|
+
expect(@card.first_name_changed?).to be_truthy
|
50
|
+
expect(@card.changes).to eq([["~", "first_name", "matt", "andrew"]])
|
55
51
|
end
|
56
52
|
end
|
57
53
|
|
@@ -62,7 +58,7 @@ describe "Dirty" do
|
|
62
58
|
it "should not save unchanged records" do
|
63
59
|
card_id = Card.create!(:first_name => "matt").id
|
64
60
|
@card = Card.find(card_id)
|
65
|
-
@card.database.
|
61
|
+
expect(@card.database).not_to receive(:save_doc)
|
66
62
|
@card.save
|
67
63
|
end
|
68
64
|
|
@@ -70,7 +66,7 @@ describe "Dirty" do
|
|
70
66
|
card_id = Card.create!(:first_name => "matt").id
|
71
67
|
@card = Card.find(card_id)
|
72
68
|
@card.first_name = "andrew"
|
73
|
-
@card.database.
|
69
|
+
expect(@card.database).to receive(:save_doc).and_return({"ok" => true})
|
74
70
|
@card.save
|
75
71
|
end
|
76
72
|
|
@@ -81,77 +77,77 @@ describe "Dirty" do
|
|
81
77
|
# match activerecord behaviour
|
82
78
|
it "should report no changes on a new object with no attributes set" do
|
83
79
|
@card = Card.new
|
84
|
-
@card.changed
|
80
|
+
expect(@card.changed?).to be_falsey
|
85
81
|
end
|
86
82
|
|
87
83
|
it "should report no changes on a hash property with a default value" do
|
88
84
|
@obj = DirtyModel.new
|
89
|
-
@obj.
|
85
|
+
expect(@obj.details_changed?).to be_falsey
|
90
86
|
end
|
91
87
|
|
92
88
|
# match activerecord behaviour
|
93
89
|
it "should report changes on a new object with attributes set" do
|
94
90
|
@card = Card.new(:first_name => "matt")
|
95
|
-
@card.changed
|
91
|
+
expect(@card.changed?).to be_truthy
|
96
92
|
end
|
97
93
|
|
98
94
|
it "should report no changes on new object with 'unique_id' set" do
|
99
95
|
@obj = DirtyUniqueIdModel.new
|
100
|
-
@obj.changed
|
101
|
-
@obj.changes.
|
96
|
+
expect(@obj.changed?).to be_falsey
|
97
|
+
expect(@obj.changes).to be_empty
|
102
98
|
end
|
103
99
|
|
104
100
|
it "should report no changes on objects fetched from the database" do
|
105
101
|
card_id = Card.create!(:first_name => "matt").id
|
106
102
|
@card = Card.find(card_id)
|
107
|
-
@card.changed
|
103
|
+
expect(@card.changed?).to be_falsey
|
108
104
|
end
|
109
105
|
|
110
106
|
it "should report changes if the record is modified" do
|
111
107
|
@card = Card.new
|
112
108
|
@card.first_name = "andrew"
|
113
|
-
@card.changed
|
114
|
-
@card.first_name_changed
|
109
|
+
expect(@card.changed?).to be_truthy
|
110
|
+
expect(@card.first_name_changed?).to be_truthy
|
115
111
|
end
|
116
112
|
|
117
113
|
it 'should report changes if the record is modified by attributes' do
|
118
114
|
@card = Card.new
|
119
115
|
@card.attributes = {:first_name => 'danny'}
|
120
|
-
@card.changed
|
121
|
-
@card.first_name_changed
|
116
|
+
expect(@card.changed?).to be_truthy
|
117
|
+
expect(@card.first_name_changed?).to be_truthy
|
122
118
|
end
|
123
119
|
|
124
120
|
it 'should report no changes if the record is modified with an invalid property by attributes' do
|
125
121
|
@card = Card.new
|
126
122
|
@card.attributes = {:middle_name => 'danny'}
|
127
|
-
@card.changed
|
128
|
-
@card.first_name_changed
|
123
|
+
expect(@card.changed?).to be_falsey
|
124
|
+
expect(@card.first_name_changed?).to be_falsey
|
129
125
|
end
|
130
126
|
|
131
127
|
it "should report no changes if the record is modified with update_attributes" do
|
132
128
|
@card = Card.new
|
133
129
|
@card.update_attributes(:first_name => 'henry')
|
134
|
-
@card.changed
|
130
|
+
expect(@card.changed?).to be_falsey
|
135
131
|
end
|
136
132
|
|
137
133
|
it "should report no changes if the record is modified with an invalid property by update_attributes" do
|
138
134
|
@card = Card.new
|
139
135
|
@card.update_attributes(:middle_name => 'peter')
|
140
|
-
@card.changed
|
136
|
+
expect(@card.changed?).to be_falsey
|
141
137
|
end
|
142
138
|
|
143
139
|
it "should report no changes for unmodified records" do
|
144
140
|
card_id = Card.create!(:first_name => "matt").id
|
145
141
|
@card = Card.find(card_id)
|
146
142
|
@card.first_name = "matt"
|
147
|
-
@card.changed
|
148
|
-
@card.first_name_changed
|
143
|
+
expect(@card.changed?).to be_falsey
|
144
|
+
expect(@card.first_name_changed?).to be_falsey
|
149
145
|
end
|
150
146
|
|
151
147
|
it "should report no changes after a new record has been saved" do
|
152
148
|
@card = Card.new(:first_name => "matt")
|
153
149
|
@card.save!
|
154
|
-
@card.changed
|
150
|
+
expect(@card.changed?).to be_falsey
|
155
151
|
end
|
156
152
|
|
157
153
|
it "should report no changes after a record has been saved" do
|
@@ -159,7 +155,7 @@ describe "Dirty" do
|
|
159
155
|
@card = Card.find(card_id)
|
160
156
|
@card.first_name = "andrew"
|
161
157
|
@card.save!
|
162
|
-
@card.changed
|
158
|
+
expect(@card.changed?).to be_falsey
|
163
159
|
end
|
164
160
|
|
165
161
|
# test changing list properties
|
@@ -168,14 +164,14 @@ describe "Dirty" do
|
|
168
164
|
cat_id = Cat.create!(:name => "Felix", :toys => [{:name => "Mouse"}]).id
|
169
165
|
@cat = Cat.find(cat_id)
|
170
166
|
@cat.toys = [{:name => "Feather"}]
|
171
|
-
@cat.changed
|
167
|
+
expect(@cat.changed?).to be_truthy
|
172
168
|
end
|
173
169
|
|
174
170
|
it "should report no changes if a list property is unmodified" do
|
175
171
|
cat_id = Cat.create!(:name => "Felix", :toys => [{:name => "Mouse"}]).id
|
176
172
|
@cat = Cat.find(cat_id)
|
177
173
|
@cat.toys = [{:name => "Mouse"}] # same as original list
|
178
|
-
@cat.changed
|
174
|
+
expect(@cat.changed?).to be_falsey
|
179
175
|
end
|
180
176
|
|
181
177
|
# attachments
|
@@ -185,7 +181,7 @@ describe "Dirty" do
|
|
185
181
|
@file = File.open(FIXTURE_PATH + '/attachments/test.html')
|
186
182
|
@cat = Cat.find(cat_id)
|
187
183
|
@cat.create_attachment(:file => @file, :name => "my_attachment")
|
188
|
-
@cat.changed
|
184
|
+
expect(@cat.changed?).to be_truthy
|
189
185
|
end
|
190
186
|
|
191
187
|
it "should report changes if an attachment is deleted" do
|
@@ -196,7 +192,7 @@ describe "Dirty" do
|
|
196
192
|
@cat.save
|
197
193
|
@cat = Cat.find(@cat.id)
|
198
194
|
@cat.delete_attachment(@attachment_name)
|
199
|
-
@cat.changed
|
195
|
+
expect(@cat.changed?).to be_truthy
|
200
196
|
end
|
201
197
|
|
202
198
|
# casted models
|
@@ -205,29 +201,29 @@ describe "Dirty" do
|
|
205
201
|
@cat = Cat.create!(:name => "Felix", :favorite_toy => { :name => "Mouse" })
|
206
202
|
@cat = Cat.find(@cat.id)
|
207
203
|
@cat.favorite_toy.name = 'Feather'
|
208
|
-
@cat.changed
|
204
|
+
expect(@cat.changed?).to be_truthy
|
209
205
|
end
|
210
206
|
|
211
207
|
it "should report changes to casted model in array" do
|
212
208
|
@obj = Cat.create!(:name => 'felix', :toys => [{:name => "Catnip"}])
|
213
209
|
@obj = Cat.get(@obj.id)
|
214
|
-
@obj.toys.first.name.
|
215
|
-
@obj.toys.first.changed
|
216
|
-
@obj.changed
|
210
|
+
expect(@obj.toys.first.name).to eql('Catnip')
|
211
|
+
expect(@obj.toys.first.changed?).to be_falsey
|
212
|
+
expect(@obj.changed?).to be_falsey
|
217
213
|
@obj.toys.first.name = "Super Catnip"
|
218
|
-
@obj.toys.first.changed
|
219
|
-
@obj.changed
|
214
|
+
expect(@obj.toys.first.changed?).to be_truthy
|
215
|
+
expect(@obj.changed?).to be_truthy
|
220
216
|
end
|
221
217
|
|
222
218
|
it "should report changes to anonymous casted models in array" do
|
223
219
|
@obj = DirtyModel.create!(:sub_models => [{:title => "Sample"}])
|
224
220
|
@obj = DirtyModel.get(@obj.id)
|
225
|
-
@obj.sub_models.first.title.
|
226
|
-
@obj.sub_models.first.changed
|
227
|
-
@obj.changed
|
221
|
+
expect(@obj.sub_models.first.title).to eql("Sample")
|
222
|
+
expect(@obj.sub_models.first.changed?).to be_falsey
|
223
|
+
expect(@obj.changed?).to be_falsey
|
228
224
|
@obj.sub_models.first.title = "Another Sample"
|
229
|
-
@obj.sub_models.first.changed
|
230
|
-
@obj.changed
|
225
|
+
expect(@obj.sub_models.first.changed?).to be_truthy
|
226
|
+
expect(@obj.changed?).to be_truthy
|
231
227
|
end
|
232
228
|
|
233
229
|
# casted arrays
|
@@ -235,63 +231,62 @@ describe "Dirty" do
|
|
235
231
|
def test_casted_array(change_expected)
|
236
232
|
obj = DirtyModel.create!
|
237
233
|
obj = DirtyModel.get(obj.id)
|
238
|
-
|
239
|
-
yield array, obj
|
234
|
+
yield obj
|
240
235
|
if change_expected
|
241
|
-
obj.changed
|
236
|
+
expect(obj.changed?).to be_truthy
|
242
237
|
else
|
243
|
-
obj.changed
|
238
|
+
expect(obj.changed?).to be_falsey
|
244
239
|
end
|
245
240
|
end
|
246
241
|
|
247
242
|
def should_change_array
|
248
|
-
test_casted_array(true) { |a
|
243
|
+
test_casted_array(true) { |a| yield a }
|
249
244
|
end
|
250
245
|
|
251
246
|
def should_not_change_array
|
252
|
-
test_casted_array(false) { |a
|
247
|
+
test_casted_array(false) { |a| yield a }
|
253
248
|
end
|
254
249
|
|
255
250
|
it "should report changes if an array index is modified" do
|
256
|
-
should_change_array do |
|
257
|
-
|
251
|
+
should_change_array do |obj|
|
252
|
+
obj.keywords[0] = "keyword"
|
258
253
|
end
|
259
254
|
end
|
260
255
|
|
261
256
|
it "should report no changes if an array index is unmodified" do
|
262
|
-
should_not_change_array do |
|
263
|
-
|
257
|
+
should_not_change_array do |obj|
|
258
|
+
obj.keywords[0] = obj.keywords[0]
|
264
259
|
end
|
265
260
|
end
|
266
261
|
|
267
262
|
it "should report changes if an array is appended with <<" do
|
268
|
-
should_change_array do |
|
269
|
-
|
263
|
+
should_change_array do |obj|
|
264
|
+
obj.keywords << 'keyword'
|
270
265
|
end
|
271
266
|
end
|
272
267
|
|
273
268
|
it "should report changes if item is inserted into array" do
|
274
|
-
should_change_array do |
|
275
|
-
|
276
|
-
obj.keywords[0].
|
269
|
+
should_change_array do |obj|
|
270
|
+
obj.keywords.insert(0, 'keyword')
|
271
|
+
expect(obj.keywords[0]).to eql('keyword')
|
277
272
|
end
|
278
273
|
end
|
279
274
|
|
280
275
|
it "should report changes if items are inserted into array" do
|
281
|
-
should_change_array do |
|
282
|
-
|
283
|
-
obj.keywords[2].
|
276
|
+
should_change_array do |obj|
|
277
|
+
obj.keywords.insert(1, 'keyword', 'keyword2')
|
278
|
+
expect(obj.keywords[2]).to eql('keyword2')
|
284
279
|
end
|
285
280
|
end
|
286
281
|
|
287
282
|
it "should report changes if an array is popped" do
|
288
|
-
should_change_array do |
|
289
|
-
|
283
|
+
should_change_array do |obj|
|
284
|
+
obj.keywords.pop
|
290
285
|
end
|
291
286
|
end
|
292
287
|
|
293
288
|
it "should report changes if an array is popped after reload" do
|
294
|
-
should_change_array do |
|
289
|
+
should_change_array do |obj|
|
295
290
|
obj.reload
|
296
291
|
obj.keywords.pop
|
297
292
|
end
|
@@ -299,86 +294,86 @@ describe "Dirty" do
|
|
299
294
|
|
300
295
|
|
301
296
|
it "should report no changes if an empty array is popped" do
|
302
|
-
should_not_change_array do |
|
303
|
-
|
297
|
+
should_not_change_array do |obj|
|
298
|
+
obj.keywords.clear
|
304
299
|
obj.save! # clears changes
|
305
|
-
|
300
|
+
obj.keywords.pop
|
306
301
|
end
|
307
302
|
end
|
308
303
|
|
309
304
|
it "should report changes on deletion from an array" do
|
310
|
-
should_change_array do |
|
311
|
-
|
305
|
+
should_change_array do |obj|
|
306
|
+
obj.keywords << "keyword"
|
312
307
|
obj.save!
|
313
|
-
|
308
|
+
obj.keywords.delete_at(0)
|
314
309
|
end
|
315
310
|
|
316
|
-
should_change_array do |
|
317
|
-
|
311
|
+
should_change_array do |obj|
|
312
|
+
obj.keywords << "keyword"
|
318
313
|
obj.save!
|
319
|
-
|
314
|
+
obj.keywords.delete("keyword")
|
320
315
|
end
|
321
316
|
end
|
322
317
|
|
323
318
|
it "should report changes on deletion from an array after reload" do
|
324
|
-
should_change_array do |
|
325
|
-
|
319
|
+
should_change_array do |obj|
|
320
|
+
obj.keywords << "keyword"
|
326
321
|
obj.save!
|
327
322
|
obj.reload
|
328
|
-
|
323
|
+
obj.keywords.delete_at(0)
|
329
324
|
end
|
330
325
|
|
331
|
-
should_change_array do |
|
332
|
-
|
326
|
+
should_change_array do |obj|
|
327
|
+
obj.keywords << "keyword"
|
333
328
|
obj.save!
|
334
329
|
obj.reload
|
335
|
-
|
330
|
+
obj.keywords.delete("keyword")
|
336
331
|
end
|
337
332
|
end
|
338
333
|
|
339
334
|
it "should report no changes on deletion from an empty array" do
|
340
|
-
should_not_change_array do |
|
341
|
-
|
335
|
+
should_not_change_array do |obj|
|
336
|
+
obj.keywords.clear
|
342
337
|
obj.save!
|
343
|
-
|
338
|
+
obj.keywords.delete_at(0)
|
344
339
|
end
|
345
340
|
|
346
|
-
should_not_change_array do |
|
347
|
-
|
341
|
+
should_not_change_array do |obj|
|
342
|
+
obj.keywords.clear
|
348
343
|
obj.save!
|
349
|
-
|
344
|
+
obj.keywords.delete("keyword")
|
350
345
|
end
|
351
346
|
end
|
352
347
|
|
353
348
|
it "should report changes if an array is pushed" do
|
354
|
-
should_change_array do |
|
355
|
-
|
349
|
+
should_change_array do |obj|
|
350
|
+
obj.keywords.push("keyword")
|
356
351
|
end
|
357
352
|
end
|
358
353
|
|
359
354
|
it "should report changes if an array is shifted" do
|
360
|
-
should_change_array do |
|
361
|
-
|
355
|
+
should_change_array do |obj|
|
356
|
+
obj.keywords.shift
|
362
357
|
end
|
363
358
|
end
|
364
359
|
|
365
360
|
it "should report no changes if an empty array is shifted" do
|
366
|
-
should_not_change_array do |
|
367
|
-
|
361
|
+
should_not_change_array do |obj|
|
362
|
+
obj.keywords.clear
|
368
363
|
obj.save! # clears changes
|
369
|
-
|
364
|
+
obj.keywords.shift
|
370
365
|
end
|
371
366
|
end
|
372
367
|
|
373
368
|
it "should report changes if an array is unshifted" do
|
374
|
-
should_change_array do |
|
375
|
-
|
369
|
+
should_change_array do |obj|
|
370
|
+
obj.keywords.unshift("keyword")
|
376
371
|
end
|
377
372
|
end
|
378
373
|
|
379
374
|
it "should report changes if an array is cleared" do
|
380
|
-
should_change_array do |
|
381
|
-
|
375
|
+
should_change_array do |obj|
|
376
|
+
obj.keywords.clear
|
382
377
|
end
|
383
378
|
end
|
384
379
|
|
@@ -390,9 +385,9 @@ describe "Dirty" do
|
|
390
385
|
hash = obj.details
|
391
386
|
yield hash, obj
|
392
387
|
if change_expected
|
393
|
-
obj.changed
|
388
|
+
expect(obj.changed?).to be_truthy
|
394
389
|
else
|
395
|
-
obj.changed
|
390
|
+
expect(obj.changed?).to be_falsey
|
396
391
|
end
|
397
392
|
end
|
398
393
|
|
@@ -488,6 +483,63 @@ describe "Dirty" do
|
|
488
483
|
|
489
484
|
end
|
490
485
|
|
486
|
+
describe "#clear_changes_information" do
|
487
|
+
|
488
|
+
let :obj do
|
489
|
+
Cat.new(:name => "Sam")
|
490
|
+
end
|
491
|
+
|
492
|
+
it "should reset any change information" do
|
493
|
+
obj.name = "Sambo"
|
494
|
+
obj.clear_changes_information
|
495
|
+
expect(obj.changed?).to be_falsey
|
496
|
+
expect(obj.name_changed?).to be_falsey
|
497
|
+
end
|
498
|
+
|
499
|
+
it "should reset nested changed information" do
|
500
|
+
obj.favorite_toy = { name: 'freddo' }
|
501
|
+
obj.save
|
502
|
+
obj.favorite_toy.name = 'froddo'
|
503
|
+
expect(obj.changed?).to be_truthy
|
504
|
+
obj.clear_changes_information
|
505
|
+
expect(obj.changed?).to be_falsey
|
506
|
+
expect(obj.favorite_toy.changed?).to be_falsey
|
507
|
+
expect(obj.favorite_toy.name_changed?).to be_falsey
|
508
|
+
end
|
509
|
+
|
510
|
+
end
|
511
|
+
|
512
|
+
describe "property methods" do
|
513
|
+
|
514
|
+
let :obj do
|
515
|
+
Card.create!(:first_name => "Sam")
|
516
|
+
end
|
517
|
+
|
518
|
+
describe "#property_changed?" do
|
519
|
+
it "should be true on change" do
|
520
|
+
expect(obj.first_name_changed?).to be_falsey
|
521
|
+
obj.first_name = "Sambo"
|
522
|
+
expect(obj.first_name_changed?).to be_truthy
|
523
|
+
end
|
524
|
+
end
|
525
|
+
|
526
|
+
describe "#property_was" do
|
527
|
+
it "should be true on change" do
|
528
|
+
expect(obj.first_name_was).to eql("Sam")
|
529
|
+
obj.first_name = "Sambo"
|
530
|
+
expect(obj.first_name_was).to eql("Sam")
|
531
|
+
end
|
532
|
+
end
|
533
|
+
|
534
|
+
describe "#property_change" do
|
535
|
+
it "should show change" do
|
536
|
+
expect(obj.first_name_change).to eql([])
|
537
|
+
obj.first_name = "Sambo"
|
538
|
+
expect(obj.first_name_change).to eql(['Sam', 'Sambo'])
|
539
|
+
end
|
540
|
+
end
|
541
|
+
|
542
|
+
end
|
491
543
|
|
492
544
|
describe "when mass_assign_any_attribute true" do
|
493
545
|
before(:each) do
|
@@ -501,32 +553,65 @@ describe "Dirty" do
|
|
501
553
|
|
502
554
|
it "should report no changes if the record is modified with update_attributes" do
|
503
555
|
@card.update_attributes(:other_name => 'henry')
|
504
|
-
@card.changed
|
556
|
+
expect(@card.changed?).to be_falsey
|
505
557
|
end
|
506
558
|
|
507
559
|
it "should report not new if the record is modified with update_attributes" do
|
508
560
|
@card.update_attributes(:other_name => 'henry')
|
509
|
-
@card.new
|
561
|
+
expect(@card.new?).to be_falsey
|
510
562
|
end
|
511
563
|
|
512
564
|
it 'should report changes when updated with attributes' do
|
513
565
|
@card.save
|
514
566
|
@card.attributes = {:testing => 'fooobar'}
|
515
|
-
@card.changed
|
567
|
+
expect(@card.changed?).to be_truthy
|
516
568
|
end
|
517
569
|
|
518
570
|
it 'should report changes when updated with a known property' do
|
519
571
|
@card.save
|
520
572
|
@card.first_name = 'Danny'
|
521
|
-
@card.changed
|
573
|
+
expect(@card.changed?).to be_truthy
|
522
574
|
end
|
523
575
|
|
524
576
|
it "should not report changes if property is updated with same value" do
|
525
577
|
@card.update_attributes :testing => 'fooobar'
|
526
578
|
@card.attributes = {'testing' => 'fooobar'}
|
527
|
-
@card.changed
|
579
|
+
expect(@card.changed?).to be_falsey
|
528
580
|
end
|
529
581
|
|
530
582
|
end
|
531
583
|
|
584
|
+
context "when dirty tracking is disabled" do
|
585
|
+
|
586
|
+
let :obj do
|
587
|
+
Project.new(:name => 'Test')
|
588
|
+
end
|
589
|
+
|
590
|
+
it "should be persisted correctly" do
|
591
|
+
obj.save!
|
592
|
+
expect(Project.get(obj.id)).to_not be_nil
|
593
|
+
end
|
594
|
+
|
595
|
+
it "should always assume the doc has changed" do
|
596
|
+
expect(obj.changed?).to be_truthy
|
597
|
+
obj.save!
|
598
|
+
expect(obj.changed?).to be_truthy
|
599
|
+
end
|
600
|
+
|
601
|
+
it "should provide a nil changes set" do
|
602
|
+
expect(obj.changes).to be_nil
|
603
|
+
end
|
604
|
+
|
605
|
+
it "should not store a changes cache" do
|
606
|
+
expect(obj.send(:original_change_data)).to be_nil
|
607
|
+
end
|
608
|
+
|
609
|
+
it "should asume all properties have changed" do
|
610
|
+
obj.save!
|
611
|
+
obj.name = "Fooo"
|
612
|
+
expect(obj.name_changed?).to be_truthy
|
613
|
+
expect(obj.name_change).to eql(nil)
|
614
|
+
end
|
615
|
+
|
616
|
+
end
|
532
617
|
end
|