8000 Merge pull request #4935 from activeadmin/default_namespace_with_unde… · bugcrowd/activeadmin@8a916e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a916e5

Browse files
Merge pull request activeadmin#4935 from activeadmin/default_namespace_with_underscores
Default namespace with underscores
2 parents 5cc47f1 + 65ced4f commit 8a916e5

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

lib/active_admin/namespace.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@ module ActiveAdmin
2727
class Namespace
2828
RegisterEvent = 'active_admin.namespace.register'.freeze
2929

30-
attr_reader :application, :resources, :name, :menus
30+
attr_reader :application, :resources, :menus
3131

3232
def initialize(application, name)
3333
@application = application
34-
@name = name.to_s.underscore.to_sym
34+
@name = name.to_s.underscore
3535
@resources = ResourceCollection.new
3636
register_module unless root?
3737
build_menu_collection
3838
end
3939

40+
def name
41+
@name.to_sym
42+
end
43+
4044
# Register a resource into this namespace. The preffered method to access this is to
4145
# use the global registration ActiveAdmin.register which delegates to the proper
4246
# namespace instance.
@@ -78,8 +82,11 @@ def root?
7882
# Namespace.new(:root).module_name # => nil
7983
#
8084
def module_name
81-
return nil if root?
82-
@module_name ||= name.to_s.camelize
85+
root? ? nil : @name.camelize
86+
end
87+
88+
def route_prefix
89+
root? ? nil : @name
8390
end
8491

8592
# Unload all the registered resources for this namespace

lib/active_admin/resource/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def route_edit_instance_path(resource, additional_params = {})
2525

2626
# Returns the routes prefix for this config
2727
def route_prefix
28-
namespace.module_name.try(:underscore)
28+
namespace.route_prefix
2929
end
3030

3131
def route_builder

spec/requests/default_namespace_spec.rb

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
include Rails.application.routes.url_helpers
66

7+
let(:resource) { ActiveAdmin.register Category }
8+
79
[false, nil].each do |value|
810

911
describe "with a #{value} default namespace" do
1012

1113
around do |example|
12-
application = ActiveAdmin::Application.new
13-
application.default_namespace = value
14+
with_custom_default_namespace(value) { example.call }
15+
end
1416

15-
with_temp_application(application) { example.call }
17+
it "should generate resource paths" do
18+
expect(resource.route_collection_path).to eq "/categories"
1619
end
1720

1821
it "should generate a log out path" do
@@ -30,10 +33,11 @@
3033
describe "with a test default namespace" do
3134

3235
around do |example|
33-
application = ActiveAdmin::Application.new
34-
application.default_namespace = :test
36+
with_custom_default_namespace(:test) { example.call }
37+
end
3538

36-
with_temp_application(application) { example.call }
39+
it "should generate resource paths" do
40+
expect(resource.route_collection_path).to eq "/test/categories"
3741
end
3842

3943
it "should generate a log out path" do
@@ -46,4 +50,32 @@
4650

4751
end
4852

53+
describe "with a namespace with underscores in the name" do
54+
55+
around do |example|
56+
with_custom_default_namespace(:abc_123) { example.call }
57+
end
58+
59+
it "should generate resource paths" do
60+
expect(resource.route_collection_path).to eq "/abc_123/categories"
61+
end
62+
63+
it "should generate a log out path" do
64+
expect(destroy_admin_user_session_path).to eq "/abc_123/logout"
65+
end
66+
67+
it "should generate a log in path" do
68+
expect(new_admin_user_session_path).to eq "/abc_123/login"
69+
end
70+
71+
end
72+
73+
private
74+
75+
def with_custom_default_namespace(namespace)
76+
application = ActiveAdmin::Application.new
77+
application.default_namespace = namespace
78+
79+
with_temp_application(application) { yield }
80+
end
4981
end

0 commit comments

Comments
 (0)
0