Michal Gabrukiewicz
2008-Jan-22 09:16 UTC
using same validation-set in more than one class
hello, lets describe my situation: i have two classes which have a lot same validations defined ... to stay DRY i want to put them in one place and use it within both ActiveRecord classes. - i think subclassing is not possible because both classes are AR of different tables - tried to include a module but this didnt work either: module CustomExtensions validates_length_of :a, :in => 1..10 validates_length_of :b, :in => 1..20 end class A < AR::Base include CustomExtensions end thx for help! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jan-22 09:34 UTC
Re: using same validation-set in more than one class
On 22 Jan 2008, at 09:16, Michal Gabrukiewicz wrote:> > hello, > lets describe my situation: i have two classes which have a lot same > validations defined ... to stay DRY i want to put them in one place > and > use it within both ActiveRecord classes. > > - i think subclassing is not possible because both classes are AR of > different tables > > - tried to include a module but this didnt work either: > > module CustomExtensions > validates_length_of :a, :in => 1..10 > validates_length_of :b, :in => 1..20 > end > > class A < AR::Base > include CustomExtensions > end >nearly there! module CommonStuff def self.included(base) base.instance_eval do validates_length_of :a, :in => 1..10 validates_length_of :b, :in => 1..20 end end ... end class A < AR::Base include CommonStuff end> thx for help! > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michal Gabrukiewicz
2008-Jan-22 09:44 UTC
Re: using same validation-set in more than one class
hey thx for the quick reply .. it worked! i am just wondering why its "instance_eval" and not "class_eval"... should the validations not be applied to the whole class?> nearly there! > > module CommonStuff > def self.included(base) > base.instance_eval do > validates_length_of :a, :in => 1..10 > validates_length_of :b, :in => 1..20 > end > end > ... > end > > class A < AR::Base > include CommonStuff > end-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jan-22 10:13 UTC
Re: using same validation-set in more than one class
On 22 Jan 2008, at 09:44, Michal Gabrukiewicz wrote:> > hey thx for the quick reply .. it worked! > i am just wondering why its "instance_eval" and not "class_eval"... > should the validations not be applied to the whole class? >In this particular case it doesn''t make any difference. instance_eval means that self will be the base class (ie A) when the validates_* calls are made. module_eval (which is the same as class eval) would do the same thing (it would be different if we were defining methods inside the eval block, but we''re not) Fred>> nearly there! >> >> module CommonStuff >> def self.included(base) >> base.instance_eval do >> validates_length_of :a, :in => 1..10 >> validates_length_of :b, :in => 1..20 >> end >> end >> ... >> end >> >> class A < AR::Base >> include CommonStuff >> end > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michal Gabrukiewicz
2008-Jan-22 10:28 UTC
Re: using same validation-set in more than one class
Frederick Cheung wrote:> In this particular case it doesn''t make any difference. instance_eval > means that self will be the base class (ie A) when the validates_* > calls are made. module_eval (which is the same as class eval) would do > the same thing (it would be different if we were defining methods > inside the eval block, but we''re not)i see.. you mean if we would do this?? def self.included(base) class_eval do ... end end> > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jan-22 10:43 UTC
Re: using same validation-set in more than one class
On 22 Jan 2008, at 10:28, Michal Gabrukiewicz wrote:> > Frederick Cheung wrote: > >> In this particular case it doesn''t make any difference. instance_eval >> means that self will be the base class (ie A) when the validates_* >> calls are made. module_eval (which is the same as class eval) would >> do >> the same thing (it would be different if we were defining methods >> inside the eval block, but we''re not) > > i see.. you mean if we would do this?? >I mean module CommonStuff def self.included(base) base.instance_eval do validates_length_of :a, :in => 1..10 validates_length_of :b, :in => 1..20 end end has the same effect as module CommonStuff def self.included(base) base.class_eval do validates_length_of :a, :in => 1..10 validates_length_of :b, :in => 1..20 end end but if your eval block had def foo end then the method would be added in different places (A.foo instead of A.new.foo) Fred> def self.included(base) > class_eval do > ... > end > end > >> >> Fred > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michal Gabrukiewicz
2008-Jan-22 10:57 UTC
Re: using same validation-set in more than one class
Frederick Cheung wrote: oh i see now... thanks for your time! learned something again> > but if your eval block had > > def foo > end > then the method would be added in different places (A.foo instead of > A.new.foo) > > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---