Extend NumericalityValidator to check precision and scale

Use new checks on Unit.multiplier
Closes #28
This commit is contained in:
cryptogopher 2024-12-07 20:16:43 +01:00
parent 25ac126df9
commit 15a5515c99
5 changed files with 27 additions and 2 deletions

View File

@ -13,7 +13,7 @@ class Unit < ApplicationRecord
length: {maximum: columns_hash['symbol'].limit}
validates :description, length: {maximum: columns_hash['description'].limit}
validates :multiplier, numericality: {equal_to: 1}, unless: :base
validates :multiplier, numericality: {other_than: 0}, if: :base
validates :multiplier, numericality: {other_than: 0, precision: true, scale: true}, if: :base
scope :defaults, ->{ where(user: nil) }
scope :defaults_diff, ->{

View File

@ -13,7 +13,8 @@
<td>
<% unless @unit.base.nil? %>
<%= form.hidden_field :base_id, form: :unit_form %>
<%= form.number_field :multiplier, form: :unit_form, required: true, step: "any",
<%= form.number_field :multiplier, form: :unit_form, required: true,
step: BigDecimal(10).power(-@unit.class.type_for_attribute(:multiplier).scale),
size: 10, autocomplete: "off" %>
<% end %>
</td>

View File

@ -1,5 +1,9 @@
require 'core_ext/big_decimal_scientific_notation'
ActiveSupport.on_load :active_record do
ActiveModel::Validations::NumericalityValidator.prepend CoreExt::ActiveModel::Validations::NumericalityValidatesPrecisionAndScale
end
ActiveSupport.on_load :action_dispatch_system_test_case do
prepend CoreExt::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelperUniqueId
end

View File

@ -1,4 +1,8 @@
en:
errors:
messages:
precision_exceeded: must not exceed %{value} significant digits
scale_exceeded: must not exceed %{value} decimal digits
activerecord:
attributes:
unit:

View File

@ -0,0 +1,16 @@
module CoreExt::ActiveModel::Validations::NumericalityValidatesPrecisionAndScale
def validate_each(record, attr_name, value, ...)
super(record, attr_name, value, ...)
if options[:precision] || options[:scale]
attr_type = record.class.type_for_attribute(attr_name)
value = BigDecimal(value) unless value.is_a? BigDecimal
if options[:precision] && (value.precision > attr_type.precision)
record.errors.add(attr_name, :precision_exceeded, **filtered_options(attr_type.precision))
end
if options[:scale] && (value.scale > attr_type.scale)
record.errors.add(attr_name, :scale_exceeded, **filtered_options(attr_type.scale))
end
end
end
end