Skip to content

Commit 5b22684

Browse files
committed
Revert "Merge pull request rails#52605 from Shopify/scope_with_keywords"
This reverts commit 9f9deaf, reversing changes made to 36f5a20.
1 parent d8d5554 commit 5b22684

File tree

4 files changed

+60
-49
lines changed

4 files changed

+60
-49
lines changed

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def default_url_options=(options)
673673
alias_method :default_url_options, :default_url_options=
674674

675675
def with_default_scope(scope, &block)
676-
scope(**scope) do
676+
scope(scope) do
677677
instance_exec(&block)
678678
end
679679
end
@@ -883,7 +883,8 @@ module Scoping
883883
# scope as: "sekret" do
884884
# resources :posts
885885
# end
886-
def scope(*args, only: nil, except: nil, **options)
886+
def scope(*args)
887+
options = args.extract_options!.dup
887888
scope = {}
888889

889890
options[:path] = args.flatten.join("/") if args.any?
@@ -904,8 +905,9 @@ def scope(*args, only: nil, except: nil, **options)
904905
block, options[:constraints] = options[:constraints], {}
905906
end
906907

907-
if only || except
908-
scope[:action_options] = { only:, except: }
908+
if options.key?(:only) || options.key?(:except)
909+
scope[:action_options] = { only: options.delete(:only),
910+
except: options.delete(:except) }
909911
end
910912

911913
if options.key? :anchor
@@ -985,16 +987,18 @@ def controller(controller)
985987
# namespace :admin, as: "sekret" do
986988
# resources :posts
987989
# end
988-
def namespace(name, as: DEFAULT, path: DEFAULT, shallow_path: DEFAULT, shallow_prefix: DEFAULT, **options, &block)
989-
name = name.to_s
990-
options[:module] ||= name
991-
as = name if as == DEFAULT
992-
path = name if path == DEFAULT
993-
shallow_path = path if shallow_path == DEFAULT
994-
shallow_prefix = as if shallow_prefix == DEFAULT
990+
def namespace(path, options = {}, &block)
991+
path = path.to_s
992+
993+
defaults = {
994+
module: path,
995+
as: options.fetch(:as, path),
996+
shallow_path: options.fetch(:path, path),
997+
shallow_prefix: options.fetch(:as, path)
998+
}
995999

996-
path_scope(path) do
997-
scope(**options, as:, shallow_path:, shallow_prefix:, &block)
1000+
path_scope(options.delete(:path) { path }) do
1001+
scope(defaults.merge!(options), &block)
9981002
end
9991003
end
10001004

@@ -1188,7 +1192,7 @@ module Resources
11881192
class Resource # :nodoc:
11891193
attr_reader :controller, :path, :param
11901194

1191-
def initialize(entities, api_only, shallow, only: nil, except: nil, **options)
1195+
def initialize(entities, api_only, shallow, options = {})
11921196
if options[:param].to_s.include?(":")
11931197
raise ArgumentError, ":param option can't contain colons"
11941198
end
@@ -1201,8 +1205,8 @@ def initialize(entities, api_only, shallow, only: nil, except: nil, **options)
12011205
@options = options
12021206
@shallow = shallow
12031207
@api_only = api_only
1204-
@only = only
1205-
@except = except
1208+
@only = options.delete :only
1209+
@except = options.delete :except
12061210
end
12071211

12081212
def default_actions
@@ -1281,7 +1285,7 @@ def singleton?; false; end
12811285
end
12821286

12831287
class SingletonResource < Resource # :nodoc:
1284-
def initialize(entities, api_only, shallow, **options)
1288+
def initialize(entities, api_only, shallow, options)
12851289
super
12861290
@as = nil
12871291
@controller = (options[:controller] || plural).to_s
@@ -1346,17 +1350,19 @@ def resources_path_names(options)
13461350
#
13471351
# ### Options
13481352
# Takes same options as [resources](rdoc-ref:#resources)
1349-
def resource(*resources, concerns: nil, **options, &block)
1350-
if apply_common_behavior_for(:resource, resources, concerns:, **options, &block)
1353+
def resource(*resources, &block)
1354+
options = resources.extract_options!.dup
1355+
1356+
if apply_common_behavior_for(:resource, resources, options, &block)
13511357
return self
13521358
end
13531359

