Skip to content

Commit 5956572

Browse files
authored
Fix passing expected hash payload argument (activeadmin#7487)
Passing non hash objects as payload arguments to an event instrumenter breaks subscribers implemented by other libraries like Sentry. https://api.rubyonrails.org/v6.1.5.1/classes/ActiveSupport/Notifications.html https://api.rubyonrails.org/classes/ActiveSupport/Notifications.html
1 parent 8ff25ba commit 5956572

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

features/step_definitions/configuration_steps.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# frozen_string_literal: true
22
module ActiveAdminReloading
33
def load_aa_config(config_content)
4-
ActiveSupport::Notifications.instrument ActiveAdmin::Application::BeforeLoadEvent, ActiveAdmin.application
4+
ActiveSupport::Notifications.instrument ActiveAdmin::Application::BeforeLoadEvent, { active_admin_application: ActiveAdmin.application }
55
eval(config_content)
6-
ActiveSupport::Notifications.instrument ActiveAdmin::Application::AfterLoadEvent, ActiveAdmin.application
6+
ActiveSupport::Notifications.instrument ActiveAdmin::Application::AfterLoadEvent, { active_admin_application: ActiveAdmin.application }
77
Rails.application.reload_routes!
88
ActiveAdmin.application.namespaces.each &:reset_menu!
99
end

lib/active_admin.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def after_load(&block)
113113
private
114114

115115
def wrap_block_for_active_support_notifications block
116-
proc { |_name, _start, _finish, _id, payload| block.call payload }
116+
proc { |_name, _start, _finish, _id, payload| block.call payload[:active_admin_application] }
117117
end
118118

119119
end

lib/active_admin/application.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def namespace(name)
7676

7777
namespace = namespaces[name.to_sym] ||= begin
7878
namespace = Namespace.new(self, name)
79-
ActiveSupport::Notifications.instrument ActiveAdmin::Namespace::RegisterEvent, namespace
79+
ActiveSupport::Notifications.instrument ActiveAdmin::Namespace::RegisterEvent, { active_admin_namespace: namespace }
8080
namespace
8181
end
8282

@@ -112,10 +112,10 @@ def unload!
112112
# To reload everything simply call `ActiveAdmin.unload!`
113113
def load!
114114
unless loaded?
115-
ActiveSupport::Notifications.instrument BeforeLoadEvent, self # before_load hook
115+
ActiveSupport::Notifications.instrument BeforeLoadEvent, { active_admin_application: self } # before_load hook
116116
files.each { |file| load file } # load files
117117
namespace(default_namespace) # init AA resources
118-
ActiveSupport::Notifications.instrument AfterLoadEvent, self # after_load hook
118+
ActiveSupport::Notifications.instrument AfterLoadEvent, { active_admin_application: self } # after_load hook
119119
@@loaded = true
120120
end
121121
end

lib/active_admin/namespace.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def method_missing(method, *args)
6060
settings.respond_to?(method) ? settings.send(method, *args) : super
6161
end
6262

63-
# Register a resource into this namespace. The preffered method to access this is to
63+
# Register a resource into this namespace. The preferred method to access this is to
6464
# use the global registration ActiveAdmin.register which delegates to the proper
6565
# namespace instance.
6666
def register(resource_class, options = {}, &block)
@@ -72,7 +72,7 @@ def register(resource_class, options = {}, &block)
7272
reset_menu!
7373

7474
# Dispatch a registration event
75-
ActiveSupport::Notifications.instrument ActiveAdmin::Resource::RegisterEvent, config
75+
ActiveSupport::Notifications.instrument ActiveAdmin::Resource::RegisterEvent, { active_admin_resource: config }
7676

7777
# Return the config
7878
config

spec/unit/resource_registration_spec.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
end
1919

2020
it "should dispatch a Resource::RegisterEvent" do
21-
expect(ActiveSupport::Notifications).to receive(:instrument).with(ActiveAdmin::Resource::RegisterEvent, an_instance_of(ActiveAdmin::Resource))
21+
expect(ActiveSupport::Notifications).to(
22+
receive(:instrument)
23+
.with(
24+
ActiveAdmin::Resource::RegisterEvent,
25+
hash_including(active_admin_resource: an_instance_of(ActiveAdmin::Resource))
26+
)
27+
)
2228

2329
application.register Category
2430
end
@@ -34,8 +40,20 @@
3440
end
3541

3642
it "should generate a Namespace::RegisterEvent and a Resource::RegisterEvent" do
37-
expect(ActiveSupport::Notifications).to receive(:instrument).with(ActiveAdmin::Namespace::RegisterEvent, an_instance_of(ActiveAdmin::Namespace))
38-
expect(ActiveSupport::Notifications).to receive(:instrument).with(ActiveAdmin::Resource::RegisterEvent, an_instance_of(ActiveAdmin::Resource))
43+
expect(ActiveSupport::Notifications).to(
44+
receive(:instrument)
45+
.with(
46+
ActiveAdmin::Namespace::RegisterEvent,
47+
hash_including(active_admin_namespace: an_instance_of(ActiveAdmin::Namespace))
48+
)
49+
)
50+
expect(ActiveSupport::Notifications).to(
51+
receive(:instrument)
52+
.with(
53+
ActiveAdmin::Resource::RegisterEvent,
54+
hash_including(active_admin_resource: an_instance_of(ActiveAdmin::Resource))
55+
)
56+
)
3957
application.register Category, namespace: :not_yet_created
4058
end
4159
end

0 commit comments

Comments
 (0)