aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_dispatch')
-rw-r--r--actionpack/lib/action_dispatch/http/cache.rb2
-rw-r--r--actionpack/lib/action_dispatch/http/filter_parameters.rb1
-rw-r--r--actionpack/lib/action_dispatch/http/mime_negotiation.rb8
-rw-r--r--actionpack/lib/action_dispatch/http/mime_type.rb1
-rwxr-xr-xactionpack/lib/action_dispatch/http/request.rb6
-rw-r--r--actionpack/lib/action_dispatch/http/response.rb1
-rw-r--r--actionpack/lib/action_dispatch/http/upload.rb2
-rw-r--r--actionpack/lib/action_dispatch/middleware/cookies.rb6
-rw-r--r--actionpack/lib/action_dispatch/middleware/params_parser.rb6
-rw-r--r--actionpack/lib/action_dispatch/middleware/session/abstract_store.rb1
-rw-r--r--actionpack/lib/action_dispatch/middleware/session/cookie_store.rb1
-rw-r--r--actionpack/lib/action_dispatch/routing/deprecated_mapper.rb2
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb69
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/routing.rb20
-rw-r--r--actionpack/lib/action_dispatch/testing/performance_test.rb4
-rw-r--r--actionpack/lib/action_dispatch/testing/test_request.rb2
16 files changed, 96 insertions, 36 deletions
diff --git a/actionpack/lib/action_dispatch/http/cache.rb b/actionpack/lib/action_dispatch/http/cache.rb
index d2404e63c5..9b9e81440b 100644
--- a/actionpack/lib/action_dispatch/http/cache.rb
+++ b/actionpack/lib/action_dispatch/http/cache.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/object/blank'
+
module ActionDispatch
module Http
module Cache
diff --git a/actionpack/lib/action_dispatch/http/filter_parameters.rb b/actionpack/lib/action_dispatch/http/filter_parameters.rb
index e42b4d09b0..152aaa2e67 100644
--- a/actionpack/lib/action_dispatch/http/filter_parameters.rb
+++ b/actionpack/lib/action_dispatch/http/filter_parameters.rb
@@ -1,5 +1,6 @@
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/hash/keys'
+require 'active_support/core_ext/object/duplicable'
module ActionDispatch
module Http
diff --git a/actionpack/lib/action_dispatch/http/mime_negotiation.rb b/actionpack/lib/action_dispatch/http/mime_negotiation.rb
index fec250e928..be89924015 100644
--- a/actionpack/lib/action_dispatch/http/mime_negotiation.rb
+++ b/actionpack/lib/action_dispatch/http/mime_negotiation.rb
@@ -5,7 +5,7 @@ module ActionDispatch
#
# For backward compatibility, the post \format is extracted from the
# X-Post-Data-Format HTTP header if present.
- def content_type
+ def content_mime_type
@env["action_dispatch.request.content_type"] ||= begin
if @env['CONTENT_TYPE'] =~ /^([^,\;]*)/
Mime::Type.lookup($1.strip.downcase)
@@ -15,13 +15,17 @@ module ActionDispatch
end
end
+ def content_type
+ content_mime_type && content_mime_type.to_s
+ end
+
# Returns the accepted MIME type for the request.
def accepts
@env["action_dispatch.request.accepts"] ||= begin
header = @env['HTTP_ACCEPT'].to_s.strip
if header.empty?
- [content_type]
+ [content_mime_type]
else
Mime::Type.parse(header)
end
diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb
index 3f1a77295d..d6a805bf3b 100644
--- a/actionpack/lib/action_dispatch/http/mime_type.rb
+++ b/actionpack/lib/action_dispatch/http/mime_type.rb
@@ -1,5 +1,6 @@
require 'set'
require 'active_support/core_ext/class/attribute_accessors'
+require 'active_support/core_ext/object/blank'
module Mime
class Mimes < Array
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb
index ea9f0f99c2..8b8426b5aa 100755
--- a/actionpack/lib/action_dispatch/http/request.rb
+++ b/actionpack/lib/action_dispatch/http/request.rb
@@ -96,11 +96,11 @@ module ActionDispatch
end
def forgery_whitelisted?
- method == :get || xhr? || content_type.nil? || !content_type.verify_request?
+ method == :get || xhr? || content_mime_type.nil? || !content_mime_type.verify_request?
end
def media_type
- content_type.to_s
+ content_mime_type.to_s
end
# Returns the content length of the request as an integer.
@@ -157,7 +157,7 @@ module ActionDispatch
end
def form_data?
- FORM_DATA_MEDIA_TYPES.include?(content_type.to_s)
+ FORM_DATA_MEDIA_TYPES.include?(content_mime_type.to_s)
end
def body_stream #:nodoc:
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb
index 9cfe5a5ea9..362e5ec970 100644
--- a/actionpack/lib/action_dispatch/http/response.rb
+++ b/actionpack/lib/action_dispatch/http/response.rb
@@ -1,5 +1,6 @@
require 'digest/md5'
require 'active_support/core_ext/module/delegation'
+require 'active_support/core_ext/object/blank'
module ActionDispatch # :nodoc:
# Represents an HTTP response generated by a controller action. One can use
diff --git a/actionpack/lib/action_dispatch/http/upload.rb b/actionpack/lib/action_dispatch/http/upload.rb
index dc6121b911..81d2517304 100644
--- a/actionpack/lib/action_dispatch/http/upload.rb
+++ b/actionpack/lib/action_dispatch/http/upload.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/object/blank'
+
module ActionDispatch
module Http
module UploadedFile
diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb
index ab7130ab08..cb0d12cab1 100644
--- a/actionpack/lib/action_dispatch/middleware/cookies.rb
+++ b/actionpack/lib/action_dispatch/middleware/cookies.rb
@@ -168,12 +168,12 @@ module ActionDispatch
class SignedCookieJar < CookieJar #:nodoc:
def initialize(parent_jar)
- unless ActionController::Base.cookie_verifier_secret
- raise "You must set ActionController::Base.cookie_verifier_secret to use signed cookies"
+ unless ActionController::Base.config.secret
+ raise "You must set ActionController::Base.config.secret"
end
@parent_jar = parent_jar
- @verifier = ActiveSupport::MessageVerifier.new(ActionController::Base.cookie_verifier_secret)
+ @verifier = ActiveSupport::MessageVerifier.new(ActionController::Base.config.secret)
end
def [](name)
diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb
index f4c4324fb0..18a3688bb0 100644
--- a/actionpack/lib/action_dispatch/middleware/params_parser.rb
+++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb
@@ -25,7 +25,9 @@ module ActionDispatch
return false if request.content_length.zero?
- mime_type = content_type_from_legacy_post_data_format_header(env) || request.content_type
+ mime_type = content_type_from_legacy_post_data_format_header(env) ||
+ request.content_mime_type
+
strategy = @parsers[mime_type]
return false unless strategy
@@ -53,7 +55,7 @@ module ActionDispatch
raise
{ "body" => request.raw_post,
- "content_type" => request.content_type,
+ "content_type" => request.content_mime_type,
"content_length" => request.content_length,
"exception" => "#{e.message} (#{e.class})",
"backtrace" => e.backtrace }
diff --git a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
index 311880cabc..dddedc832f 100644
--- a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
+++ b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
@@ -1,5 +1,6 @@
require 'rack/utils'
require 'rack/request'
+require 'active_support/core_ext/object/blank'
module ActionDispatch
module Session
diff --git a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
index 22da82479e..3331b7c25e 100644
--- a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
+++ b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
@@ -1,4 +1,5 @@
require 'active_support/core_ext/hash/keys'
+require 'active_support/core_ext/object/blank'
module ActionDispatch
module Session
diff --git a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb
index 58beaf4824..e8c2e74314 100644
--- a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/object/blank'
+
module ActionDispatch
module Routing
class RouteSet
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 39260f7ff9..74d0297898 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -1,4 +1,5 @@
require 'active_support/core_ext/hash/except'
+require 'active_support/core_ext/object/blank'
module ActionDispatch
module Routing
@@ -258,6 +259,7 @@ module ActionDispatch
def scope(*args)
options = args.extract_options!
+ options = options.dup
case args.first
when String
@@ -423,8 +425,13 @@ module ActionDispatch
singular
end
+ # Checks for uncountable plurals, and appends "_index" if they're.
def collection_name
- plural
+ uncountable? ? "#{plural}_index" : plural
+ end
+
+ def uncountable?
+ singular == plural
end
def name_for_action(action)
@@ -439,6 +446,32 @@ module ActionDispatch
def id_segment
":#{singular}_id"
end
+
+ def constraints
+ options[:constraints] || {}
+ end
+
+ def id_constraint?
+ options[:id] && options[:id].is_a?(Regexp) || constraints[:id] && constraints[:id].is_a?(Regexp)
+ end
+
+ def id_constraint
+ options[:id] || constraints[:id]
+ end
+
+ def collection_options
+ (options || {}).dup.tap do |options|
+ options.delete(:id)
+ options[:constraints] = options[:constraints].dup if options[:constraints]
+ options[:constraints].delete(:id) if options[:constraints].is_a?(Hash)
+ end
+ end
+
+ def nested_options
+ options = { :name_prefix => member_name }
+ options["#{singular}_id".to_sym] = id_constraint if id_constraint?
+ options
+ end
end
class SingletonResource < Resource #:nodoc:
@@ -483,12 +516,14 @@ module ActionDispatch
yield if block_given?
end
- get :show if resource.actions.include?(:show)
- post :create if resource.actions.include?(:create)
- put :update if resource.actions.include?(:update)
- delete :destroy if resource.actions.include?(:destroy)
- get :new, :as => resource.name if resource.actions.include?(:new)
- get :edit, :as => resource.name if resource.actions.include?(:edit)
+ scope(resource.options) do
+ get :show if resource.actions.include?(:show)
+ post :create if resource.actions.include?(:create)
+ put :update if resource.actions.include?(:update)
+ delete :destroy if resource.actions.include?(:destroy)
+ get :new, :as => resource.name if resource.actions.include?(:new)
+ get :edit, :as => resource.name if resource.actions.include?(:edit)
+ end
end
end
@@ -509,17 +544,21 @@ module ActionDispatch
yield if block_given?
with_scope_level(:collection) do
- get :index if resource.actions.include?(:index)
- post :create if resource.actions.include?(:create)
- get :new, :as => resource.singular if resource.actions.include?(:new)
+ scope(resource.collection_options) do
+ get :index if resource.actions.include?(:index)
+ post :create if resource.actions.include?(:create)
+ get :new, :as => resource.singular if resource.actions.include?(:new)
+ end
end
with_scope_level(:member) do
scope(':id') do
- get :show if resource.actions.include?(:show)
- put :update if resource.actions.include?(:update)
- delete :destroy if resource.actions.include?(:destroy)
- get :edit, :as => resource.singular if resource.actions.include?(:edit)
+ scope(resource.options) do
+ get :show if resource.actions.include?(:show)
+ put :update if resource.actions.include?(:update)
+ delete :destroy if resource.actions.include?(:destroy)
+ get :edit, :as => resource.singular if resource.actions.include?(:edit)
+ end
end
end
end
@@ -558,7 +597,7 @@ module ActionDispatch
end
with_scope_level(:nested) do
- scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do
+ scope(parent_resource.id_segment, parent_resource.nested_options) do
yield
end
end
diff --git a/actionpack/lib/action_dispatch/testing/assertions/routing.rb b/actionpack/lib/action_dispatch/testing/assertions/routing.rb
index 1bb81ede3b..b7e9b0c95a 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/routing.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/routing.rb
@@ -80,7 +80,7 @@ module ActionDispatch
expected_path = "/#{expected_path}" unless expected_path[0] == ?/
# Load routes.rb if it hasn't been loaded.
- generated_path, extra_keys = @router.generate_extras(options, defaults)
+ generated_path, extra_keys = @routes.generate_extras(options, defaults)
found_extras = options.reject {|k, v| ! extra_keys.include? k}
msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
@@ -125,7 +125,7 @@ module ActionDispatch
end
# A helper to make it easier to test different route configurations.
- # This method temporarily replaces @router
+ # This method temporarily replaces @routes
# with a new RouteSet instance.
#
# The new instance is yielded to the passed block. Typically the block
@@ -142,9 +142,9 @@ module ActionDispatch
# end
#
def with_routing
- old_routes, @router = @router, ActionDispatch::Routing::RouteSet.new
+ old_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new
old_controller, @controller = @controller, @controller.clone if @controller
- _router = @router
+ _routes = @routes
# Unfortunately, there is currently an abstraction leak between AC::Base
# and AV::Base which requires having the URL helpers in both AC and AV.
@@ -153,14 +153,14 @@ module ActionDispatch
#
# TODO: Make this unnecessary
if @controller
- @controller.singleton_class.send(:include, _router.url_helpers)
+ @controller.singleton_class.send(:include, _routes.url_helpers)
@controller.view_context_class = Class.new(@controller.view_context_class) do
- include _router.url_helpers
+ include _routes.url_helpers
end
end
- yield @router
+ yield @routes
ensure
- @router = old_routes
+ @routes = old_routes
if @controller
@controller = old_controller
end
@@ -168,7 +168,7 @@ module ActionDispatch
# ROUTES TODO: These assertions should really work in an integration context
def method_missing(selector, *args, &block)
- if @controller && @router.named_routes.helpers.include?(selector)
+ if @controller && @routes && @routes.named_routes.helpers.include?(selector)
@controller.send(selector, *args, &block)
else
super
@@ -185,7 +185,7 @@ module ActionDispatch
request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
request.path = path
- params = @router.recognize_path(path, { :method => request.method })
+ params = @routes.recognize_path(path, { :method => request.method })
request.path_parameters = params.with_indifferent_access
request
diff --git a/actionpack/lib/action_dispatch/testing/performance_test.rb b/actionpack/lib/action_dispatch/testing/performance_test.rb
index 1b9a6c18b7..33a5c68b9d 100644
--- a/actionpack/lib/action_dispatch/testing/performance_test.rb
+++ b/actionpack/lib/action_dispatch/testing/performance_test.rb
@@ -1,7 +1,7 @@
require 'active_support/testing/performance'
require 'active_support/testing/default'
-if defined?(ActiveSupport::Testing::Performance)
+begin
module ActionDispatch
# An integration test that runs a code profiler on your test methods.
# Profiling output for combinations of each test method, measurement, and
@@ -14,4 +14,6 @@ if defined?(ActiveSupport::Testing::Performance)
include ActiveSupport::Testing::Default
end
end
+rescue NameError
+ $stderr.puts "Specify ruby-prof as application's dependency in Gemfile to run benchmarks."
end \ No newline at end of file
diff --git a/actionpack/lib/action_dispatch/testing/test_request.rb b/actionpack/lib/action_dispatch/testing/test_request.rb
index 20288aa7a5..090e03cf44 100644
--- a/actionpack/lib/action_dispatch/testing/test_request.rb
+++ b/actionpack/lib/action_dispatch/testing/test_request.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/object/blank'
+
module ActionDispatch
class TestRequest < Request
DEFAULT_ENV = Rack::MockRequest.env_for('/')