Move discourse_plugin to lib

This commit is contained in:
Scott Walkinshaw
2014-07-22 19:02:22 -04:00
parent 6f749b9765
commit 7e2aa5acfb
16 changed files with 4 additions and 205 deletions
+24
View File
@@ -0,0 +1,24 @@
# This is meant to be used by plugins to trigger and listen to events
# So we can execute code when things happen.
module 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)
events[event_name] << block
end
def self.clear
@events = nil
end
end
+51
View File
@@ -0,0 +1,51 @@
# 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)
@registry = registry
end
def setup
# Initialize the plugin here
end
# 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.
def self.include_mixins
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)
end
end
# Find the modules defined in the plugin with "Mixin" in their name.
def self.mixins
constants.map { |const_name| const_get(const_name) }
.select { |const| const.class == Module && const.name["Mixin"] }
end
def register_js(file, opts={})
@registry.register_js(file, opts)
end
def register_css(file)
@registry.register_css(file)
end
def register_archetype(name, options={})
@registry.register_archetype(name, options)
end
def listen_for(event_name)
return unless self.respond_to?(event_name)
DiscourseEvent.on(event_name, &self.method(event_name))
end
end