mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 12:57:29 +08:00
FIX: Correctly support DiscourseEvent kwargs (#19788)
Fixes the support for kwargs in `DiscourseEvent.trigger()` on Ruby 3, e.g. ```rb DiscourseEvent.trigger(:before_system_message_sent, message_type: type, recipient: @recipient, post_creator_args: post_creator_args, params: method_params) ``` Fixes https://github.com/discourse/discourse-local-site-contacts
This commit is contained in:
parent
56eaf91589
commit
ff508d1ae5
|
@ -9,9 +9,9 @@ class DiscourseEvent
|
|||
@events ||= Hash.new { |hash, key| hash[key] = Set.new }
|
||||
end
|
||||
|
||||
def self.trigger(event_name, *params)
|
||||
def self.trigger(event_name, *args, **kwargs)
|
||||
events[event_name].each do |event|
|
||||
event.call(*params)
|
||||
event.call(*args, **kwargs)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe DiscourseEvent do
|
||||
|
||||
describe "#events" do
|
||||
it "defaults to {}" do
|
||||
begin
|
||||
|
@ -21,7 +20,6 @@ RSpec.describe DiscourseEvent do
|
|||
end
|
||||
|
||||
context 'when calling events' do
|
||||
|
||||
let(:harvey) {
|
||||
OpenStruct.new(
|
||||
name: 'Harvey Dent',
|
||||
|
@ -42,15 +40,12 @@ RSpec.describe DiscourseEvent do
|
|||
end
|
||||
|
||||
context 'when event does not exist' do
|
||||
|
||||
it "does not raise an error" do
|
||||
DiscourseEvent.trigger(:missing_event)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'when single event exists' do
|
||||
|
||||
it "doesn't raise an error" do
|
||||
DiscourseEvent.trigger(:acid_face, harvey)
|
||||
end
|
||||
|
@ -59,11 +54,9 @@ RSpec.describe DiscourseEvent do
|
|||
DiscourseEvent.trigger(:acid_face, harvey)
|
||||
expect(harvey.name).to eq('Two Face')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'when multiple events exist' do
|
||||
|
||||
let(:event_handler_2) do
|
||||
Proc.new { |user| user.job = 'Supervillain' }
|
||||
end
|
||||
|
@ -84,7 +77,6 @@ RSpec.describe DiscourseEvent do
|
|||
end
|
||||
|
||||
describe '#all_off' do
|
||||
|
||||
let(:event_handler_2) do
|
||||
Proc.new { |user| user.job = 'Supervillain' }
|
||||
end
|
||||
|
@ -99,7 +91,20 @@ RSpec.describe DiscourseEvent do
|
|||
DiscourseEvent.trigger(:acid_face, harvey) # Doesn't change anything
|
||||
expect(harvey.job).to eq('gardening')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "allows using kwargs" do
|
||||
begin
|
||||
handler = Proc.new do |name:, message:|
|
||||
expect(name).to eq("Supervillain")
|
||||
expect(message).to eq("Two Face")
|
||||
end
|
||||
|
||||
DiscourseEvent.on(:acid_face, &handler)
|
||||
DiscourseEvent.trigger(:acid_face, name: "Supervillain", message: "Two Face")
|
||||
ensure
|
||||
DiscourseEvent.off(:acid_face, &handler)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,9 +4,7 @@ require 'system_message'
|
|||
require 'topic_subtype'
|
||||
|
||||
RSpec.describe SystemMessage do
|
||||
|
||||
describe '#create' do
|
||||
|
||||
fab!(:admin) { Fabricate(:admin) }
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
|
||||
|
@ -81,12 +79,26 @@ RSpec.describe SystemMessage do
|
|||
|
||||
it 'sends event with post object' do
|
||||
system_message = SystemMessage.new(user)
|
||||
|
||||
event = DiscourseEvent.track(:system_message_sent) {
|
||||
system_message.create(:tl2_promotion_message)
|
||||
}
|
||||
|
||||
expect(event[:event_name]).to eq(:system_message_sent)
|
||||
expect(event[:params].first[:post]).to eq(Post.last)
|
||||
expect(event[:params].first[:message_type]).to eq(:tl2_promotion_message)
|
||||
end
|
||||
|
||||
it 'sends an event before the system message is sent' do
|
||||
system_message = SystemMessage.new(user)
|
||||
|
||||
event = DiscourseEvent.track(:before_system_message_sent) {
|
||||
system_message.create(:tl2_promotion_message)
|
||||
}
|
||||
|
||||
expect(event[:event_name]).to eq(:before_system_message_sent)
|
||||
expect(event[:params].first[:message_type]).to eq(:tl2_promotion_message)
|
||||
expect(event[:params].first[:recipient]).to eq(user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseEvent::TestHelper
|
||||
def trigger(event_name, *params)
|
||||
super(event_name, *params)
|
||||
def trigger(event_name, *params, **kwargs)
|
||||
super(event_name, *params, **kwargs)
|
||||
|
||||
if @events_trigger
|
||||
params << kwargs if kwargs != {}
|
||||
@events_trigger << { event_name: event_name, params: params }
|
||||
end
|
||||
end
|
||||
|
@ -29,7 +30,6 @@ module DiscourseEvent::TestHelper
|
|||
events = track_events(event_name, args: args) { yield }
|
||||
events.first
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
DiscourseEvent.singleton_class.prepend DiscourseEvent::TestHelper
|
||||
|
|
Loading…
Reference in New Issue
Block a user