Permalink
Browse files

Merge branch 'master' of github.com:rails/rails

  • Loading branch information...
2 parents b29a905 + 35d0d82 commit b359f9fe7cc3f664e145fae7b0d5b5c309587ef8 @dhh dhh committed May 3, 2011
Showing with 865 additions and 441 deletions.
  1. +26 −2 actionpack/CHANGELOG
  2. +1 −0 actionpack/actionpack.gemspec
  3. +1 −0 actionpack/lib/action_controller.rb
  4. +7 −3 actionpack/lib/action_controller/base.rb
  5. +1 −1 actionpack/lib/action_controller/metal/instrumentation.rb
  6. +224 −0 actionpack/lib/action_controller/metal/params_wrapper.rb
  7. +1 −1 actionpack/lib/action_controller/metal/streaming.rb
  8. +22 −5 actionpack/lib/action_dispatch/http/mime_negotiation.rb
  9. +2 −0 actionpack/lib/action_dispatch/railtie.rb
  10. +0 −125 actionpack/lib/action_view/helpers/text_helper.rb
  11. +11 −0 actionpack/test/controller/log_subscriber_test.rb
  12. +202 −0 actionpack/test/controller/params_wrapper_test.rb
  13. +53 −0 actionpack/test/dispatch/request/json_params_parsing_test.rb
  14. +38 −0 actionpack/test/dispatch/request/xml_params_parsing_test.rb
  15. +34 −0 actionpack/test/dispatch/request_test.rb
  16. +0 −254 actionpack/test/template/text_helper_test.rb
  17. +8 −0 activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb
  18. +2 −4 activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
  19. +2 −4 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
  20. +24 −3 activerecord/lib/active_record/query_cache.rb
  21. +0 −17 activerecord/test/cases/identity_map_test.rb
  22. +12 −0 activerecord/test/cases/log_subscriber_test.rb
  23. +53 −0 activerecord/test/cases/query_cache_test.rb
  24. +70 −0 activeresource/examples/performance.rb
  25. +24 −15 activeresource/lib/active_resource/base.rb
  26. +4 −7 railties/lib/rails/generators/app_base.rb
  27. +12 −0 railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt
  28. +31 −0 railties/test/application/configuration_test.rb
