teesea
2007-Oct-24 18:29 UTC
how to subclass active record to call a method on all save and destroy actions
I''m looking for a way to subclass active record (I''ll refer to the subclass I want to create as NewActiveRecord) so that I can call different methods on a save or destroy action on all models which sub class NewActiveRecord I dont want to use before_save or any of the other regular call back methods as I want to be able to use these call backs in the models which subclass NewActiveRecord without having to call super everytime I use them. Anyone have any ideas about how I can do this? Cheers Tony --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
brabuhr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-24 18:34 UTC
Re: how to subclass active record to call a method on all save and destroy actions
On 10/24/07, teesea <t.combe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m looking for a way to subclass active record (I''ll refer to the > subclass I want to create as NewActiveRecord) so that I can call > different methods on a save or destroy action on all models which sub > class NewActiveRecord > > I dont want to use before_save or any of the other regular call back > methods as I want to be able to use these call backs in the models > which subclass NewActiveRecord without having to call super everytime > I use them.Use ActiveRecord::Observer instead of ActiveRecord::Callbacks? http://www.robbyonrails.com/articles/2007/04/28/q-a-activerecord-observers-and-you http://api.rubyonrails.org/classes/ActiveRecord/Observer.html --~--~---------~--~----~------------~-------~--~----~ 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
2007-Oct-24 19:34 UTC
Re: how to subclass active record to call a method on all save and destroy actions
You probably want to set self.abstract_class = true. Fred On 24 Oct 2007, at 19:29, teesea wrote:> > I''m looking for a way to subclass active record (I''ll refer to the > subclass I want to create as NewActiveRecord) so that I can call > different methods on a save or destroy action on all models which sub > class NewActiveRecord > > I dont want to use before_save or any of the other regular call back > methods as I want to be able to use these call backs in the models > which subclass NewActiveRecord without having to call super everytime > I use them. > > Anyone have any ideas about how I can do this? > > Cheers > > Tony > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
teesea
2007-Oct-25 08:33 UTC
Re: how to subclass active record to call a method on all save and destroy actions
Thanks for the observer link, they would work nicely but I need to be able to stop a model from saving or destroying, if the additional method i call returns false. Basically what I''m trying to do is add some authentication methods into all my active record models so that whenever a save or destroy, and also a association is added the authentication method checks that the user has privileges to do this. @Frederick Thanks, I''ve heard that using self.abstract_class = true will allow me to sub class ActiveRecord, I need to know which single method in ActiveRecord is best to override to add the authentication methods. Do you know which would be best?, or could you point me in the direction of some good resources on ActiveRecord Cheers Tony On Oct 24, 8:34 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You probably want to set self.abstract_class = true. > > Fred > On 24 Oct 2007, at 19:29, teesea wrote: > > > > > I''m looking for a way to subclass active record (I''ll refer to the > > subclass I want to create as NewActiveRecord) so that I can call > > different methods on a save or destroy action on all models which sub > > class NewActiveRecord > > > I dont want to use before_save or any of the other regular call back > > methods as I want to be able to use these call backs in the models > > which subclass NewActiveRecord without having to call super everytime > > I use them. > > > Anyone have any ideas about how I can do this? > > > Cheers > > > Tony--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2007-Oct-25 17:46 UTC
Re: how to subclass active record to call a method on all save and destroy actions
On 10/25/07, teesea <t.combe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > could you point me in the direction > of some good resources on ActiveRecordFor extending ActiveRecord the fairly new "Pro ActiveRecord" book from APress is pretty good. I''m not sure I''d recommend it for an introduction/only reference to AR (AWDR 2ed does a better job IMHO) but for advanced things like extending AR, it''s the best resource I''ve found as yet. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
Brian Hogan
2007-Oct-25 20:39 UTC
Re: how to subclass active record to call a method on all save and destroy actions
We don''t do that kinda stuff in the Ruby world. We use modules instead, so we can "mix in" that behavior. In lib/ar_extentions.rb module MyActiveRecordExtentions def save(*args) if some_condition super(*args) # do the original else return false # or do your own stuff end end def destroy return false end end Then all I need to do is class User < ActiveRecord::Base include MyActiveRecordExtensions end If you really want to see how this works in action, take a look at the acts_as_paranoid plugin. It uses this technique to mark records as deleted instead of actually deleting them, by overriding the destroy method. It''s a good place to start learning about this kinda stuff. On 10/24/07, teesea <t.combe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I''m looking for a way to subclass active record (I''ll refer to the > subclass I want to create as NewActiveRecord) so that I can call > different methods on a save or destroy action on all models which sub > class NewActiveRecord > > I dont want to use before_save or any of the other regular call back > methods as I want to be able to use these call backs in the models > which subclass NewActiveRecord without having to call super everytime > I use them. > > Anyone have any ideas about how I can do this? > > Cheers > > Tony > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
teesea
2007-Oct-28 10:18 UTC
Re: how to subclass active record to call a method on all save and destroy actions
Thanks to all those who responded I''ll have a look at acts_as_paranoid and see what the best way forward is On Oct 25, 8:39 pm, "Brian Hogan" <bpho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> We don''t do that kinda stuff in the Ruby world. > > We use modules instead, so we can "mix in" that behavior. > > In lib/ar_extentions.rb > > module MyActiveRecordExtentions > > def save(*args) > if some_condition > super(*args) # do the original > else > return false # or do your own stuff > end > end > > def destroy > return false > end > > end > > Then all I need to do is > > class User < ActiveRecord::Base > > include MyActiveRecordExtensions > > end > > If you really want to see how this works in action, take a look at the > acts_as_paranoid plugin. It uses this technique to mark records as deleted > instead of actually deleting them, by overriding the destroy method. It''s a > good place to start learning about this kinda stuff. > > On 10/24/07, teesea <t.co...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''m looking for a way to subclass active record (I''ll refer to the > > subclass I want to create as NewActiveRecord) so that I can call > > different methods on a save or destroy action on all models which sub > > class NewActiveRecord > > > I dont want to use before_save or any of the other regular call back > > methods as I want to be able to use these call backs in the models > > which subclass NewActiveRecord without having to call super everytime > > I use them. > > > Anyone have any ideas about how I can do this? > > > Cheers > > > Tony--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---