views:

29

answers:

1

A couple of my model classes are getting a little cluttered with all these "validates" calls, so I thought I'd be able to move them into a separate class and 'require' that, but it doesn't seem to work.

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

It doesn't error, but the validates_each doesn't execute the block at all (tested by adding a puts "here"), and the numericality check doesn't work any longer.

With the body of this class blindly copied back into the Auction class again everything works.

Am I misunderstanding what the "require" will do with these validations?

EDIT:

In fact, none of the validations are working. Not just those two. Hmmm.

A: 

Why just not include your module ?

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
shingara
Hmmm, I'd never seen that before. Very new to both ruby and Rails, thanks.
d11wtq
However, it doesn't seem to work. I get the same results. Upon further testing I've found that in fact none of the code inside this required class is working as expected. How do I bring it into my Auction class?
d11wtq