View
@@ -1,10 +1,34 @@
*Rails 3.1.0 (unreleased)*
+* auto_link has been removed with no replacement. If you still use auto_link
+ please install the rails_autolink gem:
+ http://github.com/tenderlove/rails_autolink
+
+ [tenderlove]
+
+* Added streaming support, you can enable it with: [José Valim]
+
+ class PostsController < ActionController::Base
+ stream :only => :index
+ end
+
+ Please read the docs at `ActionController::Streaming` for more information.
+
+* Added `ActionDispatch::Request.ignore_accept_header` to ignore accept headers and only consider the format given as parameter [José Valim]
+
+* Created `ActionView::Renderer` and specified an API for `ActionView::Context`, check those objects for more information [José Valim]
+
+* Added `ActionController::ParamsWrapper` to wrap parameters into a nested hash, and will be turned on for JSON request in new applications by default [Prem Sichanugrist]
+
+ This can be customized by setting `ActionController::Base.wrap_parameters` in `config/initializer/wrap_parameters.rb`
+
* RJS has been extracted out to a gem. [fxn]
-* Implicit actions named not_implemented can be rendered [Santiago Pastorino]
+* Implicit actions named not_implemented can be rendered. [Santiago Pastorino]
+
+* Wildcard route will always match the optional format segment by default. [Prem Sichanugrist]
-* Wildcard route will always matching the optional format segment by default. For example if you have this route:
+ For example if you have this route:
map '*pages' => 'pages#show'
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
s.add_dependency('rack', '~> 1.2.1')
s.add_dependency('rack-test', '~> 0.5.7')
s.add_dependency('rack-mount', '~> 0.7.2')
+ s.add_dependency('sprockets', '~> 2.0.0.beta.1')
s.add_dependency('tzinfo', '~> 0.3.23')
s.add_dependency('erubis', '~> 2.7.0')
end
@@ -23,6 +23,7 @@ module ActionController
autoload :ImplicitRender
autoload :Instrumentation
autoload :MimeResponds
+ autoload :ParamsWrapper
autoload :RackDelegation
autoload :Redirecting
autoload :Renderers
@@ -206,13 +206,17 @@ def self.without_modules(*modules)
HttpAuthentication::Digest::ControllerMethods,
HttpAuthentication::Token::ControllerMethods,
+ # Before callbacks should also be executed the earliest as possible, so
+ # also include them at the bottom.
+ AbstractController::Callbacks,
+
# Add instrumentations hooks at the bottom, to ensure they instrument
# all the methods properly.
Instrumentation,
- # Before callbacks should also be executed the earliest as possible, so
- # also include them at the bottom.
- AbstractController::Callbacks,
+ # Params wrapper should come before instrumentation so they are
+ # properly showed in logs
+ ParamsWrapper,
# The same with rescue, append it at the end to wrap as much as possible.
Rescue
@@ -14,7 +14,7 @@ module Instrumentation
attr_internal :view_runtime
- def process_action(action, *args)
+ def process_action(*args)
raw_payload = {
:controller => self.class.name,
:action => self.action_name,
@@ -0,0 +1,224 @@
+require 'active_support/core_ext/class/attribute'
+require 'active_support/core_ext/hash/slice'
+require 'active_support/core_ext/hash/except'
+require 'active_support/core_ext/array/wrap'
+require 'action_dispatch/http/mime_types'
+
+module ActionController
+ # Wraps parameters hash into nested hash. This will allow client to submit
+ # POST request without having to specify a root element in it.
+ #
+ # By default this functionality won't be enabled. You can enable
+ # it globally by setting +ActionController::Base.wrap_parameters+:
+ #
+ # ActionController::Base.wrap_parameters = [:json]
+ #
+ # You could also turn it on per controller by setting the format array to
+ # non-empty array:
+ #
+ # class UsersController < ApplicationController
+ # wrap_parameters :format => [:json, :xml]
+ # end
+ #
+ # If you enable +ParamsWrapper+ for +:json+ format. Instead of having to
+ # send JSON parameters like this:
+ #
+ # {"user": {"name": "Konata"}}
+ #
+ # You can now just send a parameters like this:
+ #
+ # {"name": "Konata"}
+ #
+ # And it will be wrapped into a nested hash with the key name matching
+ # 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,
+ # and also the list of attributes it should wrap by using either +:only+ or
+ # +:except+ options like this:
+ #
+ # class UsersController < ApplicationController
+ # wrap_parameters :person, :only => [:username, :password]
+ # end
+ #
+ # 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
+ #
+ # You still could pass +:only+ and +:except+ to set the list of attributes
+ # 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
+ # determine the wrapper key respectively. If both of the model doesn't exists,
+ # 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
+ self._wrapper_options = {:format => []}
+ 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
+ # # enables the parmeter wrapper for XML format
+ #
+ # wrap_parameters :person
+ # # wraps parameters into +params[:person]+ hash
+ #
+ # wrap_parameters Person
+ # # wraps parameters by determine the wrapper key from Person class
+ # (+person+, in this case) and the list of attribute names
+ #
+ # wrap_parameters :only => [:username, :title]
+ # # wraps only +:username+ and +:title+ attributes from parameters.
+ #
+ # wrap_parameters false
+ # # disable parameters wrapping for this controller altogether.
+ #
+ # ==== Options
+ # * <tt>:format</tt> - The list of formats in which the parameters wrapper
+ # will be enabled.
+ # * <tt>:only</tt> - The list of attribute names which parameters wrapper
+ # will wrap into a nested hash.
+ # * <tt>:except</tt> - The list of attribute names which parameters wrapper
+ # will exclude from a nested hash.
+ def wrap_parameters(name_or_model_or_options, options = {})
+ model = nil
+
+ case name_or_model_or_options
+ when Hash
+ options = name_or_model_or_options
+ 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
+ end
+
+ _set_wrapper_defaults(_wrapper_options.slice(:format).merge(options), model)
+ 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?
+ klass._set_wrapper_defaults(klass._wrapper_options)
+ end
+ super
+ end
+
+ protected
+
+ # 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.
+ def _default_wrap_model
+ model_name = self.name.sub(/Controller$/, '').singularize
+
+ begin
+ model_klass = model_name.constantize
+ rescue NameError => e
+ unscoped_model_name = model_name.split("::", 2).last
+ break if unscoped_model_name == model_name
+ model_name = unscoped_model_name
+ end until model_klass
+
+ model_klass
+ end
+
+ def _set_wrapper_defaults(options, model=nil)
+ options = options.dup
+
+ unless options[:only] || options[:except]
+ model ||= _default_wrap_model
+ if model.respond_to?(:column_names)
+ options[:only] = model.column_names
+ end
+ end
+
+ unless options[:name]
+ model ||= _default_wrap_model
+ options[:name] = model ? model.to_s.demodulize.underscore :
+ controller_name.singularize
+ end
+
+ options[:only] = Array.wrap(options[:only]).collect(&:to_s) if options[:only]
+ options[:except] = Array.wrap(options[:except]).collect(&:to_s) if options[:except]
+ options[:format] = Array.wrap(options[:format])
+
+ self._wrapper_options = options
+ end
+ end
+
+ # Performs parameters wrapping upon the request. Will be called automatically
+ # by the metal call stack.
+ def process_action(*args)
+ if _wrapper_enabled?
+ wrapped_hash = _wrap_parameters request.request_parameters
+ wrapped_filtered_hash = _wrap_parameters request.filtered_parameters
+
+ # 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
+ request.filtered_parameters.merge! wrapped_filtered_hash
+ end
+ super
+ end
+
+ private
+
+ # Returns the wrapper key which will use to stored wrapped parameters.
+ def _wrapper_key
+ _wrapper_options[:name]
+ end
+
+ # Returns the list of enabled formats.
+ def _wrapper_formats
+ _wrapper_options[:format]
+ end
+
+ # Returns the list of parameters which will be selected for wrapped.
+ def _wrap_parameters(parameters)
+ value = if only = _wrapper_options[:only]
+ parameters.slice(*only)
+ else
+ except = _wrapper_options[:except] || []
+ parameters.except(*(except + EXCLUDE_PARAMETERS))
+ end
+
+ { _wrapper_key => value }
+ end
+
+ # Checks if we should perform parameters wrapping.
+ def _wrapper_enabled?
+ ref = request.content_mime_type.try(:ref)
+ _wrapper_formats.include?(ref) && !request.request_parameters[_wrapper_key]
+ end
+ end
+end
@@ -160,7 +160,7 @@ module ActionController #:nodoc:
# needs to inject contents in the HTML body.
#
# Also +Rack::Cache+ won't work with streaming as it does not support
- # streaming bodies yet. So, whenever streaming, Cache-Control is automatically
+ # streaming bodies yet. Whenever streaming Cache-Control is automatically
# set to "no-cache".
#
# == Errors
@@ -1,6 +1,13 @@
module ActionDispatch
module Http
module MimeNegotiation
+ extend ActiveSupport::Concern
+
+ included do
+ mattr_accessor :ignore_accept_header
+ self.ignore_accept_header = false
+ end
+
# The MIME type of the HTTP request, such as Mime::XML.
#
# For backward compatibility, the post \format is extracted from the
@@ -42,16 +49,14 @@ def format(view_path = [])
formats.first
end
- BROWSER_LIKE_ACCEPTS = /,\s*\*\/\*|\*\/\*\s*,/
-
def formats
- accept = @env['HTTP_ACCEPT']
-
@env["action_dispatch.request.formats"] ||=
if parameters[:format]
Array(Mime[parameters[:format]])
- elsif xhr? || (accept && accept !~ BROWSER_LIKE_ACCEPTS)
+ elsif use_accept_header && valid_accept_header
accepts
+ elsif xhr?
+ [Mime::JS]
else
[Mime::HTML]
end
@@ -87,6 +92,18 @@ def negotiate_mime(order)
order.include?(Mime::ALL) ? formats.first : nil
end
+
+ protected
+
+ BROWSER_LIKE_ACCEPTS = /,\s*\*\/\*|\*\/\*\s*,/
+
+ def valid_accept_header
+ xhr? || (accept && accept !~ BROWSER_LIKE_ACCEPTS)
+ end
+
+ def use_accept_header
+ !self.class.ignore_accept_header
+ end
end
end
end
@@ -9,10 +9,12 @@ class Railtie < Rails::Railtie
config.action_dispatch.show_exceptions = true
config.action_dispatch.best_standards_support = true
config.action_dispatch.tld_length = 1
+ config.action_dispatch.ignore_accept_header = false
config.action_dispatch.rack_cache = {:metastore => "rails:/", :entitystore => "rails:/", :verbose => true}
initializer "action_dispatch.configure" do |app|
ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length
+ ActionDispatch::Request.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
end
end
end
Oops, something went wrong.

0 comments on commit b359f9f

Please sign in to comment.