mongoid-rspec 3.0.0 → 4.1.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.
Files changed (64) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/README.md +220 -109
  4. data/Rakefile +8 -5
  5. data/lib/matchers/accept_nested_attributes.rb +22 -23
  6. data/lib/matchers/allow_mass_assignment.rb +12 -12
  7. data/lib/matchers/associations.rb +85 -49
  8. data/lib/matchers/be_dynamic_document.rb +26 -0
  9. data/lib/matchers/be_mongoid_document.rb +26 -0
  10. data/lib/matchers/be_stored_in.rb +55 -0
  11. data/lib/matchers/have_field.rb +90 -0
  12. data/lib/matchers/have_timestamps.rb +61 -0
  13. data/lib/matchers/indexes/have_index_for.rb +16 -0
  14. data/lib/matchers/indexes/v3/have_index_for.rb +59 -0
  15. data/lib/matchers/indexes/v4/have_index_for.rb +54 -0
  16. data/lib/matchers/validations/absence_of.rb +11 -0
  17. data/lib/matchers/validations/associated.rb +1 -1
  18. data/lib/matchers/validations/confirmation_of.rb +0 -7
  19. data/lib/matchers/validations/custom_validation_of.rb +1 -4
  20. data/lib/matchers/validations/exclusion_of.rb +5 -5
  21. data/lib/matchers/validations/format_of.rb +2 -2
  22. data/lib/matchers/validations/inclusion_of.rb +5 -5
  23. data/lib/matchers/validations/length_of.rb +13 -35
  24. data/lib/matchers/validations/numericality_of.rb +32 -16
  25. data/lib/matchers/validations/presence_of.rb +1 -1
  26. data/lib/matchers/validations/uniqueness_of.rb +7 -10
  27. data/lib/matchers/validations.rb +30 -11
  28. data/lib/mongoid/rspec/version.rb +2 -2
  29. data/lib/mongoid/rspec.rb +18 -5
  30. data/spec/models/article.rb +9 -6
  31. data/spec/models/comment.rb +1 -1
  32. data/spec/models/log.rb +3 -3
  33. data/spec/models/message.rb +17 -0
  34. data/spec/models/movie_article.rb +1 -2
  35. data/spec/models/person.rb +1 -1
  36. data/spec/models/profile.rb +2 -2
  37. data/spec/models/record.rb +1 -1
  38. data/spec/models/site.rb +5 -1
  39. data/spec/models/user.rb +12 -11
  40. data/spec/spec_helper.rb +9 -10
  41. data/spec/unit/accept_nested_attributes_spec.rb +1 -1
  42. data/spec/unit/associations_spec.rb +19 -7
  43. data/spec/unit/be_dynamic_document_spec.rb +21 -0
  44. data/spec/unit/be_mongoid_document_spec.rb +25 -0
  45. data/spec/unit/be_stored_in.rb +54 -0
  46. data/spec/unit/document_spec.rb +5 -14
  47. data/spec/unit/have_index_for_spec.rb +46 -0
  48. data/spec/unit/have_timestamps_spec.rb +71 -0
  49. data/spec/unit/validations_spec.rb +23 -15
  50. data/spec/validators/ssn_validator.rb +6 -6
  51. metadata +115 -39
  52. data/.document +0 -5
  53. data/.gitignore +0 -6
  54. data/.ruby-gemset +0 -1
  55. data/.ruby-version +0 -1
  56. data/.travis.yml +0 -10
  57. data/Gemfile +0 -4
  58. data/lib/matchers/collections.rb +0 -9
  59. data/lib/matchers/document.rb +0 -173
  60. data/lib/matchers/indexes.rb +0 -69
  61. data/lib/matchers/validations/with_message.rb +0 -27
  62. data/mongoid-rspec.gemspec +0 -26
  63. data/spec/unit/collections_spec.rb +0 -7
  64. data/spec/unit/indexes_spec.rb +0 -17
@@ -2,16 +2,9 @@ module Mongoid
2
2
  module Matchers
3
3
  module Validations
4
4
  class ValidateConfirmationOfMatcher < HaveValidationMatcher
5
- include WithMessage
6
-
7
5
  def initialize(name)
8
6
  super(name, :confirmation)
9
7
  end
10
-
11
- def with_message(message)
12
- @expected_message = message
13
- self
14
- end
15
8
  end
16
9
 
17
10
  def validate_confirmation_of(field)
@@ -2,7 +2,6 @@ module Mongoid
2
2
  module Matchers
3
3
  module Validations
4
4
  class ValidateWithCustomValidatorMatcher < HaveValidationMatcher
5
- include WithMessage
6
5
  def initialize(field)
7
6
  super(field, :custom)
8
7
  end
@@ -15,7 +14,6 @@ module Mongoid
15
14
  def matches?(actual)
16
15
  return false unless (@result = super(actual))
17
16
  check_custom_validator if @custom_validator
18
- check_expected_message if @expected_message
19
17
 
20
18
  @result
21
19
  end
@@ -23,14 +21,13 @@ module Mongoid
23
21
  def description
24
22
  options_desc = []
25
23
  options_desc << " with custom validator #{@custom_validator.name}" if @validator
26
- options_desc << " with message '#{@expected_message}'" if @expected_message
27
24
  "validate field #{@field.inspect}" << options_desc.to_sentence
28
25
  end
29
26
 
30
27
  private
31
28
 
32
29
  def check_custom_validator
33
- if @validator.kind_of? @custom_validator
30
+ if @validator.is_a? @custom_validator
34
31
  @positive_result_message << " with custom validator of type #{@custom_validator.name}"
35
32
  else
36
33
  @negative_result_message << " with custom validator not of type #{@custom_validator.name}"
@@ -18,13 +18,13 @@ module Mongoid
18
18
  raw_validator_not_allowed_values = @validator.options[:in]
19
19
 
20
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
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
24
 
25
25
  allowed_values = @not_allowed_values - validator_not_allowed_values
26
26
  if allowed_values.empty?
27
- @positive_result_message = @positive_result_message << " not allowing all values mentioned"
27
+ @positive_result_message = @positive_result_message << ' not allowing all values mentioned'
28
28
  else
29
29
  @negative_result_message = @negative_result_message << " allowing the following the ff. values: #{allowed_values.inspect}"
30
30
  result = false
@@ -46,4 +46,4 @@ module Mongoid
46
46
  end
47
47
  end
48
48
  end
49
- end
49
+ end
@@ -43,7 +43,7 @@ module Mongoid
43
43
  end
44
44
 
45
45
  if @invalid_value
46
- if !(@invalid_value =~ @validator.options[:with])
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"
@@ -68,4 +68,4 @@ module Mongoid
68
68
  end
69
69
  end
70
70
  end
71
- end
71
+ end
@@ -18,13 +18,13 @@ module Mongoid
18
18
  raw_validator_allowed_values = @validator.options[:in]
19
19
 
20
20
  validator_allowed_values = case raw_validator_allowed_values
21
- when Range then raw_validator_allowed_values.to_a
22
- when Proc then raw_validator_allowed_values.call(actual)
23
- else raw_validator_allowed_values end
21
+ when Range then raw_validator_allowed_values.to_a
22
+ when Proc then raw_validator_allowed_values.call(actual)
23
+ else raw_validator_allowed_values end
24
24
 
25
25
  not_allowed_values = @allowed_values - validator_allowed_values
26
26
  if not_allowed_values.empty?
27
- @positive_result_message = @positive_result_message << " allowing all values mentioned"
27
+ @positive_result_message = @positive_result_message << ' allowing all values mentioned'
28
28
  else
29
29
  @negative_result_message = @negative_result_message << " not allowing these values: #{not_allowed_values.inspect}"
30
30
  result = false
@@ -46,4 +46,4 @@ module Mongoid
46
46
  end
47
47
  end
48
48
  end
49
- end
49
+ end
@@ -2,8 +2,6 @@ module Mongoid
2
2
  module Matchers
3
3
  module Validations
4
4
  class ValidateLengthOfMatcher < HaveValidationMatcher
5
- include WithMessage
6
-
7
5
  def initialize(name)
8
6
  super(name, :length)
9
7
  end
@@ -12,30 +10,25 @@ module Mongoid
12
10
  @maximum = value
13
11
  self
14
12
  end
15
- alias :less_than :with_maximum
13
+ alias less_than with_maximum
16
14
 
17
15
  def with_minimum(value)
18
16
  @minimum = value
19
17
  self
20
18
  end
