Last active
August 29, 2015 14:09
-
-
Save rpanachi/c911f39606752d0be33d to your computer and use it in GitHub Desktop.
Skip Validation
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
# source: http://gistflow.com/posts/749-canceling-validations-in-activerecord | |
# declaration | |
def skipping_validation(methods, &block) | |
skipped_validations = methods.map do |method, kinds| | |
Array(kinds).map do |kind| | |
_validators[method].select { |v| v.kind == kind }.map do |validation| | |
_validate_callbacks.select { |c| c.filter == validation }.map do |callback| | |
_validate_callbacks.delete(callback) | |
end | |
end | |
end | |
end | |
begin | |
block.call | |
ensure | |
skipped_validations.flatten.each do |callback| | |
_validate_callbacks.append(callback) | |
end | |
end | |
end | |
# usage | |
skipping_validation(phone: :presence) do | |
self.save | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment