Add register_color_scheme for plugins
This commit is contained in:
parent
bb0cf87684
commit
3e16ac62c3
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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 = ""
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue