Are there filters built into Rails for before and after method invocations? I saw some interceptor definitions, but they appear to be geared towards use with the web service class. I also need the filters to be available to controllers as well as models, so I don''t know if that causes a problem. Steve
You can easily define before and after filters for your controllers. http://ap.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html Chris On 4/16/05, Steve V <ruby-ChEX1j9zMF7JbC0vcoRRxNBPR1lH4CV8@public.gmane.org> wrote:> Are there filters built into Rails for before and after method invocations? > I saw some interceptor definitions, but they appear to be geared towards use > with the web service class. I also need the filters to be available to > controllers as well as models, so I don''t know if that causes a problem. > > Steve > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> You can easily define before and after filters for your controllers. > > http://ap.rubyonrails.com/classes/ActionController/Filters/ClassMe > thods.htmlI''m aware of those filters. What I''m looking for is before and after filters specific to particular methods. So while certain before and after filters are required for the class, I have additional code that I want to be execute before specific method calls. That''s why I mentioned the interceptor filters, but they seem to only be geared towards usage with the web service class. Steve
On 17/04/2005, at 8:24 AM, Steve V wrote:>> You can easily define before and after filters for your controllers. >> >> http://ap.rubyonrails.com/classes/ActionController/Filters/ >> ClassMethods.html > > I''m aware of those filters. What I''m looking for is before and after > filters > specific to particular methods. So while certain before and after > filters > are required for the class, I have additional code that I want to be > execute > before specific method calls. That''s why I mentioned the interceptor > filters, but they seem to only be geared towards usage with the web > service > class.Read: Filter conditions http://ap.rubyonrails.com/classes/ActionController/Filters/ ClassMethods.html The (slightly modified) example from the docs: class Journal < ActionController::Base # only require authentication if the current action is edit or delete before_filter :authorize, :only => [ :edit, :delete ] def edit end def delete end def show end private def authorize # redirect to login unless authenticated end end - tim lucas
> > Read: Filter conditions > http://ap.rubyonrails.com/classes/ActionController/Filters/ > ClassMethods.html > > The (slightly modified) example from the docs: > class Journal < ActionController::Base > # only require authentication if the current action is edit or > delete > before_filter :authorize, :only => [ :edit, :delete ] > > def edit > end > def delete > end > def show > end > > private > def authorize > # redirect to login unless authenticated > end > endTo further clarify I was also looking for a solution that would work with my models as well as controllers. Steve
On 4/17/05, Steve V <ruby-ChEX1j9zMF7JbC0vcoRRxNBPR1lH4CV8@public.gmane.org> wrote:> > > > Read: Filter conditions > > http://ap.rubyonrails.com/classes/ActionController/Filters/ > > ClassMethods.html > > > > The (slightly modified) example from the docs: > > class Journal < ActionController::Base > > # only require authentication if the current action is edit or > > delete > > before_filter :authorize, :only => [ :edit, :delete ] > > > > def edit > > end > > def delete > > end > > def show > > end > > > > private > > def authorize > > # redirect to login unless authenticated > > end > > end > > To further clarify I was also looking for a solution that would work with my > models as well as controllers.I''m not sure if there''s a generic ruby ''intercept method invocations''. Someone else may be more help there. I''m more interested in what the actual problem that you''re trying to solve is. Perhaps there''s another approach rather than intercepting method calls?> Steve > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
> I''m not sure if there''s a generic ruby ''intercept method invocations''. > Someone else may be more help there. > > I''m more interested in what the actual problem that you''re trying to > solve is. Perhaps there''s another approach rather than intercepting > method calls?If there is a generic library or something out there that would be great, but I haven''t found one yet. The primary uses are for security checks(fine grained permissions, models must be useable separate from controllers), and for method parameter validation(type, range, format, etc...). I don''t *need* this functionality necessarily. It could be done through regular statements within a function, but from a development standpoint, it makes the code cleaner. It also makes more sense from a logic standpoint(at least to me), since the things I''m using it for are not really part of the method, so much as requirements that must be met before using the method. Steve
On Sunday, April 17, 2005, 1:50:19 PM, Steve wrote:>> I''m not sure if there''s a generic ruby ''intercept method invocations''. >> Someone else may be more help there. >> >> I''m more interested in what the actual problem that you''re trying to >> solve is. Perhaps there''s another approach rather than intercepting >> method calls?> If there is a generic library or something out there that would be great, > but I haven''t found one yet.Google aspect4r. It''s an Aspect Oriented Progamming library for Ruby. Gavin
On 17/04/2005, at 1:50 PM, Steve V wrote:> The primary uses are for security checks(fine grained permissions, > models > must be useable separate from controllers), and for method parameter > validation(type, range, format, etc...). I don''t *need* this > functionality > necessarily. It could be done through regular statements within a > function, > but from a development standpoint, it makes the code cleaner. It also > makes > more sense from a logic standpoint(at least to me), since the things > I''m > using it for are not really part of the method, so much as > requirements that > must be met before using the method.Considering how differently the controller and model are used I don''t believe you''ll find a good solution that''ll cover both bases. How about encapsulating what you want in your own controllers (like a service layer), separate from your ActionControllers. Have you checked out the ActionController::Verification module for your ActionControllers? - tim lucas
> > Google aspect4r. It''s an Aspect Oriented Progamming library for Ruby.I had looked at aspectr previously, but it''s status is listed as alpha, and it seems to have stagnated around 2002. Is the project being actively developed somewhere else, or is it mostly complete? I just tried googling for aspect4r in case it was different, but I can''t really find anything about it(only 3 results, two in chinese). If it is different do you know where I can find it? Steve
On Monday, April 18, 2005, 3:00:27 AM, Steve wrote:>> >> Google aspect4r. It''s an Aspect Oriented Progamming library for Ruby.> I had looked at aspectr previously, but it''s status is listed as alpha, and > it seems to have stagnated around 2002. Is the project being actively > developed somewhere else, or is it mostly complete?> I just tried googling for aspect4r in case it was different, but I can''t > really find anything about it(only 3 results, two in chinese). If it is > different do you know where I can find it?I haven''t heard any mention of it in 6-12 months, so I can''t vouch for it in any way. But the maintainer of it is still Ruby-active, IIRC, and would be responsive to email. Also, I doubt the code is very complex, so if all else fails it could be the basis for your own home-grown solution. Gavin