mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -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.
|
||||
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
|
||||
|
|
|
@ -6,27 +6,30 @@ class DiscoursePlugin
|
|||
attr_reader :registry
|
||||
|
||||
def initialize(registry)
|
||||
@registry = registry
|
||||
@registry = registry
|
||||
end
|
||||
|
||||
def setup
|
||||
# Initialize the plugin here
|
||||
def setup
|
||||
# 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
|
||||
|
@ -40,7 +43,7 @@ class DiscoursePlugin
|
|||
end
|
||||
|
||||
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))
|
||||
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'
|
||||
end
|
||||
context 'when single event exists' do
|
||||
|
||||
context 'multiple events' do
|
||||
before do
|
||||
DiscourseEvent.on(:acid_face) do |user|
|
||||
user.job = 'Supervillian'
|
||||
end
|
||||
it "doesn't raise an error" do
|
||||
DiscourseEvent.trigger(:acid_face, harvey)
|
||||
end
|
||||
|
||||
it 'triggerred the email event' do
|
||||
harvey.job.should == 'Supervillian'
|
||||
end
|
||||
|
||||
it 'triggerred the username change' do
|
||||
it "changes the name" do
|
||||
DiscourseEvent.trigger(:acid_face, harvey)
|
||||
harvey.name.should == 'Two Face'
|
||||
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
|
||||
|
|
|
@ -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 a new issue