views:

207

answers:

2

Is there a way to drop a validation that was set in Rails plugin (or included module)? Let's say I have some model with module included in it:

class User < ActiveRecord::Base
  include SomeModuleWithValidations
  # How to cancel validates_presence_of :something here?
end

module SomeModuleWithValidations
  def self.included(base)
    base.class_eval do
      validates_presence_of :something
    end
  end  
end

My only idea so far was to do something like:

validates_presence_of :something, :if => Proc.new{1==2}

which would work, I think, but it isn't particulary pretty.

+1  A: 

You could overload the validates_precense_of in the class. Something like:

def self.validates_presence_of(*args)
  return if args.first == :foo
 super
end

Or if you have validates_presence_of :foo you could do:

def foo
  self[:foo] || ""
end

However, none of those solutions are very nice. It would most likely be easier to just uncomment it in the module, or redefine it to no-op in just that module.

Kristian