- class << self
- # Factory for the standard create, process loop where the controller is discarded after processing.
- def process(request, response) #:nodoc:
- new.process(request, response)
- end
- # Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
- def controller_class_name
- @controller_class_name ||= name.demodulize
- end
- # Converts the class name from something like "OneModule::TwoModule::NeatController" to "neat".
- def controller_name
- @controller_name ||= controller_class_name.sub(/Controller$/, '').underscore
- end
- # Converts the class name from something like "OneModule::TwoModule::NeatController" to "one_module/two_module/neat".
- def controller_path
- @controller_path ||= name.gsub(/Controller$/, '').underscore
- end
- # Return an array containing the names of public methods that have been marked hidden from the action processor.
- # By default, all methods defined in ActionController::Base and included modules are hidden.
- # More methods can be hidden using <tt>hide_actions</tt>.
- def hidden_actions # 無法通過action processor進入的方法
- unless read_inheritable_attribute(:hidden_actions)
- write_inheritable_attribute(:hidden_actions, ActionController::Base.public_instance_methods.map(:to_s))
- end
- read_inheritable_attribute(:hidden_actions)
- end
- # Hide each of the given methods from being callable as actions.
- def hide_action(*names) # 添加無法通過action processor訪問的方法
- write_inheritable_attribute(:hidden_actions, hidden_actions | names.map(:to_s))
- end
- ## View load paths determine the bases from which template references can be made. So a call to
- ## render("test/template") will be looked up in the view load paths array and the closest match will be
- ## returned.
- def view_paths
- @view_paths || superclass.view_paths
- end
- def view_paths=(value)
- @view_paths = value
- ActionView::TemplateFinder.process_view_paths(value) #查找view_path
- end
- # Adds a view_path to the front of the view_paths array.
- # If the current class has no view paths, copy them from
- # the superclass. This change will be visible for all future requests.
- #
- # ArticleController.prepend_view_path("views/default")
- # ArticleController.prepend_view_path(["views/default", "views/custom"])
- #
- def prepend_view_path(path)
- @view_paths = superclass.view_paths.dup if @view_paths.nil?
- view_paths.unshift(*path)
- ActionView::TemplateFinder.process_view_paths(path)
- end
- # Adds a view_path to the end of the view_paths array.
- # If the current class has no view paths, copy them from
- # the superclass. This change will be visible for all future requests.
- #
- # ArticleController.append_view_path("views/default")
- # ArticleController.append_view_path(["views/default", "views/custom"])
- #
- def append_view_path(path)
- @view_paths = superclass.view_paths.dup if @view_paths.nil?
- view_paths.push(*path)
- ActionView::TemplateFinder.process_view_paths(path)
- end
- # Replace sensitive parameter data from the request log.
- # Filters parameters that have any of the arguments as a substring.
- # Looks in all subhashes of the param hash for keys to filter.
- # If a block is given, each key and value of the parameter hash and all
- # subhashes is passed to it, the value or key
- # can be replaced using String#replace or similar method.
- #
- # Examples:
- # filter_parameter_logging
- # => Does nothing, just slows the logging process down
- #
- # filter_parameter_logging :password
- # => replaces the value to all keys matching /password/i with "[FILTERED]"
- #
- # filter_parameter_logging :foo, "bar"
- # => replaces the value to all keys matching /foo|bar/i with "[FILTERED]"
- #
- # filter_parameter_logging { |k,v| v.reverse! if k =~ /secret/i }
- # => reverses the value to all keys matching /secret/i
- #
- # filter_parameter_logging(:foo, "bar") { |k,v| v.reverse! if k =~ /secret/i }
- # => reverses the value to all keys matching /secret/i, and
- # replaces the value to all keys matching /foo|bar/i with "[FILTERED]"
- def filter_parameter_logging(*filter_words, &block)
- parameter_filter = Regexp.new(filter_words.collect{ |s| s.to_s }.join('|'), true) if filter_words.length > 0 # 構造需要過濾得參數
- define_method(:filter_parameters) do |unfiltered_parameters|
- filtered_parameters = {}
- unfiltered_parameters.each do |key, value|
- if key =~ parameter_filter # 如果需要過濾
- filtered_parameters[key] = '[FILTERED]'
- elsif value.is_a?(Hash)
- filtered_parameters[key] = filter_parameters(value)
- elsif block_given?
- key = key.dup
- value = value.dup if value
- yield key, value
- filtered_parameters[key] = value
- else
- filtered_parameters[key] = value
- end
- end
- filtered_parameters
- end
- protected :filter_parameters
- end
- # Don't render layouts for templates with the given extensions.
- def exempt_from_layout(*extensions)
- regexps = extensions.collect do |extension|
- extension.is_a?(Regexp) ? extension : //.#{Regexp.escape(extension.to_s)}$/
- end
- @@exempt_from_layout.merge regexps
- end
- end
- def url_for(options = nil) #:doc:
- case options || {}
- when String
- options
- when Hash
- @url.rewrite(rewrite_options(options))
- else
- polymorphic_url(options)
- end
- end
- # Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
- def controller_class_name
- self.class.controller_class_name
- end
- # Converts the class name from something like "OneModule::TwoModule::NeatController" to "neat".
- def controller_name
- self.class.controller_name
- end
- # Converts the class name from something like "OneModule::TwoModule::NeatController" to "one_module/two_module/neat".
- def controller_path
- self.class.controller_path
- end
- def session_enabled?
- request.session_options && request.session_options[:disabled] != false
- end
- self.view_paths = []
- # View load paths for controller.
- def view_paths
- @template.finder.view_paths
- end
- def view_paths=(value)
- @template.finder.view_paths = value # Mutex needed
- end
- # Adds a view_path to the front of the view_paths array.
- # This change affects the current request only.
- #
- # self.prepend_view_path("views/default")
- # self.prepend_view_path(["views/default", "views/custom"])
- #
- def prepend_view_path(path)
- @template.finder.prepend_view_path(path) # Mutex needed
- end
- # Adds a view_path to the end of the view_paths array.
- # This change affects the current request only.
- #
- # self.append_view_path("views/default")
- # self.append_view_path(["views/default", "views/custom"])
- #
- def append_view_path(path)
- @template.finder.append_view_path(path) # Mutex needed
- end