On Nov 11, 10:12 pm, Carson Cole <[email protected]> wrote:
> How could one modularlize a group of filters to be used in many models? I
> like to avoid this (again, I am doing this in many models):
>
> class Something
> before_filter :a
> before_filter :b
> after_filter :c
> end
>
> and instead, just be able to do this (something like it)
>
> class Something
> ....
> end
>
> Is this solvable through inheritance?
So, you could have an abstract base class, but obviously doesn't help
if you have multiple sets of callbacks, filters etc and models need to
be able to mix and match at will
A more flexible way, using modules is
module MyCallbackPackage
extend ActiveSupport::Concern
included do
before_filter :a
before_filter :b
end
def a
end
def b
end
end
then you can do
class MyController < ActionController::Base
include MyCallbackPackage
end
The ActiveSupport::Concern gives you the included method and makes
that side of things more convenient. You don't need to use it - you
can achieve it all through self.included
Fred
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.