Forum: Ruby on Rails overridden validator not being used?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-07-30 22:58
(Received via mailing list)
hello all,

i'm trying to override the validator validates_exclusion_of to make it
case-insensitive:

module ActiveRecord::Validations::ClassMethods

  def validates_exclusion_of(*attr_names)
    raise 'hello world'
    configuration = { :message =>
ActiveRecord::Errors.default_error_messages[:exclusion], :on
=> :save }
    configuration.update(attr_names.pop) if attr_names.last.is_a?
(Hash)
    enum = configuration[:in] || configuration[:within]

    raise(ArgumentError, "An object with the method include? is
required must be supplied as the :in option of the configuration
hash") unless enum.respond_to?("include?")
    # make the compare case-insensitive
    validates_each(attr_names, configuration) do |record, attr_name,
value|
      record.errors.add(attr_name, configuration[:message]) if
enum.include?(value.downcase)
    end
  end

end

the code is in lib/validations.rb
however when i run a unit test against it, i get the default validator
instead of seeing raise "hello world"

what am i doing wrong?

thanks,
jeff
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-07-31 17:33
(Received via mailing list)
jemminger wrote:

> however when i run a unit test against it, i get the default validator
> instead of seeing raise "hello world"

Active Record includes the validations module into the Base class.
So you have to write

   class ActiveRecord::Base
     def self.validates_exclusion_of(*attr_names)
       [OMITTED]
     end
   end

--
We develop, watch us RoR, in numbers too big to ignore.
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-07-31 18:50
(Received via mailing list)
> Active Record includes the validations module into the Base class.
> So you have to write
>
>    class ActiveRecord::Base
>      def self.validates_exclusion_of(*attr_names)
>        [OMITTED]
>      end
>    end
>

thanks for your reply.  this still does not work.  i've made sure that
the file is being required in evironment.rb... out of curiosity, i
also tried changing the method name to self.validates_exclusion_of2,
and changed the reference in the model... i get a NoMethodError.  i
know that the file is being read, since if i put a raise before the
method def i will get the raise.

any ideas?
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-07-31 20:30
(Received via mailing list)
actually there might be some strange problem just with this project...
i tried your suggestion in a new test project and it works properly.
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-08-01 01:30
(Received via mailing list)
Ok, more information:  we're using observers in environment.rb.  if we
comment out the line attaching the observers, all works properly.
could this be a bug?
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-08-01 03:30
(Received via mailing list)
jemminger wrote:
> Ok, more information:  we're using observers in environment.rb.  if we
> comment out the line attaching the observers, all works properly.
> could this be a bug?

Try putting the redefinition of the validator above the observer
declaration in environment.rb.

--
We develop, watch us RoR, in numbers too big to ignore.
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-08-01 18:30
(Received via mailing list)
no luck... the observers declaration is inside the
Rails::Initializer.run block.  any attempt to require the custom
validations.rb before the end of that config block results in an error
"uninitialized constant ActiveRecord"

i'll start digging through the rails source to see if i can find
anything...
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-08-01 18:56
(Received via mailing list)
jemminger wrote:
> no luck... the observers declaration is inside the
> Rails::Initializer.run block.  any attempt to require the custom
> validations.rb before the end of that config block results in an error
> "uninitialized constant ActiveRecord"

Try seting the observers at the bottom of environment.rb like

ActiveRecord::Base.observers = :observer1, ...

--
We develop, watch us RoR, in numbers too big to ignore.
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-08-01 19:59
(Received via mailing list)
> Try seting the observers at the bottom of environment.rb like
>
> ActiveRecord::Base.observers = :observer1, ...

shoot, i thought that this one was gonna do it but no - this time the
validator worked properly but the observers don't get called.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-08-01 20:15
(Received via mailing list)
jemminger wrote:

>> Try seting the observers at the bottom of environment.rb like
>>
>> ActiveRecord::Base.observers = :observer1, ...
>
> shoot, i thought that this one was gonna do it but no - this time the
> validator worked properly but the observers don't get called.

If you're in production, add "ActiveRecord::Base.instantiate_observers".

Alternatively, if you turn your patch into a plugin (just put
your AR:B override in vendor/plugins/<something>/init.rb), that
will automatically be loaded just before the observers are set-up
in the initializer.

--
We develop, watch us RoR, in numbers too big to ignore.
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-08-01 22:44
(Received via mailing list)
awesome... that works.  thank you very much!  maybe one day i'll
understand or figure out why it didn't include the other way.
This topic is locked and can not be replied to.