Skip to content

Commit

Permalink
Merge pull request #20865 from colavitam/only-except-behavior
Browse files Browse the repository at this point in the history
:only and :except are now chained for routing resource(s)
  • Loading branch information
rafaelfranca committed Nov 20, 2018
2 parents 0bbe340 + 0c8dd2c commit 4aad2e2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -1171,10 +1171,16 @@ def default_actions
end

def actions
if @except
available_actions - Array(@except).map(&:to_sym)
else
available_actions
end
end

def available_actions
if @only
Array(@only).map(&:to_sym)
elsif @except
default_actions - Array(@except).map(&:to_sym)
else
default_actions
end
Expand Down
22 changes: 22 additions & 0 deletions actionpack/test/controller/resources_test.rb
Expand Up @@ -853,6 +853,28 @@ def test_singleton_resource_does_not_have_destroy_action
end
end

def test_resource_has_show_action_but_does_not_have_destroy_action
with_routing do |set|
set.draw do
resources :products, only: [:show, :destroy], except: :destroy
end

assert_resource_allowed_routes("products", {}, { id: "1" }, :show, [:index, :new, :create, :edit, :update, :destroy])
assert_resource_allowed_routes("products", { format: "xml" }, { id: "1" }, :show, [:index, :new, :create, :edit, :update, :destroy])
end
end

def test_singleton_resource_has_show_action_but_does_not_have_destroy_action
with_routing do |set|
set.draw do
resource :account, only: [:show, :destroy], except: :destroy
end

assert_singleton_resource_allowed_routes("accounts", {}, :show, [:new, :create, :edit, :update, :destroy])
assert_singleton_resource_allowed_routes("accounts", { format: "xml" }, :show, [:new, :create, :edit, :update, :destroy])
end
end

def test_resource_has_only_create_action_and_named_route
with_routing do |set|
set.draw do
Expand Down

0 comments on commit 4aad2e2

Please sign in to comment.