mongoid-locomotive 2.0.0.beta9
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/MIT_LICENSE +20 -0
- data/README.rdoc +47 -0
- data/lib/mongoid.rb +141 -0
- data/lib/mongoid/associations.rb +306 -0
- data/lib/mongoid/associations/embedded_in.rb +74 -0
- data/lib/mongoid/associations/embeds_many.rb +280 -0
- data/lib/mongoid/associations/embeds_one.rb +97 -0
- data/lib/mongoid/associations/foreign_key.rb +35 -0
- data/lib/mongoid/associations/meta_data.rb +38 -0
- data/lib/mongoid/associations/options.rb +62 -0
- data/lib/mongoid/associations/proxy.rb +33 -0
- data/lib/mongoid/associations/referenced_in.rb +59 -0
- data/lib/mongoid/associations/references_many.rb +245 -0
- data/lib/mongoid/associations/references_many_as_array.rb +78 -0
- data/lib/mongoid/associations/references_one.rb +99 -0
- data/lib/mongoid/atomicity.rb +55 -0
- data/lib/mongoid/attributes.rb +242 -0
- data/lib/mongoid/callbacks.rb +21 -0
- data/lib/mongoid/collection.rb +120 -0
- data/lib/mongoid/collections.rb +71 -0
- data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
- data/lib/mongoid/collections/master.rb +29 -0
- data/lib/mongoid/collections/operations.rb +41 -0
- data/lib/mongoid/collections/slaves.rb +45 -0
- data/lib/mongoid/components.rb +34 -0
- data/lib/mongoid/config.rb +263 -0
- data/lib/mongoid/contexts.rb +24 -0
- data/lib/mongoid/contexts/enumerable.rb +156 -0
- data/lib/mongoid/contexts/ids.rb +25 -0
- data/lib/mongoid/contexts/mongo.rb +285 -0
- data/lib/mongoid/contexts/paging.rb +50 -0
- data/lib/mongoid/criteria.rb +248 -0
- data/lib/mongoid/criterion/complex.rb +21 -0
- data/lib/mongoid/criterion/exclusion.rb +65 -0
- data/lib/mongoid/criterion/inclusion.rb +110 -0
- data/lib/mongoid/criterion/optional.rb +189 -0
- data/lib/mongoid/cursor.rb +81 -0
- data/lib/mongoid/deprecation.rb +21 -0
- data/lib/mongoid/dirty.rb +252 -0
- data/lib/mongoid/document.rb +210 -0
- data/lib/mongoid/errors.rb +131 -0
- data/lib/mongoid/extensions.rb +115 -0
- data/lib/mongoid/extensions/array/accessors.rb +17 -0
- data/lib/mongoid/extensions/array/assimilation.rb +26 -0
- data/lib/mongoid/extensions/array/conversions.rb +23 -0
- data/lib/mongoid/extensions/array/parentization.rb +13 -0
- data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
- data/lib/mongoid/extensions/binary/conversions.rb +17 -0
- data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
- data/lib/mongoid/extensions/date/conversions.rb +24 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/false_class/equality.rb +13 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/accessors.rb +42 -0
- data/lib/mongoid/extensions/hash/assimilation.rb +40 -0
- data/lib/mongoid/extensions/hash/conversions.rb +42 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/assimilation.rb +17 -0
- data/lib/mongoid/extensions/object/conversions.rb +21 -0
- data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +15 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +35 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +83 -0
- data/lib/mongoid/fields.rb +62 -0
- data/lib/mongoid/finders.rb +145 -0
- data/lib/mongoid/hierarchy.rb +74 -0
- data/lib/mongoid/identity.rb +47 -0
- data/lib/mongoid/indexes.rb +27 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/logger.rb +19 -0
- data/lib/mongoid/matchers.rb +35 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +26 -0
- data/lib/mongoid/matchers/exists.rb +13 -0
- data/lib/mongoid/matchers/gt.rb +11 -0
- data/lib/mongoid/matchers/gte.rb +11 -0
- data/lib/mongoid/matchers/in.rb +11 -0
- data/lib/mongoid/matchers/lt.rb +11 -0
- data/lib/mongoid/matchers/lte.rb +11 -0
- data/lib/mongoid/matchers/ne.rb +11 -0
- data/lib/mongoid/matchers/nin.rb +11 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/lib/mongoid/memoization.rb +33 -0
- data/lib/mongoid/named_scope.rb +37 -0
- data/lib/mongoid/paranoia.rb +106 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence.rb +216 -0
- data/lib/mongoid/persistence/command.rb +39 -0
- data/lib/mongoid/persistence/insert.rb +48 -0
- data/lib/mongoid/persistence/insert_embedded.rb +44 -0
- data/lib/mongoid/persistence/remove.rb +39 -0
- data/lib/mongoid/persistence/remove_all.rb +38 -0
- data/lib/mongoid/persistence/remove_embedded.rb +50 -0
- data/lib/mongoid/persistence/update.rb +71 -0
- data/lib/mongoid/railtie.rb +67 -0
- data/lib/mongoid/railties/database.rake +60 -0
- data/lib/mongoid/scope.rb +75 -0
- data/lib/mongoid/state.rb +32 -0
- data/lib/mongoid/timestamps.rb +27 -0
- data/lib/mongoid/validations.rb +51 -0
- data/lib/mongoid/validations/associated.rb +32 -0
- data/lib/mongoid/validations/locale/en.yml +5 -0
- data/lib/mongoid/validations/uniqueness.rb +56 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +26 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +24 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +15 -0
- data/lib/rails/generators/mongoid_generator.rb +61 -0
- data/spec/integration/mongoid/association_attributes_spec.rb +71 -0
- data/spec/integration/mongoid/associations_spec.rb +768 -0
- data/spec/integration/mongoid/attributes_spec.rb +59 -0
- data/spec/integration/mongoid/callback_spec.rb +33 -0
- data/spec/integration/mongoid/contexts/enumerable_spec.rb +33 -0
- data/spec/integration/mongoid/criteria_spec.rb +281 -0
- data/spec/integration/mongoid/dirty_spec.rb +85 -0
- data/spec/integration/mongoid/document_spec.rb +741 -0
- data/spec/integration/mongoid/extensions_spec.rb +22 -0
- data/spec/integration/mongoid/finders_spec.rb +119 -0
- data/spec/integration/mongoid/inheritance_spec.rb +171 -0
- data/spec/integration/mongoid/named_scope_spec.rb +58 -0
- data/spec/integration/mongoid/paranoia_spec.rb +44 -0
- data/spec/integration/mongoid/persistence/update_spec.rb +46 -0
- data/spec/integration/mongoid/persistence_spec.rb +311 -0
- data/spec/integration/mongoid/validations/uniqueness_spec.rb +206 -0
- data/spec/models/account.rb +5 -0
- data/spec/models/address.rb +40 -0
- data/spec/models/agent.rb +7 -0
- data/spec/models/animal.rb +15 -0
- data/spec/models/answer.rb +4 -0
- data/spec/models/callbacks.rb +47 -0
- data/spec/models/category.rb +13 -0
- data/spec/models/comment.rb +10 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/favorite.rb +8 -0
- data/spec/models/game.rb +9 -0
- data/spec/models/inheritance.rb +72 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/login.rb +6 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/paranoid_post.rb +18 -0
- data/spec/models/parents.rb +32 -0
- data/spec/models/patient.rb +15 -0
- data/spec/models/person.rb +106 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/post.rb +25 -0
- data/spec/models/preference.rb +7 -0
- data/spec/models/question.rb +8 -0
- data/spec/models/survey.rb +6 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/user.rb +6 -0
- data/spec/models/user_accout.rb +5 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/models/video.rb +5 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/unit/mongoid/associations/embedded_in_spec.rb +193 -0
- data/spec/unit/mongoid/associations/embeds_many_spec.rb +626 -0
- data/spec/unit/mongoid/associations/embeds_one_spec.rb +287 -0
- data/spec/unit/mongoid/associations/foreign_key_spec.rb +90 -0
- data/spec/unit/mongoid/associations/meta_data_spec.rb +110 -0
- data/spec/unit/mongoid/associations/options_spec.rb +215 -0
- data/spec/unit/mongoid/associations/referenced_in_spec.rb +145 -0
- data/spec/unit/mongoid/associations/references_many_as_array_spec.rb +424 -0
- data/spec/unit/mongoid/associations/references_many_spec.rb +502 -0
- data/spec/unit/mongoid/associations/references_one_spec.rb +204 -0
- data/spec/unit/mongoid/associations_spec.rb +688 -0
- data/spec/unit/mongoid/atomicity_spec.rb +164 -0
- data/spec/unit/mongoid/attributes_spec.rb +646 -0
- data/spec/unit/mongoid/callbacks_spec.rb +85 -0
- data/spec/unit/mongoid/collection_spec.rb +187 -0
- data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +75 -0
- data/spec/unit/mongoid/collections/master_spec.rb +41 -0
- data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
- data/spec/unit/mongoid/collections_spec.rb +98 -0
- data/spec/unit/mongoid/config_spec.rb +298 -0
- data/spec/unit/mongoid/contexts/enumerable_spec.rb +447 -0
- data/spec/unit/mongoid/contexts/mongo_spec.rb +703 -0
- data/spec/unit/mongoid/contexts_spec.rb +25 -0
- data/spec/unit/mongoid/criteria_spec.rb +873 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +17 -0
- data/spec/unit/mongoid/criterion/exclusion_spec.rb +121 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +274 -0
- data/spec/unit/mongoid/criterion/optional_spec.rb +483 -0
- data/spec/unit/mongoid/cursor_spec.rb +80 -0
- data/spec/unit/mongoid/deprecation_spec.rb +24 -0
- data/spec/unit/mongoid/dirty_spec.rb +430 -0
- data/spec/unit/mongoid/document_spec.rb +623 -0
- data/spec/unit/mongoid/errors_spec.rb +154 -0
- data/spec/unit/mongoid/extensions/array/accessors_spec.rb +50 -0
- data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +24 -0
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +52 -0
- data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
- data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +36 -0
- data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +49 -0
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +145 -0
- data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/false_class/equality_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/float/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +184 -0
- data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +59 -0
- data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +17 -0
- data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +29 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +44 -0
- data/spec/unit/mongoid/extensions/objectid/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
- data/spec/unit/mongoid/extensions/set/conversions_spec.rb +21 -0
- data/spec/unit/mongoid/extensions/string/conversions_spec.rb +28 -0
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
- data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +107 -0
- data/spec/unit/mongoid/extensions/time_conversions_spec.rb +186 -0
- data/spec/unit/mongoid/extensions/true_class/equality_spec.rb +35 -0
- data/spec/unit/mongoid/extras_spec.rb +102 -0
- data/spec/unit/mongoid/factory_spec.rb +31 -0
- data/spec/unit/mongoid/field_spec.rb +169 -0
- data/spec/unit/mongoid/fields_spec.rb +181 -0
- data/spec/unit/mongoid/finders_spec.rb +439 -0
- data/spec/unit/mongoid/hierarchy_spec.rb +68 -0
- data/spec/unit/mongoid/identity_spec.rb +109 -0
- data/spec/unit/mongoid/indexes_spec.rb +99 -0
- data/spec/unit/mongoid/javascript_spec.rb +48 -0
- data/spec/unit/mongoid/logger_spec.rb +38 -0
- data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
- data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
- data/spec/unit/mongoid/matchers_spec.rb +329 -0
- data/spec/unit/mongoid/memoization_spec.rb +75 -0
- data/spec/unit/mongoid/named_scope_spec.rb +123 -0
- data/spec/unit/mongoid/paranoia_spec.rb +108 -0
- data/spec/unit/mongoid/paths_spec.rb +272 -0
- data/spec/unit/mongoid/persistence/insert_embedded_spec.rb +154 -0
- data/spec/unit/mongoid/persistence/insert_spec.rb +144 -0
- data/spec/unit/mongoid/persistence/remove_all_spec.rb +82 -0
- data/spec/unit/mongoid/persistence/remove_embedded_spec.rb +152 -0
- data/spec/unit/mongoid/persistence/remove_spec.rb +89 -0
- data/spec/unit/mongoid/persistence/update_spec.rb +177 -0
- data/spec/unit/mongoid/persistence_spec.rb +452 -0
- data/spec/unit/mongoid/scope_spec.rb +240 -0
- data/spec/unit/mongoid/serialization_spec.rb +43 -0
- data/spec/unit/mongoid/state_spec.rb +94 -0
- data/spec/unit/mongoid/timestamps_spec.rb +30 -0
- data/spec/unit/mongoid/validations/associated_spec.rb +103 -0
- data/spec/unit/mongoid/validations/uniqueness_spec.rb +201 -0
- data/spec/unit/mongoid/validations_spec.rb +43 -0
- data/spec/unit/mongoid/versioning_spec.rb +41 -0
- data/spec/unit/mongoid_spec.rb +46 -0
- metadata +433 -0
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Mongoid::Associations::ReferencesMany do
|
|
4
|
+
|
|
5
|
+
let(:block) do
|
|
6
|
+
Proc.new do
|
|
7
|
+
def extension
|
|
8
|
+
"Testing"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
let(:options) do
|
|
14
|
+
Mongoid::Associations::Options.new(
|
|
15
|
+
:name => :posts,
|
|
16
|
+
:foreign_key => "person_id",
|
|
17
|
+
:extend => block
|
|
18
|
+
)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe "#<<" do
|
|
22
|
+
|
|
23
|
+
before do
|
|
24
|
+
@child = Post.instantiate(:id => "1")
|
|
25
|
+
@second = Post.instantiate(:id => "2")
|
|
26
|
+
@children = [@child, @second]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context "when parent document has been saved" do
|
|
30
|
+
|
|
31
|
+
before do
|
|
32
|
+
@parent = stub(:id => "1", :new_record? => false, :class => Person)
|
|
33
|
+
Post.expects(:all).returns([])
|
|
34
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "saves and appends the child document" do
|
|
38
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
39
|
+
@child.expects(:save).returns(true)
|
|
40
|
+
@association << @child
|
|
41
|
+
@association.size.should == 1
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context "when parent document has not been saved" do
|
|
47
|
+
|
|
48
|
+
context "when appending a non mongoid object" do
|
|
49
|
+
|
|
50
|
+
before do
|
|
51
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
|
52
|
+
Post.expects(:all).returns([])
|
|
53
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "appends the child document" do
|
|
57
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
58
|
+
@association << @child
|
|
59
|
+
@association.size.should == 1
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
context "when appending a mongoid document" do
|
|
64
|
+
|
|
65
|
+
before do
|
|
66
|
+
@criteria = mock
|
|
67
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
|
68
|
+
Post.expects(:all).returns(@criteria)
|
|
69
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "appends the child document" do
|
|
73
|
+
@criteria.expects(:entries).returns([])
|
|
74
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
75
|
+
@association << @child
|
|
76
|
+
@association.size.should == 1
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
context "with multiple objects" do
|
|
83
|
+
|
|
84
|
+
before do
|
|
85
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
|
86
|
+
Post.expects(:all).returns([])
|
|
87
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it "appends the child documents" do
|
|
91
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
92
|
+
@second.expects(:person_id=).with(@parent.id)
|
|
93
|
+
@association << [@child, @second]
|
|
94
|
+
@association.size.should == 2
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
describe "#build" do
|
|
102
|
+
|
|
103
|
+
before do
|
|
104
|
+
@criteria = mock
|
|
105
|
+
@criteria.expects(:entries).returns([])
|
|
106
|
+
@parent = stub(:id => "5", :class => Person, :new_record? => true)
|
|
107
|
+
Post.expects(:all).returns(@criteria)
|
|
108
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
it "adds a new object to the association" do
|
|
112
|
+
@association.build(:title => "Sassy")
|
|
113
|
+
@association.size.should == 1
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it "sets the parent object id on the child" do
|
|
117
|
+
@association.build(:title => "Sassy")
|
|
118
|
+
@association.first.person_id.should == @parent.id
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
it "returns the new object" do
|
|
122
|
+
@association.build(:title => "Sassy").title.should == "Sassy"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "sets the parent object reference on the child" do
|
|
126
|
+
@association.build(:title => "Sassy")
|
|
127
|
+
@association.first.person.should == @parent
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
context "when passing nil" do
|
|
131
|
+
|
|
132
|
+
it "builds an object with empty attributes" do
|
|
133
|
+
@association.build(nil)
|
|
134
|
+
@association.first.person.should == @parent
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
describe "#delete_all" do
|
|
140
|
+
|
|
141
|
+
before do
|
|
142
|
+
@criteria = mock
|
|
143
|
+
@parent = stub(:id => "5", :class => Person, :new_record? => true)
|
|
144
|
+
Post.expects(:all).twice.returns(@criteria)
|
|
145
|
+
@parent.expects(:reset).with("posts").yields
|
|
146
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
it "deletes all of the associated object" do
|
|
150
|
+
Post.expects(:delete_all).with(:conditions => { "person_id" => "5" }).returns(3)
|
|
151
|
+
@association.delete_all.should == 3
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
describe "#destroy_all" do
|
|
156
|
+
|
|
157
|
+
before do
|
|
158
|
+
@criteria = mock
|
|
159
|
+
@parent = stub(:id => "5", :class => Person, :new_record? => true)
|
|
160
|
+
Post.expects(:all).twice.returns(@criteria)
|
|
161
|
+
@parent.expects(:reset).with("posts").yields
|
|
162
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
it "destroys all of the associated objects" do
|
|
166
|
+
Post.expects(:destroy_all).with(:conditions => { "person_id" => "5" }).returns(3)
|
|
167
|
+
@association.destroy_all.should == 3
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
describe "#concat" do
|
|
172
|
+
|
|
173
|
+
before do
|
|
174
|
+
@child = Post.instantiate(:id => "1")
|
|
175
|
+
@second = Post.instantiate(:id => "2")
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
context "when parent document has been saved" do
|
|
179
|
+
|
|
180
|
+
before do
|
|
181
|
+
@parent = stub(:id => "1", :new_record? => false, :class => Person)
|
|
182
|
+
Post.expects(:all).returns([])
|
|
183
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it "saves and appends the child document" do
|
|
187
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
188
|
+
@child.expects(:save).returns(true)
|
|
189
|
+
@association.concat(@child)
|
|
190
|
+
@association.size.should == 1
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
context "when parent document has not been saved" do
|
|
196
|
+
|
|
197
|
+
before do
|
|
198
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
|
199
|
+
Post.expects(:all).returns([])
|
|
200
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
it "appends the child document" do
|
|
204
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
205
|
+
@association.concat(@child)
|
|
206
|
+
@association.size.should == 1
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
context "with multiple objects" do
|
|
212
|
+
|
|
213
|
+
before do
|
|
214
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
|
215
|
+
Post.expects(:all).returns([])
|
|
216
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
it "appends the child documents" do
|
|
220
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
221
|
+
@second.expects(:person_id=).with(@parent.id)
|
|
222
|
+
@association.concat([@child, @second])
|
|
223
|
+
@association.size.should == 2
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
describe "#create" do
|
|
231
|
+
|
|
232
|
+
before do
|
|
233
|
+
@post = mock
|
|
234
|
+
@parent = stub(:id => "5", :class => Person, :new_record? => true)
|
|
235
|
+
Post.expects(:all).returns([])
|
|
236
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
237
|
+
Post.expects(:instantiate).returns(@post)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it "builds and saves the new object" do
|
|
241
|
+
@post.expects(:save).returns(true)
|
|
242
|
+
@association.create(:title => "Sassy")
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
it "returns the new object" do
|
|
246
|
+
@post.expects(:save).returns(true)
|
|
247
|
+
@association.create(:title => "Sassy").should == @post
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
describe "#create!" do
|
|
253
|
+
|
|
254
|
+
before do
|
|
255
|
+
@post = mock
|
|
256
|
+
@parent = stub(:id => "5", :class => Person, :new_record? => true)
|
|
257
|
+
Post.expects(:all).returns([])
|
|
258
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
259
|
+
Post.expects(:instantiate).returns(@post)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
it "builds and saves the new object" do
|
|
263
|
+
@post.expects(:save!).returns(true)
|
|
264
|
+
@association.create!(:title => "Sassy")
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
it "returns the new object" do
|
|
268
|
+
@post.expects(:save!).returns(true)
|
|
269
|
+
@association.create!(:title => "Sassy").should == @post
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
describe "#find" do
|
|
275
|
+
|
|
276
|
+
before do
|
|
277
|
+
@parent = stub(:id => "5", :class => Person)
|
|
278
|
+
Post.expects(:all).returns([])
|
|
279
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
context "when finding by id" do
|
|
283
|
+
|
|
284
|
+
before do
|
|
285
|
+
@post = stub
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
it "returns the document in the array with that id" do
|
|
289
|
+
@association.expects(:id_criteria).with("5").returns(@post)
|
|
290
|
+
post = @association.find("5")
|
|
291
|
+
post.should == @post
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
context "when finding all with conditions" do
|
|
296
|
+
|
|
297
|
+
before do
|
|
298
|
+
@post = stub
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
it "passes the conditions to the association class" do
|
|
302
|
+
Post.expects(:find).with(:all, :conditions => { :title => "Testing", :person_id => @parent.id }).returns([@post])
|
|
303
|
+
posts = @association.find(:all, :conditions => { :title => "Testing" })
|
|
304
|
+
posts.should == [@post]
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
context "when finding first with conditions" do
|
|
310
|
+
|
|
311
|
+
before do
|
|
312
|
+
@post = stub
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
it "passes the conditions to the association class" do
|
|
316
|
+
Post.expects(:find).with(:first, :conditions => { :title => "Testing", :person_id => @parent.id }).returns(@post)
|
|
317
|
+
post = @association.find(:first, :conditions => { :title => "Testing" })
|
|
318
|
+
post.should == @post
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
context "when finding last with conditions" do
|
|
324
|
+
|
|
325
|
+
before do
|
|
326
|
+
@post = stub
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
it "passes the conditions to the association class" do
|
|
330
|
+
Post.expects(:find).with(:last, :conditions => { :title => "Testing", :person_id => @parent.id }).returns(@post)
|
|
331
|
+
post = @association.find(:last, :conditions => { :title => "Testing" })
|
|
332
|
+
post.should == @post
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
describe ".initialize" do
|
|
340
|
+
|
|
341
|
+
before do
|
|
342
|
+
@document = Person.new
|
|
343
|
+
@criteria = stub
|
|
344
|
+
@first = stub(:person_id => @document.id)
|
|
345
|
+
@second = stub(:person_id => @document.id)
|
|
346
|
+
@related = [@first, @second]
|
|
347
|
+
Post.expects(:all).with(:conditions => { "person_id" => @document.id }).returns(@related)
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
context "when related id has been set" do
|
|
351
|
+
|
|
352
|
+
it "finds the object by id" do
|
|
353
|
+
association = Mongoid::Associations::ReferencesMany.new(@document, options)
|
|
354
|
+
association.should == @related
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
context "when the options have an extension" do
|
|
360
|
+
|
|
361
|
+
it "adds the extension module" do
|
|
362
|
+
association = Mongoid::Associations::ReferencesMany.new(@document, options)
|
|
363
|
+
association.extension.should == "Testing"
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
describe ".instantiate" do
|
|
371
|
+
|
|
372
|
+
context "when related id has been set" do
|
|
373
|
+
|
|
374
|
+
before do
|
|
375
|
+
@document = Person.new
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
it "delegates to new" do
|
|
379
|
+
Mongoid::Associations::ReferencesMany.expects(:new).with(@document, options, nil)
|
|
380
|
+
association = Mongoid::Associations::ReferencesMany.instantiate(@document, options)
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
describe ".macro" do
|
|
388
|
+
|
|
389
|
+
it "returns :references_many" do
|
|
390
|
+
Mongoid::Associations::ReferencesMany.macro.should == :references_many
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
describe "#nested_build" do
|
|
396
|
+
|
|
397
|
+
before do
|
|
398
|
+
@parent = stub(:id => "1", :new_record? => false, :class => Person)
|
|
399
|
+
|
|
400
|
+
@first = Post.new(:id => 0)
|
|
401
|
+
@second = Post.new(:id => 1)
|
|
402
|
+
@related = [@first, @second]
|
|
403
|
+
Post.expects(:all).returns(@related)
|
|
404
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
it "should update existing documents" do
|
|
408
|
+
@association.expects(:find).with(0).returns(@first)
|
|
409
|
+
@association.nested_build({ "0" => { :title => "Yet Another" } })
|
|
410
|
+
@association.size.should == 2
|
|
411
|
+
@association[0].title.should == "Yet Another"
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
it "should create new documents" do
|
|
415
|
+
@association.expects(:find).with(2).raises(Mongoid::Errors::DocumentNotFound.new(Post, 2))
|
|
416
|
+
@association.nested_build({ "2" => { :title => "Yet Another" } })
|
|
417
|
+
@association.size.should == 3
|
|
418
|
+
@association[2].title.should == "Yet Another"
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
describe "#push" do
|
|
424
|
+
|
|
425
|
+
before do
|
|
426
|
+
@child = Post.instantiate(:id => "1")
|
|
427
|
+
@second = Post.instantiate(:id => "2")
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
context "when parent document has been saved" do
|
|
431
|
+
|
|
432
|
+
before do
|
|
433
|
+
@parent = stub(:id => "1", :new_record? => false, :class => Person)
|
|
434
|
+
Post.expects(:all).returns([])
|
|
435
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
it "saves and appends the child document" do
|
|
439
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
440
|
+
@child.expects(:save).returns(true)
|
|
441
|
+
@association.push(@child)
|
|
442
|
+
@association.size.should == 1
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
context "when parent document has not been saved" do
|
|
448
|
+
|
|
449
|
+
before do
|
|
450
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
|
451
|
+
Post.expects(:all).returns([])
|
|
452
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
it "appends the child document" do
|
|
456
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
457
|
+
@association.push(@child)
|
|
458
|
+
@association.size.should == 1
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
context "with multiple objects" do
|
|
464
|
+
|
|
465
|
+
before do
|
|
466
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
|
467
|
+
Post.expects(:all).returns([])
|
|
468
|
+
@association = Mongoid::Associations::ReferencesMany.new(@parent, options)
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
it "appends the child documents" do
|
|
472
|
+
@child.expects(:person_id=).with(@parent.id)
|
|
473
|
+
@second.expects(:person_id=).with(@parent.id)
|
|
474
|
+
@association.push(@child, @second)
|
|
475
|
+
@association.size.should == 2
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
describe ".update" do
|
|
483
|
+
|
|
484
|
+
before do
|
|
485
|
+
@first = Post.new
|
|
486
|
+
@second = Post.new
|
|
487
|
+
@related = [@first, @second]
|
|
488
|
+
@parent = Person.new
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
it "sets the related object id on the parent" do
|
|
492
|
+
Mongoid::Associations::ReferencesMany.update(@related, @parent, options)
|
|
493
|
+
@first.person_id.should == @parent.id
|
|
494
|
+
@second.person_id.should == @parent.id
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
it "returns the related objects" do
|
|
498
|
+
@proxy = Mongoid::Associations::ReferencesMany.update(@related, @parent, options)
|
|
499
|
+
@proxy.target.should == @related
|
|
500
|
+
end
|
|
501
|
+
end
|
|
502
|
+
end
|