2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05: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-12 23:45:10 -05:00
|
|
|
@registry = registry
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
def setup
|
|
|
|
# Initialize the plugin here
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-12 23:45:10 -05: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-05 14:16:51 -05:00
|
|
|
def self.include_mixins
|
2013-02-12 23:45:10 -05: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-06 21:27:05 -04:00
|
|
|
original_class.constantize.public_send(:include, mixin)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-12 23:45:10 -05: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-27 21:20:09 -04:00
|
|
|
.select { |const| const.class == Module && const.name["Mixin"] }
|
2013-02-12 23:45:10 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def register_js(file, opts = {})
|
2013-02-05 14:16:51 -05:00
|
|
|
@registry.register_js(file, opts)
|
|
|
|
end
|
|
|
|
|
2019-08-20 12:39:52 -04:00
|
|
|
def register_css(file, plugin_directory_name)
|
|
|
|
@registry.register_css(file, plugin_directory_name)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def register_archetype(name, options = {})
|
2013-02-05 14:16:51 -05:00
|
|
|
@registry.register_archetype(name, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def listen_for(event_name)
|
2013-02-12 23:45:10 -05:00
|
|
|
return unless self.respond_to?(event_name)
|
2013-02-05 14:16:51 -05:00
|
|
|
DiscourseEvent.on(event_name, &self.method(event_name))
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|