Rails源代码分析(23):ActionController::Verification

1 使用
这个Module提供了一个类级别方法,验证某个方法就是指定的先决条件。
可以看作是一个特别的before_filter
  1.        class GlobalController < ActionController::Base
  2.     #     # Prevent the #update_settings action from being invoked unless
  3.     #     # the 'admin_privileges' request parameter exists. The
  4.     #     # settings action will be redirected to in current controller
  5.     #     # if verification fails.
  6.          verify :params => "admin_privileges":only => :update_post,
  7.                 :redirect_to => { :action => "settings" }
  8.     #
  9.     #     # Disallow a post from being updated if there was no information
  10.     #     # submitted with the post, and if there is no active post in the
  11.     #     # session, and if there is no "note" key in the flash. The route
  12.     #     # named category_url will be redirected to if verification fails.
  13.     #
  14.          verify :params => "post":session => "post""flash" => "note",
  15.                 :only => :update_post,
  16.                 :add_flash => { "alert" => "Failed to create your message" },
  17.                 :redirect_to => :category_url 
参数介绍:
  1.       # Verify the given actions so that if certain prerequisites are not met,
  2.       # the user is redirected to a different action. The +options+ parameter
  3.       # is a hash consisting of the following key/value pairs:
  4.       #
  5.       # <tt>:params</tt>:: 
  6.       #   a single key or an array of keys that must be in the <tt>params</tt> 
  7.       #   hash in order for the action(s) to be safely called.
  8.       # <tt>:session</tt>:: 
  9.       #   a single key or an array of keys that must be in the <tt>session</tt> 
  10.       #   in order for the action(s) to be safely called.
  11.       # <tt>:flash</tt>:: 
  12.       #   a single key or an array of keys that must be in the flash in order 
  13.       #   for the action(s) to be safely called.
  14.       # <tt>:method</tt>:: 
  15.       #   a single key or an array of keys--any one of which must match the 
  16.       #   current request method in order for the action(s) to be safely called. 
  17.       #   (The key should be a symbol: <tt>:get</tt> or <tt>:post</tt>, for 
  18.       #   example.)
  19.       # <tt>:xhr</tt>:: 
  20.       #   true/false option to ensure that the request is coming from an Ajax 
  21.       #   call or not. 
  22.       # <tt>:add_flash</tt>:: 
  23.       #   a hash of name/value pairs that should be merged into the session's 
  24.       #   flash if the prerequisites cannot be satisfied.
  25.       # <tt>:add_headers</tt>:: 
  26.       #   a hash of name/value pairs that should be merged into the response's 
  27.       #   headers hash if the prerequisites cannot be satisfied.
  28.       # <tt>:redirect_to</tt>:: 
  29.       #   the redirection parameters to be used when redirecting if the 
  30.       #   prerequisites cannot be satisfied. You can redirect either to named 
  31.       #   route or to the action in some controller.
  32.       # <tt>:render</tt>:: 
  33.       #   the render parameters to be used when the prerequisites cannot be satisfied.
  34.       # <tt>:only</tt>:: 
  35.       #   only apply this verification to the actions specified in the associated 
  36.       #   array (may also be a single value).
  37.       # <tt>:except</tt>:: 
  38.       #   do not apply this verification to the actions specified in the associated 
  39.       #   array (may also be a single value).

2 实现
  1.     module ClassMethods
  2.       def verify(options={})
  3.         # 增加了一个before_filter,这个方法其实就是before_filter的包装
  4.         before_filter :only => options[:only], :except => options[:exceptdo |c|
  5.           c.send! :verify_action, options
  6.         end
  7.       end
  8.     end

  1.   private
  2.     def verify_action(options) #:nodoc:
  3.       if prereqs_invalid?(options) # 验证如果不通过
  4.         flash.update(options[:add_flash])              if options[:add_flash] # 填写flash
  5.         response.headers.update(options[:add_headers]) if options[:add_headers] # 增加header
  6.         apply_remaining_actions(options)               unless performed?
  7.       end
  8.     end
  9.     
  10.     def prereqs_invalid?(options) # 验证入口先决条件是否满足
  11.       # 下列3个只要有一个不满足,就不满足
  12.       verify_presence_of_keys_in_hash_flash_or_params(options) || 
  13.       verify_method(options) || 
  14.       verify_request_xhr_status(options)
  15.     end
  16.  
  17.     def verify_presence_of_keys_in_hash_flash_or_params(options) # :nodoc:
  18.       # 这里一个语法很特别 [*options[:params] ] 这里的*操作是将数组解成参数格式
  19.       [*options[:params] ].find { |v| params[v].nil?  } ||
  20.       [*options[:session]].find { |v| session[v].nil? } ||
  21.       [*options[:flash]  ].find { |v| flash[v].nil?   }
  22.     end
  23.     
  24.     def verify_method(options) # :nodoc:
  25.       # 验证方法是不是一致的
  26.       [*options[:method]].all? { |v| request.method != v.to_sym } if options[:method]
  27.     end
  28.     
  29.     def verify_request_xhr_status(options) # :nodoc:
  30.       # 验证是不是远程ajax调用
  31.       request.xhr? != options[:xhrunless options[:xhr].nil?
  32.     end
  33.     
  34.     def apply_redirect_to(redirect_to_option) # :nodoc:
  35.       # 如果是Symbol是方法名
  36.       redirect_to_option.is_a?(Symbol) ? self.send!(redirect_to_option) : redirect_to_option
  37.     end
  38.     
  39.     def apply_remaining_actions(options) # :nodoc:
  40.       # 根据参数执行操作剩余操作
  41.       case
  42.         when options[:render]      ; render(options[:render])
  43.         when options[:redirect_to] ; redirect_to(apply_redirect_to(options[:redirect_to]))
  44.         else head(:bad_request)
  45.       end
  46.     end
这个验证比较简单,就有一个以前不太了解的*的用法.

1.乘法
2.数组 * INT

 

  1. [1,2,3] *2 => [1,2,3],[1,2,3]

   
3.字符串* INT

 

  1. "hello world!"*2 => "hello world!hello world!"  

   
4.相当于取数组的data[]值

  1. *a = 1,2,3
  2. a => [1,2,3]  

 

  1. a= [1 , 2]  
  2. testFun(*a) => testFun(1 , 2) 而不是 testFun([1,2])  






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值