21
- alias :greater_than :with_minimum
19
+ alias greater_than with_minimum
22
20
 
23
21
  def within(value)
24
22
  @within = value
25
23
  self
26
24
  end
27
- alias :in :within
25
+ alias in within
28
26
 
29
27
  def as_exactly(value)
30
28
  @is = value
31
29
  self
32
30
  end
33
- alias :is :as_exactly
34
-
35
- def with_message(message)
36
- @expected_message = message
37
- self
38
- end
31
+ alias is as_exactly
39
32
 
40
33
  def matches?(actual)
41
34
  return false unless @result = super(actual)
@@ -44,7 +37,6 @@ module Mongoid
44
37
  check_minimum if @minimum
45
38
  check_range if @within
46
39
  check_exact if @is
47
- check_expected_message if @expected_message
48
40
 
49
41
  @result
50
42
  end
@@ -55,7 +47,6 @@ module Mongoid
55
47
  options_desc << "with maximum of #{@maximum}" if @maximum
56
48
  options_desc << "within the range of #{@within}" if @within
57
49
  options_desc << "as exactly #{@is}" if @is
58
- options_desc << "with message '#{@expected_message}'" if @expected_message
59
50
  super << " #{options_desc.to_sentence}"
60
51
  end
61
52
 
@@ -63,7 +54,7 @@ module Mongoid
63
54
 
64
55
  def check_maximum
65
56
  if actual_max.nil?
66
- @negative_result_message << " with no maximum"
57
+ @negative_result_message << ' with no maximum'
67
58
  @result = false
68
59
  elsif actual_max == @maximum
69
60
  @positive_result_message << " with maximum of #{@maximum}"
@@ -75,7 +66,7 @@ module Mongoid
75
66
 
76
67
  def check_minimum
77
68
  if actual_min.nil?
78
- @negative_result_message << " with no minimum"
69
+ @negative_result_message << ' with no minimum'
79
70
  @result = false
80
71
  elsif actual_min == @minimum
81
72
  @positive_result_message << " with minimum of #{@minimum}"
@@ -86,15 +77,15 @@ module Mongoid
86
77
  end
87
78
 
88
79
  def check_range
89
- min, max = [@within.min, @within.max]
90
- if !actual_min.nil? and actual_max.nil?
80
+ min, max = @within.minmax
81
+ if !actual_min.nil? && actual_max.nil?
91
82
  @negative_result_message << " with no minimum but with maximum of #{actual_max}"
92
83
  @result = false
93
- elsif actual_min.nil? and !actual_max.nil?
84
+ elsif actual_min.nil? && !actual_max.nil?
94
85
  @negative_result_message << " with minimum_of #{actual_min} but no maximum"
95
86
  @result = false
96
- elsif actual_min.nil? and actual_max.nil?
97
- @negative_result_message << " with no minimum and maximum"
87
+ elsif actual_min.nil? && actual_max.nil?
88
+ @negative_result_message << ' with no minimum and maximum'
98
89
  @result = false
99
90
  elsif actual_min == min && actual_max == max
100
91
  @positive_result_message << " within the range of #{@within.inspect}"
@@ -113,29 +104,16 @@ module Mongoid
113
104
  end
114
105
  end
115
106
 
116
- def check_expected_message
117
- actual_message = @validator.options[:message]
118
- if actual_message.nil?
119
- @negative_result_message << " with no custom message"
120
- @result = false
121
- elsif actual_message == @expected_message
122
- @positive_result_message << " with custom message '#{@expected_message}'"
123
- else
124
- @negative_result_message << " got message '#{actual_message}'"
125
- @result = false
126
- end
127
- end
128
-
129
107
  def actual_is
130
108
  actual_is = @validator.options[:is]
131
109
  end
132
110
 
133
111
  def actual_min
134
- @validator.options[:minimum] || ((@validator.options[:in] || @validator.options[:within]).try(&:min))
112
+ @validator.options[:minimum] || (@validator.options[:in] || @validator.options[:within]).try(&:min)
135
113
  end
136
114
 
137
115
  def actual_max
138
- @validator.options[:maximum] || ((@validator.options[:in] || @validator.options[:within]).try(&:max))
116
+ @validator.options[:maximum] || (@validator.options[:in] || @validator.options[:within]).try(&:max)
139
117
  end
