Skip to content

Commit 7f92ec3

Browse files
committed
Merge pull request activeadmin#2313 from Daxter/bugfix/namespace-pollution
Bugfix: apps no longer use the same namespace hash
2 parents 7f76980 + 7d08de8 commit 7f92ec3

File tree

11 files changed

+165
-47
lines changed

11 files changed

+165
-47
lines changed

lib/active_admin/application.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,9 @@
44
module ActiveAdmin
55
class Application
66
include Settings
7+
include Settings::Inheritance
78

8-
# Adds settings to both the Application and the Namespace instance
9-
# so that they can be configured independantly.
10-
def self.inheritable_setting(name, default)
11-
Namespace.setting name, nil
12-
setting name, default
13-
end
14-
15-
def self.deprecated_inheritable_setting(name, default)
16-
Namespace.deprecated_setting name, nil
17-
deprecated_setting name, default
18-
end
9+
settings_inherited_by Namespace
1910

2011
# The default namespace to put controllers and routes inside. Set this
2112
# in config/initializers/active_admin.rb using:
@@ -24,8 +15,10 @@ def self.deprecated_inheritable_setting(name, default)
2415
#
2516
setting :default_namespace, :admin
2617

27-
# A hash of all the registered namespaces
28-
setting :namespaces, {}
18+
attr_reader :namespaces
19+
def initialize
20+
@namespaces = {}
21+
end
2922

3023
# Load paths for admin configurations. Add folders to this load path
3124
# to load up other resources for administration. External gems can

lib/active_admin/deprecation.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ActiveAdmin
22
module Deprecation
3-
extend self
3+
module_function
44

55
def warn(message, callstack = caller)
66
ActiveSupport::Deprecation.warn "Active Admin: #{message}", callstack
@@ -23,8 +23,8 @@ def warn(message, callstack = caller)
2323
# end
2424
#
2525
def deprecate(klass, method, message)
26-
klass.class_eval <<-EOC, __FILE__, __LINE__
27-
alias_method :"deprecated_#{method}", :#{method}
26+
klass.class_eval <<-EOC, __FILE__, __LINE__ + 1
27+
alias_method :deprecated_#{method}, :#{method}
2828
def #{method}(*args)
2929
ActiveAdmin::Deprecation.warn('#{message}', caller)
3030
send(:deprecated_#{method}, *args)

lib/active_admin/helpers/settings.rb

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'active_support/concern'
2-
31
module ActiveAdmin
42

53
# Adds a class method to a class to create settings with default values.
@@ -18,7 +16,10 @@ module ActiveAdmin
1816
# conf.site_title #=> "Override Default"
1917
#
2018
module Settings
21-
extend ActiveSupport::Concern
19+
20+
def self.included(base)
21+
base.extend ClassMethods
22+
end
2223

2324
def read_default_setting(name)
2425
default_settings[name]
@@ -34,10 +35,9 @@ module ClassMethods
3435

3536
def setting(name, default)
3637
default_settings[name] = default
37-
attr_accessor(name)
38+
attr_writer name
3839

39-
# Create an accessor that grabs from the defaults
40-
# if @name has not been set yet
40+
# Creates a reader that will grab the default if no value has been set.
4141
class_eval <<-EOC, __FILE__, __LINE__ + 1
4242
def #{name}
4343
if instance_variable_defined? :@#{name}
@@ -50,10 +50,10 @@ def #{name}
5050
end
5151

5252
def deprecated_setting(name, default, message = nil)
53-
message = message || "The #{name} setting is deprecated and will be removed."
5453
setting(name, default)
5554

56-
ActiveAdmin::Deprecation.deprecate self, name, message
55+
message ||= "The #{name} setting is deprecated and will be removed."
56+
ActiveAdmin::Deprecation.deprecate self, name, message
5757
ActiveAdmin::Deprecation.deprecate self, :"#{name}=", message
5858
end
5959

@@ -62,5 +62,47 @@ def default_settings
6262
end
6363

6464
end
65+
66+
67+
# Allows you to define child classes that should receive the same
68+
# settings, as well as the same default values.
69+
#
70+
# Example from the codebase:
71+
#
72+
# class Application
73+
# include Settings
74+
# include Settings::Inheritance
75+
#
76+
# settings_inherited_by :Namespace
77+
#
78+
# inheritable_setting :root_to, 'dashboard#index'
79+
# end
80+
#
81+
module Inheritance
82+
83+
def self.included(base)
84+
base.extend ClassMethods
85+
end
86+
87+
module ClassMethods
88+
89+
def settings_inherited_by(heir)
90+
(@setting_heirs ||= []) << heir
91+
heir.send :include, ActiveAdmin::Settings
92+
end
93+
94+
def inheritable_setting(name, default)
95+
setting name, default
96+
@setting_heirs.each{ |c| c.setting name, default }
97+
end
98+
99+
def deprecated_inheritable_setting(name, default)
100+
deprecated_setting name, default
101+
@setting_heirs.each{ |c| c.deprecated_setting name, default }
102+
end
103+
104+
end
105+
end
106+
65107
end
66108
end

lib/active_admin/namespace.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'active_admin/helpers/settings'
21
require 'active_admin/resource_collection'
32

43
module ActiveAdmin
@@ -26,8 +25,6 @@ module ActiveAdmin
2625
# resource will be accessible from "/posts" and the controller will be PostsController.
2726
#
2827
class Namespace
29-
include Settings
30-
3128
RegisterEvent = 'active_admin.namespace.register'.freeze
3229

3330
attr_reader :application, :resources, :name, :menus

spec/unit/application_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@
110110
end
111111
}.to raise_error("found")
112112
end
113+
114+
it "should not pollute the global app" do
115+
application.namespaces.keys.should be_empty
116+
application.namespace(:brand_new_ns)
117+
application.namespaces.keys.should eq [:brand_new_ns]
118+
ActiveAdmin.application.namespaces.keys.should eq [:admin]
119+
end
113120
end
114121

