Move discourse_plugin to lib

This commit is contained in:
Scott Walkinshaw
2014-07-22 19:02:22 -04:00
parent 6f749b9765
commit 7e2aa5acfb
16 changed files with 4 additions and 205 deletions

View File

@@ -1,10 +0,0 @@
source 'https://rubygems.org'
group :test do
gem 'rails'
gem 'rspec'
gem 'mocha'
end
# Specify your gem's dependencies in rails_multisite.gemspec
gemspec

View File

@@ -1,105 +0,0 @@
PATH
remote: .
specs:
discourse_plugin (0.0.1)
GEM
remote: https://rubygems.org/
specs:
actionmailer (3.2.8)
actionpack (= 3.2.8)
mail (~> 2.4.4)
actionpack (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
builder (~> 3.0.0)
erubis (~> 2.7.0)
journey (~> 1.0.4)
rack (~> 1.4.0)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.1.3)
activemodel (3.2.8)
activesupport (= 3.2.8)
builder (~> 3.0.0)
activerecord (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activeresource (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
activesupport (3.2.8)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.3)
diff-lcs (1.1.3)
erubis (2.7.0)
hike (1.2.1)
i18n (0.6.1)
journey (1.0.4)
json (1.7.5)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
metaclass (0.0.1)
mime-types (1.19)
mocha (0.12.7)
metaclass (~> 0.0.1)
multi_json (1.3.6)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
rack-ssl (1.3.2)
rack
rack-test (0.6.2)
rack (>= 1.0)
rails (3.2.8)
actionmailer (= 3.2.8)
actionpack (= 3.2.8)
activerecord (= 3.2.8)
activeresource (= 3.2.8)
activesupport (= 3.2.8)
bundler (~> 1.0)
railties (= 3.2.8)
railties (3.2.8)
actionpack (= 3.2.8)
activesupport (= 3.2.8)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.3)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.3)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.33)
PLATFORMS
ruby
DEPENDENCIES
discourse_plugin!
mocha
rails
rspec

View File

@@ -1,22 +0,0 @@
Copyright (c) 2012 Robin Ward
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,3 +0,0 @@
# Discourse Plugin
The basic stuff a plugin needs to exist in Discourse

View File

@@ -1,7 +0,0 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:test) do |spec|
spec.pattern = 'spec/*_spec.rb'
end

View File

@@ -1,20 +0,0 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/discourse_plugin/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Robin Ward"]
gem.email = ["robin.ward@gmail.com"]
gem.description = %q{Toolkit for creating a discourse plugin}
gem.summary = %q{Toolkit for creating a discourse plugin}
gem.homepage = ""
# when this is extracted comment it back in, prd has no .git
# gem.files = `git ls-files`.split($\)
gem.files = Dir['README*','LICENSE','lib/**/*.rb']
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "discourse_plugin"
gem.require_paths = ["lib"]
gem.version = DiscoursePlugin::VERSION
end

View File

@@ -1,24 +0,0 @@
# This is meant to be used by plugins to trigger and listen to events
# So we can execute code when things happen.
module DiscourseEvent
# Defaults to a hash where default values are empty sets.
def self.events
@events ||= Hash.new { |hash, key| hash[key] = Set.new }
end
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

View File

@@ -1,3 +0,0 @@
require 'discourse_event'
require 'discourse_plugin/version'
require 'discourse_plugin/discourse_plugin'

View File

@@ -1,51 +0,0 @@
# 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.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

View File

@@ -1,3 +0,0 @@
class DiscoursePlugin
VERSION = "0.0.1"
end

View File

@@ -1,83 +0,0 @@
require 'spec_helper'
require 'discourse_event'
require 'ostruct'
describe DiscourseEvent do
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
describe ".clear" do
it "clears out events" do
DiscourseEvent.events["event42"] << "test event"
DiscourseEvent.clear
DiscourseEvent.events.should be_empty
end
end
context 'when calling events' do
let(:harvey) {
OpenStruct.new(
name: 'Harvey Dent',
job: 'District Attorney'
)
}
before do
DiscourseEvent.on(:acid_face) do |user|
user.name = 'Two Face'
end
end
context 'when event does not exist' do
it "does not raise an error" do
DiscourseEvent.trigger(:missing_event)
end
end
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 '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

View File

@@ -1,52 +0,0 @@
require 'spec_helper'
require 'discourse_plugin'
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')
end
it "delegates adding css to the registry" do
registry.expects(:register_css).with('test.css')
plugin.register_css('test.css')
end
it "delegates creating archetypes" do
registry.expects(:register_archetype).with('banana', oh: 'no!')
plugin.register_archetype('banana', oh: 'no!')
end
context 'registering for callbacks' do
before do
plugin.stubs(:hello)
plugin.listen_for(:hello)
end
it "calls the method when it is triggered" do
plugin.expects(:hello).with('there')
DiscourseEvent.trigger(:hello, 'there')
end
end
end

View File

@@ -1,18 +0,0 @@
require 'rubygems'
require 'rails'
ENV["RAILS_ENV"] ||= 'test'
RSpec.configure do |config|
config.mock_framework = :mocha
config.color_enabled = true
config.before(:each) do
DiscourseEvent.clear
end
end