Skip to content

Commit

Permalink
Fix deprecations for uniqueness validator (case-sensitivity)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayorova committed Aug 10, 2023
1 parent 6b835bd commit 908be5d
Show file tree
Hide file tree
Showing 30 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/lib/system_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def validates_system_name(opts = {})

return unless opts[:uniqueness_scope]

validates :system_name, uniqueness: opts[:uniqueness_scope] == true ? true : { scope: opts[:uniqueness_scope] }
validates :system_name, uniqueness: opts[:uniqueness_scope] == true ? { case_sensitive: true } : { case_sensitive: true, scope: opts[:uniqueness_scope] }
end

def has_system_name(opts = {})
Expand Down
2 changes: 1 addition & 1 deletion app/models/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def self.allowed_scopes
end

validates :owner, :value, :name, :permission, presence: true
validates :value, uniqueness: { scope: [:owner_id] }, length: { maximum: 255 }
validates :value, uniqueness: { scope: [:owner_id], case_sensitive: true }, length: { maximum: 255 }
validates :permission, inclusion: { in: PERMISSIONS.values }, length: { maximum: 255 }
validates :scopes, length: { minimum: 1, maximum: 65535 }
validate :validate_scope_exists
Expand Down
2 changes: 1 addition & 1 deletion app/models/alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Alert < ApplicationRecord

validates :account, :timestamp, :state, presence: true
validates :utilization, :level, :alert_id, :cinstance, presence: true
validates :alert_id, uniqueness: { :scope => :account_id }
validates :alert_id, uniqueness: { scope: :account_id, case_sensitive: true }
validates :level, inclusion: { :in => ALERT_LEVELS }
validates :level, numericality: { :only_integer => true }
validates :utilization, numericality: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/authentication_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AuthenticationProvider < ApplicationRecord
enum account_type: {developer: 'developer', provider: 'provider'}

has_system_name uniqueness_scope: [:account_id]
validates :kind, uniqueness: { scope: %i[account_id account_type] }, if: :developer?
validates :kind, uniqueness: { scope: %i[account_id account_type], case_sensitive: true }, if: :developer?
validate :verify_valid_kind_for_account_type
validates :name, presence: true, length: { maximum: 255 }
validates :identifier_key, presence: true, length: { maximum: 255 }
Expand Down
2 changes: 1 addition & 1 deletion app/models/backend_api_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BackendApiConfig < ApplicationRecord
has_many :backend_api_metrics, through: :backend_api, source: :metrics

validates :service_id, :backend_api_id, presence: true
validates :backend_api_id, uniqueness: { scope: :service_id }
validates :backend_api_id, uniqueness: { scope: :service_id, case_sensitive: true }
validates :path, uniqueness: { scope: :service_id, case_sensitive: false, message: "This path is already taken. Specify a different path." }
validates :path, length: { in: 1..255, allow_nil: false }, path: true

Expand Down
4 changes: 2 additions & 2 deletions app/models/cinstance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ def validate_plan_is_unique?

validate :plan_is_unique, if: :validate_plan_is_unique?
validate :application_id_is_unique, if: :validate_application_id_is_unique?
validates :application_id, uniqueness: { scope: [:service_id] }, unless: :validate_application_id_is_unique?
validates :application_id, uniqueness: { scope: [:service_id], case_sensitive: true }, unless: :validate_application_id_is_unique?

validate :user_key_is_unique, unless: :provider_can_duplicate_user_key?

validates :user_key, uniqueness: { scope: [:service_id] }, if: :provider_can_duplicate_user_key?
validates :user_key, uniqueness: { scope: [:service_id], case_sensitive: true }, if: :provider_can_duplicate_user_key?

validate :same_service, on: :update, if: :plan_id_changed?

Expand Down
2 changes: 1 addition & 1 deletion app/models/cms/builtin/legal_term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class CMS::Builtin::LegalTerm < CMS::Builtin::Partial
NEW_APPLICATION_SYSTEM_NAME = 'new_application_licence'

