【问题标题】:Removing or overriding an ActiveRecord validation added by a superclass or mixin删除或覆盖由超类或 mixin 添加的 ActiveRecord 验证
【发布时间】:2011-01-19 13:36:09
【问题描述】:

我在我的 Rails 应用程序中使用 Clearance 进行身份验证。 Clearance::User mixin 为我的 User 模型添加了几个验证,但我想删除或覆盖其中一个。这样做的最佳方法是什么?

有问题的验证是

validates_uniqueness_of :email, :case_sensitive => false

这本身还不错,但我需要添加:scope => :account_id。问题是,如果我将其添加到我的 User 模型中

validates_uniqueness_of :email, :scope => :account_id

我得到两个验证,而 Clearance 添加的一个比我的更严格,所以我的没有效果。我需要确保只有我的运行。我该怎么做?

【问题讨论】:

    标签: ruby-on-rails ruby validation clearance


    【解决方案1】:

    我最终通过以下技巧“解决”了这个问题:

    1. 在:email 类型的:taken 属性上查找错误@
    2. 检查此帐户的电子邮件是否唯一(这是我想要做的验证)
    3. 如果此帐户的电子邮件是唯一的,则删除该错误。

    在您阅读代码并发现我如何消除错误之前,这听起来很合理。 ActiveRecord::Errors 添加后没有任何方法可以消除错误,所以我必须抓住它的内部结构并自己动手。超级超级丑陋。

    这是代码:

    def validate
      super
      remove_spurious_email_taken_error!(errors)
    end
    
    def remove_spurious_email_taken_error!(errors)
      errors.each_error do |attribute, error|
        if error.attribute == :email && error.type == :taken && email_unique_for_account?
          errors_hash = errors.instance_variable_get(:@errors)
          if Array == errors_hash[attribute] && errors_hash[attribute].size > 1
            errors_hash[attribute].delete_at(errors_hash[attribute].index(error))
          else
            errors_hash.delete(attribute)
          end
        end
      end
    end
    
    def email_unique_for_account?
      match = account.users.find_by_email(email)
      match.nil? or match == self
    end
    

    如果有人知道更好的方法,我将非常感激。

    【讨论】:

    • 从 Rails 3 开始,您可以使用 errors.delete(:field) 从集合中删除错误。
    【解决方案2】:

    我最近遇到了这个问题,在谷歌没有足够快地给我答案之后,我找到了一个更简洁但仍然不理想的解决方案。现在这不一定适用于您的情况,因为您似乎使用了预先存在的超类,但对我来说这是我自己的代码,所以我只是在超类中使用带有类型检查的 :if 参数。

    def SuperClass
      validates_such_and_such_of :attr, :options => :whatever, :if => Proc.new{|obj| !(obj.is_a? SubClass)}
    end
    
    def SubClass < SuperClass
      validates_such_and_such_of :attr
    end
    

    在多个子类的情况下

    def SuperClass
      validates_such_and_such_of :attr, :options => :whatever, :if => Proc.new{|obj| [SubClass1, SubClass2].select{|sub| obj.is_a? sub}.empty?}
    end
    
    def SubClass1 < SuperClass
      validates_such_and_such_of :attr
    end
    
    def SubClass2 < SuperClass
    end
    

    【讨论】:

    • 不幸的是,我无法更改超类,它位于 gem 中。
    • 这就像在检查引擎灯上贴上胶带并说它有效。
    【解决方案3】:

    这是一个对我有用的 Rails 3“解决方案”(如果有人有更好的方法,请提供!)

    class NameUniqueForTypeValidator < ActiveModel::Validator
    
      def validate(record)
        remove_name_taken_error!(record)
      end
    
      def remove_name_taken_error!(record)
        errors = record.errors
        errors.each do |attribute, error|
          if attribute == :name && error.include?("taken") && record.name_unique_for_type?
            errors.messages[attribute].each do |msg|
              errors.messages[attribute].delete_at(errors.messages[attribute].index(msg)) if msg.include?("taken")
            end
            errors.messages.delete(attribute) if errors.messages[attribute].empty?
          end
        end
      end
    
    end
    
    
    ActsAsTaggableOn::Tag.class_eval do
      validates_with NameUniqueForTypeValidator
    
      def name_unique_for_type?
        !ActsAsTaggableOn::Tag.where(:name => name, :type => type).exists?
      end
    end
    

    【讨论】:

      【解决方案4】:

      我知道我迟到了,但是怎么样:

      module Clearance
        module User
          module Validations
            extend ActiveSupport::Concern
      
            included do
              validates :email,
                email: true,
                presence: true,
                uniqueness: { scope: :account, allow_blank: true },
                unless: :email_optional?
      
              validates :password, presence: true, unless: :password_optional?
            end
          end
        end
      end
      

      在初始化器中?

      【讨论】:

        【解决方案5】:

        Errors.delete(key) 删除属性的所有错误,我只想删除属于属性的特定类型的错误。以下方法可以添加到任何模型中。

        如果删除则返回消息,否则返回 nil。内部数据结构已修改,因此所有其他方法在错误消除后应按预期工作。

        在MIT License下发布

        运行验证后从模型中删除错误的方法。

        def remove_error!(attribute, message = :invalid, options = {})
          # -- Same code as private method ActiveModel::Errors.normalize_message(attribute, message, options).
          callbacks_options = [:if, :unless, :on, :allow_nil, :allow_blank, :strict]
          case message
          when Symbol
            message = self.errors.generate_message(attribute, message, options.except(*callbacks_options))
          when Proc
            message = message.call
          else
            message = message
          end
          # -- end block
        
          # -- Delete message - based on ActiveModel::Errors.added?(attribute, message = :invalid, options = {}).
          message = self.errors[attribute].delete(message) rescue nil
          # -- Delete attribute from errors if message array is empty.
          self.errors.messages.delete(attribute) if !self.errors.messages[attribute].present?
          return message
        end
        

        用法:

        user.remove_error!(:email, :taken)
        

        检查除指定属性和消息之外的有效性的方法。

        def valid_except?(except={})
          self.valid?
          # -- Use this to call valid? for superclass if self.valid? is overridden.
          # self.class.superclass.instance_method(:valid?).bind(self).call
          except.each do |attribute, message|
            if message.present?
              remove_error!(attribute, message)
            else
              self.errors.delete(attribute)
            end
          end
          !self.errors.present?
        end
        

        用法:

        user.valid_except?({email: :blank})
        user.valid_except?({email: "can't be blank"})
        

        【讨论】:

          【解决方案6】:

          对于我的模型,下面的代码就足够了。我不想验证邮政编码。

          after_validation :remove_nonrequired
          
          def remove_nonrequired
            errors.messages.delete(:zipcode)
          end
          

          【讨论】:

          • 对于我来说删除了错误,但不是模型上的“无效”状态。
          【解决方案7】:

          我需要删除 Spree 产品属性 :value 验证,似乎有一个更简单的解决方案,Klass.class_eval 和 clear_validators! 的 AciveRecord::Base

          module Spree
            class ProductProperty < Spree::Base
          
              #spree logic
          
              validates :property, presence: true
              validates :value, length: { maximum: 255 }
          
              #spree logic
          
          
            end
          end
          

          并在此处覆盖它

          Spree::ProductProperty.class_eval do    
            clear_validators!
            validates :property, presence: true
          end
          

          【讨论】:

          • 这不是也删除了基类上的验证器,因此也删除了所有其他子类吗?
          • 根据github.com/rails/rails/blob/…,它只是重置本地实例化的一组验证/回调。我认为回调集是在单个类上设置的,而不是所有链。我对派生类的验证没有问题。
          • 我认为应该是这样的:class Subclass &lt; Parent; class_eval { clear_validators!}; end。也许在您的应用程序中,词法范围和实际范围恰好匹配,但Parent.class_eval 将具有全局运行时范围,因此在生产中,如果您希望 Parent 在您的应用程序的其他地方拥有其验证器,您会感到惊讶。
          • 嗯,你可能是对的,但在我的情况下,我不得不处理 Rails 引擎,重写一个类对我不起作用。我们还经营生产商店。到目前为止没有问题。不过,感谢您的提示!
          • 我听起来你的情况与 OP 不同:不是使用 mixin 的子类或类,而是直接使用库类,否则不会出现“重写类”问题。如果您总是希望您的应用程序中的 Spree::ProductProperty 类没有它们的库验证器,那也没关系。否则,“本地实例化验证集”的范围可能(可能会)跨越生产中的多个请求。
          【解决方案8】:

          我会分叉 GEM 并添加一个简单的检查,然后可以覆盖它。我的示例使用了关注点。

          关注:

          module Slugify
          
            extend ActiveSupport::Concern
          
            included do
          
              validates :slug, uniqueness: true, unless: :skip_uniqueness?
            end
          
            protected
          
            def skip_uniqueness?
              false
            end
          
          end
          

          型号:

          class Category < ActiveRecord::Base
            include Slugify
          
            belongs_to :section
          
            validates :slug, uniqueness: { scope: :section_id }
          
            protected
          
            def skip_uniqueness?
              true
            end
          end
          

          【讨论】:

          • 优雅。适合关注的工作,这是我目前关注的问题,哈哈
          【解决方案9】:

          在 Rails 4 中,如果你有一个命名方便的验证方法,你应该可以使用skip_callback(:validate, :name_of_validation_method)...。 (免责声明:我还没有测试过。) 如果没有,您需要侵入回调列表以找到您想要跳过的回调,并使用其filter 对象。

          示例:

          我正在使用 Rails 4.1.11 和 Spree 2.4.11.beta 的网站上工作,并从 2.1.4 升级了 Spree。出于历史目的,我们的代码将Spree::Variants 的多个副本存储在一个表中。

          自升级以来,gem 现在是validates_uniqueness_of :sku, allow_blank: true, conditions: -&gt; { where(deleted_at: nil) },它破坏了我们的代码。但是,您会注意到,它不使用命名方法来执行此操作。这就是我在Spree::Variant.class_eval 块中所做的:

          unique_sku_filter = _validate_callbacks.find do |c|
            c.filter.is_a?(ActiveRecord::Validations::UniquenessValidator) &&
              c.filter.instance_variable_get(:@attributes) == [:sku]
          end.filter
          
          skip_callback(:validate, unique_sku_filter)
          

          这似乎完全从Variant 的链中删除了回调。

          注意。我不得不将instance_variable_get 用于@attributes,因为它没有访问器。您也可以在find 块中查看c.filter.options;在上面的例子中,这看起来像:

          c.filter.options
          #=> {:case_sensitive=>true, :allow_blank=>true, :conditions=>#<Proc:... (lambda)>}
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-01-22
            • 2014-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-05-05
            • 2011-11-25
            相关资源
            最近更新 更多