140
118
  end
141
119
 
@@ -2,8 +2,19 @@ module Mongoid
2
2
  module Matchers
3
3
  module Validations
4
4
  class ValidateNumericalityOfMatcher < HaveValidationMatcher
5
- @@allowed_options = [:equal_to, :greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to,
6
- :even, :odd, :only_integer, :allow_nil, :nil]
5
+ ALLOWED_OPTIONS =
6
+ %i[
7
+ allow_nil
8
+ equal_to
9
+ even
10
+ greater_than
11
+ greater_than_or_equal_to
12
+ less_than
13
+ less_than_or_equal_to
14
+ nil
15
+ odd
16
+ only_integer
17
+ ].freeze
7
18
 
8
19
  def initialize(field)
9
20
  super(field, :numericality)
@@ -12,9 +23,15 @@ module Mongoid
12
23
 
13
24
  def to_allow(options)
14
25
  options[:equal_to] = options if options.is_a?(Numeric)
15
- options[:allow_nil] = options.delete(:nil) if options.has_key?(:nil)
16
- raise ArgumentError, "validate_numericality_of#to_allow requires a Hash parameter containing any of the following keys: " <<
17
- @@allowed_options.map(&:inspect).join(", ") if !options.is_a?(Hash) or options.empty? or (options.keys - @@allowed_options).any?
26
+ options[:allow_nil] = options.delete(:nil) if options.key?(:nil)
27
+
28
+ if !options.is_a?(Hash) || options.empty? || (options.keys - ALLOWED_OPTIONS).any?
29
+ message =
30
+ 'validate_numericality_of#to_allow requires a Hash parameter containing' \
31
+ "any of the following keys: #{ALLOWED_OPTIONS.map(&:inspect).join(', ')}"
32
+ raise ArgumentError, message
33
+ end
34
+
18
35
  @options.merge!(options)
19
36
  self
20
37
  end
@@ -22,11 +39,10 @@ module Mongoid
22
39
  def matches?(actual)
23
40
  return false unless result = super(actual)
24
41
 
25
- @@allowed_options.each do |comparator|
26
- if @options.has_key?(comparator) and !([:even, :odd, :only_integer].include?(comparator) and !@validator.options.include?(comparator))
27
- result &= (@validator.options[comparator] == @options[comparator])
28
- end
42
+ @options.each do |comparator, expected_value|
43
+ result &= (@validator.options[comparator] == expected_value)
29
44
  end
45
+
30
46
  @positive_result_message <<= options_message(@validator.options)
31
47
  @negative_result_message <<= options_message(@validator.options)
32
48
  result
@@ -45,19 +61,19 @@ module Mongoid
45
61
  case key
46
62
  when :allow_nil
47
63
  when :only_integer
48
- type_msg << "integer" if value
64
+ type_msg << 'integer' if value
49
65
  when :odd, :even
50
- type_msg << "#{key.to_s}-numbered" if value
66
+ type_msg << "#{key}-numbered" if value
51
67
  else
52
- comp_msg << "#{key.to_s.gsub("_", " ")} #{value.inspect}"
68
+ comp_msg << "#{key.to_s.tr('_', ' ')} #{value.inspect}"
53
69
  end
54
70
  end
55
- allow_nil = (options[:allow_nil] ? "nil" : "non-nil") if options.has_key?(:allow_nil)
56
- ["", "allowing", allow_nil, type_msg.any? ? type_msg.to_sentence : nil, "values", comp_msg.any? ? comp_msg.to_sentence : nil].compact.join(" ")
71
+ allow_nil = (options[:allow_nil] ? 'nil' : 'non-nil') if options.key?(:allow_nil)
72
+ ['', 'allowing', allow_nil, type_msg.any? ? type_msg.to_sentence : nil, 'values', comp_msg.any? ? comp_msg.to_sentence : nil].compact.join(' ')
57
73
  end
58
74
 
59
75
  def method_missing(m, *args, &block)
60
- if @@allowed_options.include?(m.to_sym)
76
+ if ALLOWED_OPTIONS.include?(m.to_sym)
61
77
  raise ArgumentError, "wrong number of arguments (#{args.length} for 1)" if args.length > 1
62
78
  send :to_allow, m.to_sym => args.first
63
79
  else
