aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb138
1 files changed, 51 insertions, 87 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 62fb7977cc..e6855d10d3 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -433,12 +433,14 @@ module ActionDispatch
end
module Resources
+ # CANONICAL_ACTIONS holds all actions that does not need a prefix or
+ # a path appended since they fit properly in their scope level.
+ VALID_ON_OPTIONS = [:new, :collection, :member]
+ CANONICAL_ACTIONS = [:index, :create, :new, :show, :update, :destroy]
MERGE_FROM_SCOPE_OPTIONS = [:shallow, :constraints]
class Resource #:nodoc:
- def self.default_actions
- [:index, :create, :new, :show, :update, :destroy, :edit]
- end
+ DEFAULT_ACTIONS = [:index, :create, :new, :show, :update, :destroy, :edit]
attr_reader :controller, :path, :options
@@ -451,7 +453,7 @@ module ActionDispatch
end
def default_actions
- self.class.default_actions
+ self.class::DEFAULT_ACTIONS
end
def actions
@@ -535,8 +537,8 @@ module ActionDispatch
["#{path}/:id", options]
end
- def new_scope
- [path]
+ def new_scope(new_path)
+ ["#{path}/#{new_path}"]
end
def nested_scope
@@ -545,9 +547,7 @@ module ActionDispatch
end
class SingletonResource < Resource #:nodoc:
- def self.default_actions
- [:show, :create, :update, :destroy, :new, :edit]
- end
+ DEFAULT_ACTIONS = [:show, :create, :update, :destroy, :new, :edit]
def initialize(entities, options)
@name = entities.to_s
@@ -602,9 +602,12 @@ module ActionDispatch
yield if block_given?
collection_scope do
- post :create if parent_resource.actions.include?(:create)
- get :new if parent_resource.actions.include?(:new)
- end
+ post :create
+ end if parent_resource.actions.include?(:create)
+
+ new_scope do
+ get :new
+ end if parent_resource.actions.include?(:new)
member_scope do
get :show if parent_resource.actions.include?(:show)
@@ -630,9 +633,12 @@ module ActionDispatch
collection_scope do
get :index if parent_resource.actions.include?(:index)
post :create if parent_resource.actions.include?(:create)
- get :new if parent_resource.actions.include?(:new)
end
+ new_scope do
+ get :new
+ end if parent_resource.actions.include?(:new)
+
member_scope do
get :show if parent_resource.actions.include?(:show)
put :update if parent_resource.actions.include?(:update)
@@ -669,12 +675,8 @@ module ActionDispatch
raise ArgumentError, "can't use new outside resource(s) scope"
end
- with_scope_level(:new) do
- scope(*parent_resource.new_scope) do
- scope(action_path(:new)) do
- yield
- end
- end
+ new_scope do
+ yield
end
end
@@ -716,7 +718,6 @@ module ActionDispatch
def match(*args)
options = args.extract_options!
-
options[:anchor] = true unless options.key?(:anchor)
if args.length > 1
@@ -724,17 +725,12 @@ module ActionDispatch
return self
end
- if [:collection, :member, :new].include?(options[:on])
+ on = options.delete(:on)
+ if VALID_ON_OPTIONS.include?(on)
args.push(options)
-
- case options.delete(:on)
- when :collection
- return collection { match(*args) }
- when :member
- return member { match(*args) }
- when :new
- return new { match(*args) }
- end
+ return send(on){ match(*args) }
+ elsif on
+ raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
end
if @scope[:scope_level] == :resource
@@ -784,11 +780,11 @@ module ActionDispatch
end
protected
+
def parent_resource #:nodoc:
@scope[:scope_level_resource]
end
- private
def apply_common_behavior_for(method, resources, options, &block)
if resources.length > 1
resources.each { |r| send(method, r, options, &block) }
@@ -854,6 +850,14 @@ module ActionDispatch
end
end
+ def new_scope
+ with_scope_level(:new) do
+ scope(*parent_resource.new_scope(action_path(:new))) do
+ yield
+ end
+ end
+ end
+
def collection_scope
with_scope_level(:collection) do
scope(*parent_resource.collection_scope) do
@@ -871,34 +875,13 @@ module ActionDispatch
end
def path_for_action(action, path)
- case action
- when :index, :create
- "#{@scope[:path]}(.:format)"
- when :show, :update, :destroy
- if parent_resource.shallow?
- "#{@scope[:shallow_path]}/#{parent_resource.path}/:id(.:format)"
- else
- "#{@scope[:path]}(.:format)"
- end
- when :new
- "#{@scope[:path]}/#{action_path(:new)}(.:format)"
- when :edit
- if parent_resource.shallow?
- "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(:edit)}(.:format)"
- else
- "#{@scope[:path]}/#{action_path(:edit)}(.:format)"
- end
+ prefix = parent_resource.shallow? && @scope[:scope_level] == :member ?
+ "#{@scope[:shallow_path]}/#{parent_resource.path}/:id" : @scope[:path]
+
+ if CANONICAL_ACTIONS.include?(action)
+ "#{prefix}(.:format)"
else
- case @scope[:scope_level]
- when :collection, :new
- "#{@scope[:path]}/#{action_path(action, path)}(.:format)"
- else
- if parent_resource.shallow?
- "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(action, path)}(.:format)"
- else
- "#{@scope[:path]}/#{action_path(action, path)}(.:format)"
- end
- end
+ "#{prefix}/#{action_path(action, path)}(.:format)"
end
end
@@ -927,42 +910,23 @@ module ActionDispatch
end
def name_for_action(action)
- name_prefix = @scope[:as].blank? ? "" : "#{@scope[:as]}_"
- shallow_prefix = @scope[:shallow_prefix].blank? ? "" : "#{@scope[:shallow_prefix]}_"
+ prefix = "#{action}_" unless CANONICAL_ACTIONS.include?(action)
+ name_prefix = "#{@scope[:as]}_" if @scope[:as].present?
- case action
- when :index, :create
- "#{name_prefix}#{parent_resource.collection_name}"
- when :show, :update, :destroy
- if parent_resource.shallow?
- "#{shallow_prefix}#{parent_resource.member_name}"
- else
- "#{name_prefix}#{parent_resource.member_name}"
- end
- when :edit
- if parent_resource.shallow?
- "edit_#{shallow_prefix}#{parent_resource.member_name}"
- else
- "edit_#{name_prefix}#{parent_resource.member_name}"
- end
+ case @scope[:scope_level]
+ when :collection
+ "#{prefix}#{name_prefix}#{parent_resource.collection_name}"
when :new
- "new_#{name_prefix}#{parent_resource.member_name}"
+ "#{prefix}new_#{name_prefix}#{parent_resource.member_name}"
else
- case @scope[:scope_level]
- when :collection
- "#{action}_#{name_prefix}#{parent_resource.collection_name}"
- when :new
- "#{action}_new_#{name_prefix}#{parent_resource.member_name}"
+ if parent_resource.shallow?
+ shallow_prefix = "#{@scope[:shallow_prefix]}_" if @scope[:shallow_prefix].present?
+ "#{prefix}#{shallow_prefix}#{parent_resource.member_name}"
else
- if parent_resource.shallow?
- "#{action}_#{shallow_prefix}#{parent_resource.member_name}"
- else
- "#{action}_#{name_prefix}#{parent_resource.member_name}"
- end
+ "#{prefix}#{name_prefix}#{parent_resource.member_name}"
end
end
end
-
end
include Base