关于around_filter 的调用


 
Java代码  收藏代码
  1. def call_filter(chain, index)  
  2.   
  3.         return (performed? || perform_action_without_filters) if index >= chain.size  
  4.   
  5.         filter = chain[index]  
  6.   
  7.         return call_filter(chain, index.next) if self.class.filter_excluded_from_action?(filter,action_name)  
  8.   
  9.   
  10.   
  11.         halted = false  
  12.   
  13.         filter.call(self) do  
  14.   
  15.           halted = call_filter(chain, index.next)  
  16.   
  17.         end  
  18.   
  19.         halt_filter_chain(filter.filter, :no_yield) if halted == false unless @before_filter_chain_aborted  
  20.   
  21.         halted  
  22.   
  23.       end  


    在control 的 around_filter的函数和代码块中,总是会需要yield, 那么这个yield 执行的参数是什么呢,就是上面的 call_filter(chain,index.next). 通过这几行代码,就可以解释如下代码的调用结果
"---------test_one-------------"
"-----------test_two-----------"
"--------end of test_two-------"
"---------end of test_one------"

Java代码  收藏代码
    1. around_filter :test_one  
    2.   around_filter :test_two  
    3.   
    4.   def test_one  
    5.     p "---------test_one-------------"  
    6.     yield  
    7.     p "---------end of test_one------"  
    8.   end  
    9.   
    10.   def test_two  
    11.     p "-----------test_two-----------"  
    12.     yield  
    13.     p "--------end of test_two-------"  
    14.   end 
原文地址:https://www.cnblogs.com/wangyuyu/p/3341388.html