@@ -71,4 +87,4 @@ module Mongoid
71
87
  end
72
88
  end
73
89
  end
74
- end
90
+ end
@@ -6,4 +6,4 @@ module Mongoid
6
6
  end
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -2,7 +2,6 @@ module Mongoid
2
2
  module Matchers
3
3
  module Validations
4
4
  class ValidateUniquenessOfMatcher < HaveValidationMatcher
5
- include WithMessage
6
5
  def initialize(field)
7
6
  super(field, :uniqueness)
8
7
  end
@@ -11,7 +10,7 @@ module Mongoid
11
10
  @scope = [scope].flatten.map(&:to_sym)
12
11
  self
13
12
  end
14
- alias_method :scoped_on, :scoped_to
13
+ alias scoped_on scoped_to
15
14
 
16
15
  def case_insensitive
17
16
  @case_insensitive = true
@@ -29,7 +28,6 @@ module Mongoid
29
28
  check_scope if @scope
30
29
  check_allow_blank if @allow_blank
31
30
  check_case_sensitivity if @case_insensitive
32
- check_expected_message if @expected_message
33
31
 
34
32
  @result
35
33
  end
@@ -37,9 +35,8 @@ module Mongoid
37
35
  def description
38
36
  options_desc = []
39
37
  options_desc << " scoped to #{@scope.inspect}" if @scope
40
- options_desc << " allowing blank values" if @allow_blank
41
- options_desc << " allowing case insensitive values" if @case_insensitive
42
- options_desc << " with message '#{@expected_message}'" if @expected_message
38
+ options_desc << ' allowing blank values' if @allow_blank
39
+ options_desc << ' allowing case insensitive values' if @case_insensitive
43
40
  super << options_desc.to_sentence
44
41
  end
45
42
 
@@ -47,9 +44,9 @@ module Mongoid
47
44
 
48
45
  def check_allow_blank
49
46
  if @validator.options[:allow_blank] == @allow_blank
50
- @positive_result_message << " with blank values allowed"
47
+ @positive_result_message << ' with blank values allowed'
51
48
  else
52
- @negative_result_message << " with no blank values allowed"
49
+ @negative_result_message << ' with no blank values allowed'
53
50
  @result = false
54
51
  end
55
52
  end
@@ -66,9 +63,9 @@ module Mongoid
66
63
 
67
64
  def check_case_sensitivity
68
65
  if @validator.options[:case_sensitive] == false
69
- @positive_result_message << " with case insensitive values"
66
+ @positive_result_message << ' with case insensitive values'
70
67
  else
71
- @negative_result_message << " without case insensitive values"
68
+ @negative_result_message << ' without case insensitive values'
72
69
  @result = false
73
70
  end
74
71
  end
@@ -1,9 +1,7 @@
1
1
  module Mongoid
2
2
  module Matchers
3
3
  module Validations
4
-
5
4
  class HaveValidationMatcher
6
-
7
5
  def initialize(field, validation_type)
8
6
  @field = field.to_s
9
7
  @type = validation_type.to_s
@@ -13,9 +11,9 @@ module Mongoid
13
11
  def matches?(actual)
14
12
  @klass = actual.is_a?(Class) ? actual : actual.class
15
13
 
16
- @validator = @klass.validators_on(@field).detect{ |v|
17
- v.kind.to_s == @type and (!v.options[:on] or on_options_matches?(v))
18
- }
14
+ @validator = @klass.validators_on(@field).detect do |v|
15
+ (v.kind.to_s == @type) && (!v.options[:on] || on_options_matches?(v))
16
+ end
19
17
 
20
18
  if @validator
21
19
  @negative_result_message = "#{@type.inspect} validator on #{@field.inspect}"
@@ -26,6 +24,7 @@ module Mongoid
26
24
  end
27
25
  @result = true
28
26
  check_on if @options[:on]
27
+ check_expected_message if @expected_message
29
28
  @result
30
29
  end
31
30
 
@@ -37,12 +36,14 @@ module Mongoid
37
36
  "Expected #{@klass.inspect} to not #{description}; instead got #{@positive_result_message}"
38
37
  end
39
38
 
40
- alias :failure_message :failure_message_for_should
41
- alias :failure_message_when_negated :failure_message_for_should_not
39
+ alias failure_message failure_message_for_should
40
+ alias failure_message_when_negated failure_message_for_should_not
42
41
 
