can_has_validations 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39feb1ba720229383addaf65d761cf38ce5da9545b5e402cf3a9a064523b987a
4
- data.tar.gz: 8128047f635300ddae1771a1d997f22cb732391d79f4d49bc7c2e47bab2c51a4
3
+ metadata.gz: f14277d359dfd7f7a00e948c866c90497ac39d025fc753c83e0770c66102654b
4
+ data.tar.gz: 747a36b1d4e4c3723e5bb3455b9f5023570a1c0913520a1c21a5a9d18cf6ebae
5
5
  SHA512:
6
- metadata.gz: 212be440432bb1ff47193b1948a603138d01e813fd88d0978c8fd8c9ab1d61d39dee23914a1aa0b91abbd955a520b991c609b0a597571e253edfa3242f8006b2
7
- data.tar.gz: 190a03e36354954c46b9a1b4e45e22197cad6b6819565320446f55fb9e6f6ed3cbfe42d038afd141ba9ca61f087da392c84464636a7c5ce12bea3035c38b01e7
6
+ metadata.gz: b6891aa12d3765c1338807e0b6de3fa799cacbe7c0eb44f1b8685b105b5cb5dc11eea40962ed637fd00e1cd1d68f1419fedae3ecd3bc4dfafd64bad441afb3e6
7
+ data.tar.gz: d92195ac58fa2ea3c9bae481b9dbdbcaccc0a2a8afa23c6607b46d1917d636500fd6c410c11cb3839d79ecfbd724fc57193555ce35e5ca728cf9804a274210ba
data/README.md CHANGED
@@ -159,7 +159,9 @@ TLD, so as to not fail as ICANN continues to add TLDs.
159
159
 
160
160
  Ensures two attribute values maintain a relative order to one another. This is
161
161
  often useful when two date or range values. Validations can be written using
162
- either `:before` or `:after` to make them readable.
162
+ either `:before` or `:after` to make them readable. The special value of `:now`
163
+ will automatically become Time.now (without needing a lambda).
164
+
163
165
 
164
166
  Always skips over nil values; use `:presence` to validate those.
165
167
 
@@ -167,7 +169,9 @@ Always skips over nil values; use `:presence` to validate those.
167
169
  validates :start_at, before: :finish_at
168
170
  validates :finish_at, after: [:start_at, :alt_start_at]
169
171
  validates :start_at, presence: true, before: :finish_at
170
- validates :finish_at, after: ->(r){ Time.current }
172
+ # These two are the same, except `:now` produces a clearer error message:
173
+ validates :finish_at, after: :now
174
+ validates :finish_at, after: ->(r){ Time.now }
171
175
 
172
176
  # Long versions, if you need to add extra validation options:
173
177
  validates :start_at, before: {value_of: :finish_at, message: "..." }
@@ -229,4 +233,4 @@ Default messages are as follows:
229
233
 
230
234
  ## Compatibility ##
231
235
 
232
- Tested with Ruby 2.3-2.4 and ActiveSupport and ActiveModel 4.2-5.1.
236
+ Tested with Ruby 2.3-2.5 and ActiveSupport and ActiveModel 4.2-5.2.
@@ -28,7 +28,7 @@ module ActiveModel
28
28
  def initialize(options)
29
29
  record_class = options[:class]
30
30
  super
31
- record_class.prepend DefaultKeys
31
+ record_class.extend DefaultKeys
32
32
 
33
33
  defaults = @options.dup
34
34
  validations = defaults.slice!(*record_class.send(:_validates_default_keys), :attributes)
@@ -37,8 +37,8 @@ module ActiveModel
37
37
 
38
38
  defaults[:attributes] = attributes
39
39
 
40
- @validators = validations.map do |key, options|
41
- next unless options
40
+ @validators = validations.map do |key, sub_options|
41
+ next unless sub_options
42
42
  key = "#{key.to_s.camelize}Validator"
43
43
 
44
44
  begin
@@ -47,7 +47,7 @@ module ActiveModel
47
47
  raise ArgumentError, "Unknown validator: '#{key}'"
48
48
  end
49
49
 
50
- validator = klass.new(defaults.merge(_parse_validates_options(options)))
50
+ klass.new(defaults.merge(_parse_validates_options(sub_options)))
51
51
  end
52
52
  end
53
53
 
@@ -8,7 +8,7 @@ module ActiveModel::Validations
8
8
 
9
9
  def validate_each(record, attribute, value)
10
10
  unless value =~ EMAIL_REGEXP
11
- record.errors.add(attribute, :invalid_email, options)
11
+ record.errors.add(attribute, :invalid_email, options.merge(value: value))
12
12
  end
13
13
  end
14
14
  end
@@ -20,7 +20,7 @@ module ActiveModel::Validations
20
20
  end
21
21
  end
22
22
  unless all_match
23
- record.errors.add(attribute, :invalid, options.except(:allow_nil, :parent, :scope))
23
+ record.errors.add(attribute, :invalid, options.except(:allow_nil, :parent, :scope).merge!(value: value))
24
24
  end
25
25
  end
26
26
  end
@@ -30,6 +30,7 @@ module ActiveModel::Validations
30
30
 
31
31
  LABEL_REGEXP = /\A([a-zA-Z0-9_]([a-zA-Z0-9_-]+)?)?[a-zA-Z0-9]\z/
32
32
  FINAL_LABEL_REGEXP = /\A(xn--[a-zA-Z0-9]{2,}|[a-zA-Z]{2,})\z/
33
+ RESERVED_OPTIONS = %i(allow_ip allow_underscore allow_wildcard)
33
34
 
