From 3e16ac62c3025d55d0805712b17aa278dc8f041a Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 3 Jun 2014 12:36:34 -0400 Subject: [PATCH] Add register_color_scheme for plugins --- app/models/color_scheme.rb | 26 ++++++++++++++++++++----- lib/plugin/instance.rb | 12 +++++++++++- spec/components/plugin/instance_spec.rb | 20 +++++++++++++++++++ spec/models/color_scheme_spec.rb | 23 ++++++++++++++++++++++ 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/app/models/color_scheme.rb b/app/models/color_scheme.rb index cf659b2ecf9..a4fe017e47c 100644 --- a/app/models/color_scheme.rb +++ b/app/models/color_scheme.rb @@ -37,11 +37,27 @@ class ColorScheme < ActiveRecord::Base end def self.base - return @base_color if @base_color - @base_color = new(name: I18n.t('color_schemes.base_theme_name'), enabled: false) - @base_color.colors = base_colors.map { |name, hex| {name: name, hex: hex} } - @base_color.is_base = true - @base_color + return @base_color_scheme if @base_color_scheme + @base_color_scheme = new(name: I18n.t('color_schemes.base_theme_name'), enabled: false) + @base_color_scheme.colors = base_colors.map { |name, hex| {name: name, hex: hex} } + @base_color_scheme.is_base = true + @base_color_scheme + end + + # create_from_base will create a new ColorScheme that overrides Discourse's base color scheme with the given colors. + def self.create_from_base(params) + new_color_scheme = new(name: params[:name]) + colors = base.colors_hashes + + # Override base values + params[:colors].each do |name, hex| + c = colors.find {|x| x[:name].to_s == name.to_s} + c[:hex] = hex + end + + new_color_scheme.colors = colors + new_color_scheme.save + new_color_scheme end diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 2b8ec9218fd..e8e5ccd5145 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -5,7 +5,7 @@ require_dependency 'plugin/auth_provider' class Plugin::Instance - attr_reader :auth_providers, :assets, :styles + attr_reader :auth_providers, :assets, :styles, :color_schemes attr_accessor :path, :metadata def self.find_all(parent_path) @@ -23,6 +23,7 @@ class Plugin::Instance @metadata = metadata @path = path @assets = [] + @color_schemes = [] # Automatically include all ES6 JS files if @path @@ -32,6 +33,7 @@ class Plugin::Instance register_asset(relative) end end + end def name @@ -85,6 +87,10 @@ class Plugin::Instance end def notify_after_initialize + color_schemes.each do |c| + ColorScheme.create_from_base(name: c[:name], colors: c[:colors]) unless ColorScheme.where(name: c[:name]).exists? + end + if @after_initialize @after_initialize.each do |callback| callback.call @@ -107,6 +113,10 @@ class Plugin::Instance assets << [full_path, opts] end + def register_color_scheme(name, colors) + color_schemes << {name: name, colors: colors} + end + def automatic_assets css = "" js = "" diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rb index c61ff9cb59e..bb7de3cac1e 100644 --- a/spec/components/plugin/instance_spec.rb +++ b/spec/components/plugin/instance_spec.rb @@ -161,4 +161,24 @@ describe Plugin::Instance do end end + context "register_color_scheme" do + it "can add a color scheme for the first time" do + plugin = Plugin::Instance.new nil, "/tmp/test.rb" + expect { + plugin.register_color_scheme("Purple", {primary: 'EEE0E5'}) + plugin.notify_after_initialize + }.to change { ColorScheme.count }.by(1) + ColorScheme.where(name: "Purple").should be_present + end + + it "doesn't add the same color scheme twice" do + Fabricate(:color_scheme, name: "Halloween") + plugin = Plugin::Instance.new nil, "/tmp/test.rb" + expect { + plugin.register_color_scheme("Halloween", {primary: 'EEE0E5'}) + plugin.notify_after_initialize + }.to_not change { ColorScheme.count } + end + end + end diff --git a/spec/models/color_scheme_spec.rb b/spec/models/color_scheme_spec.rb index 288a43878d4..1e15de52ace 100644 --- a/spec/models/color_scheme_spec.rb +++ b/spec/models/color_scheme_spec.rb @@ -19,6 +19,29 @@ describe ColorScheme do end end + describe "create_from_base" do + let(:base_colors) { {first_one: 'AAAAAA', second_one: '333333', third_one: 'BEEBEE'} } + let!(:base) { Fabricate(:color_scheme, name: 'Base', color_scheme_colors: [ + Fabricate(:color_scheme_color, name: 'first_one', hex: base_colors[:first_one]), + Fabricate(:color_scheme_color, name: 'second_one', hex: base_colors[:second_one]), + Fabricate(:color_scheme_color, name: 'third_one', hex: base_colors[:third_one])]) } + + before do + described_class.stubs(:base).returns(base) + end + + it "creates a new color scheme" do + c = described_class.create_from_base(name: 'Yellow', colors: {first_one: 'FFFF00', third_one: 'F00D33'}) + c.colors.should have(base_colors.size).colors + first = c.colors.find {|x| x.name == 'first_one'} + second = c.colors.find {|x| x.name == 'second_one'} + third = c.colors.find {|x| x.name == 'third_one'} + first.hex.should == 'FFFF00' + second.hex.should == base_colors[:second_one] + third.hex.should == 'F00D33' + end + end + describe "destroy" do it "also destroys old versions" do c1 = described_class.create(valid_params.merge(version: 2))