validates :published, presence: true
validates :title, uniqueness: { :scope => [:provider_id] }
validates :title, uniqueness: { :scope => [:provider_id], case_sensitive: true }

def title
I18n.t("builtin_legal_terms.#{system_name}.title")
Expand Down
2 changes: 1 addition & 1 deletion app/models/cms/email_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CMS::EmailTemplate < CMS::Template

validates :system_name, presence: true
validates :current, presence: true
validates :system_name, uniqueness: { scope: %i[provider_id], allow_blank: true }
validates :system_name, uniqueness: { scope: %i[provider_id], allow_blank: true, case_sensitive: true }
validate :headers_formats

class_attribute :templates_path
Expand Down
2 changes: 1 addition & 1 deletion app/models/cms/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CMS::File < ApplicationRecord
validates_attachment :attachment, presence: true
do_not_validate_attachment_file_type :attachment

validates :path, uniqueness: { :scope => :provider_id }, length: { maximum: 255 }
validates :path, uniqueness: { scope: :provider_id, case_sensitive: true }, length: { maximum: 255 }
validates :attachment_content_type, :attachment_file_name, :random_secret, :name,
length: { maximum: 255 }

Expand Down
2 changes: 1 addition & 1 deletion app/models/cms/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CMS::Group < ApplicationRecord
belongs_to :provider, :class_name => "Account"

validates :name, :provider, presence: true, length: { maximum: 255 }
validates :name, uniqueness: { scope: [:provider_id] }
validates :name, uniqueness: { scope: [:provider_id], case_sensitive: true }

has_many :group_sections, :class_name => 'CMS::GroupSection'
has_many :sections, :class_name => 'CMS::Section', :through => :group_sections
Expand Down
2 changes: 1 addition & 1 deletion app/models/cms/redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CMS::Redirect < ApplicationRecord
belongs_to :provider, :class_name => 'Account'

validates :source, :target, :provider, presence: true
validates :source, uniqueness: { scope: [:provider_id] }, length: { maximum: 255 }
validates :source, uniqueness: { scope: [:provider_id], case_sensitive: true }, length: { maximum: 255 }
validates :target, length: { maximum: 255 }

attr_accessible :source, :target
Expand Down
4 changes: 2 additions & 2 deletions app/models/cms/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class CMS::Section < ApplicationRecord
validates :title, :system_name, :provider, presence: true
validates :parent_id, presence: { :unless => :root? }

validates :title, uniqueness: { :scope => [:provider_id, :parent_id] }
validates :system_name, uniqueness: { :scope => [:provider_id] }, length: { maximum: 255 }
validates :title, uniqueness: { scope: [:provider_id, :parent_id], case_sensitive: true }
validates :system_name, uniqueness: { scope: [:provider_id], case_sensitive: true }, length: { maximum: 255 }
validates :partial_path, :title, :type, length: { maximum: 255 }

before_validation :set_system_name , on: %i[create update]
Expand Down
2 changes: 1 addition & 1 deletion app/models/cms/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CMS::Template < ApplicationRecord
has_many :versions, as: :template

validates :provider, presence: true
validates :system_name, uniqueness: { :scope => [:provider_id, :type], :allow_blank => true },
validates :system_name, uniqueness: { scope: [:provider_id, :type], allow_blank: true, case_sensitive: true },
format: { :with => /\A\w[\w\-\/_]+\z/, :allow_blank => true }, length: { maximum: 255 }
validates :handler, inclusion: { in: CMS::Handler.available, allow_blank: false, allow_nil: true },
length: { maximum: 255 }
Expand Down
2 changes: 1 addition & 1 deletion app/models/country.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Country < ApplicationRecord
validates :name, :code, presence: true
validates :code, uniqueness: true
validates :code, uniqueness: { case_sensitive: true }
validates :name, :code, :currency, length: {maximum: 255}

# Cuba, Iran, North Korea, Sudan, Syria
Expand Down
2 changes: 1 addition & 1 deletion app/models/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Feature < ApplicationRecord

attr_protected :featurable_id, :featurable_type, :tenant_id, :audit_ids

validates :system_name, uniqueness: { :scope => [:featurable_id, :featurable_type] }
validates :system_name, uniqueness: { scope: [:featurable_id, :featurable_type], case_sensitive: true }
validates :system_name, :name, length: { maximum: 255 }

def hide!
Expand Down
2 changes: 1 addition & 1 deletion app/models/fields_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.editable_by(user)

validates :label, :target, :name, presence: true, length: { maximum: 255 }
validates :name, format: { :with =>/\A[A-Za-z][A-Za-z\d_-]*\z/, :message => "Name should start with letters, and can contain numbers, - and _" }
validates :name, uniqueness: { :scope => [:account_id, :target] }
validates :name, uniqueness: { scope: [:account_id, :target], case_sensitive: true }
validates :name, :exclusion => Fields::ExtraField::InputField.excludes
validates :choices, :hint, length: { maximum: 65535 }

Expand Down
2 changes: 1 addition & 1 deletion app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Invitation < ApplicationRecord
validates :account, presence: true
validates :email, presence: true
validate :email_is_not_taken
validates :email, uniqueness: { :scope => :account_id, :message => "This invitation has already been sent." } # Do you want to resend it?"
validates :email, uniqueness: { scope: :account_id, case_sensitive: true, message: "This invitation has already been sent." } # Do you want to resend it?"

#OPTIMIZE: use the same format validations in user, account and invitation, add TESTS!
validates :email, length: { :within => 6..100, :allow_blank => true } #[email protected]
Expand Down
2 changes: 1 addition & 1 deletion app/models/member_permission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class MemberPermission < ApplicationRecord
include Symbolize
belongs_to :user, touch: true
validates :admin_section, inclusion: { :in => ->(_record) { AdminSection.sections } }
validates :admin_section , uniqueness: { :scope => :user_id, if: Proc.new { |mp| mp.user_id } }
validates :admin_section , uniqueness: { scope: :user_id, case_sensitive: true, if: Proc.new { |mp| mp.user_id } }
serialize :service_ids, JSON

symbolize :admin_section
Expand Down
2 changes: 1 addition & 1 deletion app/models/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Metric < ApplicationRecord

attr_protected :service_id, :parent_id, :tenant_id, :audit_ids
validates :unit, presence: true, unless: :child?
validates :friendly_name, uniqueness: {scope: %i[owner_type owner_id]}, presence: true
validates :friendly_name, uniqueness: { scope: %i[owner_type owner_id], case_sensitive: true }, presence: true
validates :system_name, :unit, :friendly_name, :owner_type, length: { maximum: 255 }
validates :owner, presence: true

Expand Down
2 changes: 1 addition & 1 deletion app/models/payment_intent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PaymentIntent < ApplicationRecord

validates :invoice, :reference, :state, presence: true
validates :reference, :state, length: { maximum: 255 }
validates :reference, uniqueness: true
validates :reference, uniqueness: { case_sensitive: true }

scope :latest, ->(count = 1) { reorder(created_at: :desc, id: :desc).limit(count) }
scope :pending, ->() { where.not(state: SUCCEEDED_STATES) }
Expand Down
4 changes: 2 additions & 2 deletions app/models/policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Policy < ApplicationRecord

belongs_to :account, inverse_of: :policies

validates :version, uniqueness: { scope: %i[account_id name] }
validates :version, uniqueness: { scope: %i[account_id name], case_sensitive: true }
validates :name, :version, :account_id, :schema, presence: true
validate :belongs_to_a_tenant
validate :validate_schema_specification
Expand All @@ -26,7 +26,7 @@ def schema=(value)
end

before_validation :set_identifier
validates :identifier, uniqueness: { scope: :account_id }
validates :identifier, uniqueness: { scope: :account_id, case_sensitive: true }
validates :name, :version, length: { maximum: 255 }

def self.find_by_id_or_name_version(id_or_name_version)
Expand Down
2 changes: 1 addition & 1 deletion app/models/referrer_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ReferrerFilter < ApplicationRecord

validates :application, presence: true
validates :value, presence: true
validates :value, uniqueness: { scope: [:application_id] }
validates :value, uniqueness: { scope: [:application_id], case_sensitive: true }
validates :value, format: { with: /\A[a-zA-Z0-9*.-]+\z/ }, length: { maximum: 255 }

validate :keys_limit_reached
Expand Down
2 changes: 1 addition & 1 deletion app/models/sso_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SSOAuthorization < ApplicationRecord
has_many :user_sessions, dependent: :destroy

validates :uid, :authentication_provider, :user, presence: true
validates :uid, uniqueness: { scope: :authentication_provider_id }
validates :uid, uniqueness: { scope: :authentication_provider_id, case_sensitive: true }
validates :uid, length: { maximum: 255 }

scope :newest, -> { order(updated_at: :desc).first }
Expand Down
2 changes: 1 addition & 1 deletion app/models/system_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class SystemOperation < ApplicationRecord
has_many :messages
has_many :mail_dispatch_rules, :dependent => :destroy

validates :ref, uniqueness: true
validates :ref, uniqueness: { case_sensitive: true }
validates :ref, :name, length: {maximum: 255}

DEFAULTS = {'user_signup' => 'New user signup',
Expand Down
2 changes: 1 addition & 1 deletion app/models/topic_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TopicCategory < ApplicationRecord
attr_protected :forum_id, :tenant_id

validates :name, presence: true, length: {maximum: 255}
validates :name, uniqueness: { :scope => :forum_id }
validates :name, uniqueness: { scope: :forum_id, case_sensitive: true }

default_scope -> { order('name ASC') }
#scope :with_topics, :include => :topics, :conditions => 'topics.id IS NOT NULL'
Expand Down
2 changes: 1 addition & 1 deletion app/models/usage_limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UsageLimit < ApplicationRecord
validates :metric, presence: true #, :plan #plan.customize method hinders this validation
validates :plan, presence: true

validates :period, uniqueness: {scope: [:metric_id, :plan_id]}
validates :period, uniqueness: { scope: %i[metric_id plan_id], case_sensitive: true}

before_save :set_value_default_value

Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def moderatable

validate :email_is_unique
validate :username_is_unique
validates :open_id, uniqueness: true, allow_nil: true
validates :open_id, uniqueness: { case_sensitive: true }, allow_nil: true

attr_accessible :title, :username, :email, :first_name, :last_name, :password,
:password_confirmation, :conditions, :cas_identifier, :open_id,
Expand Down
2 changes: 1 addition & 1 deletion app/models/user_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class UserTopic < ApplicationRecord
validates :user, presence: true
validates :topic, presence: true

validates :user_id, uniqueness: { :scope => :topic_id }
validates :user_id, uniqueness: { scope: :topic_id, case_sensitive: true }
end
2 changes: 1 addition & 1 deletion app/models/web_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WebHook < ApplicationRecord

#TODO: limit association only to providers?
#TODO validate url as url?
validates :account_id, uniqueness: true
validates :account_id, uniqueness: { case_sensitive: true }

class << self
delegate :perform_deliveries, :sanitized_url, :to => :configuration
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(user)
clear_validators!

validates :account_id, presence: true
validates :name, presence: true, uniqueness: { scope: :account_id }, length: { maximum: 255 }
validates :name, presence: true, uniqueness: { scope: :account_id, case_sensitive: true }, length: { maximum: 255 }

belongs_to :account

Expand Down

0 comments on commit 908be5d

Please sign in to comment.