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