43
42
  def description
44
43
  desc = "have #{@type.inspect} validator on #{@field.inspect}"
45
44
  desc << " on #{@options[:on]}" if @options[:on]
45
+ desc << " with message #{@expected_message.inspect}" if @expected_message
46
+
46
47
  desc
47
48
  end
48
49
 
@@ -51,13 +52,20 @@ module Mongoid
51
52
  self
52
53
  end
53
54
 
55
+ def with_message(message)
56
+ @expected_message = message
57
+ self
58
+ end
59
+
60
+ private
61
+
54
62
  def check_on
55
63
  validator_on_methods = [@validator.options[:on]].flatten
56
64
 
57
65
  if validator_on_methods.any?
58
66
  message = " on methods: #{validator_on_methods}"
59
67
 
60
- if on_options_covered_by?( @validator )
68
+ if on_options_covered_by?(@validator)
61
69
  @positive_result_message << message
62
70
  else
63
71
  @negative_result_message << message
@@ -66,15 +74,26 @@ module Mongoid
66
74
  end
67
75
  end
68
76
 
69
- private
70
-
71
77
  def on_options_matches?(validator)
72
- @options[:on] and validator.options[:on] and on_options_covered_by?(validator)
78
+ @options[:on] && validator.options[:on] && on_options_covered_by?(validator)
73
79
  end
74
80
 
75
81
  def on_options_covered_by?(validator)
76
82
  ([@options[:on]].flatten - [validator.options[:on]].flatten).empty?
77
83
  end
84
+
85
+ def check_expected_message
86
+ actual_message = @validator.options[:message]
87
+ if actual_message.nil?
88
+ @negative_result_message << ' with no custom message'
89
+ @result = false
90
+ elsif actual_message == @expected_message
91
+ @positive_result_message << " with custom message '#{@expected_message}'"
92
+ else
93
+ @negative_result_message << " got message '#{actual_message}'"
94
+ @result = false
95
+ end
96
+ end
78
97
  end
79
98
  end
80
99
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
- module Rspec
3
- VERSION = '3.0.0'
2
+ module RSpec
3
+ VERSION = '4.1.0'.freeze
4
4
  end
5
5
  end
data/lib/mongoid/rspec.rb CHANGED
@@ -1,18 +1,19 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
 
3
3
  require 'mongoid'
4
+ require 'mongoid-compatibility'
4
5
  require 'rspec/core'
5
6
  require 'rspec/expectations'
6
7
  require 'rspec/mocks'
7
- require "active_model"
8
- require 'matchers/document'
8
+ require 'active_support/core_ext/hash/keys'
9
+ require 'active_support/core_ext/hash/transform_values' if Mongoid::Compatibility::Version.mongoid4_or_newer? && ActiveSupport.version < Gem::Version.new('6')
10
+ require 'active_support/core_ext/object/blank'
11
+ require 'active_support/core_ext/string/inflections'
12
+
9
13
  require 'matchers/associations'
10
- require 'matchers/collections'
11
- require 'matchers/indexes'
12
14
  require 'matchers/allow_mass_assignment'
13
15
  require 'matchers/accept_nested_attributes'
14
16
  require 'matchers/validations'
15
- require 'matchers/validations/with_message'
16
17
  require 'matchers/validations/associated'
17
18
  require 'matchers/validations/confirmation_of'
18
19
  require 'matchers/validations/exclusion_of'
@@ -24,6 +25,18 @@ require 'matchers/validations/presence_of'
24
25
  require 'matchers/validations/uniqueness_of'
25
26
  require 'matchers/validations/acceptance_of'
26
27
  require 'matchers/validations/custom_validation_of'
28
+ require 'matchers/validations/absence_of'
29
+ require 'matchers/be_mongoid_document'
30
+ require 'matchers/be_dynamic_document'
31
+ require 'matchers/be_stored_in'
32
+ require 'matchers/have_field'
33
+ require 'matchers/indexes/have_index_for'
34
+ if Mongoid::Compatibility::Version.mongoid3?
35
+ require 'matchers/indexes/v3/have_index_for'
36
+ else
37
+ require 'matchers/indexes/v4/have_index_for'
38
+ end
39
+ require 'matchers/have_timestamps'
27
40
 