34
35
  def validate_each(record, attribute, value)
35
36
  case options[:allow_ip]
@@ -65,7 +66,7 @@ module ActiveModel::Validations
65
66
  end
66
67
 
67
68
  unless is_valid
68
- record.errors.add(attribute, :invalid_hostname, options)
69
+ record.errors.add(attribute, :invalid_hostname, options.except(*RESERVED_OPTIONS).merge!(value: value))
69
70
  end
70
71
  end
71
72
 
@@ -1,10 +1,11 @@
1
1
  # Attribute ordering
2
2
  # Ensures one value is greater or lesser than another (set of) value(s).
3
+ # The special value of :now will automatically become Time.now (without needing a lambda).
3
4
  # Always skips over nil values; use :presence to validate those.
4
5
  # eg: validates :start_at, before: :finish_at
5
6
  # validates :start_at, before: {value_of: :finish_at, if: ... }
6
- # validates :finish_at, after: [:start_at, :alt_start_at]
7
- # validates :finish_at, after: {values_of: [:start_at, :alt_start_at], if: ... }
7
+ # validates :finish_at, after: [:start_at, :now]
8
+ # validates :finish_at, after: {values_of: [:start_at, :now], if: ... }
8
9
 
9
10
  module ActiveModel::Validations
10
11
  class BeforeValidator < ActiveModel::EachValidator
@@ -12,11 +13,12 @@ module ActiveModel::Validations
12
13
  compare_to = Array.wrap(options[:value_of] || options[:values_of] || options[:in] || options[:with])
13
14
  compare_to.each do |attr_name|
14
15
  greater = attr_name.call(record) if attr_name.respond_to?(:call)
16
+ greater ||= Time.now if attr_name==:now && !record.respond_to?(:now)
15
17
  greater ||= record.send attr_name
16
18
  next unless value && greater
17
19
  unless value < greater
18
- attr2 = record.class.human_attribute_name attr_name
19
- record.errors.add(attribute, :before, options.except(:before).merge!(attribute2: attr2))
20
+ attr2 = attr_name.respond_to?(:call) ? 'it is' : record.class.human_attribute_name(attr_name)
21
+ record.errors.add(attribute, :before, options.except(:before).merge!(attribute2: attr2, value: value))
20
22
  end
21
23
  end
22
24
  end
@@ -26,11 +28,12 @@ module ActiveModel::Validations
26
28
  compare_to = Array.wrap(options[:value_of] || options[:values_of] || options[:in] || options[:with])
27
29
  compare_to.each do |attr_name|
28
30
  lesser = attr_name.call(record) if attr_name.respond_to?(:call)
31
+ lesser ||= Time.now if attr_name==:now && !record.respond_to?(:now)
29
32
  lesser ||= record.send attr_name
30
33
  next unless value && lesser
31
34
  unless value > lesser
32
- attr2 = record.class.human_attribute_name attr_name
33
- record.errors.add(attribute, :after, options.except(:after).merge!(attribute2: attr2))
35
+ attr2 = attr_name.respond_to?(:call) ? 'it is' : record.class.human_attribute_name(attr_name)
36
+ record.errors.add(attribute, :after, options.except(:after).merge!(attribute2: attr2, value: value))
34
37
  end
35
38
  end
36
39
  end
@@ -8,7 +8,7 @@ module ActiveModel::Validations
8
8
  class UrlValidator < ActiveModel::EachValidator
9
9
  def validate_each(record, attribute, value)
10
10
  allowed_schemes = Array.wrap(options[:scheme] || %w(http https))
11
-
11
+
12
12
  if defined?(Addressable::URI)
13
13
  u = Addressable::URI.parse(value) rescue nil
14
14
  u2 = u && URI.parse(u.normalize.to_s) rescue nil
@@ -16,7 +16,7 @@ module ActiveModel::Validations
16
16
  u2 = u = URI.parse(value) rescue nil
17
17
  end
18
18
  if !u || !u2 || u.relative? || allowed_schemes.exclude?(u.scheme)
19
- record.errors.add(attribute, :invalid_url, options)
19
+ record.errors.add(attribute, :invalid_url, options.merge(value: value))
20
20
  end
21
21
  end
22
22
  end
@@ -20,7 +20,7 @@ module ActiveModel::Validations
20
20
  def validate_each(record, attribute, value)
21
21
  if record.persisted? && record.send("#{attribute}_changed?")
22
22
  if options[:immutable_nil] || !record.send("#{attribute}_was").nil?
23
- record.errors.add(attribute, :unchangeable, options)
23
+ record.errors.add(attribute, :unchangeable, options.except(:immutable_nil).merge!(value: value))
24
24
  end
25
25
  end
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module CanHasValidations
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
3
3
  end
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: can_has_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thomas morgan
@@ -90,6 +90,7 @@ files:
90
90
  - test/dummy/config/initializers/wrap_parameters.rb
91
91
  - test/dummy/config/locales/en.yml
92
92
  - test/dummy/config/routes.rb
93
+ - test/dummy/log/test.log
93
94
  - test/dummy/public/404.html
94
95
  - test/dummy/public/422.html
95
96
  - test/dummy/public/500.html
@@ -142,6 +143,7 @@ test_files:
142
143
  - test/dummy/config/locales/en.yml
143
144
  - test/dummy/config/routes.rb
144
145
  - test/dummy/config.ru
146
+ - test/dummy/log/test.log
145
147
  - test/dummy/public/404.html
146
148
  - test/dummy/public/422.html
147
149
  - test/dummy/public/500.html