Skip to content

Instantly share code, notes, and snippets.

@dpsk
Created September 25, 2012 12:31
Show Gist options
  • Save dpsk/3781497 to your computer and use it in GitHub Desktop.
Save dpsk/3781497 to your computer and use it in GitHub Desktop.
Partially validate model attributes. Good for multi-step wizards form without saving record on each step.
module PartiallyValidatable
extend ActiveSupport::Concern
included do
def self.partially_valid?(params, valid=true)
mock = self.new(params)
params.each{|attr, _| valid = false if mock.errors.messages[attr.to_sym].present?} unless mock.valid?
valid
end
def self.generate_errors(params, errors=[])
mock = self.new(params)
params.each{|attr, _| errors << mock.errors.messages[attr.to_sym] if mock.errors.messages[attr.to_sym].present?} unless mock.valid?
errors.flatten
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment