Skip to content

Instantly share code, notes, and snippets.

@joshuaclayton
Created July 28, 2008 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaclayton/2960 to your computer and use it in GitHub Desktop.
Save joshuaclayton/2960 to your computer and use it in GitHub Desktop.
# initial idea for state(or step #)-based validation
# for the model below, it would only run those validations when instance_model#step == "initial" or nil
class ActiveRecord::Base
def self.validate_on_step(attribute, &block)
attribute, step_attribute = attribute.to_s, "step"
self.send :attr_accessor, step_attribute unless self.respond_to?(step_attribute)
with_options(:if => lambda {|w| (w.send(step_attribute) == attribute) || (w.send(step_attribute).nil?) }) do |validating_model|
yield(validating_model)
end
end
end
class User < ActiveRecord::Base
validate_on_step :initial do |v|
v.validates_presence_of :title
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment