Created
August 2, 2011 09:20
-
-
Save pi3r/1119874 to your computer and use it in GitHub Desktop.
OrderValidator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrderValidator < ActiveModel::Validator | |
@@steps = { | |
:step_one => [:spec, :tac, :establishment_id], | |
:step_one_validate => [:spec_option] | |
} | |
def validate(record) | |
validates_for_presence(record) if !record.current_step.blank? && steps.include?(record.current_step.to_sym) | |
end | |
private | |
def validates_for_presence record | |
record.errors.add_on_blank validation_attributes(record.current_step.to_sym) | |
end | |
def validation_attributes current_step | |
steps[0..steps.index(current_step)].inject([]){|m,o| m << @@steps[o]}.flatten | |
end | |
def steps | |
Array(@@steps.keys) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment