views:

51

answers:

1

I have a newbie kind of question which I cant get my head around. How does the Proc in the if condition of the caches_action get executed for the caches_action method.

for example caches_action :show, :if=>Proc.new{|x| something} what i dont get its how does this get called. I know i can execute a proc defined as

proc= Proc.new by proc.call

so i dont understand how this gets called. Second how do I pass conditions like

if logged_in?

I'd appreciate any help on this

+1  A: 

The parameter pass on Proc is the current object. So in your example it's the x variable. So you can call all method of this instance. If you want call the logged_in? method. You can because it's a public instance

caches_action :show, :if => Proc.new{|x| x.logged_in? }

The proc is call before the filter. A caches_action is like a before_filter. This filter check if there are already a cache about this action or not. Unless, the cache is generate.

With the :if the filter is call only if the if is call. So the proc is call. If you don't use a Proc, the :if value is interpret only during the file reading on the server starting.

shingara
Thanks for response. What I would also like to know is what invokes the Proc or how does this get called. Is there some internal method that does a Proc.new.call kind of operation. I dont really follow how it gets called.
Sid
I added some extra information. It's like you want ?
shingara