Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Created August 29, 2012 17:49
Show Gist options
  • Select an option

  • Save jeffdeville/3516175 to your computer and use it in GitHub Desktop.

Select an option

Save jeffdeville/3516175 to your computer and use it in GitHub Desktop.
packlate_pms_integration options for selectively enabling validations.
require 'rubygems'
require 'active_model'
class Test
include ActiveModel::Validations
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
attr_accessor :jeff
# options:
# 1. validates_presence_of :jeff, :unless => Proc.new { |t| t.exclude_validation?(:jeff_not_nil) }
validates_presence_of :jeff, :unless => "exclude_validation?(:jeff_not_nil)"
def persisted?
false
end
def exclude_validation?(name)
return false unless defined?(excluded_validations)
excluded_validations.include?(name)
end
end
# Define these in the gem, and let the gem be responsible for using them where desired.
module ValidationExclusions
def excluded_validations
[:jeff_not_nil]
end
end
# What I don't really like about this, is that you have to set the excluded_validations on a per instance level. I don't want there to be danger of having different gems overwrite one anothers validation exclusions though. I think it'd be ok if there was just a good way of enforcing things transparently from the gem itself. This seemslike it'd be a good place to use extend, right?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment