Fix LocalizedNumber issues

This commit is contained in:
Matt-Yorkley
2021-04-10 15:17:44 +01:00
committed by Andy Brett
parent 67e41f4548
commit 4bd7f964d0

View File

@@ -13,7 +13,7 @@ module Spree
attributes.each do |attribute|
setter = "#{attribute}="
old_setter = instance_method(setter) if table_exists? && !column_names.include?(attribute.to_s)
old_setter = instance_method(setter) if non_activerecord_attribute?(attribute)
define_method(setter) do |number|
if Spree::Config.enable_localized_number? && Spree::LocalizedNumber.valid_localizable_number?(number)
@@ -24,8 +24,10 @@ module Spree
number = nil
end
if has_attribute?(attribute)
# In this case it's a regular AR attribute with standard setters
self[attribute] = number
else
# In this case it's a Spree preference, and the interface is very different
old_setter.bind(self).call(number)
end
end
@@ -66,5 +68,15 @@ module Spree
# If does not end in ,00 / .00 then add trailing 00 to turn it into cents
number << "00" unless number =~ /^.*[\.,]\d{2}$/
end
private
def non_activerecord_attribute?(attribute)
table_exists? && !column_names.include?(attribute.to_s)
rescue ::ActiveRecord::NoDatabaseError
# This class is now loaded during `rake db:create` (since Rails 5.2), and not only does the
# table not exist, but the database does not even exist yet, and throws a fatal error.
# We can rescue and safely ignore it in that case.
end
end
end