cleaned up discourse_plugin
This commit is contained in:
parent
69c7b5aeed
commit
afb83c1108
|
@ -2,27 +2,23 @@
|
|||
# So we can execute code when things happen.
|
||||
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)
|
||||
|
||||
return unless @events
|
||||
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 = {}
|
||||
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
|
||||
|
|
|
@ -13,20 +13,23 @@ class DiscoursePlugin
|
|||
# Initialize the plugin here
|
||||
end
|
||||
|
||||
# Find the modules in our class with the name mixin, then include them in the appropriate places
|
||||
# automagically.
|
||||
# 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
|
||||
modules = constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == Module}
|
||||
unless modules.empty?
|
||||
modules.each do |m|
|
||||
original_class = m.to_s.sub("#{self.name}::", '').sub("Mixin", "")
|
||||
dependency_file_name = original_class.underscore
|
||||
require_dependency(dependency_file_name)
|
||||
original_class.constantize.send(:include, m)
|
||||
end
|
||||
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
|
||||
|
|
|
@ -4,13 +4,35 @@ require 'ostruct'
|
|||
|
||||
describe DiscourseEvent do
|
||||
|
||||
it "doesn't raise an error if we call an event that doesn't exist" do
|
||||
DiscourseEvent.trigger(:missing_event)
|
||||
describe "#events" do
|
||||
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
|
||||
|
||||
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
|
||||
DiscourseEvent.on(:acid_face) do |user|
|
||||
|
@ -18,30 +40,42 @@ describe DiscourseEvent do
|
|||
end
|
||||
end
|
||||
|
||||
it "doesn't raise an error" do
|
||||
DiscourseEvent.trigger(:acid_face, harvey)
|
||||
context 'when event does not exist' do
|
||||
|
||||
it "does not raise an error" do
|
||||
DiscourseEvent.trigger(:missing_event)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
it "chnages the name" do
|
||||
DiscourseEvent.trigger(:acid_face, harvey)
|
||||
harvey.name.should == 'Two Face'
|
||||
context 'when single event exists' do
|
||||
|
||||
it "doesn't raise an error" do
|
||||
DiscourseEvent.trigger(:acid_face, harvey)
|
||||
end
|
||||
|
||||
it "changes the name" do
|
||||
DiscourseEvent.trigger(:acid_face, harvey)
|
||||
harvey.name.should == 'Two Face'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'multiple events' do
|
||||
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 'triggerred the email event' do
|
||||
it 'triggers both events' do
|
||||
harvey.job.should == 'Supervillian'
|
||||
end
|
||||
|
||||
it 'triggerred the username change' do
|
||||
harvey.name.should == 'Two Face'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -5,11 +5,22 @@ require 'ostruct'
|
|||
describe DiscoursePlugin do
|
||||
|
||||
class TestPlugin < DiscoursePlugin
|
||||
module SomeModule
|
||||
end
|
||||
|
||||
module TestMixin
|
||||
end
|
||||
end
|
||||
|
||||
let(:registry) { mock }
|
||||
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
|
||||
registry.expects(:register_js).with('test.js', any_parameters)
|
||||
plugin.register_js('test.js')
|
||||
|
@ -38,5 +49,4 @@ describe DiscoursePlugin do
|
|||
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue