代码之家  ›  专栏  ›  技术社区  ›  d11wtq Vadim Baryshev

Rails:将所有ActiveRecord“validates”行保存在一个单独的文件中?

  •  4
  • d11wtq Vadim Baryshev  · 技术社区  · 16 年前

    我意识到 validates 行实际上是一个方法调用(很明显),所以这样要求它们并不像我预期的那样。

    这是可行的,但我不确定它是否正确(完全符合 Auction (类名):

    class Auction::Validations
      Auction.validates :status, :presence => true,
                         :inclusion => { :in => [
                            Auction::CREATING,
                            Auction::OPEN,
                            Auction::PENDING,
                            Auction::CANCELLED,
                            Auction::SUSPENDED,
                            Auction::EXPIRED,
                            Auction::CLOSING_COMPLETED,
                            Auction::CLOSING_WON,
                            Auction::COMPLETED,
                            Auction::WON,
                            Auction::NEGOTIATING,
                            Auction::IN_ESCROW
                         ] }
      Auction.validates :user,   :presence => true
      Auction.validates :url,    :presence => true,
                         # FIXME: Move this to a URLValidator and do :url => true
                         :format => /^https?:\/\/[a-z0-9-]+(\.[a-z0-9-])*\.[a-z0-9]+\/.*/i
      Auction.validates :title,  :presence => true,
                         :length => { :maximum => 255 }
      Auction.validates :description,  :presence => true
      Auction.validates :reserve, :numericality => { :greater_than_or_equal_to => :minimum_bid }
    end
    

    当需要时( require 'auction/validations )进入 拍卖

    我的几个模型类被所有这些“validates”调用弄得有点混乱,所以我想我可以将它们移到一个单独的类中,并“要求”它们,但它似乎不起作用。

    class Auction < ActiveRecord::Base
      require 'auction/validations'
      ...
    
    class Auction::Validations
      include ActiveModel::Validations
    
      validates :status, :presence => true,
                         :inclusion => { :in => [
                            ... snip ...
                         ] }
      validates :user,   :presence => true
      validates :url,    :presence => true,
                         # FIXME: Move this to a URLValidator
                         :format => /^https?:\/\/[a-z0-9-]+(\.[a-z0-9-])*\.[a-z0-9]+\/.*/i
      validates :title,  :presence => true,
                         :length => { :maximum => 255 }
      validates :description,  :presence => true
      validates :reserve, :numericality => { :greater_than_or_equal_to => :minimum_bid }
    
      validates_each :status, :on => :update do |auction, status_attr, value|
        if auction.state_machine.current_state != value
          # FIXME: Raise an Exception instead; this is a developer error, not a user error
          auction.errors.add status_attr, "Status cannot be changed directly"
        end
      end
    end
    

    puts "here" ),并且数字性检查不再起作用。

    随着这个班的身体一味的复制回到拍卖班,一切又开始运转了。

    编辑:

    事实上,所有的验证都不起作用。不仅仅是那两个。六羟甲基三聚氰胺六甲醚。

    4 回复  |  直到 15 年前
        1
  •  2
  •   Vipin    15 年前

    我不确定这是否正确,但不知怎么的,它对我很有效:

    module MyValidations
      module User
        def self.included(base)
          base.validates_presence_of :firstname
        end
      end end
    

    然后你可以打电话

    User.class_eval do
      include MyValidations::User
    end
    

    希望有帮助。

        2
  •  1
  •   sbl    15 年前

    在拍卖结束时放置::验证:

    Auction.send :extend, Auction::Validations
    

    在拍卖结束时,把这一行。

        3
  •  0
  •   shingara    16 年前

    module Auction::Validations
      extend ActiveSupport::Concern
      def included(base)
      validates :status, :presence => true,
                         :inclusion => { :in => [
                            ... snip ...
                         ] }
      validates :user,   :presence => true
      validates :url,    :presence => true,
                         # FIXME: Move this to a URLValidator
                         :format => /^https?:\/\/[a-z0-9-]+(\.[a-z0-9-])*\.[a-z0-9]+\/.*/i
      validates :title,  :presence => true,
                         :length => { :maximum => 255 }
      validates :description,  :presence => true
      validates :reserve, :numericality => { :greater_than_or_equal_to => :minimum_bid }
    
      validates_each :status, :on => :update do |auction, status_attr, value|
        if auction.state_machine.current_state != value
          # FIXME: Raise an Exception instead; this is a developer error, not a user error
          auction.errors.add status_attr, "Status cannot be changed directly"
        end
      end
      end
    end
    
        4
  •  0
  •   Inanc Gumus    11 年前

    # put this into: app/models/concerns/auction/validations.rb
    class Auction
      module Validations
        extend ActiveSupport::Concern
    
        included do
          validates :status, presence: true
        end
      end
    end
    
    class Auction
      include Validations
    
      # other model code...
    end