I'm using AASM by Rubyist to build a 4-step wizard for an AR object. According to the state of the object, there are different validations that need to be done. What is the smartest way to validate an object according to it's state on a certain transition?
+4
A:
Use the :if
option, and with_options
to bunch 'em together.
class Post < ActiveRecord::Base
with_options :if => proc {|r| r.signup_step > 2 } do |o|
o.validates_presence_of :title
o.validates_presence_of :yeah
end
end
What exactly you ought to write in the if-proc depends on your implementation, obviously.
August Lilleaas
2009-04-06 13:22:02
Sounds very smart. :) Where does the "with_options" come from? Never seen this before.
Javier
2009-04-06 13:57:46
Ah, found it: http://api.rubyonrails.org/classes/Object.html#M000277 thanks :)
Javier
2009-04-06 14:10:26