115122
describe "#register_page" do

spec/unit/authorization/controller_authorization_spec.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,9 @@
2424
end
2525

2626
it "should authorize the create action with the new resource" do
27-
pending "This test redirects to / for some reason, when it should redirect to /admin/posts"
28-
29-
mock_post = mock("Post", :save => true, :errors => [])
30-
Post.should_receive(:new).at_least(:once).and_return(mock_post)
31-
32-
authorization.should_receive(:authorized?).with(Auth::CREATE, mock_post).and_return true
27+
authorization.should_receive(:authorized?).with(Auth::CREATE, an_instance_of(Post)).and_return true
3328
post :create
34-
response.should redirect_to action: 'index'
29+
response.should redirect_to action: 'show', id: Post.last.id
3530
end
3631

3732
it "should redirect when the user isn't authorized" do

spec/unit/namespace/authorization_spec.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,19 @@
99
describe "authorization_adapter" do
1010

1111
it "should return AuthorizationAdapter by default" do
12-
namespace.authorization_adapter.should == ActiveAdmin::AuthorizationAdapter
12+
app.authorization_adapter.should eq ActiveAdmin::AuthorizationAdapter
13+
namespace.authorization_adapter.should eq ActiveAdmin::AuthorizationAdapter
1314
end
1415

1516
it "should be settable on the namespace" do
16-
namespace.authorization_adapter.should == ActiveAdmin::AuthorizationAdapter
1717
namespace.authorization_adapter = mock_auth
18-
19-
namespace.authorization_adapter.should == mock_auth
18+
namespace.authorization_adapter.should eq mock_auth
2019
end
2120

2221
it "should be settable on the application" do
23-
namespace.authorization_adapter.should == ActiveAdmin::AuthorizationAdapter
2422
app.authorization_adapter = mock_auth
25-
26-
namespace.authorization_adapter.should == mock_auth
23+
app.authorization_adapter.should eq mock_auth
2724
end
2825

2926
end
30-
3127
end

spec/unit/resource/routes_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ::News; def self.has_many(*); end end
3939
let!(:config) { ActiveAdmin.register News }
4040
before{ reload_routes! }
4141

42-
it "should return the plurali route with _index" do
42+
it "should return the plural route with _index" do
4343
config.route_collection_path.should == "/admin/news"
4444
end
4545
end

spec/unit/resource_registration_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
require 'spec_helper'
22

33
describe "Registering an object to administer" do
4-
let(:application){ ActiveAdmin::Application.new }
4+
application = ActiveAdmin::Application.new
55

66
context "with no configuration" do
7-
let(:namespace) { ActiveAdmin::Namespace.new(application, :admin) }
7+
namespace = ActiveAdmin::Namespace.new(application, :admin)
88
it "should call register on the namespace" do
99
application.namespaces[namespace.name] = namespace
1010
namespace.should_receive(:register)

spec/unit/routing_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
reload_routes!
1010
end
1111

12-
pending "should only have the (default) admin namespace registered" do
13-
ActiveAdmin.application.namespaces.keys.should eq [:admin]
12+
it "should only have the namespaces necessary for route testing" do
13+
ActiveAdmin.application.namespaces.keys.should eq [:admin, :root]
1414
end
1515

1616
it "should route to the admin dashboard" do

0 commit comments

Comments
 (0)