mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add router support for resources :only and :except actions
This commit is contained in:
parent
5d787590f2
commit
d01716731b
2 changed files with 56 additions and 16 deletions
|
@ -324,7 +324,11 @@ module ActionDispatch
|
||||||
CRUD_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy]
|
CRUD_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy]
|
||||||
|
|
||||||
class Resource #:nodoc:
|
class Resource #:nodoc:
|
||||||
attr_reader :plural, :singular
|
def self.default_actions
|
||||||
|
[:index, :create, :new, :show, :update, :destroy, :edit]
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_reader :plural, :singular, :options
|
||||||
|
|
||||||
def initialize(entities, options = {})
|
def initialize(entities, options = {})
|
||||||
entities = entities.to_s
|
entities = entities.to_s
|
||||||
|
@ -334,8 +338,22 @@ module ActionDispatch
|
||||||
@singular = entities.singularize
|
@singular = entities.singularize
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def default_actions
|
||||||
|
self.class.default_actions
|
||||||
|
end
|
||||||
|
|
||||||
|
def actions
|
||||||
|
if only = options[:only]
|
||||||
|
only.map(&:to_sym)
|
||||||
|
elsif except = options[:except]
|
||||||
|
default_actions - except.map(&:to_sym)
|
||||||
|
else
|
||||||
|
default_actions
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
@options[:as] || plural
|
options[:as] || plural
|
||||||
end
|
end
|
||||||
|
|
||||||
def controller
|
def controller
|
||||||
|
@ -356,12 +374,16 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
class SingletonResource < Resource #:nodoc:
|
class SingletonResource < Resource #:nodoc:
|
||||||
|
def self.default_actions
|
||||||
|
[:show, :create, :update, :destroy, :new, :edit]
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(entity, options = {})
|
def initialize(entity, options = {})
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
@options[:as] || singular
|
options[:as] || singular
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -387,12 +409,12 @@ module ActionDispatch
|
||||||
with_scope_level(:resource, resource) do
|
with_scope_level(:resource, resource) do
|
||||||
yield if block_given?
|
yield if block_given?
|
||||||
|
|
||||||
get "(.:format)", :to => :show, :as => resource.member_name
|
get "(.:format)", :to => :show, :as => resource.member_name if resource.actions.include?(:show)
|
||||||
post "(.:format)", :to => :create
|
post "(.:format)", :to => :create if resource.actions.include?(:create)
|
||||||
put "(.:format)", :to => :update
|
put "(.:format)", :to => :update if resource.actions.include?(:update)
|
||||||
delete "(.:format)", :to => :destroy
|
delete "(.:format)", :to => :destroy if resource.actions.include?(:destroy)
|
||||||
get "/new(.:format)", :to => :new, :as => "new_#{resource.singular}"
|
get "/new(.:format)", :to => :new, :as => "new_#{resource.singular}" if resource.actions.include?(:new)
|
||||||
get "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}"
|
get "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}" if resource.actions.include?(:edit)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -422,22 +444,22 @@ module ActionDispatch
|
||||||
yield if block_given?
|
yield if block_given?
|
||||||
|
|
||||||
with_scope_level(:collection) do
|
with_scope_level(:collection) do
|
||||||
get "(.:format)", :to => :index, :as => resource.collection_name
|
get "(.:format)", :to => :index, :as => resource.collection_name if resource.actions.include?(:index)
|
||||||
post "(.:format)", :to => :create
|
post "(.:format)", :to => :create if resource.actions.include?(:create)
|
||||||
|
|
||||||
with_exclusive_name_prefix :new do
|
with_exclusive_name_prefix :new do
|
||||||
get "/new(.:format)", :to => :new, :as => resource.singular
|
get "/new(.:format)", :to => :new, :as => resource.singular if resource.actions.include?(:new)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
with_scope_level(:member) do
|
with_scope_level(:member) do
|
||||||
scope("/:id") do
|
scope("/:id") do
|
||||||
get "(.:format)", :to => :show, :as => resource.member_name
|
get "(.:format)", :to => :show, :as => resource.member_name if resource.actions.include?(:show)
|
||||||
put "(.:format)", :to => :update
|
put "(.:format)", :to => :update if resource.actions.include?(:update)
|
||||||
delete "(.:format)", :to => :destroy
|
delete "(.:format)", :to => :destroy if resource.actions.include?(:destroy)
|
||||||
|
|
||||||
with_exclusive_name_prefix :edit do
|
with_exclusive_name_prefix :edit do
|
||||||
get "/edit(.:format)", :to => :edit, :as => resource.singular
|
get "/edit(.:format)", :to => :edit, :as => resource.singular if resource.actions.include?(:edit)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -93,6 +93,8 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :posts, :only => [:index, :show]
|
||||||
|
|
||||||
match 'sprockets.js' => ::TestRoutingMapper::SprocketsApp
|
match 'sprockets.js' => ::TestRoutingMapper::SprocketsApp
|
||||||
|
|
||||||
match 'people/:id/update', :to => 'people#update', :as => :update_person
|
match 'people/:id/update', :to => 'people#update', :as => :update_person
|
||||||
|
@ -421,6 +423,22 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_posts
|
||||||
|
with_test_routes do
|
||||||
|
get '/posts'
|
||||||
|
assert_equal 'posts#index', @response.body
|
||||||
|
assert_equal '/posts', posts_path
|
||||||
|
|
||||||
|
get '/posts/1'
|
||||||
|
assert_equal 'posts#show', @response.body
|
||||||
|
assert_equal '/posts/1', post_path(:id => 1)
|
||||||
|
|
||||||
|
assert_raise(ActionController::RoutingError) { post '/posts' }
|
||||||
|
assert_raise(ActionController::RoutingError) { put '/posts/1' }
|
||||||
|
assert_raise(ActionController::RoutingError) { delete '/posts/1' }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_sprockets
|
def test_sprockets
|
||||||
with_test_routes do
|
with_test_routes do
|
||||||
get '/sprockets.js'
|
get '/sprockets.js'
|
||||||
|
|
Loading…
Reference in a new issue