Move `register_asset` into `PluginRegistry` -- the logic works better

there.
This commit is contained in:
Robin Ward 2014-12-09 14:20:53 -05:00
parent 2b817f826b
commit 663cd93c6d
4 changed files with 103 additions and 99 deletions

View File

@ -68,6 +68,34 @@ class DiscoursePluginRegistry
Archetype.register(name, options)
end
def self.register_asset(asset, opts=nil)
if asset =~ /\.js$|\.js\.erb$|\.js\.es6$/
if opts == :admin
self.admin_javascripts << asset
else
if opts == :server_side
self.server_side_javascripts << asset
end
self.javascripts << asset
end
elsif asset =~ /\.css$|\.scss$/
if opts == :mobile
self.mobile_stylesheets << asset
elsif opts == :desktop
self.desktop_stylesheets << asset
elsif opts == :variables
self.sass_variables << asset
else
self.stylesheets << asset
end
elsif asset =~ /\.hbs$/
self.handlebars << asset
elsif asset =~ /\.js\.handlebars$/
self.handlebars << asset
end
end
def javascripts
self.class.javascripts
end
@ -106,6 +134,17 @@ class DiscoursePluginRegistry
self.handlebars = nil
end
def self.reset!
javascripts.clear
admin_javascripts.clear
server_side_javascripts.clear
stylesheets.clear
mobile_stylesheets.clear
desktop_stylesheets.clear
sass_variables.clear
serialized_current_user_fields
end
def self.setup(plugin_class)
registry = DiscoursePluginRegistry.new
plugin = plugin_class.new(registry)

View File

@ -40,7 +40,6 @@ class Plugin::Instance
register_asset(relative)
end
end
end
def name
@ -186,7 +185,6 @@ class Plugin::Instance
Rails.configuration.assets.paths << File.dirname(path) + "/assets"
end
public_data = File.dirname(path) + "/public"
if Dir.exists?(public_data)
target = Rails.root.to_s + "/public/plugins/"
@ -243,31 +241,7 @@ class Plugin::Instance
def register_assets!
assets.each do |asset, opts|
if asset =~ /\.js$|\.js\.erb$|\.js\.es6$/
if opts == :admin
DiscoursePluginRegistry.admin_javascripts << asset
else
if opts == :server_side
DiscoursePluginRegistry.server_side_javascripts << asset
end
DiscoursePluginRegistry.javascripts << asset
end
elsif asset =~ /\.css$|\.scss$/
if opts == :mobile
DiscoursePluginRegistry.mobile_stylesheets << asset
elsif opts == :desktop
DiscoursePluginRegistry.desktop_stylesheets << asset
elsif opts == :variables
DiscoursePluginRegistry.sass_variables << asset
else
DiscoursePluginRegistry.stylesheets << asset
end
elsif asset =~ /\.hbs$/
DiscoursePluginRegistry.handlebars << asset
elsif asset =~ /\.js\.handlebars$/
DiscoursePluginRegistry.handlebars << asset
end
DiscoursePluginRegistry.register_asset(asset, opts)
end
end

View File

@ -82,4 +82,65 @@ describe DiscoursePluginRegistry do
end
end
context '#register_asset' do
let(:registry) { DiscoursePluginRegistry }
after do
registry.reset!
end
it "does register general css properly" do
registry.register_asset("test.css")
registry.register_asset("test2.css")
registry.mobile_stylesheets.count.should == 0
registry.stylesheets.count.should == 2
end
it "registers desktop css properly" do
registry.register_asset("test.css", :desktop)
registry.mobile_stylesheets.count.should == 0
registry.desktop_stylesheets.count.should == 1
registry.stylesheets.count.should == 0
end
it "registers mobile css properly" do
registry.register_asset("test.css", :mobile)
registry.mobile_stylesheets.count.should == 1
registry.stylesheets.count.should == 0
end
it "registers desktop css properly" do
registry.register_asset("test.css", :desktop)
registry.desktop_stylesheets.count.should == 1
registry.stylesheets.count.should == 0
end
it "registers sass variable properly" do
registry.register_asset("test.css", :variables)
registry.sass_variables.count.should == 1
registry.stylesheets.count.should == 0
end
it "registers admin javascript properly" do
registry.register_asset("my_admin.js", :admin)
registry.admin_javascripts.count.should == 1
registry.javascripts.count.should == 0
registry.server_side_javascripts.count.should == 0
end
it "registers server side javascript properly" do
registry.register_asset("my_admin.js", :server_side)
registry.server_side_javascripts.count.should == 1
registry.javascripts.count.should == 1
registry.admin_javascripts.count.should == 0
end
end
end

View File

@ -4,14 +4,7 @@ require_dependency 'plugin/instance'
describe Plugin::Instance do
after do
DiscoursePluginRegistry.javascripts.clear
DiscoursePluginRegistry.admin_javascripts.clear
DiscoursePluginRegistry.server_side_javascripts.clear
DiscoursePluginRegistry.stylesheets.clear
DiscoursePluginRegistry.mobile_stylesheets.clear
DiscoursePluginRegistry.desktop_stylesheets.clear
DiscoursePluginRegistry.sass_variables.clear
DiscoursePluginRegistry.serialized_current_user_fields
DiscoursePluginRegistry.reset!
end
context "find_all" do
@ -31,7 +24,7 @@ describe Plugin::Instance do
end
context "register asset" do
it "does register general css properly" do
it "populates the DiscoursePluginRegistry" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("test.css")
plugin.register_asset("test2.css")
@ -41,71 +34,8 @@ describe Plugin::Instance do
DiscoursePluginRegistry.mobile_stylesheets.count.should == 0
DiscoursePluginRegistry.stylesheets.count.should == 2
end
it "registers desktop css properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("test.css", :desktop)
plugin.send :register_assets!
DiscoursePluginRegistry.mobile_stylesheets.count.should == 0
DiscoursePluginRegistry.desktop_stylesheets.count.should == 1
DiscoursePluginRegistry.stylesheets.count.should == 0
end
it "registers mobile css properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("test.css", :mobile)
plugin.send :register_assets!
DiscoursePluginRegistry.mobile_stylesheets.count.should == 1
DiscoursePluginRegistry.stylesheets.count.should == 0
end
it "registers desktop css properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("test.css", :desktop)
plugin.send :register_assets!
DiscoursePluginRegistry.desktop_stylesheets.count.should == 1
DiscoursePluginRegistry.stylesheets.count.should == 0
end
it "registers sass variable properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("test.css", :variables)
plugin.send :register_assets!
DiscoursePluginRegistry.sass_variables.count.should == 1
DiscoursePluginRegistry.stylesheets.count.should == 0
end
it "registers admin javascript properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("my_admin.js", :admin)
plugin.send :register_assets!
DiscoursePluginRegistry.admin_javascripts.count.should == 1
DiscoursePluginRegistry.javascripts.count.should == 0
DiscoursePluginRegistry.server_side_javascripts.count.should == 0
end
it "registers server side javascript properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("my_admin.js", :server_side)
plugin.send :register_assets!
DiscoursePluginRegistry.server_side_javascripts.count.should == 1
DiscoursePluginRegistry.javascripts.count.should == 1
DiscoursePluginRegistry.admin_javascripts.count.should == 0
end
end
context "activate!" do
it "can activate plugins correctly" do
plugin = Plugin::Instance.new