Rails源代码分析(29):回到ActionController(1)

来源:互联网 发布:语音朗读软件安卓版 编辑:程序博客网 时间:2024/05/22 15:25
前面那么长时间都是在分析Controller,基本有了比较清晰的认识。
现在回到controller里面看看还有哪些方面遗漏了

类方法:
  1.     class << self
  2.       # Factory for the standard create, process loop where the controller is discarded after processing.
  3.       def process(request, response) #:nodoc:
  4.         new.process(request, response)
  5.       end
  6.       # Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
  7.       def controller_class_name
  8.         @controller_class_name ||= name.demodulize
  9.       end
  10.       # Converts the class name from something like "OneModule::TwoModule::NeatController" to "neat".
  11.       def controller_name
  12.         @controller_name ||= controller_class_name.sub(/Controller$/, '').underscore
  13.       end
  14.       # Converts the class name from something like "OneModule::TwoModule::NeatController" to "one_module/two_module/neat".
  15.       def controller_path
  16.         @controller_path ||= name.gsub(/Controller$/, '').underscore
  17.       end
  18.       # Return an array containing the names of public methods that have been marked hidden from the action processor.
  19.       # By default, all methods defined in ActionController::Base and included modules are hidden.
  20.       # More methods can be hidden using <tt>hide_actions</tt>.
  21.       def hidden_actions  # 无法通过action processor进入的方法
  22.         unless read_inheritable_attribute(:hidden_actions)
  23.           write_inheritable_attribute(:hidden_actions, ActionController::Base.public_instance_methods.map(:to_s))
  24.         end
  25.         read_inheritable_attribute(:hidden_actions)
  26.       end
  27.       # Hide each of the given methods from being callable as actions.
  28.       def hide_action(*names) # 添加无法通过action processor访问的方法
  29.         write_inheritable_attribute(:hidden_actions, hidden_actions | names.map(:to_s))
  30.       end
  31.       ## View load paths determine the bases from which template references can be made. So a call to
  32.       ## render("test/template") will be looked up in the view load paths array and the closest match will be
  33.       ## returned.
  34.       def view_paths
  35.         @view_paths || superclass.view_paths
  36.       end
  37.       def view_paths=(value)
  38.         @view_paths = value
  39.         ActionView::TemplateFinder.process_view_paths(value) #查找view_path
  40.       end
  41.       # Adds a view_path to the front of the view_paths array.
  42.       # If the current class has no view paths, copy them from
  43.       # the superclass.  This change will be visible for all future requests.
  44.       #
  45.       #   ArticleController.prepend_view_path("views/default")
  46.       #   ArticleController.prepend_view_path(["views/default", "views/custom"])
  47.       #
  48.       def prepend_view_path(path)
  49.         @view_paths = superclass.view_paths.dup if @view_paths.nil?
  50.         view_paths.unshift(*path)
  51.         ActionView::TemplateFinder.process_view_paths(path)
  52.       end
  53.       # Adds a view_path to the end of the view_paths array.
  54.       # If the current class has no view paths, copy them from
  55.       # the superclass. This change will be visible for all future requests.
  56.       #
  57.       #   ArticleController.append_view_path("views/default")
  58.       #   ArticleController.append_view_path(["views/default", "views/custom"])
  59.       #
  60.       def append_view_path(path)
  61.         @view_paths = superclass.view_paths.dup if @view_paths.nil?
  62.         view_paths.push(*path)
  63.         ActionView::TemplateFinder.process_view_paths(path)
  64.       end
  65.       # Replace sensitive parameter data from the request log.
  66.       # Filters parameters that have any of the arguments as a substring.
  67.       # Looks in all subhashes of the param hash for keys to filter.
  68.       # If a block is given, each key and value of the parameter hash and all
  69.       # subhashes is passed to it, the value or key
  70.       # can be replaced using String#replace or similar method.
  71.       #
  72.       # Examples:
  73.       #   filter_parameter_logging
  74.       #   => Does nothing, just slows the logging process down
  75.       #
  76.       #   filter_parameter_logging :password
  77.       #   => replaces the value to all keys matching /password/i with "[FILTERED]"
  78.       #
  79.       #   filter_parameter_logging :foo, "bar"
  80.       #   => replaces the value to all keys matching /foo|bar/i with "[FILTERED]"
  81.       #
  82.       #   filter_parameter_logging { |k,v| v.reverse! if k =~ /secret/i }
  83.       #   => reverses the value to all keys matching /secret/i
  84.       #
  85.       #   filter_parameter_logging(:foo, "bar") { |k,v| v.reverse! if k =~ /secret/i }
  86.       #   => reverses the value to all keys matching /secret/i, and
  87.       #      replaces the value to all keys matching /foo|bar/i with "[FILTERED]"
  88.       def filter_parameter_logging(*filter_words, &block)
  89.         parameter_filter = Regexp.new(filter_words.collect{ |s| s.to_s }.join('|'), trueif filter_words.length > 0 # 构造需要过滤得参数
  90.         define_method(:filter_parametersdo |unfiltered_parameters|
  91.           filtered_parameters = {}
  92.           unfiltered_parameters.each do |key, value|
  93.             if key =~ parameter_filter # 如果需要过滤
  94.               filtered_parameters[key] = '[FILTERED]'
  95.             elsif value.is_a?(Hash)
  96.               filtered_parameters[key] = filter_parameters(value)
  97.             elsif block_given?
  98.               key = key.dup
  99.               value = value.dup if value
  100.               yield key, value
  101.               filtered_parameters[key] = value
  102.             else
  103.               filtered_parameters[key] = value
  104.             end
  105.           end
  106.           filtered_parameters
  107.         end
  108.         protected :filter_parameters
  109.       end
  110.       # Don't render layouts for templates with the given extensions.
  111.       def exempt_from_layout(*extensions)
  112.         regexps = extensions.collect do |extension|
  113.           extension.is_a?(Regexp) ? extension : //.#{Regexp.escape(extension.to_s)}$/
  114.         end
  115.         @@exempt_from_layout.merge regexps
  116.       end
  117.     end
实例方法:

  1.       def url_for(options = nil#:doc:
  2.         case options || {}
  3.           when String
  4.             options
  5.           when Hash
  6.             @url.rewrite(rewrite_options(options))
  7.           else
  8.             polymorphic_url(options)
  9.         end
  10.       end
  11.       # Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
  12.       def controller_class_name
  13.         self.class.controller_class_name
  14.       end
  15.       # Converts the class name from something like "OneModule::TwoModule::NeatController" to "neat".
  16.       def controller_name
  17.         self.class.controller_name
  18.       end
  19.       # Converts the class name from something like "OneModule::TwoModule::NeatController" to "one_module/two_module/neat".
  20.       def controller_path
  21.         self.class.controller_path
  22.       end
  23.       def session_enabled?
  24.         request.session_options && request.session_options[:disabled] != false
  25.       end
  26.       self.view_paths = []
  27.       # View load paths for controller.
  28.       def view_paths
  29.         @template.finder.view_paths
  30.       end
  31.       def view_paths=(value)
  32.         @template.finder.view_paths = value  # Mutex needed
  33.       end
  34.       # Adds a view_path to the front of the view_paths array.
  35.       # This change affects the current request only.
  36.       #
  37.       #   self.prepend_view_path("views/default")
  38.       #   self.prepend_view_path(["views/default", "views/custom"])
  39.       #
  40.       def prepend_view_path(path)
  41.         @template.finder.prepend_view_path(path)  # Mutex needed
  42.       end
  43.       # Adds a view_path to the end of the view_paths array.
  44.       # This change affects the current request only.
  45.       #
  46.       #   self.append_view_path("views/default")
  47.       #   self.append_view_path(["views/default", "views/custom"])
  48.       #
  49.       def append_view_path(path)
  50.         @template.finder.append_view_path(path)  # Mutex needed
  51.       end