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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/034o-mqwHkMJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Meant to save callback instead of filter. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2VtMqdlQkFUJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 11, 10:12 pm, Carson Cole <carson.c...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fred, Thank you so much for the reply, and it was a great one. Did not know about that and you broadened my knowledge of extend vs. include and included. I had only been using modules to extend instance methods, but this opened up a new world. From your answer, I was able to remove 5 lines of code from 12 models and can now manage the function from just one location (rather than having to fix 5 lines X 12 models--tedious!). Honored to have you respond, since I know that you have given many, many hours to responses over the years, and they have always been top quality and educational. How about writing a good Ruby book with some Rails 3.1 goodness? You can call it "Fred''s Ruby Tips & Tricks". I''d buy it. Carson Cole -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/-5wOyR06OoMJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.