Add register_color_scheme for plugins
This commit is contained in:
parent
bb0cf87684
commit
3e16ac62c3
|
@ -37,11 +37,27 @@ class ColorScheme < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.base
|
def self.base
|
||||||
return @base_color if @base_color
|
return @base_color_scheme if @base_color_scheme
|
||||||
@base_color = new(name: I18n.t('color_schemes.base_theme_name'), enabled: false)
|
@base_color_scheme = 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_scheme.colors = base_colors.map { |name, hex| {name: name, hex: hex} }
|
||||||
@base_color.is_base = true
|
@base_color_scheme.is_base = true
|
||||||
@base_color
|
@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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ require_dependency 'plugin/auth_provider'
|
||||||
|
|
||||||
class Plugin::Instance
|
class Plugin::Instance
|
||||||
|
|
||||||
attr_reader :auth_providers, :assets, :styles
|
attr_reader :auth_providers, :assets, :styles, :color_schemes
|
||||||
attr_accessor :path, :metadata
|
attr_accessor :path, :metadata
|
||||||
|
|
||||||
def self.find_all(parent_path)
|
def self.find_all(parent_path)
|
||||||
|
@ -23,6 +23,7 @@ class Plugin::Instance
|
||||||
@metadata = metadata
|
@metadata = metadata
|
||||||
@path = path
|
@path = path
|
||||||
@assets = []
|
@assets = []
|
||||||
|
@color_schemes = []
|
||||||
|
|
||||||
# Automatically include all ES6 JS files
|
# Automatically include all ES6 JS files
|
||||||
if @path
|
if @path
|
||||||
|
@ -32,6 +33,7 @@ class Plugin::Instance
|
||||||
register_asset(relative)
|
register_asset(relative)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
|
@ -85,6 +87,10 @@ class Plugin::Instance
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_after_initialize
|
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
|
if @after_initialize
|
||||||
@after_initialize.each do |callback|
|
@after_initialize.each do |callback|
|
||||||
callback.call
|
callback.call
|
||||||
|
@ -107,6 +113,10 @@ class Plugin::Instance
|
||||||
assets << [full_path, opts]
|
assets << [full_path, opts]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def register_color_scheme(name, colors)
|
||||||
|
color_schemes << {name: name, colors: colors}
|
||||||
|
end
|
||||||
|
|
||||||
def automatic_assets
|
def automatic_assets
|
||||||
css = ""
|
css = ""
|
||||||
js = ""
|
js = ""
|
||||||
|
|
|
@ -161,4 +161,24 @@ describe Plugin::Instance do
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -19,6 +19,29 @@ describe ColorScheme do
|
||||||
end
|
end
|
||||||
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
|
describe "destroy" do
|
||||||
it "also destroys old versions" do
|
it "also destroys old versions" do
|
||||||
c1 = described_class.create(valid_params.merge(version: 2))
|
c1 = described_class.create(valid_params.merge(version: 2))
|
||||||
|
|
Loading…
Reference in New Issue