discourse/lib/discourse_plugin.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-02-06 03:16:51 +08:00
# A basic plugin for Discourse. Meant to be extended and filled in.
# Most work is delegated to a registry.
class DiscoursePlugin
attr_reader :registry
def initialize(registry)
2013-02-13 12:45:10 +08:00
@registry = registry
2013-02-06 03:16:51 +08:00
end
2013-02-13 12:45:10 +08:00
def setup
# Initialize the plugin here
2013-02-06 03:16:51 +08:00
end
2013-02-13 12:45:10 +08:00
# Loads and mixes in the plugin's mixins into the host app's classes.
# A mixin named "UserMixin" will be included into the "User" class.
2013-02-06 03:16:51 +08:00
def self.include_mixins
2013-02-13 12:45:10 +08:00
mixins.each do |mixin|
original_class = mixin.to_s.demodulize.sub("Mixin", "")
dependency_file_name = original_class.underscore
require_dependency(dependency_file_name)
2019-05-07 09:27:05 +08:00
original_class.constantize.public_send(:include, mixin)
2013-02-06 03:16:51 +08:00
end
end
2013-02-13 12:45:10 +08:00
# Find the modules defined in the plugin with "Mixin" in their name.
def self.mixins
constants.map { |const_name| const_get(const_name) }
2017-07-28 09:20:09 +08:00
.select { |const| const.class == Module && const.name["Mixin"] }
2013-02-13 12:45:10 +08:00
end
2017-07-28 09:20:09 +08:00
def register_js(file, opts = {})
2013-02-06 03:16:51 +08:00
@registry.register_js(file, opts)
end
def register_css(file, plugin_directory_name)
@registry.register_css(file, plugin_directory_name)
2013-02-06 03:16:51 +08:00
end
2017-07-28 09:20:09 +08:00
def register_archetype(name, options = {})
2013-02-06 03:16:51 +08:00
@registry.register_archetype(name, options)
end
def listen_for(event_name)
2013-02-13 12:45:10 +08:00
return unless self.respond_to?(event_name)
2013-02-06 03:16:51 +08:00
DiscourseEvent.on(event_name, &self.method(event_name))
end
end