mongomodel 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Appraisals +12 -0
- data/bin/console +1 -2
- data/gemfiles/mongo_mapper.gemfile +12 -0
- data/gemfiles/mongoid.gemfile +12 -0
- data/lib/mongomodel.rb +3 -0
- data/lib/mongomodel/compatibility/mongo_mapper.rb +23 -0
- data/lib/mongomodel/compatibility/mongoid.rb +17 -0
- data/lib/mongomodel/concerns/properties.rb +5 -0
- data/lib/mongomodel/concerns/validations.rb +5 -3
- data/lib/mongomodel/support/core_extensions.rb +4 -4
- data/lib/mongomodel/support/mongo_options.rb +4 -2
- data/lib/mongomodel/support/mongo_order.rb +4 -0
- data/lib/mongomodel/support/scope.rb +1 -1
- data/lib/mongomodel/version.rb +1 -1
- data/spec/mongomodel/attributes/store_spec.rb +12 -12
- data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +13 -13
- data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +25 -25
- data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +25 -25
- data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +5 -5
- data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +21 -21
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +3 -3
- data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +10 -10
- data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +5 -5
- data/spec/mongomodel/concerns/attribute_methods_spec.rb +5 -5
- data/spec/mongomodel/concerns/attributes_spec.rb +13 -13
- data/spec/mongomodel/concerns/callbacks_spec.rb +11 -11
- data/spec/mongomodel/concerns/logging_spec.rb +2 -2
- data/spec/mongomodel/concerns/observing_spec.rb +3 -3
- data/spec/mongomodel/concerns/pretty_inspect_spec.rb +5 -5
- data/spec/mongomodel/concerns/properties_spec.rb +6 -6
- data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +6 -6
- data/spec/mongomodel/concerns/timestamps_spec.rb +10 -10
- data/spec/mongomodel/concerns/translation_spec.rb +1 -1
- data/spec/mongomodel/concerns/validations_spec.rb +12 -4
- data/spec/mongomodel/document/callbacks_spec.rb +10 -10
- data/spec/mongomodel/document/collection_modifiers_spec.rb +1 -1
- data/spec/mongomodel/document/dynamic_finders_spec.rb +5 -5
- data/spec/mongomodel/document/finders_spec.rb +9 -9
- data/spec/mongomodel/document/indexes_spec.rb +19 -19
- data/spec/mongomodel/document/optimistic_locking_spec.rb +8 -8
- data/spec/mongomodel/document/persistence_spec.rb +38 -38
- data/spec/mongomodel/document/scopes_spec.rb +8 -8
- data/spec/mongomodel/document/validations/uniqueness_spec.rb +9 -9
- data/spec/mongomodel/document/validations_spec.rb +16 -16
- data/spec/mongomodel/document_spec.rb +11 -11
- data/spec/mongomodel/embedded_document_spec.rb +13 -13
- data/spec/mongomodel/mongomodel_spec.rb +4 -4
- data/spec/mongomodel/support/collection_spec.rb +40 -40
- data/spec/mongomodel/support/map_spec.rb +41 -41
- data/spec/mongomodel/support/mongo_operator_spec.rb +11 -9
- data/spec/mongomodel/support/mongo_options_spec.rb +17 -17
- data/spec/mongomodel/support/mongo_order_spec.rb +22 -22
- data/spec/mongomodel/support/property_spec.rb +21 -12
- data/spec/mongomodel/support/scope_spec.rb +134 -134
- data/spec/spec_helper.rb +1 -0
- data/spec/specdoc.opts +0 -3
- metadata +7 -5
data/Appraisals
CHANGED
@@ -12,3 +12,15 @@ appraise "rails-4" do
|
|
12
12
|
gem "activesupport", :git => "https://github.com/rails/rails.git"
|
13
13
|
gem "activemodel", :git => "https://github.com/rails/rails.git"
|
14
14
|
end
|
15
|
+
|
16
|
+
appraise "mongoid" do
|
17
|
+
gem "mongoid"
|
18
|
+
gem "activesupport", "3.2.8"
|
19
|
+
gem "activemodel", "3.2.8"
|
20
|
+
end
|
21
|
+
|
22
|
+
appraise "mongo_mapper" do
|
23
|
+
gem "mongo_mapper"
|
24
|
+
gem "activesupport", "3.2.8"
|
25
|
+
gem "activemodel", "3.2.8"
|
26
|
+
end
|
data/bin/console
CHANGED
@@ -4,14 +4,13 @@ puts "Loading MongoModel sandbox..."
|
|
4
4
|
|
5
5
|
require "rubygems"
|
6
6
|
require "bundler/setup"
|
7
|
+
Bundler.require
|
7
8
|
|
8
9
|
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
9
10
|
|
10
11
|
require 'irb'
|
11
12
|
require 'irb/completion'
|
12
13
|
|
13
|
-
require 'mongomodel'
|
14
|
-
|
15
14
|
MongoModel.logger = ActiveSupport::BufferedLogger.new(STDERR)
|
16
15
|
|
17
16
|
IRB.setup(nil)
|
data/lib/mongomodel.rb
CHANGED
@@ -103,6 +103,9 @@ module MongoModel
|
|
103
103
|
end
|
104
104
|
|
105
105
|
require 'mongomodel/railtie' if defined?(Rails)
|
106
|
+
|
107
|
+
require 'mongomodel/compatibility/mongoid' if defined?(Mongoid)
|
108
|
+
require 'mongomodel/compatibility/mongo_mapper' if defined?(MongoMapper)
|
106
109
|
end
|
107
110
|
|
108
111
|
I18n.load_path << File.dirname(__FILE__) + '/mongomodel/locale/en.yml'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class SymbolOperator
|
2
|
+
def to_mongo_operator
|
3
|
+
MongoModel::MongoOperator.new(field, operator)
|
4
|
+
end
|
5
|
+
|
6
|
+
def to_mongo_order_clause
|
7
|
+
MongoModel::MongoOrder::Clause.new(field, operator.to_s == 'asc' ? :ascending : :descending)
|
8
|
+
end
|
9
|
+
|
10
|
+
def eql?(other)
|
11
|
+
self == other
|
12
|
+
end
|
13
|
+
|
14
|
+
def hash
|
15
|
+
field.hash ^ operator.hash
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class NilClass
|
20
|
+
def to_mongo(value=nil)
|
21
|
+
value
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Origin::Key
|
2
|
+
def to_mongo_operator
|
3
|
+
MongoModel::MongoOperator.new(name, operator.sub(/^\$/, ""))
|
4
|
+
end
|
5
|
+
|
6
|
+
def to_mongo_order_clause
|
7
|
+
MongoModel::MongoOrder::Clause.new(name, operator == 1 ? :ascending : :descending)
|
8
|
+
end
|
9
|
+
|
10
|
+
def eql?(other)
|
11
|
+
self == other
|
12
|
+
end
|
13
|
+
|
14
|
+
def hash
|
15
|
+
name.hash ^ operator.hash
|
16
|
+
end
|
17
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support/core_ext/object/duplicable'
|
1
2
|
require 'active_support/core_ext/module/delegation'
|
2
3
|
require 'active_support/core_ext/array/extract_options'
|
3
4
|
require 'active_support/core_ext/hash/except'
|
@@ -81,6 +82,10 @@ module MongoModel
|
|
81
82
|
as =~ /^_/ || options[:internal]
|
82
83
|
end
|
83
84
|
|
85
|
+
def validate?
|
86
|
+
options[:validate] != false
|
87
|
+
end
|
88
|
+
|
84
89
|
private
|
85
90
|
def type_converter
|
86
91
|
@type_converter ||= Types.converter_for(type)
|
@@ -8,9 +8,11 @@ module MongoModel
|
|
8
8
|
def property(name, *args, &block) #:nodoc:
|
9
9
|
property = super
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
if property.validate?
|
12
|
+
validates_associated(name) if property.embeddable?
|
13
|
+
validates_presence_of(name) if property.options[:required]
|
14
|
+
validates_format_of(name, property.options[:format]) if property.options[:format]
|
15
|
+
end
|
14
16
|
|
15
17
|
property
|
16
18
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
class Boolean
|
1
|
+
class Boolean; end unless defined?(Boolean)
|
2
2
|
|
3
3
|
class Symbol
|
4
4
|
[:lt, :lte, :gt, :gte, :ne, :in, :nin, :mod, :all, :size, :exists, :near].each do |operator|
|
5
|
-
define_method(operator) { MongoModel::MongoOperator.new(self, operator) }
|
5
|
+
define_method(operator) { MongoModel::MongoOperator.new(self, operator) } unless method_defined?(operator)
|
6
6
|
end
|
7
7
|
|
8
|
-
define_method(:asc) { MongoModel::MongoOrder::Clause.new(self, :ascending) }
|
9
|
-
define_method(:desc) { MongoModel::MongoOrder::Clause.new(self, :descending) }
|
8
|
+
define_method(:asc) { MongoModel::MongoOrder::Clause.new(self, :ascending) } unless method_defined?(:asc)
|
9
|
+
define_method(:desc) { MongoModel::MongoOrder::Clause.new(self, :descending) } unless method_defined?(:desc)
|
10
10
|
end
|
@@ -26,7 +26,9 @@ module MongoModel
|
|
26
26
|
result = {}
|
27
27
|
|
28
28
|
(options[:conditions] || {}).each do |k, v|
|
29
|
-
|
29
|
+
k = k.to_mongo_operator if k.respond_to?(:to_mongo_operator)
|
30
|
+
|
31
|
+
key = k.respond_to?(:field) ? k.field : k
|
30
32
|
|
31
33
|
if @model.respond_to?(:properties) && property = @model.properties[key]
|
32
34
|
key = property.as
|
@@ -35,7 +37,7 @@ module MongoModel
|
|
35
37
|
value = Types.converter_for(v.class).to_mongo(v)
|
36
38
|
end
|
37
39
|
|
38
|
-
if k.
|
40
|
+
if k.respond_to?(:to_mongo_selector)
|
39
41
|
selector = k.to_mongo_selector(value)
|
40
42
|
|
41
43
|
if result[key].is_a?(Hash)
|
@@ -43,6 +43,8 @@ module MongoModel
|
|
43
43
|
new(*order.split(',').map { |c| Clause.parse(c) })
|
44
44
|
when Array
|
45
45
|
new(*order.map { |c| Clause.parse(c) })
|
46
|
+
else
|
47
|
+
new(order.to_mongo_order_clause) if order.respond_to?(:to_mongo_order_clause)
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
@@ -81,6 +83,8 @@ module MongoModel
|
|
81
83
|
when String, Symbol
|
82
84
|
field, order = clause.to_s.strip.split(/ /)
|
83
85
|
new(field, order =~ /^desc/i ? :descending : :ascending)
|
86
|
+
else
|
87
|
+
clause.to_mongo_order_clause if clause.respond_to?(:to_mongo_order_clause)
|
84
88
|
end
|
85
89
|
end
|
86
90
|
end
|
@@ -141,7 +141,7 @@ module MongoModel
|
|
141
141
|
end
|
142
142
|
|
143
143
|
def options_for_create
|
144
|
-
@options_for_create ||= finder_conditions.reject { |k, v| k.is_a?(MongoModel::MongoOperator) }
|
144
|
+
@options_for_create ||= finder_conditions.reject { |k, v| k.is_a?(MongoModel::MongoOperator) || k.respond_to?(:to_mongo_operator) }
|
145
145
|
end
|
146
146
|
|
147
147
|
def respond_to?(method, include_private = false)
|
data/lib/mongomodel/version.rb
CHANGED
@@ -31,12 +31,12 @@ module MongoModel
|
|
31
31
|
|
32
32
|
subject { Attributes::Store.new(instance) }
|
33
33
|
|
34
|
-
it "
|
34
|
+
it "sets default property values" do
|
35
35
|
subject.keys.should == properties.keys
|
36
36
|
subject[:default].should == 'Default'
|
37
37
|
end
|
38
38
|
|
39
|
-
it "
|
39
|
+
it "sets default property value using mongomodel_default if defined by class" do
|
40
40
|
subject[:custom_default].should == CustomClassWithDefault.new("Custom class default")
|
41
41
|
end
|
42
42
|
|
@@ -49,7 +49,7 @@ module MongoModel
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
it "
|
52
|
+
it "sets attributes that aren't properties" do
|
53
53
|
subject[:non_property] = "Hello World"
|
54
54
|
subject[:non_property].should == "Hello World"
|
55
55
|
end
|
@@ -168,12 +168,12 @@ module MongoModel
|
|
168
168
|
context "assigning to #{type} property" do
|
169
169
|
examples.each do |assigned, expected|
|
170
170
|
if expected.is_a?(Proc)
|
171
|
-
it "
|
171
|
+
it "casts #{assigned.inspect} to match proc" do
|
172
172
|
subject[type] = assigned
|
173
173
|
expected.call(subject[type]).should be_true
|
174
174
|
end
|
175
175
|
else
|
176
|
-
it "
|
176
|
+
it "casts #{assigned.inspect} to #{expected.inspect}" do
|
177
177
|
subject[type] = assigned
|
178
178
|
subject[type].should == expected
|
179
179
|
end
|
@@ -187,12 +187,12 @@ module MongoModel
|
|
187
187
|
@custom = CustomClass.new('instance name')
|
188
188
|
end
|
189
189
|
|
190
|
-
it "
|
190
|
+
it "does not alter instances of CustomClass" do
|
191
191
|
subject[:custom] = @custom
|
192
192
|
subject[:custom].should == @custom
|
193
193
|
end
|
194
194
|
|
195
|
-
it "
|
195
|
+
it "casts strings to CustomClass" do
|
196
196
|
subject[:custom] = "foobar"
|
197
197
|
subject[:custom].should == CustomClass.new('foobar')
|
198
198
|
end
|
@@ -217,7 +217,7 @@ module MongoModel
|
|
217
217
|
BeforeTypeCastExamples.each do |type, examples|
|
218
218
|
context "assigning to #{type} property" do
|
219
219
|
examples.each do |example|
|
220
|
-
it "
|
220
|
+
it "accesses pre-typecast value of #{example.inspect}" do
|
221
221
|
subject[type] = example
|
222
222
|
subject.before_type_cast(type).should == example
|
223
223
|
end
|
@@ -260,7 +260,7 @@ module MongoModel
|
|
260
260
|
TrueExamples.each do |type, examples|
|
261
261
|
context "assigning to #{type} property" do
|
262
262
|
examples.each do |example|
|
263
|
-
it "
|
263
|
+
it "returns true for #{example.inspect}" do
|
264
264
|
subject[type] = example
|
265
265
|
subject.has?(type).should == true
|
266
266
|
end
|
@@ -271,7 +271,7 @@ module MongoModel
|
|
271
271
|
FalseExamples.each do |type, examples|
|
272
272
|
context "assigning to #{type} property" do
|
273
273
|
examples.each do |example|
|
274
|
-
it "
|
274
|
+
it "returns false for #{example.inspect}" do
|
275
275
|
subject[type] = example
|
276
276
|
subject.has?(type).should == false
|
277
277
|
end
|
@@ -281,7 +281,7 @@ module MongoModel
|
|
281
281
|
end
|
282
282
|
|
283
283
|
describe "serialization" do
|
284
|
-
it "
|
284
|
+
it "converts to mongo representation" do
|
285
285
|
subject[:string] = 'string'
|
286
286
|
subject[:integer] = 42
|
287
287
|
subject[:float] = 123.45
|
@@ -317,7 +317,7 @@ module MongoModel
|
|
317
317
|
})
|
318
318
|
end
|
319
319
|
|
320
|
-
it "
|
320
|
+
it "loads from mongo representation" do
|
321
321
|
subject.from_mongo!({
|
322
322
|
'string' => 'string',
|
323
323
|
'integer' => 42,
|
@@ -9,21 +9,21 @@ module MongoModel
|
|
9
9
|
let(:special_user) { SpecialUser.create! }
|
10
10
|
|
11
11
|
context "when uninitialized" do
|
12
|
-
it "
|
12
|
+
it "is nil" do
|
13
13
|
subject.user.should be_nil
|
14
14
|
end
|
15
15
|
|
16
|
-
it "
|
16
|
+
it "is settable" do
|
17
17
|
subject.user = user
|
18
18
|
subject.user.should == user
|
19
19
|
end
|
20
20
|
|
21
|
-
it "
|
21
|
+
it "is not truthy" do
|
22
22
|
subject.user.should_not be_truthy
|
23
23
|
end
|
24
24
|
|
25
25
|
describe "setting a subclass type" do
|
26
|
-
it "
|
26
|
+
it "sets successfully" do
|
27
27
|
subject.user = special_user
|
28
28
|
subject.user.should == special_user
|
29
29
|
end
|
@@ -45,15 +45,15 @@ module MongoModel
|
|
45
45
|
let(:reloaded) { Article.find(subject.id) }
|
46
46
|
end
|
47
47
|
|
48
|
-
it "
|
48
|
+
it "accesses the user through the association" do
|
49
49
|
reloaded.user.should == user
|
50
50
|
end
|
51
51
|
|
52
|
-
it "
|
52
|
+
it "is truthy" do
|
53
53
|
subject.user.should be_truthy
|
54
54
|
end
|
55
55
|
|
56
|
-
it "
|
56
|
+
it "allows the user to be reloaded" do
|
57
57
|
user = reloaded.user.target
|
58
58
|
|
59
59
|
user.should equal(reloaded.user.target)
|
@@ -64,7 +64,7 @@ module MongoModel
|
|
64
64
|
describe "setting a subclass type" do
|
65
65
|
subject { Article.new(:user => special_user) }
|
66
66
|
|
67
|
-
it "
|
67
|
+
it "loads successfully" do
|
68
68
|
reloaded.user.should == special_user
|
69
69
|
end
|
70
70
|
end
|
@@ -86,7 +86,7 @@ module MongoModel
|
|
86
86
|
|
87
87
|
let(:non_user) { NonUser.create! }
|
88
88
|
|
89
|
-
it "
|
89
|
+
it "raises a AssociationTypeMismatch exception" do
|
90
90
|
lambda { subject.user = non_user }.should raise_error(AssociationTypeMismatch, "expected instance of User but got NonUser")
|
91
91
|
end
|
92
92
|
end
|
@@ -94,7 +94,7 @@ module MongoModel
|
|
94
94
|
describe "#build_user" do
|
95
95
|
let(:user) { subject.build_user(:id => '123') }
|
96
96
|
|
97
|
-
it "
|
97
|
+
it "returns a new unsaved user with the given attributes" do
|
98
98
|
user.should be_an_instance_of(User)
|
99
99
|
user.should be_a_new_record
|
100
100
|
user.id.should == '123'
|
@@ -102,7 +102,7 @@ module MongoModel
|
|
102
102
|
end
|
103
103
|
|
104
104
|
describe "#create_user" do
|
105
|
-
it "
|
105
|
+
it "returns a new saved user with the given attributes" do
|
106
106
|
user = subject.create_user(:id => '123')
|
107
107
|
user.should be_an_instance_of(User)
|
108
108
|
user.should_not be_a_new_record
|
@@ -125,7 +125,7 @@ module MongoModel
|
|
125
125
|
it_should_behave_like "assigning correct class to belongs_to association"
|
126
126
|
|
127
127
|
describe "setting a different class type" do
|
128
|
-
it "
|
128
|
+
it "sets successfully" do
|
129
129
|
subject.user = non_user
|
130
130
|
subject.user.should == non_user
|
131
131
|
end
|
@@ -149,7 +149,7 @@ module MongoModel
|
|
149
149
|
describe "setting a different class type" do
|
150
150
|
subject { Article.new(:user => non_user) }
|
151
151
|
|
152
|
-
it "
|
152
|
+
it "loads successfully" do
|
153
153
|
reloaded.user.should == non_user
|
154
154
|
end
|
155
155
|
end
|
@@ -7,11 +7,11 @@ module MongoModel
|
|
7
7
|
has_many :chapters
|
8
8
|
end
|
9
9
|
|
10
|
-
it "
|
10
|
+
it "defaults to :by => :foreign_key" do
|
11
11
|
MyBook.associations[:chapters].should be_a(Associations::HasManyByForeignKey)
|
12
12
|
end
|
13
13
|
|
14
|
-
it "
|
14
|
+
it "sets default inverse_of value" do
|
15
15
|
MyBook.associations[:chapters].inverse_of.should == :my_book
|
16
16
|
end
|
17
17
|
end
|
@@ -34,46 +34,46 @@ module MongoModel
|
|
34
34
|
context "when uninitialized" do
|
35
35
|
subject { Book.new }
|
36
36
|
|
37
|
-
it "
|
37
|
+
it "is empty" do
|
38
38
|
subject.chapters.should be_empty
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
shared_examples_for "accessing and manipulating a has_many :by => :foreign_key association" do
|
43
|
-
it "
|
43
|
+
it "accesses chapters" do
|
44
44
|
subject.chapters.should include(chapter1, chapter2)
|
45
45
|
end
|
46
46
|
|
47
|
-
it "
|
47
|
+
it "accesses chapter ids through association" do
|
48
48
|
subject.chapters.ids.should include(chapter1.id, chapter2.id)
|
49
49
|
end
|
50
50
|
|
51
|
-
it "
|
51
|
+
it "adds chapters with <<" do
|
52
52
|
subject.chapters << chapter3
|
53
53
|
subject.chapters.should include(chapter1, chapter2, chapter3)
|
54
54
|
chapter3.book.should == subject
|
55
55
|
end
|
56
56
|
|
57
|
-
it "
|
57
|
+
it "adds/change chapters with []=" do
|
58
58
|
subject.chapters[2] = chapter3
|
59
59
|
subject.chapters.should include(chapter1, chapter2, chapter3)
|
60
60
|
chapter3.book.should == subject
|
61
61
|
end
|
62
62
|
|
63
|
-
it "
|
63
|
+
it "replaces chapters with []=" do
|
64
64
|
subject.chapters[1] = chapter3
|
65
65
|
subject.chapters.should include(chapter1, chapter3)
|
66
66
|
subject.chapters.should_not include(chapter2)
|
67
67
|
#chapter2.book.should be_nil
|
68
68
|
end
|
69
69
|
|
70
|
-
it "
|
70
|
+
it "adds chapters with concat" do
|
71
71
|
subject.chapters.concat([chapter3])
|
72
72
|
subject.chapters.should include(chapter1, chapter2, chapter3)
|
73
73
|
chapter3.book.should == subject
|
74
74
|
end
|
75
75
|
|
76
|
-
it "
|
76
|
+
it "inserts chapters" do
|
77
77
|
subject.chapters.insert(1, chapter3)
|
78
78
|
subject.chapters.should include(chapter1, chapter2, chapter3)
|
79
79
|
chapter3.book.should == subject
|
@@ -85,13 +85,13 @@ module MongoModel
|
|
85
85
|
# subject.chapter_ids.should == [chapter2.id, chapter3.id]
|
86
86
|
# end
|
87
87
|
|
88
|
-
it "
|
88
|
+
it "adds chapters with push" do
|
89
89
|
subject.chapters.push(chapter3)
|
90
90
|
subject.chapters.should include(chapter1, chapter2, chapter3)
|
91
91
|
chapter3.book.should == subject
|
92
92
|
end
|
93
93
|
|
94
|
-
it "
|
94
|
+
it "adds chapters with unshift" do
|
95
95
|
subject.chapters.unshift(chapter3)
|
96
96
|
subject.chapters.should include(chapter3, chapter1, chapter2)
|
97
97
|
chapter3.book.should == subject
|
@@ -103,13 +103,13 @@ module MongoModel
|
|
103
103
|
# [chapter1, chapter2].each { |c| c.book.should be_nil }
|
104
104
|
# end
|
105
105
|
|
106
|
-
it "
|
106
|
+
it "removes chapters with delete" do
|
107
107
|
subject.chapters.delete(chapter1)
|
108
108
|
subject.chapters.should == [chapter2]
|
109
109
|
chapter1.book.should be_nil
|
110
110
|
end
|
111
111
|
|
112
|
-
it "
|
112
|
+
it "removes chapters with delete_at" do
|
113
113
|
subject.chapters.delete_at(0)
|
114
114
|
subject.chapters.should == [chapter2]
|
115
115
|
#chapter1.book.should be_nil
|
@@ -121,7 +121,7 @@ module MongoModel
|
|
121
121
|
# subject.chapter_ids.should == [chapter2.id]
|
122
122
|
# end
|
123
123
|
|
124
|
-
it "
|
124
|
+
it "builds a chapter" do
|
125
125
|
chapter4 = subject.chapters.build(:id => '4')
|
126
126
|
subject.chapters.should include(chapter1, chapter2, chapter4)
|
127
127
|
|
@@ -131,7 +131,7 @@ module MongoModel
|
|
131
131
|
chapter4.book_id.should == subject.id
|
132
132
|
end
|
133
133
|
|
134
|
-
it "
|
134
|
+
it "creates a chapter" do
|
135
135
|
chapter4 = subject.chapters.create(:id => '4')
|
136
136
|
subject.chapters.should == [chapter1, chapter2, chapter4]
|
137
137
|
|
@@ -141,7 +141,7 @@ module MongoModel
|
|
141
141
|
chapter4.book_id.should == subject.id
|
142
142
|
end
|
143
143
|
|
144
|
-
it "
|
144
|
+
it "finds chapters" do
|
145
145
|
# Create bogus chapters
|
146
146
|
Chapter.create!(:id => '999')
|
147
147
|
Chapter.create!(:id => '998')
|
@@ -150,18 +150,18 @@ module MongoModel
|
|
150
150
|
result.should == [chapter2, chapter1]
|
151
151
|
end
|
152
152
|
|
153
|
-
it "
|
153
|
+
it "finds chapters with association options" do
|
154
154
|
# Create bogus chapters
|
155
155
|
10.times { subject.chapters.create! }
|
156
156
|
|
157
157
|
subject.chapters.all.size.should == 5 # limit clause
|
158
158
|
end
|
159
159
|
|
160
|
-
it "
|
160
|
+
it "supports scope select method" do
|
161
161
|
subject.chapters.select(:id, :_type, :book_id).should == [chapter1, chapter2]
|
162
162
|
end
|
163
163
|
|
164
|
-
it "
|
164
|
+
it "supports array select method" do
|
165
165
|
subject.chapters.select { |c| c.is_a?(IllustratedChapter) }.should == [chapter2]
|
166
166
|
end
|
167
167
|
end
|
@@ -185,7 +185,7 @@ module MongoModel
|
|
185
185
|
subject { Book.create!(:chapters => [chapter1, chapter2, chapter3]) }
|
186
186
|
|
187
187
|
context "when the parent object is destroyed" do
|
188
|
-
it "
|
188
|
+
it "calls destroy on the child objects" do
|
189
189
|
chapter1.should_receive(:destroy)
|
190
190
|
chapter2.should_receive(:destroy)
|
191
191
|
chapter3.should_receive(:destroy)
|
@@ -193,7 +193,7 @@ module MongoModel
|
|
193
193
|
subject.destroy
|
194
194
|
end
|
195
195
|
|
196
|
-
it "
|
196
|
+
it "removes the child objects from their collection" do
|
197
197
|
subject.destroy
|
198
198
|
|
199
199
|
Chapter.exists?(chapter1.id).should be_false
|
@@ -211,7 +211,7 @@ module MongoModel
|
|
211
211
|
subject { Book.create!(:chapters => [chapter1, chapter2, chapter3]) }
|
212
212
|
|
213
213
|
context "when the parent object is destroyed" do
|
214
|
-
it "
|
214
|
+
it "does not call destroy on the child objects" do
|
215
215
|
chapter1.should_not_receive(:destroy)
|
216
216
|
chapter2.should_not_receive(:destroy)
|
217
217
|
chapter3.should_not_receive(:destroy)
|
@@ -219,7 +219,7 @@ module MongoModel
|
|
219
219
|
subject.destroy
|
220
220
|
end
|
221
221
|
|
222
|
-
it "
|
222
|
+
it "removes the child objects from their collection" do
|
223
223
|
subject.destroy
|
224
224
|
|
225
225
|
Chapter.exists?(chapter1.id).should be_false
|
@@ -235,7 +235,7 @@ module MongoModel
|
|
235
235
|
describe "defining a has_many :by => :foreign_key association" do
|
236
236
|
define_class(:Book, EmbeddedDocument)
|
237
237
|
|
238
|
-
it "
|
238
|
+
it "raises an exception" do
|
239
239
|
lambda { Book.has_many :chapters, :by => :foreign_key }.should raise_error
|
240
240
|
end
|
241
241
|
end
|