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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jemminger wrote:> 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) > [OMITTED] > 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"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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
actually there might be some strange problem just with this project... i tried your suggestion in a new test project and it works properly. On Jul 31, 11:32 am, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> jemminger wrote: > > 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) > > [OMITTED] > > 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" > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? On Jul 31, 2:30 pm, jemminger <jemmin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> actually there might be some strange problem just with this project... > i tried your suggestion in a new test project and it works properly. > > On Jul 31, 11:32 am, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote: > > > jemminger wrote: > > > 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) > > > [OMITTED] > > > 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" > > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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... On Jul 31, 9:28 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
awesome... that works. thank you very much! maybe one day i''ll understand or figure out why it didn''t include the other way. On Aug 1, 2:14 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---