2011-04-28 04:56:11 -04:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
2011-05-02 18:37:40 -04:00
|
|
|
require 'active_support/core_ext/hash/slice'
|
2011-05-02 19:36:58 -04:00
|
|
|
require 'active_support/core_ext/hash/except'
|
2011-05-17 14:51:44 -04:00
|
|
|
require 'active_support/core_ext/module/anonymous'
|
2011-04-28 04:56:11 -04:00
|
|
|
require 'action_dispatch/http/mime_types'
|
|
|
|
|
|
|
|
module ActionController
|
2011-08-27 16:55:54 -04:00
|
|
|
# Wraps the parameters hash into a nested hash. This will allow clients to submit
|
|
|
|
# POST requests without having to specify any root elements.
|
2011-04-28 04:56:11 -04:00
|
|
|
#
|
2011-08-20 14:19:57 -04:00
|
|
|
# This functionality is enabled in +config/initializers/wrap_parameters.rb+
|
2011-08-27 16:55:54 -04:00
|
|
|
# and can be customized. If you are upgrading to \Rails 3.1, this file will
|
2011-08-20 14:19:57 -04:00
|
|
|
# need to be created for the functionality to be enabled.
|
2011-04-28 04:56:11 -04:00
|
|
|
#
|
|
|
|
# You could also turn it on per controller by setting the format array to
|
2011-08-27 16:55:54 -04:00
|
|
|
# a non-empty array:
|
2011-04-28 04:56:11 -04:00
|
|
|
#
|
|
|
|
# class UsersController < ApplicationController
|
|
|
|
# wrap_parameters :format => [:json, :xml]
|
|
|
|
# end
|
|
|
|
#
|
2011-08-27 16:55:54 -04:00
|
|
|
# If you enable +ParamsWrapper+ for +:json+ format, instead of having to
|
2011-04-28 04:56:11 -04:00
|
|
|
# send JSON parameters like this:
|
|
|
|
#
|
|
|
|
# {"user": {"name": "Konata"}}
|
|
|
|
#
|
2011-08-27 16:55:54 -04:00
|
|
|
# You can send parameters like this:
|
2011-04-28 04:56:11 -04:00
|
|
|
#
|
|
|
|
# {"name": "Konata"}
|
|
|
|
#
|
2011-08-27 16:55:54 -04:00
|
|
|
# And it will be wrapped into a nested hash with the key name matching the
|
2011-04-28 04:56:11 -04:00
|
|
|
# controller's name. For example, if you're posting to +UsersController+,
|
|
|
|
# your new +params+ hash will look like this:
|
|
|
|
#
|
|
|
|
# {"name" => "Konata", "user" => {"name" => "Konata"}}
|
|
|
|
#
|
|
|
|
# You can also specify the key in which the parameters should be wrapped to,
|
2011-05-19 10:33:25 -04:00
|
|
|
# and also the list of attributes it should wrap by using either +:include+ or
|
|
|
|
# +:exclude+ options like this:
|
2011-04-28 04:56:11 -04:00
|
|
|
#
|
|
|
|
# class UsersController < ApplicationController
|
2011-05-19 10:33:25 -04:00
|
|
|
# wrap_parameters :person, :include => [:username, :password]
|
2011-04-28 04:56:11 -04:00
|
|
|
# end
|
|
|
|
#
|
2012-01-05 15:18:54 -05:00
|
|
|
# On ActiveRecord models with no +:include+ or +:exclude+ option set,
|
2011-12-07 22:50:01 -05:00
|
|
|
# if attr_accessible is set on that model, it will only wrap the accessible
|
2012-01-05 15:18:54 -05:00
|
|
|
# parameters, else it will only wrap the parameters returned by the class
|
2011-12-07 22:50:01 -05:00
|
|
|
# method attribute_names.
|
|
|
|
#
|
2011-04-28 04:56:11 -04:00
|
|
|
# If you're going to pass the parameters to an +ActiveModel+ object (such as
|
|
|
|
# +User.new(params[:user])+), you might consider passing the model class to
|
|
|
|
# the method instead. The +ParamsWrapper+ will actually try to determine the
|
|
|
|
# list of attribute names from the model and only wrap those attributes:
|
|
|
|
#
|
|
|
|
# class UsersController < ApplicationController
|
|
|
|
# wrap_parameters Person
|
|
|
|
# end
|
|
|
|
#
|
2011-05-19 10:33:25 -04:00
|
|
|
# You still could pass +:include+ and +:exclude+ to set the list of attributes
|
2011-04-28 04:56:11 -04:00
|
|
|
# you want to wrap.
|
|
|
|
#
|
|
|
|
# By default, if you don't specify the key in which the parameters would be
|
|
|
|
# wrapped to, +ParamsWrapper+ will actually try to determine if there's
|
|
|
|
# a model related to it or not. This controller, for example:
|
|
|
|
#
|
|
|
|
# class Admin::UsersController < ApplicationController
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# will try to check if +Admin::User+ or +User+ model exists, and use it to
|
2011-06-04 10:41:44 -04:00
|
|
|
# determine the wrapper key respectively. If both models don't exist,
|
2011-04-28 04:56:11 -04:00
|
|
|
# it will then fallback to use +user+ as the key.
|
|
|
|
module ParamsWrapper
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
EXCLUDE_PARAMETERS = %w(authenticity_token _method utf8)
|
|
|
|
|
|
|
|
included do
|
|
|
|
class_attribute :_wrapper_options
|
2011-05-19 10:33:25 -04:00
|
|
|
self._wrapper_options = { :format => [] }
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
# Sets the name of the wrapper key, or the model which +ParamsWrapper+
|
|
|
|
# would use to determine the attribute names from.
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
# wrap_parameters :format => :xml
|
2011-08-27 16:55:54 -04:00
|
|
|
# # enables the parameter wrapper for XML format
|
2011-04-28 04:56:11 -04:00
|
|
|
#
|
|
|
|
# wrap_parameters :person
|
|
|
|
# # wraps parameters into +params[:person]+ hash
|
|
|
|
#
|
|
|
|
# wrap_parameters Person
|
2011-06-04 10:41:44 -04:00
|
|
|
# # wraps parameters by determining the wrapper key from Person class
|
2011-04-28 04:56:11 -04:00
|
|
|
# (+person+, in this case) and the list of attribute names
|
|
|
|
#
|
2011-05-19 10:33:25 -04:00
|
|
|
# wrap_parameters :include => [:username, :title]
|
2011-04-28 04:56:11 -04:00
|
|
|
# # wraps only +:username+ and +:title+ attributes from parameters.
|
|
|
|
#
|
|
|
|
# wrap_parameters false
|
2011-06-04 10:41:44 -04:00
|
|
|
# # disables parameters wrapping for this controller altogether.
|
2011-04-28 04:56:11 -04:00
|
|
|
#
|
|
|
|
# ==== Options
|
|
|
|
# * <tt>:format</tt> - The list of formats in which the parameters wrapper
|
|
|
|
# will be enabled.
|
2011-05-19 10:33:25 -04:00
|
|
|
# * <tt>:include</tt> - The list of attribute names which parameters wrapper
|
2011-04-28 04:56:11 -04:00
|
|
|
# will wrap into a nested hash.
|
2011-05-19 10:33:25 -04:00
|
|
|
# * <tt>:exclude</tt> - The list of attribute names which parameters wrapper
|
2011-04-28 04:56:11 -04:00
|
|
|
# will exclude from a nested hash.
|
|
|
|
def wrap_parameters(name_or_model_or_options, options = {})
|
2011-05-02 18:37:40 -04:00
|
|
|
model = nil
|
|
|
|
|
|
|
|
case name_or_model_or_options
|
|
|
|
when Hash
|
2011-04-28 04:56:11 -04:00
|
|
|
options = name_or_model_or_options
|
2011-05-02 18:37:40 -04:00
|
|
|
when false
|
|
|
|
options = options.merge(:format => [])
|
|
|
|
when Symbol, String
|
|
|
|
options = options.merge(:name => name_or_model_or_options)
|
|
|
|
else
|
|
|
|
model = name_or_model_or_options
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
|
2011-05-02 18:37:40 -04:00
|
|
|
_set_wrapper_defaults(_wrapper_options.slice(:format).merge(options), model)
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the default wrapper key or model which will be used to determine
|
|
|
|
# wrapper key and attribute names. Will be called automatically when the
|
|
|
|
# module is inherited.
|
|
|
|
def inherited(klass)
|
|
|
|
if klass._wrapper_options[:format].present?
|
2011-05-06 01:11:06 -04:00
|
|
|
klass._set_wrapper_defaults(klass._wrapper_options.slice(:format))
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2011-05-02 18:37:40 -04:00
|
|
|
protected
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
# Determine the wrapper model from the controller's name. By convention,
|
|
|
|
# this could be done by trying to find the defined model that has the
|
|
|
|
# same singularize name as the controller. For example, +UsersController+
|
|
|
|
# will try to find if the +User+ model exists.
|
2011-05-10 18:08:18 -04:00
|
|
|
#
|
|
|
|
# This method also does namespace lookup. Foo::Bar::UsersController will
|
|
|
|
# try to find Foo::Bar::User, Foo::User and finally User.
|
|
|
|
def _default_wrap_model #:nodoc:
|
2011-05-17 14:51:44 -04:00
|
|
|
return nil if self.anonymous?
|
2011-12-05 11:05:03 -05:00
|
|
|
model_name = self.name.sub(/Controller$/, '').classify
|
2011-04-28 04:56:11 -04:00
|
|
|
|
|
|
|
begin
|
2011-09-23 10:46:33 -04:00
|
|
|
if model_klass = model_name.safe_constantize
|
|
|
|
model_klass
|
|
|
|
else
|
2011-05-10 18:08:18 -04:00
|
|
|
namespaces = model_name.split("::")
|
|
|
|
namespaces.delete_at(-2)
|
|
|
|
break if namespaces.last == model_name
|
|
|
|
model_name = namespaces.join("::")
|
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
end until model_klass
|
|
|
|
|
|
|
|
model_klass
|
|
|
|
end
|
2011-05-02 18:37:40 -04:00
|
|
|
|
|
|
|
def _set_wrapper_defaults(options, model=nil)
|
|
|
|
options = options.dup
|
|
|
|
|
2011-05-19 10:33:25 -04:00
|
|
|
unless options[:include] || options[:exclude]
|
2011-05-02 18:37:40 -04:00
|
|
|
model ||= _default_wrap_model
|
2011-12-07 22:50:01 -05:00
|
|
|
if model.respond_to?(:accessible_attributes) && model.accessible_attributes.present?
|
|
|
|
options[:include] = model.accessible_attributes.to_a
|
|
|
|
elsif model.respond_to?(:attribute_names) && model.attribute_names.present?
|
2011-05-19 10:33:25 -04:00
|
|
|
options[:include] = model.attribute_names
|
2011-05-02 18:37:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-17 14:51:44 -04:00
|
|
|
unless options[:name] || self.anonymous?
|
2011-05-02 18:37:40 -04:00
|
|
|
model ||= _default_wrap_model
|
|
|
|
options[:name] = model ? model.to_s.demodulize.underscore :
|
|
|
|
controller_name.singularize
|
|
|
|
end
|
|
|
|
|
2012-01-05 15:18:54 -05:00
|
|
|
options[:include] = Array(options[:include]).collect(&:to_s) if options[:include]
|
|
|
|
options[:exclude] = Array(options[:exclude]).collect(&:to_s) if options[:exclude]
|
|
|
|
options[:format] = Array(options[:format])
|
2011-05-02 18:37:40 -04:00
|
|
|
|
|
|
|
self._wrapper_options = options
|
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Performs parameters wrapping upon the request. Will be called automatically
|
|
|
|
# by the metal call stack.
|
|
|
|
def process_action(*args)
|
|
|
|
if _wrapper_enabled?
|
2011-05-02 19:36:58 -04:00
|
|
|
wrapped_hash = _wrap_parameters request.request_parameters
|
|
|
|
wrapped_filtered_hash = _wrap_parameters request.filtered_parameters
|
2011-04-28 04:56:11 -04:00
|
|
|
|
|
|
|
# This will make the wrapped hash accessible from controller and view
|
|
|
|
request.parameters.merge! wrapped_hash
|
|
|
|
request.request_parameters.merge! wrapped_hash
|
|
|
|
|
|
|
|
# This will make the wrapped hash displayed in the log file
|
2011-05-02 19:36:58 -04:00
|
|
|
request.filtered_parameters.merge! wrapped_filtered_hash
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2011-05-02 19:36:58 -04:00
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
# Returns the wrapper key which will use to stored wrapped parameters.
|
|
|
|
def _wrapper_key
|
2011-05-02 18:37:40 -04:00
|
|
|
_wrapper_options[:name]
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the list of enabled formats.
|
|
|
|
def _wrapper_formats
|
2011-05-02 18:37:40 -04:00
|
|
|
_wrapper_options[:format]
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
|
2011-05-02 19:36:58 -04:00
|
|
|
# Returns the list of parameters which will be selected for wrapped.
|
|
|
|
def _wrap_parameters(parameters)
|
2011-05-19 10:33:25 -04:00
|
|
|
value = if include_only = _wrapper_options[:include]
|
|
|
|
parameters.slice(*include_only)
|
2011-05-02 19:36:58 -04:00
|
|
|
else
|
2011-05-19 10:33:25 -04:00
|
|
|
exclude = _wrapper_options[:exclude] || []
|
|
|
|
parameters.except(*(exclude + EXCLUDE_PARAMETERS))
|
2011-05-02 19:36:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
{ _wrapper_key => value }
|
|
|
|
end
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
# Checks if we should perform parameters wrapping.
|
|
|
|
def _wrapper_enabled?
|
2011-05-02 18:37:40 -04:00
|
|
|
ref = request.content_mime_type.try(:ref)
|
2011-05-17 06:55:03 -04:00
|
|
|
_wrapper_formats.include?(ref) && _wrapper_key && !request.request_parameters[_wrapper_key]
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|