langalex-couch_potato 0.1 → 0.1.1
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.
- data/README.textile +340 -0
- data/VERSION.yml +4 -0
- data/lib/couch_potato/ordering.rb +5 -9
- data/lib/couch_potato/persistence.rb +32 -7
- data/lib/couch_potato/persistence/belongs_to_property.rb +17 -3
- data/lib/couch_potato/persistence/callbacks.rb +9 -0
- data/lib/couch_potato/persistence/custom_view.rb +41 -0
- data/lib/couch_potato/persistence/dirty_attributes.rb +19 -0
- data/lib/couch_potato/persistence/external_collection.rb +31 -2
- data/lib/couch_potato/persistence/external_has_many_property.rb +4 -0
- data/lib/couch_potato/persistence/finder.rb +21 -65
- data/lib/couch_potato/persistence/inline_has_many_property.rb +4 -0
- data/lib/couch_potato/persistence/json.rb +1 -1
- data/lib/couch_potato/persistence/simple_property.rb +29 -1
- data/lib/couch_potato/persistence/view_query.rb +81 -0
- data/lib/couch_potato/versioning.rb +1 -1
- data/spec/attributes_spec.rb +35 -15
- data/spec/belongs_to_spec.rb +18 -0
- data/spec/callbacks_spec.rb +31 -12
- data/spec/create_spec.rb +5 -0
- data/spec/custom_view_spec.rb +44 -0
- data/spec/destroy_spec.rb +4 -0
- data/spec/dirty_attributes_spec.rb +82 -0
- data/spec/find_spec.rb +11 -3
- data/spec/finder_spec.rb +10 -0
- data/spec/has_many_spec.rb +64 -1
- data/spec/ordering_spec.rb +1 -0
- data/spec/property_spec.rb +4 -0
- data/spec/reload_spec.rb +4 -0
- data/spec/spec_helper.rb +2 -1
- data/spec/unit/external_collection_spec.rb +84 -0
- data/spec/unit/finder_spec.rb +10 -0
- data/spec/unit/view_query_spec.rb +10 -0
- data/spec/update_spec.rb +6 -0
- data/spec/versioning_spec.rb +1 -0
- metadata +21 -48
- data/CREDITS +0 -3
- data/init.rb +0 -5
data/spec/attributes_spec.rb
CHANGED
@@ -1,22 +1,42 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
class Plant
|
4
|
+
include CouchPotato::Persistence
|
5
|
+
property :leaf_count
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "attributes" do
|
9
|
+
before(:all) do
|
10
|
+
CouchPotato::Persistence.Db!
|
8
11
|
end
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
describe 'attributes=' do
|
14
|
+
it "should assign the attributes" do
|
15
|
+
plant = Plant.new
|
16
|
+
plant.attributes = {:leaf_count => 1}
|
17
|
+
plant.leaf_count.should == 1
|
18
|
+
end
|
14
19
|
end
|
15
|
-
end
|
16
20
|
|
17
|
-
describe "attributes" do
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
+
describe "attributes" do
|
22
|
+
it "should return the attributes" do
|
23
|
+
plant = Plant.new :leaf_count => 1
|
24
|
+
plant.attributes .should == {:leaf_count => 1}
|
25
|
+
end
|
21
26
|
end
|
22
|
-
|
27
|
+
|
28
|
+
describe 'update_attributes' do
|
29
|
+
it "should assign the attributes" do
|
30
|
+
plant = Plant.new
|
31
|
+
plant.update_attributes :leaf_count => 1
|
32
|
+
plant.leaf_count.should == 1
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should save the object" do
|
36
|
+
plant = Plant.new
|
37
|
+
plant.update_attributes :leaf_count => 1
|
38
|
+
plant.should_not be_new_document
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
data/spec/belongs_to_spec.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
3
|
describe 'belongs_to' do
|
4
|
+
before(:all) do
|
5
|
+
CouchPotato::Persistence.Db!
|
6
|
+
end
|
7
|
+
|
4
8
|
before(:each) do
|
5
9
|
@commenter = Commenter.create!
|
6
10
|
end
|
@@ -25,6 +29,20 @@ describe 'belongs_to' do
|
|
25
29
|
c.commenter_id.should == @commenter.id
|
26
30
|
end
|
27
31
|
|
32
|
+
it "should unassign the parent object" do
|
33
|
+
c = Comment.new :title => 'title', :commenter => stub('comenter', :id => 1)
|
34
|
+
c.commenter = nil
|
35
|
+
c.commenter.should be_nil
|
36
|
+
c.commenter_id.should be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should unassign the parent object id" do
|
40
|
+
c = Comment.new :title => 'title', :commenter => stub('comenter', :id => 1)
|
41
|
+
c.commenter_id = nil
|
42
|
+
c.commenter.should be_nil
|
43
|
+
c.commenter_id.should be_nil
|
44
|
+
end
|
45
|
+
|
28
46
|
it "should persist the link to the parent object" do
|
29
47
|
c = Comment.new :title => 'title'
|
30
48
|
c.commenter_id = @commenter.id
|
data/spec/callbacks_spec.rb
CHANGED
@@ -27,6 +27,9 @@ class CallbackRecorder
|
|
27
27
|
end
|
28
28
|
|
29
29
|
describe "multiple callbacks at once" do
|
30
|
+
before(:all) do
|
31
|
+
CouchPotato::Persistence.Db!
|
32
|
+
end
|
30
33
|
class Monkey
|
31
34
|
include CouchPotato::Persistence
|
32
35
|
attr_accessor :eaten_banana, :eaten_apple
|
@@ -51,6 +54,9 @@ describe "multiple callbacks at once" do
|
|
51
54
|
end
|
52
55
|
|
53
56
|
describe 'create callbacks' do
|
57
|
+
before(:all) do
|
58
|
+
CouchPotato::Persistence.Db!
|
59
|
+
end
|
54
60
|
|
55
61
|
before(:each) do
|
56
62
|
@recorder = CallbackRecorder.new
|
@@ -131,41 +137,43 @@ describe 'create callbacks' do
|
|
131
137
|
end
|
132
138
|
|
133
139
|
describe "update callbacks" do
|
140
|
+
before(:all) do
|
141
|
+
CouchPotato::Persistence.Db!
|
142
|
+
end
|
134
143
|
|
135
144
|
before(:each) do
|
136
145
|
@recorder = CallbackRecorder.create! :required_property => 1
|
146
|
+
@recorder.required_property = 2
|
137
147
|
@recorder.callbacks.clear
|
138
148
|
end
|
139
149
|
|
140
150
|
describe "successful update" do
|
141
151
|
|
142
|
-
|
152
|
+
before(:each) do
|
143
153
|
@recorder.save!
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should call before_validation_on_update" do
|
144
157
|
@recorder.callbacks.should include(:before_validation_on_update)
|
145
158
|
end
|
146
159
|
|
147
160
|
it "should call before_validation_on_save" do
|
148
|
-
@recorder.save!
|
149
161
|
@recorder.callbacks.should include(:before_validation_on_save)
|
150
162
|
end
|
151
163
|
|
152
164
|
it "should call before_save" do
|
153
|
-
@recorder.save!
|
154
165
|
@recorder.callbacks.should include(:before_save)
|
155
166
|
end
|
156
167
|
|
157
168
|
it "should call after_save" do
|
158
|
-
@recorder.save!
|
159
169
|
@recorder.callbacks.should include(:after_save)
|
160
170
|
end
|
161
171
|
|
162
172
|
it "should call before_update" do
|
163
|
-
@recorder.save!
|
164
173
|
@recorder.callbacks.should include(:before_update)
|
165
174
|
end
|
166
175
|
|
167
176
|
it "should call after_update" do
|
168
|
-
@recorder.save!
|
169
177
|
@recorder.callbacks.should include(:after_update)
|
170
178
|
end
|
171
179
|
|
@@ -175,35 +183,30 @@ describe "update callbacks" do
|
|
175
183
|
|
176
184
|
before(:each) do
|
177
185
|
@recorder.required_property = nil
|
186
|
+
@recorder.save
|
178
187
|
end
|
179
188
|
|
180
189
|
it "should call before_validation_on_update" do
|
181
|
-
@recorder.save
|
182
190
|
@recorder.callbacks.should include(:before_validation_on_update)
|
183
191
|
end
|
184
192
|
|
185
193
|
it "should call before_validation_on_save" do
|
186
|
-
@recorder.save
|
187
194
|
@recorder.callbacks.should include(:before_validation_on_save)
|
188
195
|
end
|
189
196
|
|
190
197
|
it "should not call before_save" do
|
191
|
-
@recorder.save
|
192
198
|
@recorder.callbacks.should_not include(:before_save)
|
193
199
|
end
|
194
200
|
|
195
201
|
it "should not call after_save" do
|
196
|
-
@recorder.save
|
197
202
|
@recorder.callbacks.should_not include(:after_save)
|
198
203
|
end
|
199
204
|
|
200
205
|
it "should not call before_update" do
|
201
|
-
@recorder.save
|
202
206
|
@recorder.callbacks.should_not include(:before_update)
|
203
207
|
end
|
204
208
|
|
205
209
|
it "should not call after_update" do
|
206
|
-
@recorder.save
|
207
210
|
@recorder.callbacks.should_not include(:after_update)
|
208
211
|
end
|
209
212
|
|
@@ -212,6 +215,10 @@ describe "update callbacks" do
|
|
212
215
|
end
|
213
216
|
|
214
217
|
describe "destroy callbacks" do
|
218
|
+
before(:all) do
|
219
|
+
CouchPotato::Persistence.Db!
|
220
|
+
end
|
221
|
+
|
215
222
|
before(:each) do
|
216
223
|
@recorder = CallbackRecorder.create! :required_property => 1
|
217
224
|
@recorder.callbacks.clear
|
@@ -226,4 +233,16 @@ describe "destroy callbacks" do
|
|
226
233
|
@recorder.destroy
|
227
234
|
@recorder.callbacks.should include(:after_destroy)
|
228
235
|
end
|
236
|
+
end
|
237
|
+
|
238
|
+
describe 'save_without_callbacks' do
|
239
|
+
before(:all) do
|
240
|
+
CouchPotato::Persistence.Db!
|
241
|
+
end
|
242
|
+
|
243
|
+
it "should not run any callbacks" do
|
244
|
+
@recorder = CallbackRecorder.new
|
245
|
+
@recorder.save_without_callbacks
|
246
|
+
@recorder.callbacks.should be_empty
|
247
|
+
end
|
229
248
|
end
|
data/spec/create_spec.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
3
|
describe "create" do
|
4
|
+
before(:all) do
|
5
|
+
CouchPotato::Persistence.Db!
|
6
|
+
end
|
7
|
+
|
4
8
|
describe "succeeds" do
|
5
9
|
before(:each) do
|
6
10
|
@comment = Comment.new :title => 'my_title'
|
@@ -33,6 +37,7 @@ describe "create" do
|
|
33
37
|
describe "fails" do
|
34
38
|
before(:each) do
|
35
39
|
CouchPotato::Persistence.Db.delete!
|
40
|
+
CouchPotato::Persistence.Db!
|
36
41
|
@comment = Comment.new
|
37
42
|
@comment.save
|
38
43
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class Build
|
4
|
+
include CouchPotato::Persistence
|
5
|
+
|
6
|
+
property :state
|
7
|
+
property :time
|
8
|
+
|
9
|
+
view :timeline, :key => :time
|
10
|
+
view :minimal_timeline, :key => :time, :properties => [:state]
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'custom view' do
|
14
|
+
before(:each) do
|
15
|
+
CouchPotato::Persistence.Db.delete!
|
16
|
+
CouchPotato::Persistence.Db!
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return instances of the class" do
|
20
|
+
Build.db.save({:state => 'success', :time => '2008-01-01'})
|
21
|
+
Build.timeline.map(&:class).should == [Build]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should assign all properties to the objects by default" do
|
25
|
+
Build.db.save({:state => 'success', :time => '2008-01-01'})
|
26
|
+
Build.timeline.first.state.should == 'success'
|
27
|
+
Build.timeline.first.time.should == '2008-01-01'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should assign the configured properties" do
|
31
|
+
Build.db.save({:state => 'success', :time => '2008-01-01'})
|
32
|
+
Build.minimal_timeline.first.state.should == 'success'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should assign the id when properties are configured" do
|
36
|
+
id = Build.db.save({:state => 'success', :time => '2008-01-01'})['id']
|
37
|
+
Build.minimal_timeline.first._id.should == id
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not assign the properties not configured" do
|
41
|
+
Build.db.save({:state => 'success', :time => '2008-01-01'})
|
42
|
+
Build.minimal_timeline.first.time.should be_nil
|
43
|
+
end
|
44
|
+
end
|
data/spec/destroy_spec.rb
CHANGED
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class Plate
|
4
|
+
include CouchPotato::Persistence
|
5
|
+
|
6
|
+
property :food
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'dirty attribute tracking' do
|
10
|
+
before(:all) do
|
11
|
+
CouchPotato::Persistence.Db!
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "save" do
|
15
|
+
it "should not save when nothing dirty" do
|
16
|
+
plate = Plate.create! :food => 'sushi'
|
17
|
+
old_rev = plate._rev
|
18
|
+
plate.save
|
19
|
+
plate._rev.should == old_rev
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should save when there are dirty attributes" do
|
23
|
+
plate = Plate.create! :food => 'sushi'
|
24
|
+
old_rev = plate._rev
|
25
|
+
plate.food = 'burger'
|
26
|
+
plate.save
|
27
|
+
plate._rev.should_not == old_rev
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "newly created object" do
|
32
|
+
|
33
|
+
before(:each) do
|
34
|
+
@plate = Plate.new :food => 'sushi'
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "access old values" do
|
38
|
+
it "should return the old value" do
|
39
|
+
@plate.food = 'burger'
|
40
|
+
@plate.food_was.should == 'sushi'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "check for dirty" do
|
45
|
+
it "should return true if attribute changed" do
|
46
|
+
@plate.food = 'burger'
|
47
|
+
@plate.should be_food_changed
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return false if attribute not changed" do
|
51
|
+
@plate.should_not be_food_changed
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "object loaded from database" do
|
57
|
+
before(:each) do
|
58
|
+
@plate = Plate.create! :food => 'sushi'
|
59
|
+
@plate = Plate.get @plate._id
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "access old values" do
|
63
|
+
it "should return the old value" do
|
64
|
+
@plate.food = 'burger'
|
65
|
+
@plate.food_was.should == 'sushi'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "check for dirty" do
|
70
|
+
it "should return true if attribute changed" do
|
71
|
+
@plate.food = 'burger'
|
72
|
+
@plate.should be_food_changed
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return false if attribute not changed" do
|
76
|
+
@plate.should_not be_food_changed
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
end
|
data/spec/find_spec.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
3
|
describe "find" do
|
4
|
+
before(:all) do
|
5
|
+
CouchPotato::Persistence.Db!
|
6
|
+
end
|
4
7
|
before(:each) do
|
5
8
|
@comment = Comment.create! :title => 'title'
|
6
9
|
end
|
@@ -25,8 +28,8 @@ end
|
|
25
28
|
describe 'first' do
|
26
29
|
before(:each) do
|
27
30
|
CouchPotato::Persistence.Db.delete!
|
31
|
+
CouchPotato::Persistence.Db!
|
28
32
|
@comment = Comment.create! :title => 'title'
|
29
|
-
Comment.create! :title => 'title3'
|
30
33
|
end
|
31
34
|
|
32
35
|
it "should find the first matching object" do
|
@@ -41,7 +44,7 @@ end
|
|
41
44
|
describe 'last' do
|
42
45
|
before(:each) do
|
43
46
|
CouchPotato::Persistence.Db.delete!
|
44
|
-
|
47
|
+
CouchPotato::Persistence.Db!
|
45
48
|
@comment = Comment.create! :title => 'title'
|
46
49
|
end
|
47
50
|
|
@@ -58,12 +61,16 @@ end
|
|
58
61
|
describe 'all' do
|
59
62
|
before(:each) do
|
60
63
|
CouchPotato::Persistence.Db.delete!
|
64
|
+
CouchPotato::Persistence.Db!
|
61
65
|
@comment = Comment.create! :title => 'title'
|
62
66
|
@comment2 = Comment.create! :title => 'title'
|
63
67
|
end
|
64
68
|
|
65
69
|
it "should find the matching objects" do
|
66
|
-
Comment.all(:title =>'title')
|
70
|
+
comments = Comment.all(:title =>'title')
|
71
|
+
comments.size.should == 2
|
72
|
+
comments.should include(@comment)
|
73
|
+
comments.should include(@comment2)
|
67
74
|
end
|
68
75
|
|
69
76
|
it "should return [] if nothing found" do
|
@@ -74,6 +81,7 @@ end
|
|
74
81
|
describe 'count' do
|
75
82
|
before(:each) do
|
76
83
|
CouchPotato::Persistence.Db.delete!
|
84
|
+
CouchPotato::Persistence.Db!
|
77
85
|
@comment = Comment.create! :title => 'title'
|
78
86
|
@comment2 = Comment.create! :title => 'title'
|
79
87
|
end
|