Skip to content

Instantly share code, notes, and snippets.

@pi3r
Created August 2, 2011 09:20
Show Gist options
  • Save pi3r/1119874 to your computer and use it in GitHub Desktop.
Save pi3r/1119874 to your computer and use it in GitHub Desktop.
OrderValidator
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