From de9aa49b528cba3a9309fd056bee42211080d8e1 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 10 Apr 2014 16:30:22 +1000 Subject: [PATCH] BUGFIX: default should register CSS for both mobile and desktop If you want desktop only use :desktop option --- lib/plugin/instance.rb | 77 +++++++++++-------------- spec/components/plugin/instance_spec.rb | 69 +++++++++++++++------- 2 files changed, 81 insertions(+), 65 deletions(-) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 8badcde94b4..585a86e0882 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -5,8 +5,7 @@ require_dependency 'plugin/auth_provider' class Plugin::Instance - attr_reader :auth_providers, :assets, :admin_javascripts, - :server_side_javascripts, :styles, :mobile_styles + attr_reader :auth_providers, :assets, :styles attr_accessor :path, :metadata def self.find_all(parent_path) @@ -96,19 +95,7 @@ class Plugin::Instance def register_asset(file, opts=nil) full_path = File.dirname(path) << "/assets/" << file - if opts == :admin - @admin_javascripts ||= [] - @admin_javascripts << full_path - elsif opts == :mobile - @mobile_styles ||= [] - @mobile_styles << full_path - else - assets << full_path - end - if opts == :server_side - @server_side_javascripts ||= [] - @server_side_javascripts << full_path - end + assets << [full_path, opts] end def automatic_assets @@ -152,47 +139,22 @@ class Plugin::Instance end + # note, we need to be able to parse seperately to activation. # this allows us to present information about a plugin in the UI # prior to activations def activate! self.instance_eval File.read(path), path if auto_assets = generate_automatic_assets! - assets.concat auto_assets + assets.concat auto_assets.map{|a| [a]} end unless assets.blank? - assets.each do |asset| - if asset =~ /\.js$|\.js\.erb$/ - DiscoursePluginRegistry.javascripts << asset - elsif asset =~ /\.css$|\.scss$/ - DiscoursePluginRegistry.stylesheets << asset - elsif asset =~ /\.js\.handlebars$/ - DiscoursePluginRegistry.handlebars << asset - end - end - + register_assets! # TODO possibly amend this to a rails engine Rails.configuration.assets.paths << auto_generated_path Rails.configuration.assets.paths << File.dirname(path) + "/assets" end - if @admin_javascripts - @admin_javascripts.each do |js| - DiscoursePluginRegistry.admin_javascripts << js - end - end - - if @mobile_styles - @mobile_styles.each do |style| - DiscoursePluginRegistry.mobile_stylesheets << style - end - end - - if @server_side_javascripts - @server_side_javascripts.each do |js| - DiscoursePluginRegistry.server_side_javascripts << js - end - end public_data = File.dirname(path) + "/public" if Dir.exists?(public_data) @@ -205,6 +167,7 @@ class Plugin::Instance end end + def auth_provider(opts) @auth_providers ||= [] provider = Plugin::AuthProvider.new @@ -245,4 +208,32 @@ class Plugin::Instance end end + protected + + def register_assets! + assets.each do |asset, opts| + if asset =~ /\.js$|\.js\.erb$/ + 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$/ + + unless opts == :mobile + DiscoursePluginRegistry.stylesheets << asset + end + unless opts == :desktop + DiscoursePluginRegistry.mobile_stylesheets << asset + end + + elsif asset =~ /\.js\.handlebars$/ + DiscoursePluginRegistry.handlebars << asset + end + end + end + end diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rb index 733846d96a9..f65a1a76a0f 100644 --- a/spec/components/plugin/instance_spec.rb +++ b/spec/components/plugin/instance_spec.rb @@ -28,33 +28,57 @@ describe Plugin::Instance do end context "register asset" do - it "does register css properly" do + it "does register general css properly" do plugin = Plugin::Instance.new nil, "/tmp/test.rb" plugin.register_asset("test.css") - plugin.assets.count.should == 1 plugin.register_asset("test2.css") - plugin.assets.count.should == 2 + plugin.send :register_assets! + + DiscoursePluginRegistry.mobile_stylesheets.count.should == 2 + DiscoursePluginRegistry.stylesheets.count.should == 2 end - it "does register mobile css properly" do + + 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.stylesheets.count.should == 1 + end + + it "registers mobile css properly" do plugin = Plugin::Instance.new nil, "/tmp/test.rb" plugin.register_asset("test.css", :mobile) - plugin.assets.count.should == 0 - plugin.mobile_styles.count.should == 1 + plugin.send :register_assets! + + DiscoursePluginRegistry.mobile_stylesheets.count.should == 1 + DiscoursePluginRegistry.stylesheets.count.should == 0 end - it "does register admin javascript properly" do + + it "registers admin javascript properly" do plugin = Plugin::Instance.new nil, "/tmp/test.rb" plugin.register_asset("my_admin.js", :admin) - plugin.assets.count.should == 0 - plugin.admin_javascripts.count.should == 1 + + plugin.send :register_assets! + + DiscoursePluginRegistry.admin_javascripts.count.should == 1 + DiscoursePluginRegistry.javascripts.count.should == 0 + DiscoursePluginRegistry.server_side_javascripts.count.should == 0 end - it "does register server side javascript properly" do + + it "registers server side javascript properly" do plugin = Plugin::Instance.new nil, "/tmp/test.rb" plugin.register_asset("my_admin.js", :server_side) - # server side is both in assets and in server_side - plugin.assets.count.should == 1 - plugin.server_side_javascripts.count.should == 1 + + plugin.send :register_assets! + + DiscoursePluginRegistry.server_side_javascripts.count.should == 1 + DiscoursePluginRegistry.javascripts.count.should == 1 + DiscoursePluginRegistry.admin_javascripts.count.should == 0 end + end @@ -74,7 +98,7 @@ describe Plugin::Instance do # calls ensure_assets! make sure they are there plugin.assets.count.should == 1 - plugin.assets.each do |a| + plugin.assets.each do |a, opts| File.exists?(a).should be_true end @@ -85,16 +109,17 @@ describe Plugin::Instance do it "finds all the custom assets" do plugin = Plugin::Instance.new plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb" - # two styles + plugin.register_asset("test.css") plugin.register_asset("test2.scss") - # one javascript + plugin.register_asset("mobile.css", :mobile) + plugin.register_asset("desktop.css", :desktop) + plugin.register_asset("desktop2.css", :desktop) + plugin.register_asset("code.js") - # one mobile - plugin.register_asset("test.css", :mobile) - # a server side + plugin.register_asset("server_side.js", :server_side) - # and two admin + plugin.register_asset("my_admin.js", :admin) plugin.register_asset("my_admin2.js", :admin) @@ -103,8 +128,8 @@ describe Plugin::Instance do DiscoursePluginRegistry.javascripts.count.should == 3 DiscoursePluginRegistry.admin_javascripts.count.should == 2 DiscoursePluginRegistry.server_side_javascripts.count.should == 1 - DiscoursePluginRegistry.stylesheets.count.should == 2 - DiscoursePluginRegistry.mobile_stylesheets.count.should == 1 + DiscoursePluginRegistry.stylesheets.count.should == 4 + DiscoursePluginRegistry.mobile_stylesheets.count.should == 3 end end