mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:29:30 +08:00
7826acc4a7
* DEV: Replace site_setting_saved DiscourseEvent with site_setting_changed site_setting_saved is confusing for a few reasons: - It is attached to the after_save of the ActiveRecord model. This is confusing because it only works 'properly' with the db_provider - It passes the activerecord model as a parameter, which is confusing because you get access to the 'database' version of the setting, rather than the ruby setting. For example, booleans appear as 'y' or 'n' strings. - When the event is called, the local process cache has not yet been updated. So if you call SiteSetting.setting_name inside the event handler, you will receive the old site setting value I have deprecated that event, and added a new site_setting_changed event. It passes three parameters: - Setting name (symbol) - Old value (in ruby format) - New value (in ruby format) It is triggered after the setting has been persisted, and the local process cache has been updated. This commit also includes a test case which describes the confusing behavior. This can be removed once site_setting_saved is removed.
28 lines
762 B
Ruby
28 lines
762 B
Ruby
# This is meant to be used by plugins to trigger and listen to events
|
|
# So we can execute code when things happen.
|
|
class DiscourseEvent
|
|
|
|
# Defaults to a hash where default values are empty sets.
|
|
def self.events
|
|
@events ||= Hash.new { |hash, key| hash[key] = Set.new }
|
|
end
|
|
|
|
def self.trigger(event_name, *params)
|
|
events[event_name].each do |event|
|
|
event.call(*params)
|
|
end
|
|
end
|
|
|
|
def self.on(event_name, &block)
|
|
if event_name == :site_setting_saved
|
|
Discourse.deprecate("The :site_setting_saved event is deprecated. Please use :site_setting_changed instead", since: "2.3.0beta8", drop_from: "2.4")
|
|
end
|
|
events[event_name] << block
|
|
end
|
|
|
|
def self.off(event_name, &block)
|
|
events[event_name].delete(block)
|
|
end
|
|
|
|
end
|