13541360
with_scope_level(:resource) do
13551361
options = apply_action_options options
1356-
resource_scope(SingletonResource.new(resources.pop, api_only?, @scope[:shallow], **options)) do
1362+
resource_scope(SingletonResource.new(resources.pop, api_only?, @scope[:shallow], options)) do
13571363
yield if block_given?
13581364

1359-
concerns(*concerns) if concerns
1365+
concerns(options[:concerns]) if options[:concerns]
13601366

13611367
new do
13621368
get :new
@@ -1514,17 +1520,19 @@ def resource(*resources, concerns: nil, **options, &block)
15141520
#
15151521
# # resource actions are at /admin/posts.
15161522
# resources :posts, path: "admin/posts"
1517-
def resources(*resources, concerns: nil, **options, &block)
1518-
if apply_common_behavior_for(:resources, resources, concerns:, **options, &block)
1523+
def resources(*resources, &block)
1524+
options = resources.extract_options!.dup
1525+
1526+
if apply_common_behavior_for(:resources, resources, options, &block)
15191527
return self
15201528
end
15211529

15221530
with_scope_level(:resources) do
15231531
options = apply_action_options options
1524-
resource_scope(Resource.new(resources.pop, api_only?, @scope[:shallow], **options)) do
1532+
resource_scope(Resource.new(resources.pop, api_only?, @scope[:shallow], options)) do
15251533
yield if block_given?
15261534

1527-
concerns(*concerns) if concerns
1535+
concerns(options[:concerns]) if options[:concerns]
15281536

15291537
collection do
15301538
get :index if parent_resource.actions.include?(:index)
@@ -1609,19 +1617,19 @@ def nested(&block)
16091617
if shallow? && shallow_nesting_depth >= 1
16101618
shallow_scope do
16111619
path_scope(parent_resource.nested_scope) do
1612-
scope(**nested_options, &block)
1620+
scope(nested_options, &block)
16131621
end
16141622
end
16151623
else
16161624
path_scope(parent_resource.nested_scope) do
1617-
scope(**nested_options, &block)
1625+
scope(nested_options, &block)
16181626
end
16191627
end
16201628
end
16211629
end
16221630

16231631
# See ActionDispatch::Routing::Mapper::Scoping#namespace.
1624-
def namespace(name, as: DEFAULT, path: DEFAULT, shallow_path: DEFAULT, shallow_prefix: DEFAULT, **options, &block)
1632+
def namespace(path, options = {})
16251633
if resource_scope?
16261634
nested { super }
16271635
else
@@ -1776,21 +1784,22 @@ def parent_resource
17761784
@scope[:scope_level_resource]
17771785
end
17781786

1779-
def apply_common_behavior_for(method, resources, shallow: nil, **options, &block)
1787+
def apply_common_behavior_for(method, resources, options, &block)
17801788
if resources.length > 1
1781-
resources.each { |r| public_send(method, r, shallow:, **options, &block) }
1789+
resources.each { |r| public_send(method, r, options, &block) }
17821790
return true
17831791
end
17841792

1785-
if shallow
1786-
self.shallow do
1787-
public_send(method, resources.pop, **options, &block)
1793+
if options[:shallow]
1794+
options.delete(:shallow)
1795+
shallow do
1796+
public_send(method, resources.pop, options, &block)
17881797
end
17891798
return true
17901799
end
17911800

17921801
if resource_scope?
1793-
nested { public_send(method, resources.pop, shallow:, **options, &block) }
1802+
nested { public_send(method, resources.pop, options, &block) }
17941803
return true
17951804
end
17961805

