mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
cleaned up discourse_plugin
This commit is contained in:
parent
69c7b5aeed
commit
afb83c1108
4 changed files with 97 additions and 54 deletions
|
@ -2,27 +2,23 @@
|
||||||
# So we can execute code when things happen.
|
# So we can execute code when things happen.
|
||||||
module DiscourseEvent
|
module DiscourseEvent
|
||||||
|
|
||||||
class << self
|
# Defaults to a hash where default values are empty sets.
|
||||||
|
def self.events
|
||||||
|
@events ||= Hash.new { |hash, key| hash[key] = Set.new }
|
||||||
|
end
|
||||||
|
|
||||||
def trigger(event_name, *params)
|
def self.trigger(event_name, *params)
|
||||||
|
events[event_name].each do |event|
|
||||||
return unless @events
|
event.call(*params)
|
||||||
return unless event_list = @events[event_name]
|
|
||||||
|
|
||||||
event_list.each do |ev|
|
|
||||||
ev.call(*params)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def on(event_name, &block)
|
|
||||||
@events ||= {}
|
|
||||||
@events[event_name] ||= Set.new
|
|
||||||
@events[event_name] << block
|
|
||||||
end
|
|
||||||
|
|
||||||
def clear
|
|
||||||
@events = {}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.on(event_name, &block)
|
||||||
|
events[event_name] << block
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.clear
|
||||||
|
@events = nil
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,27 +6,30 @@ class DiscoursePlugin
|
||||||
attr_reader :registry
|
attr_reader :registry
|
||||||
|
|
||||||
def initialize(registry)
|
def initialize(registry)
|
||||||
@registry = registry
|
@registry = registry
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
# Initialize the plugin here
|
# Initialize the plugin here
|
||||||
end
|
end
|
||||||
|
|
||||||
# Find the modules in our class with the name mixin, then include them in the appropriate places
|
# Loads and mixes in the plugin's mixins into the host app's classes.
|
||||||
# automagically.
|
# A mixin named "UserMixin" will be included into the "User" class.
|
||||||
def self.include_mixins
|
def self.include_mixins
|
||||||
modules = constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == Module}
|
mixins.each do |mixin|
|
||||||
unless modules.empty?
|
original_class = mixin.to_s.demodulize.sub("Mixin", "")
|
||||||
modules.each do |m|
|
dependency_file_name = original_class.underscore
|
||||||
original_class = m.to_s.sub("#{self.name}::", '').sub("Mixin", "")
|
require_dependency(dependency_file_name)
|
||||||
dependency_file_name = original_class.underscore
|
original_class.constantize.send(:include, mixin)
|
||||||
require_dependency(dependency_file_name)
|
|
||||||
original_class.constantize.send(:include, m)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
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={})
|
def register_js(file, opts={})
|
||||||
@registry.register_js(file, opts)
|
@registry.register_js(file, opts)
|
||||||
end
|
end
|
||||||
|
@ -40,7 +43,7 @@ class DiscoursePlugin
|
||||||
end
|
end
|
||||||
|
|
||||||
def listen_for(event_name)
|
def listen_for(event_name)
|
||||||
return unless self.respond_to?(event_name)
|
return unless self.respond_to?(event_name)
|
||||||
DiscourseEvent.on(event_name, &self.method(event_name))
|
DiscourseEvent.on(event_name, &self.method(event_name))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,13 +4,35 @@ require 'ostruct'
|
||||||
|
|
||||||
describe DiscourseEvent do
|
describe DiscourseEvent do
|
||||||
|
|
||||||
it "doesn't raise an error if we call an event that doesn't exist" do
|
describe "#events" do
|
||||||
DiscourseEvent.trigger(:missing_event)
|
it "defaults to {}" do
|
||||||
|
DiscourseEvent.instance_variable_set(:@events, nil)
|
||||||
|
DiscourseEvent.events.should == {}
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "key value" do
|
||||||
|
it "defaults to an empty set" do
|
||||||
|
DiscourseEvent.events["event42"].should == Set.new
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with an event to call' do
|
describe ".clear" do
|
||||||
|
it "clears out events" do
|
||||||
|
DiscourseEvent.events["event42"] << "test event"
|
||||||
|
DiscourseEvent.clear
|
||||||
|
DiscourseEvent.events.should be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
let(:harvey) { OpenStruct.new(name: 'Harvey Dent', job: 'District Attorney') }
|
context 'when calling events' do
|
||||||
|
|
||||||
|
let(:harvey) {
|
||||||
|
OpenStruct.new(
|
||||||
|
name: 'Harvey Dent',
|
||||||
|
job: 'District Attorney'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
before do
|
before do
|
||||||
DiscourseEvent.on(:acid_face) do |user|
|
DiscourseEvent.on(:acid_face) do |user|
|
||||||
|
@ -18,30 +40,42 @@ describe DiscourseEvent do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't raise an error" do
|
context 'when event does not exist' do
|
||||||
DiscourseEvent.trigger(:acid_face, harvey)
|
|
||||||
|
it "does not raise an error" do
|
||||||
|
DiscourseEvent.trigger(:missing_event)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "chnages the name" do
|
context 'when single event exists' do
|
||||||
DiscourseEvent.trigger(:acid_face, harvey)
|
|
||||||
harvey.name.should == 'Two Face'
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'multiple events' do
|
it "doesn't raise an error" do
|
||||||
before do
|
|
||||||
DiscourseEvent.on(:acid_face) do |user|
|
|
||||||
user.job = 'Supervillian'
|
|
||||||
end
|
|
||||||
DiscourseEvent.trigger(:acid_face, harvey)
|
DiscourseEvent.trigger(:acid_face, harvey)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'triggerred the email event' do
|
it "changes the name" do
|
||||||
harvey.job.should == 'Supervillian'
|
DiscourseEvent.trigger(:acid_face, harvey)
|
||||||
end
|
|
||||||
|
|
||||||
it 'triggerred the username change' do
|
|
||||||
harvey.name.should == 'Two Face'
|
harvey.name.should == 'Two Face'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when multiple events exist' do
|
||||||
|
|
||||||
|
before do
|
||||||
|
DiscourseEvent.on(:acid_face) do |user|
|
||||||
|
user.job = 'Supervillian'
|
||||||
|
end
|
||||||
|
|
||||||
|
DiscourseEvent.trigger(:acid_face, harvey)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'triggers both events' do
|
||||||
|
harvey.job.should == 'Supervillian'
|
||||||
|
harvey.name.should == 'Two Face'
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,11 +5,22 @@ require 'ostruct'
|
||||||
describe DiscoursePlugin do
|
describe DiscoursePlugin do
|
||||||
|
|
||||||
class TestPlugin < DiscoursePlugin
|
class TestPlugin < DiscoursePlugin
|
||||||
|
module SomeModule
|
||||||
|
end
|
||||||
|
|
||||||
|
module TestMixin
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:registry) { mock }
|
let(:registry) { mock }
|
||||||
let(:plugin) { TestPlugin.new(registry) }
|
let(:plugin) { TestPlugin.new(registry) }
|
||||||
|
|
||||||
|
describe ".mixins" do
|
||||||
|
it "finds its mixins" do
|
||||||
|
TestPlugin.mixins.should == [TestPlugin::TestMixin]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "delegates adding js to the registry" do
|
it "delegates adding js to the registry" do
|
||||||
registry.expects(:register_js).with('test.js', any_parameters)
|
registry.expects(:register_js).with('test.js', any_parameters)
|
||||||
plugin.register_js('test.js')
|
plugin.register_js('test.js')
|
||||||
|
@ -38,5 +49,4 @@ describe DiscoursePlugin do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue