2014-04-16 09:49:06 -04:00
|
|
|
class ColorScheme < ActiveRecord::Base
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
attr_accessor :is_base
|
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
has_many :color_scheme_colors, -> { order('id ASC') }, dependent: :destroy
|
|
|
|
|
|
|
|
alias_method :colors, :color_scheme_colors
|
|
|
|
|
|
|
|
scope :current_version, ->{ where(versioned_id: nil) }
|
|
|
|
|
|
|
|
after_destroy :destroy_versions
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
validates_associated :color_scheme_colors
|
|
|
|
|
|
|
|
BASE_COLORS_FILE = "#{Rails.root}/app/assets/stylesheets/common/foundation/colors.scss"
|
|
|
|
|
|
|
|
@mutex = Mutex.new
|
|
|
|
|
|
|
|
def self.base_colors
|
|
|
|
@mutex.synchronize do
|
|
|
|
return @base_colors if @base_colors
|
|
|
|
@base_colors = {}
|
2014-05-22 11:06:19 -04:00
|
|
|
read_colors_file.each do |line|
|
2014-05-02 17:46:03 -04:00
|
|
|
matches = /\$([\w]+):\s*#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(?:[;]|\s)/.match(line.strip)
|
|
|
|
@base_colors[matches[1]] = matches[2] if matches
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@base_colors
|
|
|
|
end
|
|
|
|
|
2014-05-22 11:06:19 -04:00
|
|
|
def self.read_colors_file
|
|
|
|
File.readlines(BASE_COLORS_FILE)
|
|
|
|
end
|
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
def self.enabled
|
2014-05-02 17:46:03 -04:00
|
|
|
current_version.find_by(enabled: true)
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
def self.base
|
2014-06-03 12:36:34 -04:00
|
|
|
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
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2014-07-17 12:31:37 -04:00
|
|
|
def self.hex_for_name(name)
|
|
|
|
# Can't use `where` here because base doesn't allow it
|
|
|
|
(enabled || base).colors.find {|c| c.name == name }.try(:hex)
|
|
|
|
end
|
2014-05-02 17:46:03 -04:00
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
def colors=(arr)
|
|
|
|
@colors_by_name = nil
|
|
|
|
arr.each do |c|
|
2014-05-02 17:46:03 -04:00
|
|
|
self.color_scheme_colors << ColorSchemeColor.new( name: c[:name], hex: c[:hex] )
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def colors_by_name
|
|
|
|
@colors_by_name ||= self.colors.inject({}) { |sum,c| sum[c.name] = c; sum; }
|
|
|
|
end
|
|
|
|
def clear_colors_cache
|
|
|
|
@colors_by_name = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def colors_hashes
|
|
|
|
color_scheme_colors.map do |c|
|
2014-05-02 17:46:03 -04:00
|
|
|
{name: c.name, hex: c.hex}
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_version
|
|
|
|
ColorScheme.where(versioned_id: self.id).where('version < ?', self.version).order('version DESC').first
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_versions
|
|
|
|
ColorScheme.where(versioned_id: self.id).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-05-08 02:45:49 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: color_schemes
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string(255) not null
|
|
|
|
# enabled :boolean default(FALSE), not null
|
|
|
|
# versioned_id :integer
|
|
|
|
# version :integer default(1), not null
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
#
|