You are here: Forums Ask a Rails expert calling class method in before...
Posted in Forums : Ask a Rails expert
Authority 12
Posting Rating 0
Sign in to rate this post
|
Im using a ruby plugin and it asked me to use following code fragment as before filter. before_filter CASClient::Frameworks::Rails::Filter Then i went through the code and noticed that the class Filter in inside the modules Rails, Framworks,CASClient hierarchically. so in above code fragment it has mentioned only the class name (Filter) as before filter. But as far as i know we have to specify the method name in there. So how does this happen in this case. Is it something like default method taking care of the action? If so how does it identify it???? Thanx in advance |
Authority 25
Posting Rating 88
Sign in to rate this post
|
There’s a bit of magic going on there, but basically when Rails is about to call the filter it does a series of checks fo see if the supplied filter is a Symbol, String, Proc, or Method, and then does the appropriate thing with each case. I.e. in case of Symbol it calls the method named in the symbol, in case of String it evaluates the string etc.. Now since in your example the result doesn’t match any of the above cases the final check Rails does is to see if the supplied argument responds to “filter”, and if so it will then call the “filter” method on the supplied object. Now CASClient::Frameworks::Rails::Filter is a Module which actually contains the class method “filter”. So when Rails asks “respons_to?(:filter)” it will return true, and Rails will then call that method…neat, eh? :) The relevant magic bit is the “evaluate_method” defined in active_support/callbacks.rb: Hope it helps.. |
Authority 87
Posting Rating 84
Sign in to rate this post
|
Controller filters can be either the name of a method, a block, or in this case, a class that responds to a “filter” method. When invoked, the filter method is passed the instance of whichever controller you’ve declared it in. For additional information, see the docs here: ActionController::Filters |
Authority 12
Posting Rating 0
Sign in to rate this post
|
wow what a wonderful explanation… Thanx a lot. In fact it’s weired at the first instance to understand by looking the code. But these magics can be understood very well after such kind of explanation…..Bravo |
Authority 87
Posting Rating 84
Sign in to rate this post
|
Hey, recommend me if you have the chance ;) |
Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController
Ask a Rails expert : Best way to structure a database for a large/static dataset
Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)
Ask a Rails expert : Above Ground Pool Supplies
Ask a Rails expert : How to get url params in observer or model in Rails 3.1
Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?
Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?
Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )
Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob
Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )