shoulda-matchers 4.2.0 → 4.5.1
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 +4 -4
- data/{MIT-LICENSE → LICENSE} +1 -1
- data/README.md +167 -85
- data/lib/shoulda/matchers/action_controller/callback_matcher.rb +4 -2
- data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +3 -2
- data/lib/shoulda/matchers/action_controller/permit_matcher.rb +26 -21
- data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +6 -8
- data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +6 -8
- data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +16 -13
- data/lib/shoulda/matchers/action_controller/rescue_from_matcher.rb +2 -1
- data/lib/shoulda/matchers/action_controller/route_matcher.rb +5 -6
- data/lib/shoulda/matchers/action_controller/route_params.rb +1 -1
- data/lib/shoulda/matchers/action_controller/set_session_or_flash_matcher.rb +19 -13
- data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +18 -16
- data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +29 -27
- data/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_changed_value_error.rb +1 -1
- data/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb +5 -5
- data/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter_and_validator.rb +2 -2
- data/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters.rb +1 -1
- data/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb +1 -1
- data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +1 -1
- data/lib/shoulda/matchers/active_model/have_secure_password_matcher.rb +51 -25
- data/lib/shoulda/matchers/active_model/helpers.rb +1 -1
- data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +32 -30
- data/lib/shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher.rb +1 -1
- data/lib/shoulda/matchers/active_model/qualifiers/ignoring_interference_by_writer.rb +1 -1
- data/lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb +9 -1
- data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +2 -2
- data/lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb +9 -8
- data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +28 -46
- data/lib/shoulda/matchers/active_model/validate_length_of_matcher.rb +33 -9
- data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +71 -26
- data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +2 -2
- data/lib/shoulda/matchers/active_model/validation_matcher.rb +31 -6
- data/lib/shoulda/matchers/active_model/validation_matcher/build_description.rb +2 -4
- data/lib/shoulda/matchers/active_model/validation_message_finder.rb +2 -4
- data/lib/shoulda/matchers/active_model/validator.rb +3 -3
- data/lib/shoulda/matchers/active_record.rb +26 -23
- data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +6 -3
- data/lib/shoulda/matchers/active_record/association_matcher.rb +118 -47
- data/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb +5 -2
- data/lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb +4 -4
- data/lib/shoulda/matchers/active_record/association_matchers/inverse_of_matcher.rb +1 -1
- data/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb +11 -6
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +14 -15
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb +30 -8
- data/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb +23 -5
- data/lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb +3 -3
- data/lib/shoulda/matchers/active_record/association_matchers/order_matcher.rb +1 -1
- data/lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb +3 -3
- data/lib/shoulda/matchers/active_record/association_matchers/source_matcher.rb +3 -2
- data/lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb +7 -5
- data/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb +8 -8
- data/lib/shoulda/matchers/active_record/have_attached_matcher.rb +185 -0
- data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +39 -17
- data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +1 -1
- data/lib/shoulda/matchers/active_record/have_implicit_order_column.rb +106 -0
- data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +12 -10
- data/lib/shoulda/matchers/active_record/have_rich_text_matcher.rb +83 -0
- data/lib/shoulda/matchers/active_record/have_secure_token_matcher.rb +30 -9
- data/lib/shoulda/matchers/active_record/serialize_matcher.rb +13 -9
- data/lib/shoulda/matchers/active_record/uniqueness.rb +1 -1
- data/lib/shoulda/matchers/active_record/uniqueness/test_model_creator.rb +1 -3
- data/lib/shoulda/matchers/active_record/uniqueness/test_models.rb +0 -2
- data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +80 -73
- data/lib/shoulda/matchers/doublespeak.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/double.rb +1 -1
- data/lib/shoulda/matchers/doublespeak/double_collection.rb +3 -3
- data/lib/shoulda/matchers/doublespeak/double_implementation_registry.rb +8 -5
- data/lib/shoulda/matchers/doublespeak/object_double.rb +1 -1
- data/lib/shoulda/matchers/doublespeak/stub_implementation.rb +1 -5
- data/lib/shoulda/matchers/doublespeak/world.rb +2 -2
- data/lib/shoulda/matchers/error.rb +1 -1
- data/lib/shoulda/matchers/independent.rb +0 -1
- data/lib/shoulda/matchers/independent/delegate_method_matcher.rb +14 -13
- data/lib/shoulda/matchers/integrations/configuration.rb +1 -1
- data/lib/shoulda/matchers/integrations/libraries/action_controller.rb +1 -1
- data/lib/shoulda/matchers/integrations/libraries/rails.rb +2 -2
- data/lib/shoulda/matchers/integrations/test_frameworks/active_support_test_case.rb +1 -1
- data/lib/shoulda/matchers/integrations/test_frameworks/minitest_4.rb +1 -1
- data/lib/shoulda/matchers/integrations/test_frameworks/minitest_5.rb +1 -1
- data/lib/shoulda/matchers/integrations/test_frameworks/missing_test_framework.rb +1 -1
- data/lib/shoulda/matchers/integrations/test_frameworks/test_unit.rb +1 -1
- data/lib/shoulda/matchers/rails_shim.rb +9 -3
- data/lib/shoulda/matchers/util.rb +16 -4
- data/lib/shoulda/matchers/util/word_wrap.rb +8 -8
- data/lib/shoulda/matchers/version.rb +1 -1
- data/lib/shoulda/matchers/warn.rb +3 -3
- data/shoulda-matchers.gemspec +10 -7
- metadata +11 -9
- data/lib/shoulda/matchers/independent/delegate_method_matcher/stubbed_target.rb +0 -37
@@ -23,7 +23,7 @@ module Shoulda
|
|
23
23
|
@value_written = args.fetch(:value)
|
24
24
|
@ignore_interference_by_writer = args.fetch(
|
25
25
|
:ignore_interference_by_writer,
|
26
|
-
Qualifiers::IgnoreInterferenceByWriter.new
|
26
|
+
Qualifiers::IgnoreInterferenceByWriter.new,
|
27
27
|
)
|
28
28
|
@after_set_callback = args.fetch(:after_set_callback, -> { })
|
29
29
|
|
@@ -36,9 +36,9 @@ module Shoulda
|
|
36
36
|
description << Shoulda::Matchers::Util.inspect_value(value_written)
|
37
37
|
|
38
38
|
if attribute_changed_value?
|
39
|
-
description <<
|
39
|
+
description << ' -- which was read back as '
|
40
40
|
description << Shoulda::Matchers::Util.inspect_value(value_read)
|
41
|
-
description <<
|
41
|
+
description << ' --'
|
42
42
|
end
|
43
43
|
|
44
44
|
description
|
@@ -206,7 +206,7 @@ module Shoulda
|
|
206
206
|
model: object.class,
|
207
207
|
attribute_name: attribute_name,
|
208
208
|
value_written: value_written,
|
209
|
-
value_read: value_read
|
209
|
+
value_read: value_read,
|
210
210
|
)
|
211
211
|
end
|
212
212
|
|
@@ -218,7 +218,7 @@ module Shoulda
|
|
218
218
|
AttributeDoesNotExistError.create(
|
219
219
|
model: object.class,
|
220
220
|
attribute_name: attribute_name,
|
221
|
-
value: value_written
|
221
|
+
value: value_written,
|
222
222
|
)
|
223
223
|
end
|
224
224
|
|
data/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter_and_validator.rb
CHANGED
@@ -34,7 +34,7 @@ module Shoulda
|
|
34
34
|
attribute_name: attribute_name,
|
35
35
|
value: value,
|
36
36
|
ignore_interference_by_writer: ignore_interference_by_writer,
|
37
|
-
after_set_callback: after_setting_value_callback
|
37
|
+
after_set_callback: after_setting_value_callback,
|
38
38
|
)
|
39
39
|
end
|
40
40
|
|
@@ -48,7 +48,7 @@ module Shoulda
|
|
48
48
|
attribute_to_check_message_against,
|
49
49
|
context: context,
|
50
50
|
expects_strict: expects_strict?,
|
51
|
-
expected_message: expected_message
|
51
|
+
expected_message: expected_message,
|
52
52
|
)
|
53
53
|
end
|
54
54
|
|
@@ -10,49 +10,51 @@ module Shoulda
|
|
10
10
|
# include ActiveModel::Model
|
11
11
|
# include ActiveModel::SecurePassword
|
12
12
|
# attr_accessor :password
|
13
|
+
# attr_accessor :reset_password
|
13
14
|
#
|
14
15
|
# has_secure_password
|
16
|
+
# has_secure_password :reset_password
|
15
17
|
# end
|
16
18
|
#
|
17
19
|
# # RSpec
|
18
20
|
# RSpec.describe User, type: :model do
|
19
21
|
# it { should have_secure_password }
|
22
|
+
# it { should have_secure_password(:reset_password) }
|
20
23
|
# end
|
21
24
|
#
|
22
25
|
# # Minitest (Shoulda)
|
23
26
|
# class UserTest < ActiveSupport::TestCase
|
24
27
|
# should have_secure_password
|
28
|
+
# should have_secure_password(:reset_password)
|
25
29
|
# end
|
26
30
|
#
|
27
31
|
# @return [HaveSecurePasswordMatcher]
|
28
32
|
#
|
29
|
-
def have_secure_password
|
30
|
-
HaveSecurePasswordMatcher.new
|
33
|
+
def have_secure_password(attr = :password)
|
34
|
+
HaveSecurePasswordMatcher.new(attr)
|
31
35
|
end
|
32
36
|
|
33
37
|
# @private
|
34
38
|
class HaveSecurePasswordMatcher
|
35
39
|
attr_reader :failure_message
|
36
40
|
|
37
|
-
CORRECT_PASSWORD =
|
38
|
-
INCORRECT_PASSWORD =
|
39
|
-
|
40
|
-
EXPECTED_METHODS = [
|
41
|
-
:authenticate,
|
42
|
-
:password=,
|
43
|
-
:password_confirmation=,
|
44
|
-
:password_digest,
|
45
|
-
:password_digest=,
|
46
|
-
]
|
41
|
+
CORRECT_PASSWORD = 'aBcDe12345'.freeze
|
42
|
+
INCORRECT_PASSWORD = 'password'.freeze
|
47
43
|
|
48
44
|
MESSAGES = {
|
49
|
-
authenticated_incorrect_password:
|
50
|
-
|
51
|
-
|
52
|
-
|
45
|
+
authenticated_incorrect_password: 'expected %{subject} to not'\
|
46
|
+
' authenticate an incorrect %{attribute}',
|
47
|
+
did_not_authenticate_correct_password: 'expected %{subject} to'\
|
48
|
+
' authenticate the correct %{attribute}',
|
49
|
+
method_not_found: 'expected %{subject} to respond to %{methods}',
|
50
|
+
}.freeze
|
51
|
+
|
52
|
+
def initialize(attribute)
|
53
|
+
@attribute = attribute.to_sym
|
54
|
+
end
|
53
55
|
|
54
56
|
def description
|
55
|
-
"have a secure password"
|
57
|
+
"have a secure password, defined on #{@attribute} attribute"
|
56
58
|
end
|
57
59
|
|
58
60
|
def matches?(subject)
|
@@ -60,7 +62,8 @@ module Shoulda
|
|
60
62
|
|
61
63
|
if failure = validate
|
62
64
|
key, params = failure
|
63
|
-
@failure_message =
|
65
|
+
@failure_message =
|
66
|
+
MESSAGES[key] % { subject: subject.class }.merge(params)
|
64
67
|
end
|
65
68
|
|
66
69
|
failure.nil?
|
@@ -71,21 +74,44 @@ module Shoulda
|
|
71
74
|
attr_reader :subject
|
72
75
|
|
73
76
|
def validate
|
74
|
-
missing_methods =
|
77
|
+
missing_methods = expected_methods.reject do |m|
|
78
|
+
subject.respond_to?(m)
|
79
|
+
end
|
75
80
|
|
76
81
|
if missing_methods.present?
|
77
82
|
[:method_not_found, { methods: missing_methods.to_sentence }]
|
78
83
|
else
|
79
|
-
subject.
|
80
|
-
subject.
|
84
|
+
subject.send("#{@attribute}=", CORRECT_PASSWORD)
|
85
|
+
subject.send("#{@attribute}_confirmation=", CORRECT_PASSWORD)
|
81
86
|
|
82
|
-
if not subject.
|
83
|
-
[:did_not_authenticate_correct_password,
|
84
|
-
|
85
|
-
|
87
|
+
if not subject.send(authenticate_method, CORRECT_PASSWORD)
|
88
|
+
[:did_not_authenticate_correct_password,
|
89
|
+
{ attribute: @attribute },]
|
90
|
+
elsif subject.send(authenticate_method, INCORRECT_PASSWORD)
|
91
|
+
[:authenticated_incorrect_password, { attribute: @attribute }]
|
86
92
|
end
|
87
93
|
end
|
88
94
|
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def expected_methods
|
99
|
+
@_expected_methods ||= %I[
|
100
|
+
#{authenticate_method}
|
101
|
+
#{@attribute}=
|
102
|
+
#{@attribute}_confirmation=
|
103
|
+
#{@attribute}_digest
|
104
|
+
#{@attribute}_digest=
|
105
|
+
]
|
106
|
+
end
|
107
|
+
|
108
|
+
def authenticate_method
|
109
|
+
if @attribute == :password
|
110
|
+
:authenticate
|
111
|
+
else
|
112
|
+
"authenticate_#{@attribute}".to_sym
|
113
|
+
end
|
114
|
+
end
|
89
115
|
end
|
90
116
|
end
|
91
117
|
end
|
@@ -5,22 +5,42 @@ module Shoulda
|
|
5
5
|
# @private
|
6
6
|
class ComparisonMatcher < ValidationMatcher
|
7
7
|
ERROR_MESSAGES = {
|
8
|
-
:> =>
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
:> => {
|
9
|
+
label: :greater_than,
|
10
|
+
assertions: [false, false, true],
|
11
|
+
},
|
12
|
+
:>= => {
|
13
|
+
label: :greater_than_or_equal_to,
|
14
|
+
assertions: [false, true, true],
|
15
|
+
},
|
16
|
+
:< => {
|
17
|
+
label: :less_than,
|
18
|
+
assertions: [true, false, false],
|
19
|
+
},
|
20
|
+
:<= => {
|
21
|
+
label: :less_than_or_equal_to,
|
22
|
+
assertions: [true, true, false],
|
23
|
+
},
|
24
|
+
:== => {
|
25
|
+
label: :equal_to,
|
26
|
+
assertions: [false, true, false],
|
27
|
+
},
|
28
|
+
:!= => {
|
29
|
+
label: :other_than,
|
30
|
+
assertions: [true, false, true],
|
31
|
+
},
|
32
|
+
}.freeze
|
14
33
|
|
15
34
|
def initialize(numericality_matcher, value, operator)
|
16
35
|
super(nil)
|
17
36
|
unless numericality_matcher.respond_to? :diff_to_compare
|
18
37
|
raise ArgumentError, 'numericality_matcher is invalid'
|
19
38
|
end
|
39
|
+
|
20
40
|
@numericality_matcher = numericality_matcher
|
21
41
|
@value = value
|
22
42
|
@operator = operator
|
23
|
-
@message = ERROR_MESSAGES[operator]
|
43
|
+
@message = ERROR_MESSAGES[operator][:label]
|
24
44
|
end
|
25
45
|
|
26
46
|
def simple_description
|
@@ -93,10 +113,9 @@ module Shoulda
|
|
93
113
|
end
|
94
114
|
|
95
115
|
def submatchers_and_results
|
96
|
-
@_submatchers_and_results ||=
|
97
|
-
|
98
|
-
|
99
|
-
end
|
116
|
+
@_submatchers_and_results ||= submatchers.map do |matcher|
|
117
|
+
{ matcher: matcher, matched: matcher.matches?(@subject) }
|
118
|
+
end
|
100
119
|
end
|
101
120
|
|
102
121
|
def comparison_combos
|
@@ -114,18 +133,7 @@ module Shoulda
|
|
114
133
|
end
|
115
134
|
|
116
135
|
def assertions
|
117
|
-
|
118
|
-
when :>
|
119
|
-
[false, false, true]
|
120
|
-
when :>=
|
121
|
-
[false, true, true]
|
122
|
-
when :==
|
123
|
-
[false, true, false]
|
124
|
-
when :<
|
125
|
-
[true, false, false]
|
126
|
-
when :<=
|
127
|
-
[true, true, false]
|
128
|
-
end
|
136
|
+
ERROR_MESSAGES[@operator][:assertions]
|
129
137
|
end
|
130
138
|
|
131
139
|
def diffs_to_compare
|
@@ -140,13 +148,7 @@ module Shoulda
|
|
140
148
|
end
|
141
149
|
|
142
150
|
def comparison_expectation
|
143
|
-
|
144
|
-
when :> then "greater than"
|
145
|
-
when :>= then "greater than or equal to"
|
146
|
-
when :== then "equal to"
|
147
|
-
when :< then "less than"
|
148
|
-
when :<= then "less than or equal to"
|
149
|
-
end
|
151
|
+
ERROR_MESSAGES[@operator][:label].to_s.tr('_', ' ')
|
150
152
|
end
|
151
153
|
end
|
152
154
|
end
|
@@ -103,10 +103,12 @@ module Shoulda
|
|
103
103
|
if reflection
|
104
104
|
obj = reflection.klass.new
|
105
105
|
if collection?
|
106
|
-
[
|
106
|
+
[obj]
|
107
107
|
else
|
108
108
|
obj
|
109
109
|
end
|
110
|
+
elsif array_column?
|
111
|
+
['an arbitary value']
|
110
112
|
else
|
111
113
|
case column_type
|
112
114
|
when :integer, :float then 1
|
@@ -137,6 +139,12 @@ module Shoulda
|
|
137
139
|
@subject.class.respond_to?(:reflect_on_association) &&
|
138
140
|
@subject.class.reflect_on_association(@attribute)
|
139
141
|
end
|
142
|
+
|
143
|
+
def array_column?
|
144
|
+
@subject.class.respond_to?(:columns_hash) &&
|
145
|
+
@subject.class.columns_hash[@attribute.to_s].respond_to?(:array) &&
|
146
|
+
@subject.class.columns_hash[@attribute.to_s].array
|
147
|
+
end
|
140
148
|
end
|
141
149
|
end
|
142
150
|
end
|
@@ -148,12 +148,12 @@ module Shoulda
|
|
148
148
|
|
149
149
|
def qualify_matcher(matcher, confirmation_attribute_value)
|
150
150
|
matcher.values_to_preset = {
|
151
|
-
confirmation_attribute => confirmation_attribute_value
|
151
|
+
confirmation_attribute => confirmation_attribute_value,
|
152
152
|
}
|
153
153
|
matcher.with_message(
|
154
154
|
@expected_message,
|
155
155
|
against: confirmation_attribute,
|
156
|
-
values: { attribute: attribute }
|
156
|
+
values: { attribute: attribute },
|
157
157
|
)
|
158
158
|
end
|
159
159
|
end
|
@@ -35,7 +35,7 @@ module Shoulda
|
|
35
35
|
# include ActiveModel::Model
|
36
36
|
# attr_accessor :supported_os
|
37
37
|
#
|
38
|
-
# validates_exclusion_of :supported_os, in:
|
38
|
+
# validates_exclusion_of :supported_os, in: 5..8
|
39
39
|
# end
|
40
40
|
#
|
41
41
|
# # RSpec
|
@@ -146,11 +146,12 @@ module Shoulda
|
|
146
146
|
else
|
147
147
|
description = "validate that :#{@attribute}"
|
148
148
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
149
|
+
description <<
|
150
|
+
if @array.many?
|
151
|
+
" is neither #{inspected_array}"
|
152
|
+
else
|
153
|
+
" is not #{inspected_array}"
|
154
|
+
end
|
154
155
|
|
155
156
|
description
|
156
157
|
end
|
@@ -238,8 +239,8 @@ module Shoulda
|
|
238
239
|
|
239
240
|
def inspected_array
|
240
241
|
Shoulda::Matchers::Util.inspect_values(@array).to_sentence(
|
241
|
-
two_words_connector:
|
242
|
-
last_word_connector:
|
242
|
+
two_words_connector: ' nor ',
|
243
|
+
last_word_connector: ', nor ',
|
243
244
|
)
|
244
245
|
end
|
245
246
|
end
|
@@ -268,20 +268,20 @@ module Shoulda
|
|
268
268
|
|
269
269
|
# @private
|
270
270
|
class ValidateInclusionOfMatcher < ValidationMatcher
|
271
|
-
BLANK_VALUES = ['', ' ', "\n", "\r", "\t", "\f"]
|
272
|
-
ARBITRARY_OUTSIDE_STRING = 'shoulda-matchers test string'
|
271
|
+
BLANK_VALUES = ['', ' ', "\n", "\r", "\t", "\f"].freeze
|
272
|
+
ARBITRARY_OUTSIDE_STRING = 'shoulda-matchers test string'.freeze
|
273
273
|
ARBITRARY_OUTSIDE_INTEGER = 123456789
|
274
274
|
ARBITRARY_OUTSIDE_DECIMAL = BigDecimal('0.123456789')
|
275
275
|
ARBITRARY_OUTSIDE_DATE = Date.jd(9999999)
|
276
276
|
ARBITRARY_OUTSIDE_DATETIME = DateTime.jd(9999999)
|
277
277
|
ARBITRARY_OUTSIDE_TIME = Time.at(9999999999)
|
278
|
-
BOOLEAN_ALLOWS_BOOLEAN_MESSAGE = <<EOT
|
278
|
+
BOOLEAN_ALLOWS_BOOLEAN_MESSAGE = <<EOT.freeze
|
279
279
|
You are using `validate_inclusion_of` to assert that a boolean column allows
|
280
280
|
boolean values and disallows non-boolean ones. Be aware that it is not possible
|
281
281
|
to fully test this, as boolean columns will automatically convert non-boolean
|
282
282
|
values to boolean ones. Hence, you should consider removing this test.
|
283
283
|
EOT
|
284
|
-
BOOLEAN_ALLOWS_NIL_MESSAGE = <<EOT
|
284
|
+
BOOLEAN_ALLOWS_NIL_MESSAGE = <<EOT.freeze
|
285
285
|
You are using `validate_inclusion_of` to assert that a boolean column allows nil.
|
286
286
|
Be aware that it is not possible to fully test this, as anything other than
|
287
287
|
true, false or nil will be converted to false. Hence, you should consider
|
@@ -311,15 +311,6 @@ EOT
|
|
311
311
|
self
|
312
312
|
end
|
313
313
|
|
314
|
-
def allow_blank
|
315
|
-
@options[:allow_blank] = true
|
316
|
-
self
|
317
|
-
end
|
318
|
-
|
319
|
-
def expects_to_allow_blank?
|
320
|
-
@options[:allow_blank]
|
321
|
-
end
|
322
|
-
|
323
314
|
def allow_nil
|
324
315
|
@options[:allow_nil] = true
|
325
316
|
self
|
@@ -363,11 +354,12 @@ EOT
|
|
363
354
|
else
|
364
355
|
description = "validate that :#{@attribute}"
|
365
356
|
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
357
|
+
description <<
|
358
|
+
if @array.many?
|
359
|
+
" is either #{inspected_array}"
|
360
|
+
else
|
361
|
+
" is #{inspected_array}"
|
362
|
+
end
|
371
363
|
|
372
364
|
description
|
373
365
|
end
|
@@ -423,14 +415,14 @@ EOT
|
|
423
415
|
allows_all_values_in_array? &&
|
424
416
|
disallows_all_values_outside_of_array? &&
|
425
417
|
allows_nil_value? &&
|
426
|
-
|
418
|
+
allow_blank_matches?
|
427
419
|
end
|
428
420
|
|
429
421
|
def does_not_match_for_array?
|
430
422
|
disallows_any_values_in_array? ||
|
431
423
|
allows_any_value_outside_of_array? ||
|
432
424
|
disallows_nil_value? ||
|
433
|
-
|
425
|
+
allow_blank_does_not_match?
|
434
426
|
end
|
435
427
|
|
436
428
|
def allows_lower_value
|
@@ -554,10 +546,10 @@ EOT
|
|
554
546
|
values = []
|
555
547
|
|
556
548
|
values << case @array
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
549
|
+
when [true] then false
|
550
|
+
when [false] then true
|
551
|
+
else raise CouldNotDetermineValueOutsideOfArray
|
552
|
+
end
|
561
553
|
|
562
554
|
if attribute_allows_nil?
|
563
555
|
values << nil
|
@@ -590,21 +582,21 @@ EOT
|
|
590
582
|
|
591
583
|
def column_type_to_attribute_type(type)
|
592
584
|
case type
|
593
|
-
|
594
|
-
|
595
|
-
|
585
|
+
when :float then :integer
|
586
|
+
when :timestamp then :datetime
|
587
|
+
else type
|
596
588
|
end
|
597
589
|
end
|
598
590
|
|
599
591
|
def value_to_attribute_type(value)
|
600
592
|
case value
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
593
|
+
when true, false then :boolean
|
594
|
+
when BigDecimal then :decimal
|
595
|
+
when Integer then :integer
|
596
|
+
when Date then :date
|
597
|
+
when DateTime then :datetime
|
598
|
+
when Time then :time
|
599
|
+
else :unknown
|
608
600
|
end
|
609
601
|
end
|
610
602
|
|
@@ -616,20 +608,10 @@ EOT
|
|
616
608
|
@options[:allow_nil] && disallows_value_of(nil)
|
617
609
|
end
|
618
610
|
|
619
|
-
def allows_blank_value?
|
620
|
-
@options[:allow_blank] != true ||
|
621
|
-
BLANK_VALUES.all? { |value| allows_value_of(value) }
|
622
|
-
end
|
623
|
-
|
624
|
-
def disallows_blank_value?
|
625
|
-
@options[:allow_blank] &&
|
626
|
-
BLANK_VALUES.any? { |value| disallows_value_of(value) }
|
627
|
-
end
|
628
|
-
|
629
611
|
def inspected_array
|
630
612
|
Shoulda::Matchers::Util.inspect_values(@array).to_sentence(
|
631
|
-
two_words_connector:
|
632
|
-
last_word_connector:
|
613
|
+
two_words_connector: ' or ',
|
614
|
+
last_word_connector: ', or ',
|
633
615
|
)
|
634
616
|
end
|
635
617
|
end
|