28
41
  module Mongoid
29
42
  module Matchers
@@ -8,22 +8,25 @@ class Article
8
8
  field :allow_comments, type: Boolean, default: true
9
9
  field :number_of_comments, type: Integer
10
10
  field :status, type: Symbol
11
+ field :deletion_date, type: DateTime, default: nil
11
12
 
12
- embeds_many :comments, cascade_callbacks: true
13
- embeds_one :permalink
13
+ embeds_many :comments, cascade_callbacks: true, inverse_of: :article
14
+ embeds_one :permalink, inverse_of: :linkable
14
15
  belongs_to :author, class_name: 'User', inverse_of: :articles, index: true
15
16
 
16
17
  validates :title, presence: true
17
18
 
18
19
  validates_inclusion_of :status, in: [:pending], on: :create
19
- validates_inclusion_of :status, in: [:approved, :rejected ], on: :update
20
+ validates_inclusion_of :status, in: %i[approved rejected], on: :update
20
21
 
21
22
  validates_length_of :title, within: 8..16
22
23
  validates_length_of :content, minimum: 200
23
24
 
24
- index({ title: 1 }, { unique: true, background: true, drop_dups: true })
25
- index({ published: 1 })
26
- index({ 'permalink._id' => 1 })
25
+ validates_absence_of :deletion_date if Mongoid::Compatibility::Version.mongoid4_or_newer?
26
+
27
+ index({ title: 1 }, unique: true, background: true, drop_dups: true)
28
+ index(published: 1)
29
+ index('permalink._id' => 1)
27
30
 
28
31
  accepts_nested_attributes_for :permalink
29
32
  end
@@ -3,4 +3,4 @@ class Comment
3
3
 
4
4
  embedded_in :article, inverse_of: :comments, polymorphic: true
5
5
  belongs_to :user, inverse_of: :comments
6
- end
6
+ end
data/spec/models/log.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  class Log
2
2
  include Mongoid::Document
3
3
  include Mongoid::Timestamps
4
- include Mongoid::Attributes::Dynamic
4
+ include Mongoid::Attributes::Dynamic if Mongoid::Compatibility::Version.mongoid4_or_newer?
5
5
 
6
- store_in collection: "logs"
6
+ store_in collection: 'logs'
7
7
 
8
- index({ created_at: 1 }, { bucket_size: 100, expire_after_seconds: 3600 } )
8
+ index({ created_at: 1 }, bucket_size: 100, expire_after_seconds: 3600)
9
9
  end
@@ -0,0 +1,17 @@
1
+ class Message
2
+ include Mongoid::Document
3
+
4
+ field :identifier
5
+ field :from
6
+ field :to
7
+
8
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
9
+ belongs_to :user, optional: true
10
+ else
11
+ belongs_to :user
12
+ end
13
+
14
+ validates :identifier, uniqueness: { message: 'uniqueness' }
15
+ validates :from, presence: { message: 'required' }
16
+ validates :to, format: { with: /[a-z]+/, message: 'format' }
17
+ end
@@ -1,8 +1,7 @@
1
1
  class MovieArticle < Article
2
-
3
2
  field :rating, type: Float
4
3
  field :classification, type: Integer
5
4
 
6
5
  validates :rating, numericality: { greater_than: 0, less_than_or_equal_to: 5 }
7
6
  validates :classification, numericality: { even: true, only_integer: true, allow_nil: false }
8
- end
7
+ end
@@ -7,4 +7,4 @@ class Person
7
7
  field :ssn
8
8
 
9
9
  validates :ssn, ssn: true
10
- end
10
+ end
@@ -10,7 +10,7 @@ class Profile
10
10
 
11
11
  validates :age, numericality: { greater_than: 0 }
12
12
  validates :terms_of_service, acceptance: true
13
- validates :hobbies, length: { minimum: 1, message: "requires at least one hobby" }
13
+ validates :hobbies, length: { minimum: 1, message: 'requires at least one hobby' }
14
14
 
15
- index({ first_name: 1, last_name: 1 })
15
+ index(first_name: 1, last_name: 1)
16
16
  end
@@ -2,4 +2,4 @@ class Record
2
2
  include Mongoid::Document
3
3
 
4
4
  belongs_to :user, inverse_of: :record
5
- end
5
+ end