forked from pingcap/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscourse_plugin.rb
52 lines (40 loc) · 1.32 KB
/
discourse_plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true
# 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.public_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