mark_mapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.rdoc +39 -0
  4. data/examples/attr_accessible.rb +24 -0
  5. data/examples/attr_protected.rb +24 -0
  6. data/examples/cache_key.rb +26 -0
  7. data/examples/custom_types.rb +26 -0
  8. data/examples/identity_map.rb +30 -0
  9. data/examples/identity_map/automatic.rb +2 -0
  10. data/examples/keys.rb +42 -0
  11. data/examples/modifiers/set.rb +27 -0
  12. data/examples/plugins.rb +40 -0
  13. data/examples/querying.rb +39 -0
  14. data/examples/sample_app.rb +43 -0
  15. data/examples/scopes.rb +56 -0
  16. data/examples/validating/embedded_docs.rb +31 -0
  17. data/lib/mark_mapper.rb +125 -0
  18. data/lib/mark_mapper/config.rb +90 -0
  19. data/lib/mark_mapper/connection.rb +60 -0
  20. data/lib/mark_mapper/criteria_hash.rb +194 -0
  21. data/lib/mark_mapper/document.rb +46 -0
  22. data/lib/mark_mapper/embedded_document.rb +32 -0
  23. data/lib/mark_mapper/exceptions.rb +33 -0
  24. data/lib/mark_mapper/extensions/array.rb +27 -0
  25. data/lib/mark_mapper/extensions/boolean.rb +45 -0
  26. data/lib/mark_mapper/extensions/date.rb +29 -0
  27. data/lib/mark_mapper/extensions/duplicable.rb +86 -0
  28. data/lib/mark_mapper/extensions/float.rb +18 -0
  29. data/lib/mark_mapper/extensions/hash.rb +26 -0
  30. data/lib/mark_mapper/extensions/integer.rb +27 -0
  31. data/lib/mark_mapper/extensions/kernel.rb +11 -0
  32. data/lib/mark_mapper/extensions/nil_class.rb +18 -0
  33. data/lib/mark_mapper/extensions/object.rb +30 -0
  34. data/lib/mark_mapper/extensions/object_id.rb +18 -0
  35. data/lib/mark_mapper/extensions/set.rb +20 -0
  36. data/lib/mark_mapper/extensions/string.rb +31 -0
  37. data/lib/mark_mapper/extensions/symbol.rb +87 -0
  38. data/lib/mark_mapper/extensions/time.rb +29 -0
  39. data/lib/mark_mapper/locale/en.yml +5 -0
  40. data/lib/mark_mapper/middleware/identity_map.rb +41 -0
  41. data/lib/mark_mapper/normalizers/criteria_hash_key.rb +17 -0
  42. data/lib/mark_mapper/normalizers/criteria_hash_value.rb +66 -0
  43. data/lib/mark_mapper/normalizers/fields_value.rb +26 -0
  44. data/lib/mark_mapper/normalizers/hash_key.rb +19 -0
  45. data/lib/mark_mapper/normalizers/integer.rb +19 -0
  46. data/lib/mark_mapper/normalizers/options_hash_value.rb +83 -0
  47. data/lib/mark_mapper/normalizers/sort_value.rb +55 -0
  48. data/lib/mark_mapper/options_hash.rb +103 -0
  49. data/lib/mark_mapper/pagination.rb +6 -0
  50. data/lib/mark_mapper/pagination/collection.rb +32 -0
  51. data/lib/mark_mapper/pagination/paginator.rb +46 -0
  52. data/lib/mark_mapper/plugins.rb +22 -0
  53. data/lib/mark_mapper/plugins/accessible.rb +61 -0
  54. data/lib/mark_mapper/plugins/active_model.rb +18 -0
  55. data/lib/mark_mapper/plugins/associations.rb +96 -0
  56. data/lib/mark_mapper/plugins/associations/base.rb +98 -0
  57. data/lib/mark_mapper/plugins/associations/belongs_to_association.rb +63 -0
  58. data/lib/mark_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +35 -0
  59. data/lib/mark_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
  60. data/lib/mark_mapper/plugins/associations/collection.rb +29 -0
  61. data/lib/mark_mapper/plugins/associations/embedded_collection.rb +44 -0
  62. data/lib/mark_mapper/plugins/associations/in_array_proxy.rb +133 -0
  63. data/lib/mark_mapper/plugins/associations/many_association.rb +63 -0
  64. data/lib/mark_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  65. data/lib/mark_mapper/plugins/associations/many_documents_proxy.rb +142 -0
  66. data/lib/mark_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  67. data/lib/mark_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  68. data/lib/mark_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  69. data/lib/mark_mapper/plugins/associations/one_as_proxy.rb +22 -0
  70. data/lib/mark_mapper/plugins/associations/one_association.rb +48 -0
  71. data/lib/mark_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
  72. data/lib/mark_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
  73. data/lib/mark_mapper/plugins/associations/one_proxy.rb +95 -0
  74. data/lib/mark_mapper/plugins/associations/proxy.rb +138 -0
  75. data/lib/mark_mapper/plugins/associations/single_association.rb +46 -0
  76. data/lib/mark_mapper/plugins/caching.rb +21 -0
  77. data/lib/mark_mapper/plugins/callbacks.rb +42 -0
  78. data/lib/mark_mapper/plugins/clone.rb +24 -0
  79. data/lib/mark_mapper/plugins/counter_cache.rb +97 -0
  80. data/lib/mark_mapper/plugins/dirty.rb +61 -0
  81. data/lib/mark_mapper/plugins/document.rb +41 -0
  82. data/lib/mark_mapper/plugins/dumpable.rb +22 -0
  83. data/lib/mark_mapper/plugins/dynamic_querying.rb +45 -0
  84. data/lib/mark_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  85. data/lib/mark_mapper/plugins/embedded_callbacks.rb +81 -0
  86. data/lib/mark_mapper/plugins/embedded_document.rb +53 -0
  87. data/lib/mark_mapper/plugins/equality.rb +23 -0
  88. data/lib/mark_mapper/plugins/identity_map.rb +144 -0
  89. data/lib/mark_mapper/plugins/indexable.rb +86 -0
  90. data/lib/mark_mapper/plugins/inspect.rb +16 -0
  91. data/lib/mark_mapper/plugins/keys.rb +470 -0
  92. data/lib/mark_mapper/plugins/keys/key.rb +134 -0
  93. data/lib/mark_mapper/plugins/keys/static.rb +45 -0
  94. data/lib/mark_mapper/plugins/logger.rb +18 -0
  95. data/lib/mark_mapper/plugins/modifiers.rb +140 -0
  96. data/lib/mark_mapper/plugins/pagination.rb +16 -0
  97. data/lib/mark_mapper/plugins/partial_updates.rb +77 -0
  98. data/lib/mark_mapper/plugins/persistence.rb +79 -0
  99. data/lib/mark_mapper/plugins/protected.rb +45 -0
  100. data/lib/mark_mapper/plugins/querying.rb +173 -0
  101. data/lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb +75 -0
  102. data/lib/mark_mapper/plugins/rails.rb +79 -0
  103. data/lib/mark_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
  104. data/lib/mark_mapper/plugins/sci.rb +82 -0
  105. data/lib/mark_mapper/plugins/scopes.rb +28 -0
  106. data/lib/mark_mapper/plugins/serialization.rb +109 -0
  107. data/lib/mark_mapper/plugins/timestamps.rb +29 -0
  108. data/lib/mark_mapper/plugins/touch.rb +18 -0
  109. data/lib/mark_mapper/plugins/userstamps.rb +18 -0
  110. data/lib/mark_mapper/plugins/validations.rb +96 -0
  111. data/lib/mark_mapper/query.rb +278 -0
  112. data/lib/mark_mapper/railtie.rb +52 -0
  113. data/lib/mark_mapper/railtie/database.rake +65 -0
  114. data/lib/mark_mapper/translation.rb +10 -0
  115. data/lib/mark_mapper/version.rb +4 -0
  116. data/lib/rails/generators/mark_mapper/config/config_generator.rb +37 -0
  117. data/lib/rails/generators/mark_mapper/config/templates/marklogic.yml +19 -0
  118. data/lib/rails/generators/mark_mapper/model/model_generator.rb +40 -0
  119. data/lib/rails/generators/mark_mapper/model/templates/model.rb +17 -0
  120. data/spec/config/mark_mapper.yml +6 -0
  121. data/spec/examples_spec.rb +25 -0
  122. data/spec/functional/accessible_spec.rb +198 -0
  123. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +64 -0
  124. data/spec/functional/associations/belongs_to_proxy_spec.rb +255 -0
  125. data/spec/functional/associations/in_array_proxy_spec.rb +349 -0
  126. data/spec/functional/associations/many_documents_as_proxy_spec.rb +230 -0
  127. data/spec/functional/associations/many_documents_proxy_spec.rb +968 -0
  128. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +238 -0
  129. data/spec/functional/associations/many_embedded_proxy_spec.rb +288 -0
  130. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +302 -0
  131. data/spec/functional/associations/one_as_proxy_spec.rb +489 -0
  132. data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +207 -0
  133. data/spec/functional/associations/one_embedded_proxy_spec.rb +100 -0
  134. data/spec/functional/associations/one_proxy_spec.rb +406 -0
  135. data/spec/functional/associations_spec.rb +48 -0
  136. data/spec/functional/caching_spec.rb +75 -0
  137. data/spec/functional/callbacks_spec.rb +330 -0
  138. data/spec/functional/counter_cache_spec.rb +235 -0
  139. data/spec/functional/dirty_spec.rb +316 -0
  140. data/spec/functional/document_spec.rb +310 -0
  141. data/spec/functional/dumpable_spec.rb +24 -0
  142. data/spec/functional/dynamic_querying_spec.rb +75 -0
  143. data/spec/functional/embedded_document_spec.rb +316 -0
  144. data/spec/functional/equality_spec.rb +20 -0
  145. data/spec/functional/extensions_spec.rb +16 -0
  146. data/spec/functional/identity_map_spec.rb +483 -0
  147. data/spec/functional/keys_spec.rb +339 -0
  148. data/spec/functional/logger_spec.rb +20 -0
  149. data/spec/functional/modifiers_spec.rb +446 -0
  150. data/spec/functional/options_hash_spec.rb +41 -0
  151. data/spec/functional/pagination_spec.rb +89 -0
  152. data/spec/functional/partial_updates_spec.rb +530 -0
  153. data/spec/functional/protected_spec.rb +199 -0
  154. data/spec/functional/querying_spec.rb +984 -0
  155. data/spec/functional/rails_spec.rb +55 -0
  156. data/spec/functional/sci_spec.rb +374 -0
  157. data/spec/functional/scopes_spec.rb +204 -0
  158. data/spec/functional/static_keys_spec.rb +153 -0
  159. data/spec/functional/timestamps_spec.rb +97 -0
  160. data/spec/functional/touch_spec.rb +125 -0
  161. data/spec/functional/userstamps_spec.rb +46 -0
  162. data/spec/functional/validations_spec.rb +416 -0
  163. data/spec/quality_spec.rb +51 -0
  164. data/spec/spec_helper.rb +150 -0
  165. data/spec/support/matchers.rb +15 -0
  166. data/spec/support/models.rb +256 -0
  167. data/spec/symbol_operator_spec.rb +70 -0
  168. data/spec/symbol_spec.rb +9 -0
  169. data/spec/unit/associations/base_spec.rb +146 -0
  170. data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
  171. data/spec/unit/associations/many_association_spec.rb +64 -0
  172. data/spec/unit/associations/one_association_spec.rb +48 -0
  173. data/spec/unit/associations/proxy_spec.rb +103 -0
  174. data/spec/unit/clone_spec.rb +79 -0
  175. data/spec/unit/config_generator_spec.rb +24 -0
  176. data/spec/unit/criteria_hash_spec.rb +218 -0
  177. data/spec/unit/document_spec.rb +251 -0
  178. data/spec/unit/dynamic_finder_spec.rb +125 -0
  179. data/spec/unit/embedded_document_spec.rb +676 -0
  180. data/spec/unit/equality_spec.rb +38 -0
  181. data/spec/unit/exceptions_spec.rb +12 -0
  182. data/spec/unit/extensions_spec.rb +368 -0
  183. data/spec/unit/identity_map_middleware_spec.rb +134 -0
  184. data/spec/unit/inspect_spec.rb +47 -0
  185. data/spec/unit/key_spec.rb +276 -0
  186. data/spec/unit/keys_spec.rb +155 -0
  187. data/spec/unit/mark_mapper_spec.rb +37 -0
  188. data/spec/unit/model_generator_spec.rb +45 -0
  189. data/spec/unit/normalizers/criteria_hash_key_spec.rb +37 -0
  190. data/spec/unit/normalizers/criteria_hash_value_spec.rb +200 -0
  191. data/spec/unit/normalizers/fields_value_spec.rb +45 -0
  192. data/spec/unit/normalizers/hash_key_spec.rb +15 -0
  193. data/spec/unit/normalizers/integer_spec.rb +24 -0
  194. data/spec/unit/normalizers/options_hash_value_spec.rb +99 -0
  195. data/spec/unit/normalizers/sort_value_spec.rb +98 -0
  196. data/spec/unit/options_hash_spec.rb +64 -0
  197. data/spec/unit/pagination/collection_spec.rb +30 -0
  198. data/spec/unit/pagination/paginator_spec.rb +118 -0
  199. data/spec/unit/pagination_spec.rb +11 -0
  200. data/spec/unit/plugins_spec.rb +89 -0
  201. data/spec/unit/query_spec.rb +837 -0
  202. data/spec/unit/rails_compatibility_spec.rb +40 -0
  203. data/spec/unit/rails_reflect_on_association_spec.rb +118 -0
  204. data/spec/unit/rails_spec.rb +188 -0
  205. data/spec/unit/serialization_spec.rb +169 -0
  206. data/spec/unit/serializers/json_serializer_spec.rb +218 -0
  207. data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
  208. data/spec/unit/time_zones_spec.rb +44 -0
  209. data/spec/unit/translation_spec.rb +27 -0
  210. data/spec/unit/validations_spec.rb +588 -0
  211. metadata +307 -0
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Translation" do
4
+ it "should translate add mark_mapper translations" do
5
+ I18n.translate("mark_mapper.errors.messages.taken").should == "has already been taken"
6
+ end
7
+
8
+ it "should set i18n_scope" do
9
+ Doc().i18n_scope.should == :mark_mapper
10
+ end
11
+
12
+ it "should translate document attributes" do
13
+ I18n.config.backend.store_translations(:en, :mark_mapper => {:attributes => {:thing => {:foo => 'Bar'}}})
14
+ doc = Doc('Thing') do
15
+ key :foo, String
16
+ end
17
+ doc.human_attribute_name(:foo).should == 'Bar'
18
+ end
19
+
20
+ it "should translate embedded document attributes" do
21
+ I18n.config.backend.store_translations(:en, :mark_mapper => {:attributes => {:thing => {:foo => 'Bar'}}})
22
+ doc = EDoc('Thing') do
23
+ key :foo, String
24
+ end
25
+ doc.human_attribute_name(:foo).should == 'Bar'
26
+ end
27
+ end
@@ -0,0 +1,588 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Validations" do
4
+ context "Validations" do
5
+ context "on a Document" do
6
+ before do
7
+ @document = Doc('John')
8
+ end
9
+
10
+ context "Validating acceptance of" do
11
+ it "should work with validates_acceptance_of macro" do
12
+ @document.key :terms, String
13
+ @document.validates_acceptance_of :terms
14
+ doc = @document.new(:terms => '')
15
+ doc.should have_error_on(:terms)
16
+ doc.terms = '1'
17
+ doc.should_not have_error_on(:terms)
18
+ end
19
+ end
20
+
21
+ context "validating confirmation of" do
22
+ it "should work with validates_confirmation_of macro" do
23
+ @document.key :password, String
24
+ @document.validates_confirmation_of :password
25
+
26
+ # NOTE: Api change as ActiveModel passes if password_confirmation is nil
27
+ doc = @document.new
28
+ doc.password = 'foobar'
29
+ doc.password_confirmation = 'foobar1'
30
+ doc.should have_error_on(:password).or(have_error_on(:password_confirmation))
31
+
32
+ doc.password_confirmation = 'foobar'
33
+ doc.should_not have_error_on(:password)
34
+ end
35
+ end
36
+
37
+ context "validating uniqueness of" do
38
+ it "should work with :unique => true passed to ::key" do
39
+ @document.key :email, String, :unique => true
40
+
41
+ @document.create(:email => "cheald@gmail.com")
42
+ doc = @document.new :email => "cheald@gmail.com"
43
+ doc.should_not be_valid
44
+ doc.should have_error_on(:email)
45
+ doc.email = "cheald@mashable.com"
46
+ doc.should be_valid
47
+ end
48
+ end
49
+
50
+ context "validating format of" do
51
+ it "should work with validates_format_of macro" do
52
+ @document.key :name, String
53
+ @document.validates_format_of :name, :with => /.+/
54
+ doc = @document.new
55
+ doc.should have_error_on(:name)
56
+ doc.name = 'John'
57
+ doc.should_not have_error_on(:name)
58
+ end
59
+
60
+ it "should work with :format shorcut key" do
61
+ @document.key :name, String, :format => /.+/
62
+ doc = @document.new
63
+ doc.should have_error_on(:name)
64
+ doc.name = 'John'
65
+ doc.should_not have_error_on(:name)
66
+ end
67
+ end
68
+
69
+ context "validating length of" do
70
+ it "should work with validates_length_of macro" do
71
+ @document.key :name, String
72
+ @document.validates_length_of :name, :minimum => 5
73
+ doc = @document.new
74
+ doc.should have_error_on(:name)
75
+ end
76
+
77
+ context "with :length => integer shortcut" do
78
+ it "should set maximum of integer provided" do
79
+ @document.key :name, String, :length => 5
80
+ doc = @document.new
81
+ doc.name = '123456'
82
+ doc.should have_error_on(:name)
83
+ doc.name = '12345'
84
+ doc.should_not have_error_on(:name)
85
+ end
86
+ end
87
+
88
+ context "with :length => range shortcut" do
89
+ before do
90
+ @document.key :name, String, :length => 5..7
91
+ end
92
+
93
+ it "should set minimum of range min" do
94
+ doc = @document.new
95
+ doc.should have_error_on(:name)
96
+ doc.name = '123456'
97
+ doc.should_not have_error_on(:name)
98
+ end
99
+
100
+ it "should set maximum of range max" do
101
+ doc = @document.new
102
+ doc.should have_error_on(:name)
103
+ doc.name = '12345678'
104
+ doc.should have_error_on(:name)
105
+ doc.name = '123456'
106
+ doc.should_not have_error_on(:name)
107
+ end
108
+ end
109
+
110
+ context "with :length => hash shortcut" do
111
+ it "should pass options through" do
112
+ @document.key :name, String, :length => {:minimum => 2}
113
+ doc = @document.new
114
+ doc.should have_error_on(:name)
115
+ doc.name = '12'
116
+ doc.should_not have_error_on(:name)
117
+ end
118
+ end
119
+ end # validates_length_of
120
+
121
+ context "Validating numericality of" do
122
+ it "should work with validates_numericality_of macro" do
123
+ @document.key :age, Integer
124
+ @document.validates_numericality_of :age
125
+ doc = @document.new
126
+ doc.age = 'String'
127
+ doc.should have_error_on(:age)
128
+ doc.age = 23
129
+ doc.should_not have_error_on(:age)
130
+ end
131
+
132
+ context "with :numeric shortcut" do
133
+ it "should work with integer or float" do
134
+ @document.key :weight, Float, :numeric => true
135
+ doc = @document.new
136
+ doc.weight = 'String'
137
+ doc.should have_error_on(:weight)
138
+ doc.weight = 23.0
139
+ doc.should_not have_error_on(:weight)
140
+ doc.weight = 23
141
+ doc.should_not have_error_on(:weight)
142
+ end
143
+ end
144
+
145
+ context "with :numeric shortcut on Integer key" do
146
+ it "should only work with integers" do
147
+ @document.key :age, Integer, :numeric => true
148
+ doc = @document.new
149
+ doc.age = 'String'
150
+ doc.should have_error_on(:age)
151
+ doc.age = 23.1
152
+ doc.should have_error_on(:age)
153
+ doc.age = 23
154
+ doc.should_not have_error_on(:age)
155
+ end
156
+ end
157
+ end # numericality of
158
+
159
+ context "validating presence of" do
160
+ it "should work with validates_presence_of macro" do
161
+ @document.key :name, String
162
+ @document.validates_presence_of :name
163
+ doc = @document.new
164
+ doc.should have_error_on(:name)
165
+ end
166
+
167
+ it "should work with :required shortcut on key definition" do
168
+ @document.key :name, String, :required => true
169
+ doc = @document.new
170
+ doc.should have_error_on(:name)
171
+ end
172
+ end
173
+
174
+ context "validating exclusion of" do
175
+ it "should throw error if enumerator not provided" do
176
+ @document.key :action, String
177
+ lambda {
178
+ @document.validates_exclusion_of :action
179
+ }.should raise_error(ArgumentError)
180
+ end
181
+
182
+ it "should work with validates_exclusion_of macro" do
183
+ @document.key :action, String
184
+ @document.validates_exclusion_of :action, :in => %w(kick run)
185
+
186
+ doc = @document.new
187
+ doc.should_not have_error_on(:action)
188
+
189
+ doc.action = 'fart'
190
+ doc.should_not have_error_on(:action)
191
+
192
+ doc.action = 'kick'
193
+ doc.should have_error_on(:action)
194
+ doc.errors[:action].should == ['is reserved']
195
+ end
196
+
197
+ it "should work with :not_in shortcut on key definition" do
198
+ @document.key :action, String, :not_in => %w(kick run)
199
+
200
+ doc = @document.new
201
+ doc.should_not have_error_on(:action)
202
+
203
+ doc.action = 'fart'
204
+ doc.should_not have_error_on(:action)
205
+
206
+ doc.action = 'kick'
207
+ doc.should have_error_on(:action)
208
+ doc.errors[:action].should == ['is reserved']
209
+ end
210
+
211
+ it "should not have error if allow nil is true and value is nil" do
212
+ @document.key :action, String
213
+ @document.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
214
+
215
+ doc = @document.new
216
+ doc.should_not have_error_on(:action)
217
+ end
218
+
219
+ it "should not have error if allow blank is true and value is blank" do
220
+ @document.key :action, String
221
+ @document.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
222
+
223
+ doc = @document.new(:action => '')
224
+ doc.should_not have_error_on(:action)
225
+ end
226
+ end
227
+
228
+ context "validating inclusion of" do
229
+ it "should throw error if enumerator not provided" do
230
+ @document.key :action, String
231
+ lambda {
232
+ @document.validates_inclusion_of :action
233
+ }.should raise_error(ArgumentError)
234
+ end
235
+
236
+ it "should work with validates_inclusion_of macro" do
237
+ @document.key :action, String
238
+ @document.validates_inclusion_of :action, :in => %w(kick run)
239
+
240
+ doc = @document.new
241
+ doc.should have_error_on(:action)
242
+ doc.errors[:action].should == ['is not included in the list']
243
+
244
+ doc.action = 'fart'
245
+ doc.should have_error_on(:action)
246
+ doc.errors[:action].should == ['is not included in the list']
247
+
248
+ doc.action = 'kick'
249
+ doc.should_not have_error_on(:action)
250
+ end
251
+
252
+ it "should work with :in shortcut on key definition" do
253
+ @document.key :action, String, :in => %w(kick run)
254
+
255
+ doc = @document.new
256
+ doc.should have_error_on(:action)
257
+ doc.errors[:action].should == ['is not included in the list']
258
+
259
+ doc.action = 'fart'
260
+ doc.should have_error_on(:action)
261
+ doc.errors[:action].should == ['is not included in the list']
262
+
263
+ doc.action = 'kick'
264
+ doc.should_not have_error_on(:action)
265
+ end
266
+
267
+ it "should work with :required shortcut on Boolean type" do
268
+ @document.key :flag, Boolean, :required => true
269
+
270
+ doc = @document.new
271
+ doc.should have_error_on(:flag)
272
+ doc.errors[:flag].should == ['is not included in the list']
273
+
274
+ doc.flag = true
275
+ doc.should_not have_error_on(:action)
276
+
277
+ doc.flag = false
278
+ doc.should_not have_error_on(:action)
279
+ end
280
+
281
+ it "should not have error if allow nil is true and value is nil" do
282
+ @document.key :action, String
283
+ @document.validates_inclusion_of :action, :in => %w(kick run), :allow_nil => true
284
+
285
+ doc = @document.new
286
+ doc.should_not have_error_on(:action)
287
+ end
288
+
289
+ it "should not have error if allow blank is true and value is blank" do
290
+ @document.key :action, String
291
+ @document.validates_inclusion_of :action, :in => %w(kick run), :allow_blank => true
292
+
293
+ doc = @document.new(:action => '')
294
+ doc.should_not have_error_on(:action)
295
+ end
296
+ end
297
+
298
+ end # End on a Document
299
+
300
+ context "On an EmbeddedDocument" do
301
+ before do
302
+ @embedded_doc = EDoc()
303
+ end
304
+
305
+ context "Validating acceptance of" do
306
+ it "should work with validates_acceptance_of macro" do
307
+ @embedded_doc.key :terms, String
308
+ @embedded_doc.validates_acceptance_of :terms
309
+ doc = @embedded_doc.new(:terms => '')
310
+ doc.should have_error_on(:terms)
311
+ doc.terms = '1'
312
+ doc.should_not have_error_on(:terms)
313
+ end
314
+ end
315
+
316
+ context "validating confirmation of" do
317
+ it "should work with validates_confirmation_of macro" do
318
+ @embedded_doc.key :password, String
319
+ @embedded_doc.validates_confirmation_of :password
320
+ doc = @embedded_doc.new
321
+ doc.password = 'foobar'
322
+ doc.password_confirmation = 'foobar1'
323
+ doc.should have_error_on(:password).or(have_error_on(:password_confirmation))
324
+ doc.password_confirmation = 'foobar'
325
+ doc.should_not have_error_on(:password)
326
+ end
327
+ end
328
+
329
+ context "validating format of" do
330
+ it "should work with validates_format_of macro" do
331
+ @embedded_doc.key :name, String
332
+ @embedded_doc.validates_format_of :name, :with => /.+/
333
+ doc = @embedded_doc.new
334
+ doc.should have_error_on(:name)
335
+ doc.name = 'John'
336
+ doc.should_not have_error_on(:name)
337
+ end
338
+
339
+ it "should work with :format shorcut key" do
340
+ @embedded_doc.key :name, String, :format => /.+/
341
+ doc = @embedded_doc.new
342
+ doc.should have_error_on(:name)
343
+ doc.name = 'John'
344
+ doc.should_not have_error_on(:name)
345
+ end
346
+ end
347
+
348
+ context "validating length of" do
349
+ it "should work with validates_length_of macro" do
350
+ @embedded_doc.key :name, String
351
+ @embedded_doc.validates_length_of :name, :minimum => 5
352
+ doc = @embedded_doc.new
353
+ doc.should have_error_on(:name)
354
+ end
355
+
356
+ context "with :length => integer shortcut" do
357
+ it "should set maximum of integer provided" do
358
+ @embedded_doc.key :name, String, :length => 5
359
+ doc = @embedded_doc.new
360
+ doc.name = '123456'
361
+ doc.should have_error_on(:name)
362
+ doc.name = '12345'
363
+ doc.should_not have_error_on(:name)
364
+ end
365
+ end
366
+
367
+ context "with :length => range shortcut" do
368
+ before do
369
+ @embedded_doc.key :name, String, :length => 5..7
370
+ end
371
+
372
+ it "should set minimum of range min" do
373
+ doc = @embedded_doc.new
374
+ doc.should have_error_on(:name)
375
+ doc.name = '123456'
376
+ doc.should_not have_error_on(:name)
377
+ end
378
+
379
+ it "should set maximum of range max" do
380
+ doc = @embedded_doc.new
381
+ doc.should have_error_on(:name)
382
+ doc.name = '12345678'
383
+ doc.should have_error_on(:name)
384
+ doc.name = '123456'
385
+ doc.should_not have_error_on(:name)
386
+ end
387
+ end
388
+
389
+ context "with :length => hash shortcut" do
390
+ it "should pass options through" do
391
+ @embedded_doc.key :name, String, :length => {:minimum => 2}
392
+ doc = @embedded_doc.new
393
+ doc.should have_error_on(:name)
394
+ doc.name = '12'
395
+ doc.should_not have_error_on(:name)
396
+ end
397
+ end
398
+ end # validates_length_of
399
+
400
+ context "Validating numericality of" do
401
+ it "should work with validates_numericality_of macro" do
402
+ @embedded_doc.key :age, Integer
403
+ @embedded_doc.validates_numericality_of :age
404
+ doc = @embedded_doc.new
405
+ doc.age = 'String'
406
+ doc.should have_error_on(:age)
407
+ doc.age = 23
408
+ doc.should_not have_error_on(:age)
409
+ end
410
+
411
+ context "with :numeric shortcut" do
412
+ it "should work with integer or float" do
413
+ @embedded_doc.key :weight, Float, :numeric => true
414
+ doc = @embedded_doc.new
415
+ doc.weight = 'String'
416
+ doc.should have_error_on(:weight)
417
+ doc.weight = 23.0
418
+ doc.should_not have_error_on(:weight)
419
+ doc.weight = 23
420
+ doc.should_not have_error_on(:weight)
421
+ end
422
+ end
423
+
424
+ context "with :numeric shortcut on Integer key" do
425
+ it "should only work with integers" do
426
+ @embedded_doc.key :age, Integer, :numeric => true
427
+ doc = @embedded_doc.new
428
+ doc.age = 'String'
429
+ doc.should have_error_on(:age)
430
+ doc.age = 23.1
431
+ doc.should have_error_on(:age)
432
+ doc.age = 23
433
+ doc.should_not have_error_on(:age)
434
+ end
435
+ end
436
+ end # numericality of
437
+
438
+ context "validating presence of" do
439
+ it "should work with validates_presence_of macro" do
440
+ @embedded_doc.key :name, String
441
+ @embedded_doc.validates_presence_of :name
442
+ doc = @embedded_doc.new
443
+ doc.should have_error_on(:name)
444
+ end
445
+
446
+ it "should work with :required shortcut on key definition" do
447
+ @embedded_doc.key :name, String, :required => true
448
+ doc = @embedded_doc.new
449
+ doc.should have_error_on(:name)
450
+ end
451
+ end
452
+
453
+ context "validating exclusion of" do
454
+ it "should throw error if enumerator not provided" do
455
+ @embedded_doc.key :action, String
456
+ lambda {
457
+ @embedded_doc.validates_exclusion_of :action
458
+ }.should raise_error(ArgumentError)
459
+ end
460
+
461
+ it "should work with validates_exclusion_of macro" do
462
+ @embedded_doc.key :action, String
463
+ @embedded_doc.validates_exclusion_of :action, :in => %w(kick run)
464
+
465
+ doc = @embedded_doc.new
466
+ doc.should_not have_error_on(:action)
467
+
468
+ doc.action = 'fart'
469
+ doc.should_not have_error_on(:action)
470
+
471
+ doc.action = 'kick'
472
+ doc.should have_error_on(:action)
473
+ doc.errors[:action].should == ['is reserved']
474
+ end
475
+
476
+ it "should work with :not_in shortcut on key definition" do
477
+ @embedded_doc.key :action, String, :not_in => %w(kick run)
478
+
479
+ doc = @embedded_doc.new
480
+ doc.should_not have_error_on(:action)
481
+
482
+ doc.action = 'fart'
483
+ doc.should_not have_error_on(:action)
484
+
485
+ doc.action = 'kick'
486
+ doc.should have_error_on(:action)
487
+ doc.errors[:action].should == ['is reserved']
488
+ end
489
+
490
+ it "should not have error if allow nil is true and value is nil" do
491
+ @embedded_doc.key :action, String
492
+ @embedded_doc.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
493
+
494
+ doc = @embedded_doc.new
495
+ doc.should_not have_error_on(:action)
496
+ end
497
+
498
+ it "should not have error if allow blank is true and value is blank" do
499
+ @embedded_doc.key :action, String
500
+ @embedded_doc.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
501
+
502
+ doc = @embedded_doc.new(:action => '')
503
+ doc.should_not have_error_on(:action)
504
+ end
505
+ end
506
+
507
+ context "validating inclusion of" do
508
+ it "should throw error if enumerator not provided" do
509
+ @embedded_doc.key :action, String
510
+ lambda {
511
+ @embedded_doc.validates_inclusion_of :action
512
+ }.should raise_error(ArgumentError)
513
+ end
514
+
515
+ it "should work with validates_inclusion_of macro" do
516
+ @embedded_doc.key :action, String
517
+ @embedded_doc.validates_inclusion_of :action, :in => %w(kick run)
518
+
519
+ doc = @embedded_doc.new
520
+ doc.should have_error_on(:action)
521
+ doc.errors[:action].should == ['is not included in the list']
522
+
523
+ doc.action = 'fart'
524
+ doc.should have_error_on(:action)
525
+ doc.errors[:action].should == ['is not included in the list']
526
+
527
+ doc.action = 'kick'
528
+ doc.should_not have_error_on(:action)
529
+ end
530
+
531
+ it "should work with :in shortcut on key definition" do
532
+ @embedded_doc.key :action, String, :in => %w(kick run)
533
+
534
+ doc = @embedded_doc.new
535
+ doc.should have_error_on(:action)
536
+ doc.errors[:action].should == ['is not included in the list']
537
+
538
+ doc.action = 'fart'
539
+ doc.should have_error_on(:action)
540
+ doc.errors[:action].should == ['is not included in the list']
541
+
542
+ doc.action = 'kick'
543
+ doc.should_not have_error_on(:action)
544
+ end
545
+
546
+ it "should not have error if allow nil is true and value is nil" do
547
+ @embedded_doc.key :action, String
548
+ @embedded_doc.validates_inclusion_of :action, :in => %w(kick run), :allow_nil => true
549
+
550
+ doc = @embedded_doc.new
551
+ doc.should_not have_error_on(:action)
552
+ end
553
+
554
+ it "should not have error if allow blank is true and value is blank" do
555
+ @embedded_doc.key :action, String
556
+ @embedded_doc.validates_inclusion_of :action, :in => %w(kick run), :allow_blank => true
557
+
558
+ doc = @embedded_doc.new(:action => '')
559
+ doc.should_not have_error_on(:action)
560
+ end
561
+ end
562
+
563
+ end # End on an EmbeddedDocument
564
+
565
+ end # Validations
566
+
567
+ context "Adding validation errors" do
568
+ before do
569
+ @document = Doc do
570
+ key :action, String
571
+ def action_present
572
+ errors.add(:action, 'is invalid') if action.blank?
573
+ end
574
+ end
575
+ end
576
+
577
+ it "should work with validate callback" do
578
+ @document.validate :action_present
579
+
580
+ doc = @document.new
581
+ doc.action = nil
582
+ doc.should have_error_on(:action)
583
+
584
+ doc.action = 'kick'
585
+ doc.should_not have_error_on(:action)
586
+ end
587
+ end
588
+ end