1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Clean up the logic to specify the name and path for action a bit.

This commit is contained in:
José Valim 2010-06-30 11:33:15 +02:00
parent 7bd00fcb7f
commit ccbb3bb3d8

View file

@ -433,12 +433,14 @@ module ActionDispatch
end end
module Resources 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] MERGE_FROM_SCOPE_OPTIONS = [:shallow, :constraints]
class Resource #:nodoc: class Resource #:nodoc:
def self.default_actions DEFAULT_ACTIONS = [:index, :create, :new, :show, :update, :destroy, :edit]
[:index, :create, :new, :show, :update, :destroy, :edit]
end
attr_reader :controller, :path, :options attr_reader :controller, :path, :options
@ -451,7 +453,7 @@ module ActionDispatch
end end
def default_actions def default_actions
self.class.default_actions self.class::DEFAULT_ACTIONS
end end
def actions def actions
@ -535,8 +537,8 @@ module ActionDispatch
["#{path}/:id", options] ["#{path}/:id", options]
end end
def new_scope def new_scope(new_path)
[path] ["#{path}/#{new_path}"]
end end
def nested_scope def nested_scope
@ -545,9 +547,7 @@ module ActionDispatch
end end
class SingletonResource < Resource #:nodoc: class SingletonResource < Resource #:nodoc:
def self.default_actions DEFAULT_ACTIONS = [:show, :create, :update, :destroy, :new, :edit]
[:show, :create, :update, :destroy, :new, :edit]
end
def initialize(entities, options) def initialize(entities, options)
@name = entities.to_s @name = entities.to_s
@ -602,9 +602,12 @@ module ActionDispatch
yield if block_given? yield if block_given?
collection_scope do collection_scope do
post :create if parent_resource.actions.include?(:create) post :create
get :new if parent_resource.actions.include?(:new) end if parent_resource.actions.include?(:create)
end
new_scope do
get :new
end if parent_resource.actions.include?(:new)
member_scope do member_scope do
get :show if parent_resource.actions.include?(:show) get :show if parent_resource.actions.include?(:show)
@ -630,9 +633,12 @@ module ActionDispatch
collection_scope do collection_scope do
get :index if parent_resource.actions.include?(:index) get :index if parent_resource.actions.include?(:index)
post :create if parent_resource.actions.include?(:create) post :create if parent_resource.actions.include?(:create)
get :new if parent_resource.actions.include?(:new)
end end
new_scope do
get :new
end if parent_resource.actions.include?(:new)
member_scope do member_scope do
get :show if parent_resource.actions.include?(:show) get :show if parent_resource.actions.include?(:show)
put :update if parent_resource.actions.include?(:update) put :update if parent_resource.actions.include?(:update)
@ -669,14 +675,10 @@ module ActionDispatch
raise ArgumentError, "can't use new outside resource(s) scope" raise ArgumentError, "can't use new outside resource(s) scope"
end end
with_scope_level(:new) do new_scope do
scope(*parent_resource.new_scope) do
scope(action_path(:new)) do
yield yield
end end
end end
end
end
def nested def nested
unless resource_scope? unless resource_scope?
@ -716,7 +718,6 @@ module ActionDispatch
def match(*args) def match(*args)
options = args.extract_options! options = args.extract_options!
options[:anchor] = true unless options.key?(:anchor) options[:anchor] = true unless options.key?(:anchor)
if args.length > 1 if args.length > 1
@ -724,17 +725,12 @@ module ActionDispatch
return self return self
end end
if [:collection, :member, :new].include?(options[:on]) on = options.delete(:on)
if VALID_ON_OPTIONS.include?(on)
args.push(options) args.push(options)
return send(on){ match(*args) }
case options.delete(:on) elsif on
when :collection raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
return collection { match(*args) }
when :member
return member { match(*args) }
when :new
return new { match(*args) }
end
end end
if @scope[:scope_level] == :resource if @scope[:scope_level] == :resource
@ -784,11 +780,11 @@ module ActionDispatch
end end
protected protected
def parent_resource #:nodoc: def parent_resource #:nodoc:
@scope[:scope_level_resource] @scope[:scope_level_resource]
end end
private
def apply_common_behavior_for(method, resources, options, &block) def apply_common_behavior_for(method, resources, options, &block)
if resources.length > 1 if resources.length > 1
resources.each { |r| send(method, r, options, &block) } resources.each { |r| send(method, r, options, &block) }
@ -854,6 +850,14 @@ module ActionDispatch
end end
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 def collection_scope
with_scope_level(:collection) do with_scope_level(:collection) do
scope(*parent_resource.collection_scope) do scope(*parent_resource.collection_scope) do
@ -871,34 +875,13 @@ module ActionDispatch
end end
def path_for_action(action, path) def path_for_action(action, path)
case action prefix = parent_resource.shallow? && @scope[:scope_level] == :member ?
when :index, :create "#{@scope[:shallow_path]}/#{parent_resource.path}/:id" : @scope[:path]
"#{@scope[:path]}(.:format)"
when :show, :update, :destroy if CANONICAL_ACTIONS.include?(action)
if parent_resource.shallow? "#{prefix}(.:format)"
"#{@scope[:shallow_path]}/#{parent_resource.path}/:id(.:format)"
else else
"#{@scope[:path]}(.:format)" "#{prefix}/#{action_path(action, 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
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
end end
end end
@ -927,44 +910,25 @@ module ActionDispatch
end end
def name_for_action(action) def name_for_action(action)
name_prefix = @scope[:as].blank? ? "" : "#{@scope[:as]}_" prefix = "#{action}_" unless CANONICAL_ACTIONS.include?(action)
shallow_prefix = @scope[:shallow_prefix].blank? ? "" : "#{@scope[:shallow_prefix]}_" 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
when :new
"new_#{name_prefix}#{parent_resource.member_name}"
else
case @scope[:scope_level] case @scope[:scope_level]
when :collection when :collection
"#{action}_#{name_prefix}#{parent_resource.collection_name}" "#{prefix}#{name_prefix}#{parent_resource.collection_name}"
when :new when :new
"#{action}_new_#{name_prefix}#{parent_resource.member_name}" "#{prefix}new_#{name_prefix}#{parent_resource.member_name}"
else else
if parent_resource.shallow? if parent_resource.shallow?
"#{action}_#{shallow_prefix}#{parent_resource.member_name}" shallow_prefix = "#{@scope[:shallow_prefix]}_" if @scope[:shallow_prefix].present?
"#{prefix}#{shallow_prefix}#{parent_resource.member_name}"
else else
"#{action}_#{name_prefix}#{parent_resource.member_name}" "#{prefix}#{name_prefix}#{parent_resource.member_name}"
end end
end end
end end
end end
end
include Base include Base
include HttpHelpers include HttpHelpers
include Scoping include Scoping