mongoid-rspec 1.5.3 → 2.0.0
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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -2
- data/README.md +227 -152
- data/lib/matchers/accept_nested_attributes.rb +67 -0
- data/lib/matchers/allow_mass_assignment.rb +1 -0
- data/lib/matchers/associations.rb +127 -22
- data/lib/matchers/document.rb +31 -2
- data/lib/matchers/indexes.rb +48 -15
- data/lib/matchers/validations/acceptance_of.rb +2 -2
- data/lib/matchers/validations/associated.rb +5 -5
- data/lib/matchers/validations/confirmation_of.rb +2 -2
- data/lib/matchers/validations/custom_validation_of.rb +2 -19
- data/lib/matchers/validations/exclusion_of.rb +8 -1
- data/lib/matchers/validations/format_of.rb +21 -21
- data/lib/matchers/validations/inclusion_of.rb +13 -13
- data/lib/matchers/validations/length_of.rb +66 -19
- data/lib/matchers/validations/numericality_of.rb +10 -10
- data/lib/matchers/validations/presence_of.rb +2 -2
- data/lib/matchers/validations/uniqueness_of.rb +13 -30
- data/lib/matchers/validations/with_message.rb +27 -0
- data/lib/matchers/validations.rb +18 -3
- data/lib/mongoid-rspec/version.rb +1 -1
- data/lib/mongoid-rspec.rb +5 -1
- data/mongoid-rspec.gemspec +5 -5
- data/spec/models/article.rb +18 -12
- data/spec/models/comment.rb +2 -2
- data/spec/models/movie_article.rb +6 -5
- data/spec/models/permalink.rb +1 -1
- data/spec/models/person.rb +0 -3
- data/spec/models/profile.rb +9 -7
- data/spec/models/record.rb +1 -1
- data/spec/models/site.rb +2 -3
- data/spec/models/user.rb +14 -14
- data/spec/unit/accept_nested_attributes_spec.rb +12 -0
- data/spec/unit/associations_spec.rb +6 -6
- data/spec/unit/document_spec.rb +6 -6
- data/spec/unit/indexes_spec.rb +3 -2
- data/spec/unit/validations_spec.rb +7 -0
- data/spec/validators/ssn_validator.rb +3 -1
- metadata +43 -32
- data/.rvmrc +0 -1
- data/spec/unit/allow_mass_assignment_spec.rb +0 -14
|
@@ -12,16 +12,11 @@ module Mongoid
|
|
|
12
12
|
EMBEDS_ONE = Mongoid::Relations::Embedded::One
|
|
13
13
|
EMBEDDED_IN = Mongoid::Relations::Embedded::In
|
|
14
14
|
|
|
15
|
-
|
|
16
15
|
class HaveAssociationMatcher
|
|
17
16
|
def initialize(name, association_type)
|
|
18
17
|
@association = {}
|
|
19
18
|
@association[:name] = name.to_s
|
|
20
19
|
@association[:type] = association_type
|
|
21
|
-
# begin
|
|
22
|
-
# @association[:class] = name.to_s.classify.constantize
|
|
23
|
-
# rescue
|
|
24
|
-
# end
|
|
25
20
|
@expectation_message = "#{type_description} #{@association[:name].inspect}"
|
|
26
21
|
@expectation_message << " of type #{@association[:class].inspect}" unless @association[:class].nil?
|
|
27
22
|
end
|
|
@@ -39,6 +34,19 @@ module Mongoid
|
|
|
39
34
|
self
|
|
40
35
|
end
|
|
41
36
|
|
|
37
|
+
def ordered_by(association_field_name)
|
|
38
|
+
raise "#{@association[:type].inspect} does not respond to :order" unless [HAS_MANY, HAS_AND_BELONGS_TO_MANY, EMBEDS_MANY].include?(@association[:type])
|
|
39
|
+
@association[:order] = association_field_name.to_s
|
|
40
|
+
@expectation_message << " ordered by #{@association[:order].inspect}"
|
|
41
|
+
|
|
42
|
+
if association_field_name.is_a? Origin::Key
|
|
43
|
+
@association[:order_operator] = association_field_name.operator
|
|
44
|
+
@expectation_message << " #{order_way(@association[:order_operator])}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
self
|
|
48
|
+
end
|
|
49
|
+
|
|
42
50
|
def with_dependent(method_name)
|
|
43
51
|
@association[:dependent] = method_name
|
|
44
52
|
@expectation_message << " which specifies dependent as #{@association[:dependent].to_s}"
|
|
@@ -57,8 +65,34 @@ module Mongoid
|
|
|
57
65
|
self
|
|
58
66
|
end
|
|
59
67
|
|
|
68
|
+
def with_autobuild
|
|
69
|
+
@association[:autobuild] = true
|
|
70
|
+
@expectation_message << " which specifies autobuild as #{@association[:autobuild].to_s}"
|
|
71
|
+
self
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def with_polymorphism
|
|
75
|
+
@association[:polymorphic] = true
|
|
76
|
+
@expectation_message << " which specifies polymorphic as #{@association[:polymorphic].to_s}"
|
|
77
|
+
self
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def with_cascading_callbacks
|
|
81
|
+
@association[:cascade_callbacks] = true
|
|
82
|
+
@expectation_message << " which specifies cascade_callbacks as #{@association[:cascade_callbacks].to_s}"
|
|
83
|
+
self
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def cyclic
|
|
87
|
+
@association[:cyclic] = true
|
|
88
|
+
@expectation_message << " which specifies cyclic as #{@association[:cyclic].to_s}"
|
|
89
|
+
self
|
|
90
|
+
end
|
|
91
|
+
|
|
60
92
|
def stored_as(store_as)
|
|
61
|
-
|
|
93
|
+
@association[:store_as] = store_as.to_s
|
|
94
|
+
@expectation_message << " which is stored as #{@association[:store_as].inspect}"
|
|
95
|
+
self
|
|
62
96
|
end
|
|
63
97
|
|
|
64
98
|
def with_foreign_key(foreign_key)
|
|
@@ -102,6 +136,24 @@ module Mongoid
|
|
|
102
136
|
end
|
|
103
137
|
end
|
|
104
138
|
|
|
139
|
+
if @association[:order]
|
|
140
|
+
if @association[:order].to_s != metadata.order.to_s
|
|
141
|
+
@negative_result_message = "#{@positive_result_message} ordered by #{metadata.order}"
|
|
142
|
+
return false
|
|
143
|
+
else
|
|
144
|
+
@positive_result_message = "#{@positive_result_message} ordered by #{metadata.order}"
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
if @association[:order_operator]
|
|
149
|
+
if @association[:order_operator] != metadata.order.operator
|
|
150
|
+
@negative_result_message = "#{@positive_result_message} #{order_way(@association[:order_operator] * -1)}"
|
|
151
|
+
return false
|
|
152
|
+
else
|
|
153
|
+
@positive_result_message = "#{@positive_result_message} #{order_way(@association[:order_operator])}"
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
105
157
|
if @association[:dependent]
|
|
106
158
|
if @association[:dependent].to_s != metadata.dependent.to_s
|
|
107
159
|
@negative_result_message = "#{@positive_result_message} which specified dependent as #{metadata.dependent}"
|
|
@@ -120,6 +172,51 @@ module Mongoid
|
|
|
120
172
|
end
|
|
121
173
|
end
|
|
122
174
|
|
|
175
|
+
if @association[:autobuild]
|
|
176
|
+
if metadata.autobuilding? != true
|
|
177
|
+
@negative_result_message = "#{@positive_result_message} which did not set autobuild"
|
|
178
|
+
return false
|
|
179
|
+
else
|
|
180
|
+
@positive_result_message = "#{@positive_result_message} which set autobuild"
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
if @association[:polymorphic]
|
|
185
|
+
if metadata.polymorphic? != true
|
|
186
|
+
@negative_result_message = "#{@positive_result_message} which did not set polymorphic"
|
|
187
|
+
return false
|
|
188
|
+
else
|
|
189
|
+
@positive_result_message = "#{@positive_result_message} which set polymorphic"
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
if @association[:cascade_callbacks]
|
|
194
|
+
if metadata.cascading_callbacks? != true
|
|
195
|
+
@negative_result_message = "#{@positive_result_message} which did not set cascade_callbacks"
|
|
196
|
+
return false
|
|
197
|
+
else
|
|
198
|
+
@positive_result_message = "#{@positive_result_message} which set cascade_callbacks"
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
if @association[:cyclic]
|
|
203
|
+
if metadata.cyclic? != true
|
|
204
|
+
@negative_result_message = "#{@positive_result_message} which did not set cyclic"
|
|
205
|
+
return false
|
|
206
|
+
else
|
|
207
|
+
@positive_result_message = "#{@positive_result_message} which set cyclic"
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
if @association[:store_as]
|
|
212
|
+
if metadata.store_as != @association[:store_as]
|
|
213
|
+
@negative_result_message = "#{@positive_result_message} which is stored as #{metadata.store_as}"
|
|
214
|
+
return false
|
|
215
|
+
else
|
|
216
|
+
@positive_result_message = "#{@positive_result_message} which is stored as #{metadata.store_as}"
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
123
220
|
if @association[:index]
|
|
124
221
|
if metadata.index != true
|
|
125
222
|
@negative_result_message = "#{@positive_result_message} which did not set index"
|
|
@@ -149,6 +246,9 @@ module Mongoid
|
|
|
149
246
|
"Expected #{@actual.inspect} to not #{@expectation_message}, got #{@positive_result_message}"
|
|
150
247
|
end
|
|
151
248
|
|
|
249
|
+
alias :failure_message :failure_message_for_should
|
|
250
|
+
alias :failure_message_when_negated :failure_message_for_should_not
|
|
251
|
+
|
|
152
252
|
def description
|
|
153
253
|
@expectation_message
|
|
154
254
|
end
|
|
@@ -156,24 +256,29 @@ module Mongoid
|
|
|
156
256
|
def type_description(type = nil, passive = true)
|
|
157
257
|
type ||= @association[:type]
|
|
158
258
|
case type.name
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
259
|
+
when EMBEDS_ONE.name
|
|
260
|
+
(passive ? 'embed' : 'embeds') << ' one'
|
|
261
|
+
when EMBEDS_MANY.name
|
|
262
|
+
(passive ? 'embed' : 'embeds') << ' many'
|
|
263
|
+
when EMBEDDED_IN.name
|
|
264
|
+
(passive ? 'be' : 'is') << ' embedded in'
|
|
265
|
+
when HAS_ONE.name
|
|
266
|
+
(passive ? 'reference' : 'references') << ' one'
|
|
267
|
+
when HAS_MANY.name
|
|
268
|
+
(passive ? 'reference' : 'references') << ' many'
|
|
269
|
+
when HAS_AND_BELONGS_TO_MANY.name
|
|
270
|
+
(passive ? 'reference' : 'references') << ' and referenced in many'
|
|
271
|
+
when BELONGS_TO.name
|
|
272
|
+
(passive ? 'be referenced in' : 'referenced in')
|
|
273
|
+
else
|
|
274
|
+
raise "Unknown association type '%s'" % type
|
|
175
275
|
end
|
|
176
276
|
end
|
|
277
|
+
|
|
278
|
+
private
|
|
279
|
+
def order_way(operator)
|
|
280
|
+
[nil, "ascending", "descending"][operator]
|
|
281
|
+
end
|
|
177
282
|
end
|
|
178
283
|
|
|
179
284
|
def embed_one(association_name)
|
data/lib/matchers/document.rb
CHANGED
|
@@ -5,11 +5,21 @@ module Mongoid
|
|
|
5
5
|
@attributes = attrs.collect(&:to_s)
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
+
def localized
|
|
9
|
+
@localized = true
|
|
10
|
+
self
|
|
11
|
+
end
|
|
12
|
+
|
|
8
13
|
def of_type(type)
|
|
9
14
|
@type = type
|
|
10
15
|
self
|
|
11
16
|
end
|
|
12
17
|
|
|
18
|
+
def with_alias(field_alias)
|
|
19
|
+
@field_alias = field_alias
|
|
20
|
+
self
|
|
21
|
+
end
|
|
22
|
+
|
|
13
23
|
def with_default_value_of(default)
|
|
14
24
|
@default = default
|
|
15
25
|
self
|
|
@@ -25,11 +35,26 @@ module Mongoid
|
|
|
25
35
|
error << " of type #{@klass.fields[attr].type}"
|
|
26
36
|
end
|
|
27
37
|
|
|
28
|
-
if !@default.nil?
|
|
29
|
-
|
|
38
|
+
if !@default.nil?
|
|
39
|
+
if @klass.fields[attr].default_val.nil?
|
|
40
|
+
error << " with default not set"
|
|
41
|
+
elsif @klass.fields[attr].default_val != @default
|
|
42
|
+
error << " with default value of #{@klass.fields[attr].default_val}"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
if @field_alias and @klass.fields[attr].options[:as] != @field_alias
|
|
47
|
+
error << " with alias #{@klass.fields[attr].options[:as]}"
|
|
30
48
|
end
|
|
31
49
|
|
|
32
50
|
@errors.push("field #{attr.inspect}" << error) unless error.blank?
|
|
51
|
+
|
|
52
|
+
if @localized
|
|
53
|
+
unless @klass.fields[attr].localized?
|
|
54
|
+
@errors.push "is not localized #{attr.inspect}"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
33
58
|
else
|
|
34
59
|
@errors.push "no field named #{attr.inspect}"
|
|
35
60
|
end
|
|
@@ -45,9 +70,13 @@ module Mongoid
|
|
|
45
70
|
"Expected #{@klass.inspect} to not #{description}, got #{@klass.inspect} to #{description}"
|
|
46
71
|
end
|
|
47
72
|
|
|
73
|
+
alias :failure_message :failure_message_for_should
|
|
74
|
+
alias :failure_message_when_negated :failure_message_for_should_not
|
|
75
|
+
|
|
48
76
|
def description
|
|
49
77
|
desc = "have #{@attributes.size > 1 ? 'fields' : 'field'} named #{@attributes.collect(&:inspect).to_sentence}"
|
|
50
78
|
desc << " of type #{@type.inspect}" if @type
|
|
79
|
+
desc << " with alias #{@field_alias}" if @field_alias
|
|
51
80
|
desc << " with default value of #{@default.inspect}" if !@default.nil?
|
|
52
81
|
desc
|
|
53
82
|
end
|
data/lib/matchers/indexes.rb
CHANGED
|
@@ -2,33 +2,50 @@ module Mongoid
|
|
|
2
2
|
module Matchers
|
|
3
3
|
class HaveIndexForMatcher # :nodoc:
|
|
4
4
|
def initialize(index_fields)
|
|
5
|
-
@index_fields = index_fields
|
|
5
|
+
@index_fields = index_fields.symbolize_keys!
|
|
6
6
|
end
|
|
7
|
-
|
|
8
|
-
def with_options(options = {})
|
|
7
|
+
|
|
8
|
+
def with_options(options = { })
|
|
9
9
|
@options = options
|
|
10
10
|
self
|
|
11
11
|
end
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
def matches?(klass)
|
|
14
|
-
@klass
|
|
14
|
+
@klass = klass.is_a?(Class) ? klass : klass.class
|
|
15
15
|
@errors = []
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
|
|
17
|
+
if @klass.respond_to?(:index_options)
|
|
18
|
+
# Mongoid 3
|
|
19
|
+
unless @klass.index_options[@index_fields]
|
|
20
|
+
@errors.push "no index for #{@index_fields}"
|
|
21
|
+
else
|
|
22
|
+
if !@options.nil? && !@options.empty?
|
|
23
|
+
@options.each do |option, option_value|
|
|
24
|
+
if denormalising_options(@klass.index_options[@index_fields])[option] != option_value
|
|
25
|
+
@errors.push "index for #{@index_fields.inspect} with options of #{@klass.index_options[@index_fields].inspect}"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
19
30
|
else
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
31
|
+
# Mongoid 4
|
|
32
|
+
unless @klass.index_specifications.map(&:key).include?(@index_fields)
|
|
33
|
+
@errors.push "no index for #{@index_fields}"
|
|
34
|
+
else
|
|
35
|
+
if !@options.nil? && !@options.empty?
|
|
36
|
+
index_options = @klass.index_specifications.select { |is| is.key == @index_fields }.first.options
|
|
37
|
+
@options.each do |option, option_value|
|
|
38
|
+
if index_options[option] != option_value
|
|
39
|
+
@errors.push "index for #{@index_fields.inspect} with options of #{index_options.inspect}"
|
|
40
|
+
end
|
|
24
41
|
end
|
|
25
42
|
end
|
|
26
43
|
end
|
|
27
44
|
end
|
|
28
|
-
|
|
45
|
+
|
|
29
46
|
@errors.empty?
|
|
30
47
|
end
|
|
31
|
-
|
|
48
|
+
|
|
32
49
|
def failure_message_for_should
|
|
33
50
|
"Expected #{@klass.inspect} to #{description}, got #{@errors.to_sentence}"
|
|
34
51
|
end
|
|
@@ -37,13 +54,29 @@ module Mongoid
|
|
|
37
54
|
"Expected #{@klass.inspect} to not #{description}, got #{@klass.inspect} to #{description}"
|
|
38
55
|
end
|
|
39
56
|
|
|
57
|
+
alias :failure_message :failure_message_for_should
|
|
58
|
+
alias :failure_message_when_negated :failure_message_for_should_not
|
|
59
|
+
|
|
40
60
|
def description
|
|
41
61
|
desc = "have an index for #{@index_fields.inspect}"
|
|
42
62
|
desc << " with options of #{@options.inspect}" if @options
|
|
43
63
|
desc
|
|
44
64
|
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
MAPPINGS = {
|
|
68
|
+
dropDups: :drop_dups
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
def denormalising_options(opts)
|
|
72
|
+
options = {}
|
|
73
|
+
opts.each_pair do |option, value|
|
|
74
|
+
options[MAPPINGS[option] || option] = value
|
|
75
|
+
end
|
|
76
|
+
options
|
|
77
|
+
end
|
|
45
78
|
end
|
|
46
|
-
|
|
79
|
+
|
|
47
80
|
def have_index_for(index_fields)
|
|
48
81
|
HaveIndexForMatcher.new(index_fields)
|
|
49
82
|
end
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
module Mongoid
|
|
2
2
|
module Matchers
|
|
3
|
-
module Validations
|
|
3
|
+
module Validations
|
|
4
4
|
class ValidateAssociatedMatcher < HaveValidationMatcher
|
|
5
5
|
def initialize(name)
|
|
6
6
|
super(name, :associated)
|
|
7
7
|
end
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
def description
|
|
10
10
|
"validate associated #{@field.inspect}"
|
|
11
11
|
end
|
|
12
12
|
end
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
def validate_associated(association_name)
|
|
15
15
|
ValidateAssociatedMatcher.new(association_name)
|
|
16
|
-
end
|
|
16
|
+
end
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
|
-
end
|
|
19
|
+
end
|
|
@@ -2,6 +2,7 @@ module Mongoid
|
|
|
2
2
|
module Matchers
|
|
3
3
|
module Validations
|
|
4
4
|
class ValidateWithCustomValidatorMatcher < HaveValidationMatcher
|
|
5
|
+
include WithMessage
|
|
5
6
|
def initialize(field)
|
|
6
7
|
super(field, :custom)
|
|
7
8
|
end
|
|
@@ -11,11 +12,6 @@ module Mongoid
|
|
|
11
12
|
self
|
|
12
13
|
end
|
|
13
14
|
|
|
14
|
-
def with_message(message)
|
|
15
|
-
@expected_message = message
|
|
16
|
-
self
|
|
17
|
-
end
|
|
18
|
-
|
|
19
15
|
def matches?(actual)
|
|
20
16
|
return false unless (@result = super(actual))
|
|
21
17
|
check_custom_validator if @custom_validator
|
|
@@ -41,19 +37,6 @@ module Mongoid
|
|
|
41
37
|
@result = false
|
|
42
38
|
end
|
|
43
39
|
end
|
|
44
|
-
|
|
45
|
-
def check_expected_message
|
|
46
|
-
actual_message = @validator.options[:message]
|
|
47
|
-
if actual_message.nil?
|
|
48
|
-
@negative_result_message << " with no custom message"
|
|
49
|
-
@result = false
|
|
50
|
-
elsif actual_message == @expected_message
|
|
51
|
-
@positive_result_message << " with custom message '#{@expected_message}'"
|
|
52
|
-
else
|
|
53
|
-
@negative_result_message << " got message '#{actual_message}'"
|
|
54
|
-
@result = false
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
40
|
end
|
|
58
41
|
|
|
59
42
|
def custom_validate(field)
|
|
@@ -61,4 +44,4 @@ module Mongoid
|
|
|
61
44
|
end
|
|
62
45
|
end
|
|
63
46
|
end
|
|
64
|
-
end
|
|
47
|
+
end
|
|
@@ -15,7 +15,14 @@ module Mongoid
|
|
|
15
15
|
return false unless result = super(actual)
|
|
16
16
|
|
|
17
17
|
if @not_allowed_values
|
|
18
|
-
|
|
18
|
+
raw_validator_not_allowed_values = @validator.options[:in]
|
|
19
|
+
|
|
20
|
+
validator_not_allowed_values = case raw_validator_not_allowed_values
|
|
21
|
+
when Range then raw_validator_not_allowed_values.to_a
|
|
22
|
+
when Proc then raw_validator_not_allowed_values.call(actual)
|
|
23
|
+
else raw_validator_not_allowed_values end
|
|
24
|
+
|
|
25
|
+
allowed_values = @not_allowed_values - validator_not_allowed_values
|
|
19
26
|
if allowed_values.empty?
|
|
20
27
|
@positive_result_message = @positive_result_message << " not allowing all values mentioned"
|
|
21
28
|
else
|
|
@@ -1,71 +1,71 @@
|
|
|
1
1
|
module Mongoid
|
|
2
2
|
module Matchers
|
|
3
|
-
module Validations
|
|
3
|
+
module Validations
|
|
4
4
|
class ValidateFormatOfMatcher < HaveValidationMatcher
|
|
5
5
|
def initialize(field)
|
|
6
6
|
super(field, :format)
|
|
7
7
|
end
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
def with_format(format)
|
|
10
10
|
@format = format
|
|
11
11
|
self
|
|
12
12
|
end
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
def to_allow(valid_value)
|
|
15
15
|
@valid_value = valid_value
|
|
16
|
-
self
|
|
16
|
+
self
|
|
17
17
|
end
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
def not_to_allow(invalid_value)
|
|
20
20
|
@invalid_value = invalid_value
|
|
21
|
-
self
|
|
21
|
+
self
|
|
22
22
|
end
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
def matches?(actual)
|
|
25
25
|
return false unless result = super(actual)
|
|
26
|
-
|
|
26
|
+
|
|
27
27
|
if @format
|
|
28
28
|
if @validator.options[:with] == @format
|
|
29
29
|
@positive_result_message = @positive_result_message << " with format #{@validator.options[:format].inspect}"
|
|
30
30
|
else
|
|
31
31
|
@negative_result_message = @negative_result_message << " with format #{@validator.options[:format].inspect}"
|
|
32
32
|
result = false
|
|
33
|
-
end
|
|
33
|
+
end
|
|
34
34
|
end
|
|
35
|
-
|
|
35
|
+
|
|
36
36
|
if @valid_value
|
|
37
37
|
if @validator.options[:with] =~ @valid_value
|
|
38
38
|
@positive_result_message = @positive_result_message << " with #{@valid_value.inspect} as a valid value"
|
|
39
39
|
else
|
|
40
40
|
@negative_result_message = @negative_result_message << " with #{@valid_value.inspect} as an invalid value"
|
|
41
41
|
result = false
|
|
42
|
-
end
|
|
42
|
+
end
|
|
43
43
|
end
|
|
44
|
-
|
|
45
|
-
if @invalid_value
|
|
44
|
+
|
|
45
|
+
if @invalid_value
|
|
46
46
|
if !(@invalid_value =~ @validator.options[:with])
|
|
47
47
|
@positive_result_message = @positive_result_message << " with #{@invalid_value.inspect} as an invalid value"
|
|
48
48
|
else
|
|
49
49
|
@negative_result_message = @negative_result_message << " with #{@invalid_value.inspect} as a valid value"
|
|
50
50
|
result = false
|
|
51
|
-
end
|
|
51
|
+
end
|
|
52
52
|
end
|
|
53
|
-
|
|
54
|
-
result
|
|
53
|
+
|
|
54
|
+
result
|
|
55
55
|
end
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
def description
|
|
58
58
|
options_desc = []
|
|
59
59
|
options_desc << " with format #{@format.inspect}" if @format
|
|
60
60
|
options_desc << " allowing the value #{@valid_value.inspect}" if @valid_value
|
|
61
|
-
options_desc << " not allowing the value #{@invalid_value.inspect}" if @invalid_value
|
|
61
|
+
options_desc << " not allowing the value #{@invalid_value.inspect}" if @invalid_value
|
|
62
62
|
super << options_desc.to_sentence
|
|
63
|
-
end
|
|
63
|
+
end
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
def validate_format_of(field)
|
|
67
67
|
ValidateFormatOfMatcher.new(field)
|
|
68
|
-
end
|
|
68
|
+
end
|
|
69
69
|
end
|
|
70
70
|
end
|
|
71
|
-
end
|
|
71
|
+
end
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
module Mongoid
|
|
2
2
|
module Matchers
|
|
3
|
-
module Validations
|
|
3
|
+
module Validations
|
|
4
4
|
class ValidateInclusionOfMatcher < HaveValidationMatcher
|
|
5
5
|
def initialize(name)
|
|
6
6
|
super(name, :inclusion)
|
|
7
7
|
end
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
def to_allow(*values)
|
|
10
|
-
@allowed_values = values.map(
|
|
10
|
+
@allowed_values = values.map { |v| v.respond_to?(:to_a) ? v.to_a : v }.flatten
|
|
11
11
|
self
|
|
12
12
|
end
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
def matches?(actual)
|
|
15
15
|
return false unless result = super(actual)
|
|
16
16
|
|
|
17
17
|
if @allowed_values
|
|
18
18
|
raw_validator_allowed_values = @validator.options[:in]
|
|
19
19
|
|
|
20
|
-
validator_allowed_values = case raw_validator_allowed_values
|
|
20
|
+
validator_allowed_values = case raw_validator_allowed_values
|
|
21
21
|
when Range then raw_validator_allowed_values.to_a
|
|
22
22
|
when Proc then raw_validator_allowed_values.call(actual)
|
|
23
23
|
else raw_validator_allowed_values end
|
|
@@ -26,24 +26,24 @@ module Mongoid
|
|
|
26
26
|
if not_allowed_values.empty?
|
|
27
27
|
@positive_result_message = @positive_result_message << " allowing all values mentioned"
|
|
28
28
|
else
|
|
29
|
-
@negative_result_message = @negative_result_message << " not allowing
|
|
29
|
+
@negative_result_message = @negative_result_message << " not allowing these values: #{not_allowed_values.inspect}"
|
|
30
30
|
result = false
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
|
-
|
|
34
|
-
result
|
|
33
|
+
|
|
34
|
+
result
|
|
35
35
|
end
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
def description
|
|
38
38
|
options_desc = []
|
|
39
|
-
options_desc << " allowing
|
|
39
|
+
options_desc << " allowing these values: #{@allowed_values}" if @allowed_values
|
|
40
40
|
super << options_desc.to_sentence
|
|
41
|
-
end
|
|
41
|
+
end
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
def validate_inclusion_of(field)
|
|
45
45
|
ValidateInclusionOfMatcher.new(field)
|
|
46
|
-
end
|
|
46
|
+
end
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
|
-
end
|
|
49
|
+
end
|