mongomodel 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +23 -11
- data/gemfiles/rails-3.2.gemfile +2 -2
- data/gemfiles/rails-4-observers.gemfile +1 -1
- data/gemfiles/rails-4.1.gemfile +2 -2
- data/gemfiles/{mongoid.gemfile → rails-4.2.gemfile} +2 -3
- data/gemfiles/{mongo_mapper.gemfile → rails-5.0.gemfile} +2 -3
- data/gemfiles/rails-5.1.gemfile +10 -0
- data/gemfiles/rails-5.2.gemfile +10 -0
- data/lib/mongomodel.rb +15 -15
- data/lib/mongomodel/attributes/mongo.rb +7 -7
- data/lib/mongomodel/attributes/store.rb +4 -4
- data/lib/mongomodel/attributes/typecasting.rb +7 -7
- data/lib/mongomodel/compatibility/mongo_mapper.rb +3 -3
- data/lib/mongomodel/compatibility/mongoid.rb +3 -3
- data/lib/mongomodel/concerns/abstract_class.rb +3 -3
- data/lib/mongomodel/concerns/activemodel.rb +4 -4
- data/lib/mongomodel/concerns/associations.rb +8 -8
- data/lib/mongomodel/concerns/associations/base/association.rb +5 -5
- data/lib/mongomodel/concerns/associations/base/definition.rb +4 -4
- data/lib/mongomodel/concerns/associations/base/proxy.rb +1 -1
- data/lib/mongomodel/concerns/associations/belongs_to.rb +19 -19
- data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +36 -36
- data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +34 -34
- data/lib/mongomodel/concerns/attribute_methods.rb +10 -10
- data/lib/mongomodel/concerns/attribute_methods/before_type_cast.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/dirty.rb +95 -13
- data/lib/mongomodel/concerns/attribute_methods/forbidden.rb +1 -1
- data/lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb +6 -6
- data/lib/mongomodel/concerns/attribute_methods/nested.rb +18 -18
- data/lib/mongomodel/concerns/attribute_methods/protected.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/query.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/read.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/write.rb +4 -4
- data/lib/mongomodel/concerns/attributes.rb +18 -18
- data/lib/mongomodel/concerns/callbacks.rb +7 -7
- data/lib/mongomodel/concerns/document_parent.rb +2 -2
- data/lib/mongomodel/concerns/logging.rb +2 -2
- data/lib/mongomodel/concerns/map_reduce.rb +11 -11
- data/lib/mongomodel/concerns/pretty_inspect.rb +3 -3
- data/lib/mongomodel/concerns/properties.rb +18 -18
- data/lib/mongomodel/concerns/record_status.rb +9 -13
- data/lib/mongomodel/concerns/serialization.rb +4 -4
- data/lib/mongomodel/concerns/timestamps.rb +4 -4
- data/lib/mongomodel/concerns/translation.rb +2 -2
- data/lib/mongomodel/concerns/validations.rb +5 -5
- data/lib/mongomodel/concerns/validations/associated.rb +1 -1
- data/lib/mongomodel/document.rb +6 -6
- data/lib/mongomodel/document/callbacks.rb +15 -21
- data/lib/mongomodel/document/collection_modifiers.rb +5 -5
- data/lib/mongomodel/document/dynamic_finders.rb +1 -1
- data/lib/mongomodel/document/indexes.rb +19 -19
- data/lib/mongomodel/document/optimistic_locking.rb +7 -7
- data/lib/mongomodel/document/persistence.rb +23 -23
- data/lib/mongomodel/document/scopes.rb +20 -20
- data/lib/mongomodel/document/validations.rb +6 -6
- data/lib/mongomodel/document/validations/uniqueness.rb +11 -11
- data/lib/mongomodel/embedded_document.rb +11 -11
- data/lib/mongomodel/locale/en.yml +0 -1
- data/lib/mongomodel/log_subscriber.rb +5 -5
- data/lib/mongomodel/railtie.rb +13 -13
- data/lib/mongomodel/support/collection.rb +31 -31
- data/lib/mongomodel/support/configuration.rb +11 -11
- data/lib/mongomodel/support/core_extensions.rb +1 -1
- data/lib/mongomodel/support/dynamic_finder.rb +12 -12
- data/lib/mongomodel/support/exceptions.rb +6 -6
- data/lib/mongomodel/support/instrumented_collection.rb +20 -20
- data/lib/mongomodel/support/map.rb +33 -33
- data/lib/mongomodel/support/mongo_operator.rb +6 -6
- data/lib/mongomodel/support/mongo_options.rb +18 -18
- data/lib/mongomodel/support/mongo_order.rb +16 -16
- data/lib/mongomodel/support/paginator.rb +8 -8
- data/lib/mongomodel/support/reference.rb +10 -10
- data/lib/mongomodel/support/scope.rb +37 -37
- data/lib/mongomodel/support/scope/array_methods.rb +1 -1
- data/lib/mongomodel/support/scope/batches.rb +1 -1
- data/lib/mongomodel/support/scope/dynamic_finders.rb +1 -1
- data/lib/mongomodel/support/scope/finder_methods.rb +7 -7
- data/lib/mongomodel/support/scope/load_methods.rb +1 -1
- data/lib/mongomodel/support/scope/pagination.rb +1 -1
- data/lib/mongomodel/support/scope/query_methods.rb +6 -6
- data/lib/mongomodel/support/scope/spawn_methods.rb +8 -8
- data/lib/mongomodel/support/types.rb +2 -2
- data/lib/mongomodel/support/types/array.rb +1 -1
- data/lib/mongomodel/support/types/boolean.rb +3 -3
- data/lib/mongomodel/support/types/custom.rb +3 -3
- data/lib/mongomodel/support/types/date.rb +2 -2
- data/lib/mongomodel/support/types/date_time.rb +6 -16
- data/lib/mongomodel/support/types/float.rb +1 -1
- data/lib/mongomodel/support/types/hash.rb +1 -1
- data/lib/mongomodel/support/types/integer.rb +13 -10
- data/lib/mongomodel/support/types/object.rb +5 -5
- data/lib/mongomodel/support/types/rational.rb +3 -3
- data/lib/mongomodel/support/types/time.rb +2 -2
- data/lib/mongomodel/version.rb +1 -1
- data/lib/rails/generators/mongo_model/config/templates/mongomodel.yml +3 -4
- data/lib/rails/generators/mongo_model/model/model_generator.rb +3 -3
- data/mongomodel.gemspec +5 -4
- data/spec/mongomodel/attributes/store_spec.rb +21 -21
- data/spec/mongomodel/concerns/activemodel_spec.rb +4 -4
- data/spec/mongomodel/concerns/associations/base/association_spec.rb +12 -12
- data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +34 -21
- data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +53 -53
- data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +30 -30
- data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +45 -41
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/nested_spec.rb +31 -31
- data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +15 -15
- data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +19 -19
- data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +9 -9
- data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods_spec.rb +13 -13
- data/spec/mongomodel/concerns/attributes_spec.rb +34 -34
- data/spec/mongomodel/concerns/callbacks_spec.rb +25 -21
- data/spec/mongomodel/concerns/logging_spec.rb +5 -3
- data/spec/mongomodel/concerns/map_reduce_spec.rb +19 -19
- data/spec/mongomodel/concerns/observing_spec.rb +3 -3
- data/spec/mongomodel/concerns/pretty_inspect_spec.rb +10 -10
- data/spec/mongomodel/concerns/properties_spec.rb +11 -11
- data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +13 -13
- data/spec/mongomodel/concerns/timestamps_spec.rb +39 -39
- data/spec/mongomodel/concerns/validations_spec.rb +41 -38
- data/spec/mongomodel/document/callbacks_spec.rb +20 -16
- data/spec/mongomodel/document/collection_modifiers_spec.rb +16 -16
- data/spec/mongomodel/document/dynamic_finders_spec.rb +46 -46
- data/spec/mongomodel/document/finders_spec.rb +15 -15
- data/spec/mongomodel/document/indexes_spec.rb +29 -29
- data/spec/mongomodel/document/optimistic_locking_spec.rb +16 -16
- data/spec/mongomodel/document/persistence_spec.rb +39 -39
- data/spec/mongomodel/document/scopes_spec.rb +17 -17
- data/spec/mongomodel/document/validations/uniqueness_spec.rb +46 -46
- data/spec/mongomodel/document/validations_spec.rb +35 -35
- data/spec/mongomodel/document_spec.rb +19 -19
- data/spec/mongomodel/embedded_document_spec.rb +19 -19
- data/spec/mongomodel/mongomodel_spec.rb +7 -6
- data/spec/mongomodel/support/collection_spec.rb +54 -54
- data/spec/mongomodel/support/configuration_spec.rb +1 -1
- data/spec/mongomodel/support/map_spec.rb +66 -66
- data/spec/mongomodel/support/mongo_operator_spec.rb +5 -5
- data/spec/mongomodel/support/mongo_options_spec.rb +42 -42
- data/spec/mongomodel/support/mongo_order_spec.rb +24 -24
- data/spec/mongomodel/support/paginator_spec.rb +15 -15
- data/spec/mongomodel/support/property_spec.rb +29 -23
- data/spec/mongomodel/support/scope_spec.rb +205 -204
- data/spec/spec_helper.rb +13 -2
- data/spec/support/callbacks.rb +3 -8
- data/spec/support/helpers/define_class.rb +7 -7
- data/spec/support/helpers/document_finder_stubs.rb +6 -6
- data/spec/support/helpers/specs_for.rb +1 -1
- data/spec/support/helpers/validations.rb +1 -1
- data/spec/support/matchers/find_with.rb +8 -8
- data/spec/support/matchers/respond_to_boolean.rb +3 -3
- data/spec/support/matchers/run_callbacks.rb +6 -9
- data/spec/support/models.rb +5 -5
- metadata +23 -7
@@ -8,43 +8,43 @@ module MongoModel
|
|
8
8
|
property :age, Integer
|
9
9
|
property :position, Array
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def subclass(klass)
|
13
13
|
Class.new(klass)
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
it "has an indexes collection" do
|
17
17
|
Article.indexes.should be_an_instance_of(Array)
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
it "inherits indexes from parent classes" do
|
21
21
|
index = double('index')
|
22
22
|
Article.indexes << index
|
23
23
|
subclass(Article).indexes.should include(index)
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
describe "#index" do
|
27
27
|
it "adds an index to the indexes collection" do
|
28
28
|
Article.index :title, :unique => true
|
29
29
|
Article.indexes.last.should == Index.new(:title, :unique => true)
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
it "marks indexes as uninitialized" do
|
33
33
|
Article.ensure_indexes!
|
34
|
-
|
35
|
-
Article.indexes_initialized?.should
|
34
|
+
|
35
|
+
Article.indexes_initialized?.should be true
|
36
36
|
Article.index :age
|
37
|
-
Article.indexes_initialized?.should
|
37
|
+
Article.indexes_initialized?.should be false
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
describe "#ensure_indexes!" do
|
42
42
|
before(:each) do
|
43
43
|
Article.index :title, :unique => true
|
44
44
|
Article.index :age => :descending
|
45
45
|
Article.index :position => :geo2d, :min => -100, :max => 100
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
it "creates indexes on the collection" do
|
49
49
|
Article.collection.should_receive(:create_index).with(:_type)
|
50
50
|
Article.collection.should_receive(:create_index).with(:title, :unique => true)
|
@@ -52,20 +52,20 @@ module MongoModel
|
|
52
52
|
Article.collection.should_receive(:create_index).with([[:position, Mongo::GEO2D]], :min => -100, :max => 100)
|
53
53
|
Article.ensure_indexes!
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
it "marks indexes as initialized" do
|
57
|
-
Article.indexes_initialized?.should
|
57
|
+
Article.indexes_initialized?.should be false
|
58
58
|
Article.ensure_indexes!
|
59
|
-
Article.indexes_initialized?.should
|
59
|
+
Article.indexes_initialized?.should be true
|
60
60
|
end
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
describe "#_find" do
|
64
64
|
it "runs ensure_indexes!" do
|
65
65
|
Article.should_receive(:ensure_indexes!)
|
66
66
|
Article.first
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
it "reruns ensure_indexes! if indexes are initialized" do
|
70
70
|
Article.ensure_indexes!
|
71
71
|
Article.should_not_receive(:ensure_indexes!)
|
@@ -73,10 +73,10 @@ module MongoModel
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
describe "index shortcuts" do
|
78
78
|
define_class(:TestDocument, Document)
|
79
|
-
|
79
|
+
|
80
80
|
describe ":index => true" do
|
81
81
|
it "adds an index on the property" do
|
82
82
|
TestDocument.should_receive(:index).with(:title)
|
@@ -85,56 +85,56 @@ module MongoModel
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
describe Index do
|
90
90
|
describe "#to_args" do
|
91
91
|
it "converts index with single key" do
|
92
92
|
Index.new(:title).to_args.should == [:title]
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
it "converts nested index with single key" do
|
96
96
|
Index.new('page.title').to_args.should == [:'page.title']
|
97
97
|
end
|
98
|
-
|
98
|
+
|
99
99
|
it "converts index with unique option" do
|
100
100
|
Index.new(:title, :unique => true).to_args.should == [:title, { :unique => true }]
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
it "converts index with name option" do
|
104
104
|
Index.new(:title, :name => "my_name").to_args.should == [:title, { :name => "my_name" }]
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
it "converts index with multiple options" do
|
108
108
|
Index.new(:title, :name => "my_name", :unique => true).to_args.should == [:title, { :name => "my_name", :unique => true }]
|
109
109
|
end
|
110
|
-
|
110
|
+
|
111
111
|
it "converts index with descending key" do
|
112
112
|
Index.new(:title => :descending).to_args.should == [[[:title, Mongo::DESCENDING]]]
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
it "converts index with multiple keys" do
|
116
116
|
Index.new(:title, :age).to_args.should == [[[:age, Mongo::ASCENDING], [:title, Mongo::ASCENDING]]]
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
it "converts index with multiple keys (ascending and descending)" do
|
120
120
|
Index.new(:title => :ascending, :age => :descending).to_args.should == [[[:age, Mongo::DESCENDING], [:title, Mongo::ASCENDING]]]
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
it "converts geospatial index with no options" do
|
124
124
|
Index.new(:position => :geo2d).to_args.should == [[[:position, Mongo::GEO2D]]]
|
125
125
|
end
|
126
|
-
|
126
|
+
|
127
127
|
it "converts geospatial index with min/max options" do
|
128
128
|
Index.new(:position => :geo2d, :min => -50, :max => 50).to_args.should == [[[:position, Mongo::GEO2D]], { :min => -50, :max => 50 }]
|
129
129
|
end
|
130
130
|
end
|
131
|
-
|
131
|
+
|
132
132
|
describe "equality" do
|
133
133
|
it "is equal to an equivalent index" do
|
134
134
|
Index.new(:title).should == Index.new(:title)
|
135
135
|
Index.new(:title, :age).should == Index.new(:title => :ascending, :age => :ascending)
|
136
136
|
end
|
137
|
-
|
137
|
+
|
138
138
|
it "is not equal to an non-equivalent index" do
|
139
139
|
Index.new(:title).should_not == Index.new(:age)
|
140
140
|
Index.new(:title, :age).should_not == Index.new(:title => :ascending, :age => :descending)
|
@@ -4,50 +4,50 @@ module MongoModel
|
|
4
4
|
specs_for(Document) do
|
5
5
|
describe "optimistic locking" do
|
6
6
|
define_class(:TestDocument, Document)
|
7
|
-
|
7
|
+
|
8
8
|
it "does not lock optimistically by default" do
|
9
|
-
TestDocument.locking_enabled?.should
|
9
|
+
TestDocument.locking_enabled?.should be false
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
it "does not include a lock version property" do
|
13
13
|
TestDocument.properties.should_not include(:_lock_version)
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
context "with locking enabled" do
|
17
17
|
define_class(:TestDocument, Document) do
|
18
18
|
property :name, String
|
19
19
|
self.lock_optimistically = true
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
before(:each) do
|
23
23
|
@fresh = TestDocument.create!(:name => 'Original')
|
24
24
|
@stale = @fresh.dup
|
25
|
-
|
25
|
+
|
26
26
|
@fresh.name = 'Changed'
|
27
27
|
@fresh.save!
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
it "is enabled" do
|
31
|
-
TestDocument.locking_enabled?.should
|
31
|
+
TestDocument.locking_enabled?.should be true
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
it "defines a lock version property" do
|
35
35
|
TestDocument.properties.should include(:_lock_version)
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
it "saves a fresh document" do
|
39
|
-
@fresh.save.should
|
39
|
+
@fresh.save.should be true
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
it "saves! a fresh document" do
|
43
|
-
@fresh.save!.should
|
43
|
+
@fresh.save!.should be true
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
it "does not save a stale document" do
|
47
|
-
@stale.save.should
|
47
|
+
@stale.save.should be false
|
48
48
|
@stale._lock_version.should == 1
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
it "raises an error when trying to save! a stale document" do
|
52
52
|
lambda { @stale.save! }.should raise_error(DocumentNotSaved)
|
53
53
|
end
|
@@ -7,7 +7,7 @@ module MongoModel
|
|
7
7
|
property :name, String
|
8
8
|
property :age, Integer
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
context "an unsaved instance" do
|
12
12
|
subject { User.new(:name => 'Test') }
|
13
13
|
|
@@ -64,7 +64,7 @@ module MongoModel
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
describe "#collection_name" do
|
69
69
|
it "infers the default collection name" do
|
70
70
|
User.collection_name.should == 'users'
|
@@ -94,7 +94,7 @@ module MongoModel
|
|
94
94
|
|
95
95
|
::CustomCollectionNameSubclass.collection_name.should == 'foobar'
|
96
96
|
end
|
97
|
-
|
97
|
+
|
98
98
|
it "allows subclasses to set a custom collection name" do
|
99
99
|
class ::CustomCollectionName < Document; end
|
100
100
|
class ::CustomCollectionNameSubclass < ::CustomCollectionName
|
@@ -113,7 +113,7 @@ module MongoModel
|
|
113
113
|
it "uses the correct collection name" do
|
114
114
|
User.collection.name.should == 'users'
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
it "is updated when the collection name changes" do
|
118
118
|
collection = User.collection
|
119
119
|
User.collection_name = "custom"
|
@@ -126,7 +126,7 @@ module MongoModel
|
|
126
126
|
User.database.should == MongoModel.database
|
127
127
|
end
|
128
128
|
end
|
129
|
-
|
129
|
+
|
130
130
|
describe "#create" do
|
131
131
|
context "attributes hash" do
|
132
132
|
it "passes attributes to instance" do
|
@@ -149,7 +149,7 @@ module MongoModel
|
|
149
149
|
u.should be_a_new_record
|
150
150
|
end
|
151
151
|
|
152
|
-
block_called.should
|
152
|
+
block_called.should be true
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
@@ -184,7 +184,7 @@ module MongoModel
|
|
184
184
|
end
|
185
185
|
end
|
186
186
|
end
|
187
|
-
|
187
|
+
|
188
188
|
describe "#delete (class method)" do
|
189
189
|
before(:each) do
|
190
190
|
User.create(:id => 'user-1', :name => 'Test', :age => 10)
|
@@ -195,16 +195,16 @@ module MongoModel
|
|
195
195
|
it "deletes by id" do
|
196
196
|
User.delete('user-1')
|
197
197
|
|
198
|
-
User.exists?('user-1').should
|
199
|
-
User.exists?('user-2').should
|
198
|
+
User.exists?('user-1').should be false
|
199
|
+
User.exists?('user-2').should be true
|
200
200
|
end
|
201
|
-
|
201
|
+
|
202
202
|
it "deletes by multiple ids in array" do
|
203
203
|
User.delete(['user-1', 'user-2'])
|
204
204
|
|
205
|
-
User.exists?('user-1').should
|
206
|
-
User.exists?('user-2').should
|
207
|
-
User.exists?('user-3').should
|
205
|
+
User.exists?('user-1').should be false
|
206
|
+
User.exists?('user-2').should be false
|
207
|
+
User.exists?('user-3').should be true
|
208
208
|
end
|
209
209
|
end
|
210
210
|
|
@@ -217,8 +217,8 @@ module MongoModel
|
|
217
217
|
it "deletes the instance from the database" do
|
218
218
|
@user.delete
|
219
219
|
|
220
|
-
User.exists?('user-1').should
|
221
|
-
User.exists?('user-2').should
|
220
|
+
User.exists?('user-1').should be false
|
221
|
+
User.exists?('user-2').should be true
|
222
222
|
end
|
223
223
|
|
224
224
|
it "returns the instance" do
|
@@ -229,7 +229,7 @@ module MongoModel
|
|
229
229
|
@user.delete
|
230
230
|
@user.should be_frozen
|
231
231
|
end
|
232
|
-
|
232
|
+
|
233
233
|
it "marks the instance as destroyed" do
|
234
234
|
@user.delete
|
235
235
|
@user.should be_destroyed
|
@@ -245,8 +245,8 @@ module MongoModel
|
|
245
245
|
it "deletes the instance from the database" do
|
246
246
|
@user.destroy
|
247
247
|
|
248
|
-
User.exists?('user-1').should
|
249
|
-
User.exists?('user-2').should
|
248
|
+
User.exists?('user-1').should be false
|
249
|
+
User.exists?('user-2').should be true
|
250
250
|
end
|
251
251
|
|
252
252
|
it "returns the instance" do
|
@@ -257,7 +257,7 @@ module MongoModel
|
|
257
257
|
@user.destroy
|
258
258
|
@user.should be_frozen
|
259
259
|
end
|
260
|
-
|
260
|
+
|
261
261
|
it "marks the instance as destroyed" do
|
262
262
|
@user.destroy
|
263
263
|
@user.should be_destroyed
|
@@ -274,64 +274,64 @@ module MongoModel
|
|
274
274
|
it "destroys by id" do
|
275
275
|
User.destroy('user-1')
|
276
276
|
|
277
|
-
User.exists?('user-1').should
|
278
|
-
User.exists?('user-2').should
|
277
|
+
User.exists?('user-1').should be false
|
278
|
+
User.exists?('user-2').should be true
|
279
279
|
end
|
280
280
|
|
281
281
|
it "destroys by multiple ids in array" do
|
282
282
|
User.destroy(['user-1', 'user-2'])
|
283
283
|
|
284
|
-
User.exists?('user-1').should
|
285
|
-
User.exists?('user-2').should
|
286
|
-
User.exists?('user-3').should
|
284
|
+
User.exists?('user-1').should be false
|
285
|
+
User.exists?('user-2').should be false
|
286
|
+
User.exists?('user-3').should be true
|
287
287
|
end
|
288
288
|
end
|
289
|
-
|
289
|
+
|
290
290
|
describe "#update_attributes" do
|
291
291
|
let(:user) { User.new(:name => 'Original', :age => 10) }
|
292
|
-
|
292
|
+
|
293
293
|
before(:each) { user.update_attributes(:name => 'Changed', :age => 20) }
|
294
|
-
|
294
|
+
|
295
295
|
it "updates the attributes" do
|
296
296
|
user.name.should == 'Changed'
|
297
297
|
user.age.should == 20
|
298
298
|
end
|
299
|
-
|
299
|
+
|
300
300
|
it "saves the document" do
|
301
301
|
user.should_not be_a_new_record
|
302
302
|
end
|
303
303
|
end
|
304
|
-
|
304
|
+
|
305
305
|
describe "#update_attribute" do
|
306
306
|
let(:user) { User.new(:name => 'Original', :age => 10) }
|
307
|
-
|
307
|
+
|
308
308
|
before(:each) { user.update_attribute(:name, 'Changed') }
|
309
|
-
|
309
|
+
|
310
310
|
it "updates the given attribute" do
|
311
311
|
user.name.should == 'Changed'
|
312
312
|
end
|
313
|
-
|
313
|
+
|
314
314
|
it "saves the document" do
|
315
315
|
user.should_not be_a_new_record
|
316
316
|
end
|
317
317
|
end
|
318
|
-
|
318
|
+
|
319
319
|
describe "#reload" do
|
320
320
|
define_class(:UserComment, Document) do
|
321
321
|
property :title, String
|
322
322
|
property :body, String
|
323
|
-
|
323
|
+
|
324
324
|
belongs_to :user
|
325
325
|
end
|
326
|
-
|
326
|
+
|
327
327
|
let(:user) { User.create!(:name => "Bob") }
|
328
|
-
|
328
|
+
|
329
329
|
subject { UserComment.create!(:title => "Test", :user => user) }
|
330
|
-
|
330
|
+
|
331
331
|
it "returns itself" do
|
332
332
|
subject.reload.should == subject
|
333
333
|
end
|
334
|
-
|
334
|
+
|
335
335
|
it "resets the attributes" do
|
336
336
|
subject.title = "New Value"
|
337
337
|
subject.body = "Blah blah blah"
|
@@ -339,7 +339,7 @@ module MongoModel
|
|
339
339
|
subject.title.should == "Test"
|
340
340
|
subject.body.should == nil
|
341
341
|
end
|
342
|
-
|
342
|
+
|
343
343
|
it "resets the associations" do
|
344
344
|
subject.user.should == user
|
345
345
|
subject.user = User.new(:name => "Bill")
|
@@ -3,29 +3,29 @@ require 'spec_helper'
|
|
3
3
|
module MongoModel
|
4
4
|
describe Document do
|
5
5
|
define_class(:Post, Document)
|
6
|
-
|
6
|
+
|
7
7
|
describe "#scoped" do
|
8
8
|
subject { Post.scoped }
|
9
|
-
|
9
|
+
|
10
10
|
it { should be_an_instance_of(MongoModel::Scope) }
|
11
|
-
|
11
|
+
|
12
12
|
it "sets the target class" do
|
13
13
|
subject.klass.should == Post
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
context "with default scope(s) set" do
|
17
17
|
define_class(:Post, Document) do
|
18
18
|
default_scope order(:title.asc)
|
19
19
|
default_scope limit(5)
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it "returns the default scope" do
|
23
23
|
scope = Post.scoped
|
24
24
|
scope.order_values.should == [:title.asc]
|
25
25
|
scope.limit_value.should == 5
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
context "within a with_scope block" do
|
30
30
|
it "returns the current scope" do
|
31
31
|
Post.class_eval do
|
@@ -35,7 +35,7 @@ module MongoModel
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
context "within nested with_scope blocks" do
|
40
40
|
it "returns the merged scope" do
|
41
41
|
Post.class_eval do
|
@@ -49,50 +49,50 @@ module MongoModel
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
describe "#scope" do
|
54
54
|
define_class(:Post, Document) do
|
55
55
|
scope :published, where(:published => true)
|
56
|
-
|
56
|
+
|
57
57
|
scope :latest, lambda { |num| order(:created_at.desc).limit(num) }
|
58
|
-
|
58
|
+
|
59
59
|
scope :recent, order(:created_at.desc).limit(5)
|
60
60
|
scope :recently_published, recent.where(:published => true)
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
define_class(:SpecialPost, :Post)
|
64
|
-
|
64
|
+
|
65
65
|
it "creates a method returning the scope" do
|
66
66
|
scope = Post.published
|
67
67
|
scope.should be_an_instance_of(MongoModel::Scope)
|
68
68
|
scope.where_values.should == [{:published => true}]
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
it "creates parameterized method returning the scope when given a lambda" do
|
72
72
|
scope = Post.latest(4)
|
73
73
|
scope.order_values.should == [:created_at.desc]
|
74
74
|
scope.limit_value.should == 4
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
it "allows existing scopes to be built upon" do
|
78
78
|
scope = Post.recently_published
|
79
79
|
scope.where_values.should == [{:published => true}]
|
80
80
|
scope.order_values.should == [:created_at.desc]
|
81
81
|
scope.limit_value.should == 5
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
it "merges the scope with the current scope of the class it is called upon" do
|
85
85
|
scope = SpecialPost.published
|
86
86
|
scope.klass.should == SpecialPost
|
87
87
|
end
|
88
88
|
end
|
89
|
-
|
89
|
+
|
90
90
|
describe "named scopes" do
|
91
91
|
define_class(:Post, Document) do
|
92
92
|
scope :published, where(:published => true)
|
93
93
|
scope :recent, order(:created_at.desc).limit(5)
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
it "is chainable" do
|
97
97
|
scope = Post.published.recent
|
98
98
|
scope.where_values.should == [{:published => true}]
|