@@ -1799,9 +1808,9 @@ def apply_common_behavior_for(method, resources, shallow: nil, **options, &block
17991808
end
18001809

18011810
scope_options = options.slice!(*RESOURCE_OPTIONS)
1802-
if !scope_options.empty? || !shallow.nil?
1803-
scope(**scope_options, shallow:) do
1804-
public_send(method, resources.pop, **options, &block)
1811+
unless scope_options.empty?
1812+
scope(scope_options) do
1813+
public_send(method, resources.pop, options, &block)
18051814
end
18061815
return true
18071816
end
@@ -1877,10 +1886,9 @@ def canonical_action?(action)
18771886
end
18781887

18791888
def shallow_scope
1880-
@scope = @scope.new(
1881-
as: @scope[:shallow_prefix],
1882-
path: @scope[:shallow_path],
1883-
)
1889+
scope = { as: @scope[:shallow_prefix],
1890+
path: @scope[:shallow_path] }
1891+
@scope = @scope.new scope
18841892

18851893
yield
18861894
ensure
@@ -2144,7 +2152,8 @@ def concern(name, callable = nil, &block)
21442152
# namespace :posts do
21452153
# concerns :commentable
21462154
# end
2147-
def concerns(*args, **options)
2155+
def concerns(*args)
2156+
options = args.extract_options!
21482157
args.flatten.each do |name|
21492158
if concern = @concerns[name]
21502159
concern.call(self, options)

actionpack/test/controller/resources_test.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_multiple_default_restful_routes
7676
def test_multiple_resources_with_options
7777
expected_options = { controller: "threads", action: "index" }
7878

79-
with_restful_routing :messages, :comments, controller: "threads" do
79+
with_restful_routing :messages, :comments, expected_options.slice(:controller) do
8080
assert_recognizes(expected_options, path: "comments")
8181
assert_recognizes(expected_options, path: "messages")
8282
end
@@ -1109,15 +1109,17 @@ def test_singleton_resource_name_is_not_singularized
11091109
end
11101110

11111111
private
1112-
def with_restful_routing(*args, **options)
1112+
def with_restful_routing(*args)
1113+
options = args.extract_options!
11131114
collection_methods = options.delete(:collection)
11141115
member_methods = options.delete(:member)
11151116
path_prefix = options.delete(:path_prefix)
1117+
args.push(options)
11161118

11171119
with_routing do |set|
11181120
set.draw do
11191121
scope(path_prefix || "") do
1120-
resources(*args, **options) do
1122+
resources(*args) do
11211123
if collection_methods
11221124
collection do
11231125
collection_methods.each do |name, method|

actionpack/test/dispatch/routing/concerns_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ class ReviewsController < ResourcesController; end
77
class RoutingConcernsTest < ActionDispatch::IntegrationTest
88
class Reviewable
99
def self.call(mapper, options = {})
10-
mapper.resources :reviews, **options
10+
mapper.resources :reviews, options
1111
end
1212
end
1313

1414
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
1515
app.draw do
1616
concern :commentable do |options|
17-
resources :comments, **options
17+
resources :comments, options
1818
end
1919

2020
concern :image_attachable do

actionpack/test/dispatch/routing_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,13 +990,13 @@ def test_resources_for_uncountable_names
990990

991991
def test_resource_does_not_modify_passed_options
992992
options = { id: /.+?/, format: /json|xml/ }
993-
draw { resource :user, **options }
993+
draw { resource :user, options }
994994
assert_equal({ id: /.+?/, format: /json|xml/ }, options)
995995
end
996996

997997
def test_resources_does_not_modify_passed_options
998998
options = { id: /.+?/, format: /json|xml/ }
999-
draw { resources :users, **options }
999+
draw { resources :users, options }
10001000
assert_equal({ id: /.+?/, format: /json|xml/ }, options)
10011001
end
10021002

0 commit comments

Comments
 (0)