discourse/lib/discourse_plugin.rb

51 lines
1.3 KiB
Ruby
Raw Normal View History

2013-02-05 13:16:51 -06: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 22:45:10 -06:00
@registry = registry
2013-02-05 13:16:51 -06:00
end
2013-02-12 22:45:10 -06:00
def setup
# Initialize the plugin here
2013-02-05 13:16:51 -06:00
end
2013-02-12 22:45:10 -06: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 13:16:51 -06:00
def self.include_mixins
2013-02-12 22:45:10 -06:00
mixins.each do |mixin|
original_class = mixin.to_s.demodulize.sub("Mixin", "")
dependency_file_name = original_class.underscore
require_dependency(dependency_file_name)
original_class.constantize.send(:include, mixin)
2013-02-05 13:16:51 -06:00
end
end
2013-02-12 22:45:10 -06: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 20:20:09 -05:00
.select { |const| const.class == Module && const.name["Mixin"] }
2013-02-12 22:45:10 -06:00
end
2017-07-27 20:20:09 -05:00
def register_js(file, opts = {})
2013-02-05 13:16:51 -06:00
@registry.register_js(file, opts)
end
def register_css(file)
@registry.register_css(file)
end
2017-07-27 20:20:09 -05:00
def register_archetype(name, options = {})
2013-02-05 13:16:51 -06:00
@registry.register_archetype(name, options)
end
def listen_for(event_name)
2013-02-12 22:45:10 -06:00
return unless self.respond_to?(event_name)
2013-02-05 13:16:51 -06:00
DiscourseEvent.on(event_name, &self.method(event_name))
end
end