2014-04-16 09:49:06 -04:00
|
|
|
class ColorScheme < ActiveRecord::Base
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def self.enabled
|
2014-05-06 09:41:59 -04:00
|
|
|
current_version.find_by(enabled: true) || find(1)
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit?
|
|
|
|
self.id != 1 # base theme shouldn't be edited, except by seed data
|
|
|
|
end
|
|
|
|
|
|
|
|
def colors=(arr)
|
|
|
|
@colors_by_name = nil
|
|
|
|
arr.each do |c|
|
|
|
|
self.color_scheme_colors << ColorSchemeColor.new(
|
|
|
|
name: c[:name],
|
|
|
|
hex: c[:hex],
|
|
|
|
opacity: c[:opacity].to_i
|
|
|
|
)
|
|
|
|
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|
|
|
|
|
{name: c.name, hex: c.hex, opacity: c.opacity}
|
|
|
|
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
|
|
|
|
#
|