measured 3.1.0 → 3.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b304fbf20408beb13b8408d74c17747f00ccb991b586775b91249f2e90e9073
4
- data.tar.gz: d8df7e3a16743234d79b1d8375f2f6d83e69f18824fdd1ff95c530fdd333d3a3
3
+ metadata.gz: d08724bae7ca2587e6ac427df7588e08fcec42dfecbd5d0fd9858560b7d1ec1f
4
+ data.tar.gz: aa934b2932b9df4648acbd6d65378fdd00add45840834e5ef9119bfd2e627ff9
5
5
  SHA512:
6
- metadata.gz: 3f7e92b9cdc9165c11dca6857ae9bbc141b938eb071e89f56ca1e13198fc6d281bde0abe4d3436d2f102d869057c5b1f60562bdcb2f9376739a4927b11ccb248
7
- data.tar.gz: 4b3d830a1814b6b64dd9123a40e444918adf3a546b925bb57088bc2c4324da0ccd7040253a838d24f0c53ea1c749cbade548dee29f2995678be903f87af04dcf
6
+ metadata.gz: 5aea963586e68d4806e06d98993b2122b13fd19c79a825c035d413b908c8d30213d7fe4d23d4485b7cfdcd63c448db98150f11941bbb32d461a773e781ed5120
7
+ data.tar.gz: 55dd6b127ae8dcba52fca728246116a55274549bf0ee0ac31931a12393783554cb05dd5b7521714573e9d3c24350f6c88883a30a0110921e1594325660156f42
data/CHANGELOG.md CHANGED
@@ -1,6 +1,9 @@
1
1
  Unreleased
2
2
  -----
3
3
 
4
+ 3.2.0
5
+ -----
6
+ * Make the ActiveRecord validation errors have the correct type. (@alexcarruthers)
4
7
 
5
8
  3.1.0
6
9
  -----
@@ -24,11 +24,11 @@ class MeasuredValidator < ActiveModel::EachValidator
24
24
  return unless measurable_unit_name.present? || measurable_value.present?
25
25
 
26
26
  measurable_unit = measured_class.unit_system.unit_for(measurable_unit_name)
27
- record.errors.add(attribute, message(record, "is not a valid unit")) unless measurable_unit
27
+ record.errors.add(attribute, :invalid, message: message(record, "is not a valid unit")) unless measurable_unit
28
28
 
29
29
  if options[:units] && measurable_unit.present?
30
30
  valid_units = Array(options[:units]).map { |unit| measured_class.unit_system.unit_for(unit) }
31
- record.errors.add(attribute, message(record, "is not a valid unit")) unless valid_units.include?(measurable_unit)
31
+ record.errors.add(attribute, :invalid, message: message(record, "is not a valid unit")) unless valid_units.include?(measurable_unit)
32
32
  end
33
33
 
34
34
  if measurable_unit && measurable_value.present?
@@ -36,7 +36,7 @@ class MeasuredValidator < ActiveModel::EachValidator
36
36
  comparable_value = value_for(value, record)
37
37
  comparable_value = measured_class.new(comparable_value, measurable_unit) unless comparable_value.is_a?(Measured::Measurable)
38
38
  unless measurable.public_send(CHECKS[option], comparable_value)
39
- record.errors.add(attribute, message(record, "#{measurable.to_s} must be #{CHECKS[option]} #{comparable_value}"))
39
+ record.errors.add(attribute, option, message: message(record, "#{measurable.to_s} must be #{CHECKS[option]} #{comparable_value}"))
40
40
  end
41
41
  end
42
42
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Measured
3
- VERSION = "3.1.0"
3
+ VERSION = "3.2.0"
4
4
  end
@@ -39,6 +39,12 @@ class Measured::Rails::ValidationTest < ActiveSupport::TestCase
39
39
  assert_equal ["Length is not a valid unit"], thing.errors.full_messages
40
40
  end
41
41
 
42
+ test "validation sets error codes when unit is invalid" do
43
+ thing.length_unit = "junk"
44
+ refute thing.valid?
45
+ assert thing.errors.of_kind?(:length, :invalid)
46
+ end
47
+
42
48
  test "validation can override the message with a static string" do
43
49
  thing.length_message_unit = "junk"
44
50
  refute thing.valid?
@@ -181,6 +187,28 @@ class Measured::Rails::ValidationTest < ActiveSupport::TestCase
181
187
  refute thing.valid?
182
188
  end
183
189
 
190
+ test "validation for numericality puts the proper error types" do
191
+ thing.length_numericality_inclusive_value = 5
192
+ refute thing.valid?
193
+ assert thing.errors.of_kind?(:length_numericality_inclusive, :greater_than_or_equal_to)
194
+
195
+ thing.length_numericality_inclusive_value = 25
196
+ refute thing.valid?
197
+ assert thing.errors.of_kind?(:length_numericality_inclusive, :less_than_or_equal_to)
198
+
199
+ thing.length_numericality_exclusive_value = 2
200
+ refute thing.valid?
201
+ assert thing.errors.of_kind?(:length_numericality_exclusive, :greater_than)
202
+
203
+ thing.length_numericality_exclusive_value = 550
204
+ refute thing.valid?
205
+ assert thing.errors.of_kind?(:length_numericality_exclusive, :less_than)
206
+
207
+ thing.length_numericality_equality_value = 200
208
+ refute thing.valid?
209
+ assert thing.errors.of_kind?(:length_numericality_equality, :equal_to)
210
+ end
211
+
184
212
  test "validation for numericality handles a nil unit but a valid value" do
185
213
  thing.length_numericality_exclusive_unit = nil
186
214
  thing.length_numericality_exclusive_value = 1
metadata CHANGED
@@ -1,16 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measured
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips
8
8
  - Jason Gedge
9
9
  - Javier Honduvilla Coto
10
- autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2024-10-03 00:00:00.000000000 Z
12
+ date: 2025-01-28 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: activesupport
@@ -227,7 +226,6 @@ licenses:
227
226
  - MIT
228
227
  metadata:
229
228
  allowed_push_host: https://rubygems.org
230
- post_install_message:
231
229
  rdoc_options: []
232
230
  require_paths:
233
231
  - lib
@@ -242,8 +240,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
242
240
  - !ruby/object:Gem::Version
243
241
  version: '0'
244
242
  requirements: []
245
- rubygems_version: 3.5.20
246
- signing_key:
243
+ rubygems_version: 3.6.3
247
244
  specification_version: 4
248
245
  summary: Encapsulate measurements with their units in Ruby
249
246
  test_files: