Rails: Saving all active ActiveRecord lines in a separate file?

UPDATE (December 4, 2010):

I realized that each validates line is actually a method call (obviously), so requiring them like this was not quite the way I expected.

This works, but I'm not sure if it is right (fully qualify the Auction class name):

 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 

If necessary ( require 'auction/validations ) in the Auction class, it does the right thing.

The original question follows:

Several of my model classes clutter up all of these β€œchecks” a bit, so I thought I could move them to a separate class and β€œrequire”, but that 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 

This is not an error, but validates_each does not execute the block at all (verified by adding puts "here" ), and checking the numerical value no longer works.

With the body of this class, which is blindly copied back to the Auction class, everything will work.

Do I really not understand what will require these validations?

EDIT:

In fact, none of the validations work. Not just these two. Hmmm.

+4
source share
4 answers

I'm not sure if this is correct, but it somehow works for me:

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

Then u can cause

 User.class_eval do include MyValidations::User end 

Hope this helps.

+2
source

Put at the end of the auction :: Validations:

 Auction.send :extend, Auction::Validations 

and at the end of the auction put this line.

+1
source

Why not just turn on 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 
0
source

In Rails 4, this is very simple. Use model problems.

 # 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 
0
source

All Articles