From a0cc6b7551389f319f8cde998de9677e2663af0f Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Sat, 28 Mar 2020 14:14:57 +0100 Subject: [PATCH] Validate uniqueness with case_sensitive: true explicitly This will fix the following deprecation warning DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :attr attribute in Model model, pass `case_sensitive: true` option explicitly to the uniqueness validator. Please note that this is not needed for non-text columns since this option is ignored in that case, see: https://github.com/rails/rails/blob/5665d0867bb34b941db778630d558c7694bb1506/activerecord/lib/active_record/validations/uniqueness.rb#L143-L144 --- core/app/models/spree/option_type.rb | 2 +- core/app/models/spree/option_value.rb | 2 +- core/app/models/spree/order.rb | 2 +- core/app/models/spree/preference.rb | 2 +- core/app/models/spree/product.rb | 2 +- core/app/models/spree/promotion.rb | 2 +- core/app/models/spree/promotion_code.rb | 2 +- core/app/models/spree/role.rb | 2 +- core/app/models/spree/store.rb | 2 +- core/app/models/spree/tax_category.rb | 2 +- core/app/models/spree/variant.rb | 2 +- core/app/models/spree/zone.rb | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/app/models/spree/option_type.rb b/core/app/models/spree/option_type.rb index 861678e0ceb..979f846f0ba 100644 --- a/core/app/models/spree/option_type.rb +++ b/core/app/models/spree/option_type.rb @@ -15,7 +15,7 @@ class OptionType < Spree::Base has_many :product_option_types, dependent: :destroy, inverse_of: :option_type has_many :products, through: :product_option_types - validates :name, presence: true, uniqueness: { allow_blank: true } + validates :name, presence: true, uniqueness: { allow_blank: true, case_sensitive: true } validates :presentation, presence: true default_scope -> { order(:position) } diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rb index c25574b842a..4d529b4d0ef 100644 --- a/core/app/models/spree/option_value.rb +++ b/core/app/models/spree/option_value.rb @@ -8,7 +8,7 @@ class OptionValue < Spree::Base has_many :option_values_variants, dependent: :destroy has_many :variants, through: :option_values_variants - validates :name, presence: true, uniqueness: { scope: :option_type_id, allow_blank: true } + validates :name, presence: true, uniqueness: { scope: :option_type_id, allow_blank: true, case_sensitive: true } validates :presentation, presence: true after_save :touch, if: :saved_changes? diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 4a0f28ed48c..470f0304bb0 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -126,7 +126,7 @@ def states validates :email, presence: true, if: :require_email validates :email, 'spree/email' => true, allow_blank: true validates :guest_token, presence: { allow_nil: true } - validates :number, presence: true, uniqueness: { allow_blank: true } + validates :number, presence: true, uniqueness: { allow_blank: true, case_sensitive: true } validates :store_id, presence: true def self.find_by_param(value) diff --git a/core/app/models/spree/preference.rb b/core/app/models/spree/preference.rb index ac318a251a9..ec03f9ef9c5 100644 --- a/core/app/models/spree/preference.rb +++ b/core/app/models/spree/preference.rb @@ -3,5 +3,5 @@ class Spree::Preference < Spree::Base serialize :value - validates :key, presence: true, uniqueness: { allow_blank: true } + validates :key, presence: true, uniqueness: { allow_blank: true, case_sensitive: true } end diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb index 1bcc1cac57c..176557f37a2 100644 --- a/core/app/models/spree/product.rb +++ b/core/app/models/spree/product.rb @@ -115,7 +115,7 @@ def find_or_build_master validates :name, presence: true validates :price, presence: true, if: proc { Spree::Config[:require_master_price] } validates :shipping_category_id, presence: true - validates :slug, presence: true, uniqueness: { allow_blank: true } + validates :slug, presence: true, uniqueness: { allow_blank: true, case_sensitive: true } attr_accessor :option_values_hash diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb index 299a64974e9..4cbaac68a0d 100644 --- a/core/app/models/spree/promotion.rb +++ b/core/app/models/spree/promotion.rb @@ -28,7 +28,7 @@ class Promotion < Spree::Base validates_associated :rules validates :name, presence: true - validates :path, uniqueness: { allow_blank: true } + validates :path, uniqueness: { allow_blank: true, case_sensitive: true } validates :usage_limit, numericality: { greater_than: 0, allow_nil: true } validates :per_code_usage_limit, numericality: { greater_than_or_equal_to: 0, allow_nil: true } validates :description, length: { maximum: 255 } diff --git a/core/app/models/spree/promotion_code.rb b/core/app/models/spree/promotion_code.rb index a6afc02e8eb..9c3dcca9d12 100644 --- a/core/app/models/spree/promotion_code.rb +++ b/core/app/models/spree/promotion_code.rb @@ -5,7 +5,7 @@ class Spree::PromotionCode < Spree::Base belongs_to :promotion_code_batch, class_name: "Spree::PromotionCodeBatch", optional: true has_many :adjustments - validates :value, presence: true, uniqueness: { allow_blank: true } + validates :value, presence: true, uniqueness: { allow_blank: true, case_sensitive: true } validates :promotion, presence: true before_save :normalize_code diff --git a/core/app/models/spree/role.rb b/core/app/models/spree/role.rb index 113e72e5d13..3e2d7d1a234 100644 --- a/core/app/models/spree/role.rb +++ b/core/app/models/spree/role.rb @@ -5,7 +5,7 @@ class Role < Spree::Base has_many :role_users, class_name: "Spree::RoleUser", dependent: :destroy has_many :users, through: :role_users - validates_uniqueness_of :name + validates_uniqueness_of :name, case_sensitive: true def admin? name == "admin" diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb index 267514550cf..bb0d4e70d52 100644 --- a/core/app/models/spree/store.rb +++ b/core/app/models/spree/store.rb @@ -17,7 +17,7 @@ class Store < Spree::Base has_many :orders, class_name: "Spree::Order" - validates :code, presence: true, uniqueness: { allow_blank: true } + validates :code, presence: true, uniqueness: { allow_blank: true, case_sensitive: true } validates :name, presence: true validates :url, presence: true validates :mail_from_address, presence: true diff --git a/core/app/models/spree/tax_category.rb b/core/app/models/spree/tax_category.rb index 5eac0340607..f1d75da1910 100644 --- a/core/app/models/spree/tax_category.rb +++ b/core/app/models/spree/tax_category.rb @@ -9,7 +9,7 @@ class TaxCategory < Spree::Base end validates :name, presence: true - validates_uniqueness_of :name, unless: :deleted_at + validates_uniqueness_of :name, case_sensitive: true, unless: :deleted_at has_many :tax_rate_tax_categories, class_name: 'Spree::TaxRateTaxCategory', diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index 08e972f20ce..a09617a239e 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -74,7 +74,7 @@ class Variant < Spree::Base validates :cost_price, numericality: { greater_than_or_equal_to: 0, allow_nil: true } validates :price, numericality: { greater_than_or_equal_to: 0, allow_nil: true } - validates_uniqueness_of :sku, allow_blank: true, if: :enforce_unique_sku? + validates_uniqueness_of :sku, allow_blank: true, case_sensitive: true, if: :enforce_unique_sku? after_create :create_stock_items after_create :set_position diff --git a/core/app/models/spree/zone.rb b/core/app/models/spree/zone.rb index 8d27d80e641..f1075f4b5f4 100644 --- a/core/app/models/spree/zone.rb +++ b/core/app/models/spree/zone.rb @@ -13,7 +13,7 @@ class Zone < Spree::Base has_many :shipping_method_zones, dependent: :destroy has_many :shipping_methods, through: :shipping_method_zones - validates :name, presence: true, uniqueness: { allow_blank: true } + validates :name, presence: true, uniqueness: { allow_blank: true, case_sensitive: true } after_save :remove_defunct_members scope :with_member_ids, ->(state_ids, country_ids) do