From afb83c11081b7b6b9fd045665233afcc21c52e32 Mon Sep 17 00:00:00 2001 From: Dan Neumann Date: Tue, 12 Feb 2013 22:45:10 -0600 Subject: [PATCH] cleaned up discourse_plugin --- .../discourse_plugin/lib/discourse_event.rb | 34 ++++----- .../lib/discourse_plugin/discourse_plugin.rb | 31 ++++---- .../spec/discourse_event_spec.rb | 74 ++++++++++++++----- .../spec/discourse_plugin_spec.rb | 12 ++- 4 files changed, 97 insertions(+), 54 deletions(-) diff --git a/vendor/gems/discourse_plugin/lib/discourse_event.rb b/vendor/gems/discourse_plugin/lib/discourse_event.rb index 12ecde64b..c0ec064d3 100644 --- a/vendor/gems/discourse_plugin/lib/discourse_event.rb +++ b/vendor/gems/discourse_plugin/lib/discourse_event.rb @@ -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 diff --git a/vendor/gems/discourse_plugin/lib/discourse_plugin/discourse_plugin.rb b/vendor/gems/discourse_plugin/lib/discourse_plugin/discourse_plugin.rb index 67d4b01ac..b39b676c0 100644 --- a/vendor/gems/discourse_plugin/lib/discourse_plugin/discourse_plugin.rb +++ b/vendor/gems/discourse_plugin/lib/discourse_plugin/discourse_plugin.rb @@ -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 diff --git a/vendor/gems/discourse_plugin/spec/discourse_event_spec.rb b/vendor/gems/discourse_plugin/spec/discourse_event_spec.rb index 0aa334eda..94a2bb481 100644 --- a/vendor/gems/discourse_plugin/spec/discourse_event_spec.rb +++ b/vendor/gems/discourse_plugin/spec/discourse_event_spec.rb @@ -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 diff --git a/vendor/gems/discourse_plugin/spec/discourse_plugin_spec.rb b/vendor/gems/discourse_plugin/spec/discourse_plugin_spec.rb index 19b0e5d54..2b22fb3fc 100644 --- a/vendor/gems/discourse_plugin/spec/discourse_plugin_spec.rb +++ b/vendor/gems/discourse_plugin/spec/discourse_plugin_spec.rb @@ -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