Theming: color scheme editing. Unfinished! Doesn't have any effect on css files yet.

This commit is contained in:
Neil Lalonde
2014-04-16 09:49:06 -04:00
parent 0f4014eef1
commit feaaf55a0c
34 changed files with 1086 additions and 84 deletions

View File

@ -0,0 +1,72 @@
require 'spec_helper'
describe Admin::ColorSchemesController do
it "is a subclass of AdminController" do
(described_class < Admin::AdminController).should be_true
end
context "while logged in as an admin" do
let!(:user) { log_in(:admin) }
let(:valid_params) { { color_scheme: {
name: 'Such Design',
enabled: true,
colors: [
{name: '$primary_background_color', hex: 'FFBB00', opacity: '100'},
{name: '$secondary_background_color', hex: '888888', opacity: '70'}
]
}
} }
describe "index" do
it "returns success" do
xhr :get, :index
response.should be_success
end
it "returns JSON" do
Fabricate(:color_scheme)
xhr :get, :index
::JSON.parse(response.body).should be_present
end
end
describe "create" do
it "returns success" do
xhr :post, :create, valid_params
response.should be_success
end
it "returns JSON" do
xhr :post, :create, valid_params
::JSON.parse(response.body)['id'].should be_present
end
end
describe "update" do
let(:existing) { Fabricate(:color_scheme) }
it "returns success" do
ColorSchemeRevisor.expects(:revise).returns(existing)
xhr :put, :update, valid_params.merge(id: existing.id)
response.should be_success
end
it "returns JSON" do
ColorSchemeRevisor.expects(:revise).returns(existing)
xhr :put, :update, valid_params.merge(id: existing.id)
::JSON.parse(response.body)['id'].should be_present
end
end
describe "destroy" do
let!(:existing) { Fabricate(:color_scheme) }
it "returns success" do
expect {
xhr :delete, :destroy, id: existing.id
}.to change { ColorScheme.count }.by(-1)
response.should be_success
end
end
end
end

View File

@ -0,0 +1,6 @@
Fabricator(:color_scheme_color) do
color_scheme
name { sequence(:name) {|i| "$color_#{i}" } }
hex "333333"
opacity 100
end

View File

@ -0,0 +1,5 @@
Fabricator(:color_scheme) do
name { sequence(:name) {|i| "Palette #{i}" } }
enabled false
color_scheme_colors(count: 2) { |attrs, i| Fabricate.build(:color_scheme_color, color_scheme: nil) }
end

View File

@ -0,0 +1,43 @@
require 'spec_helper'
describe ColorScheme do
let(:valid_params) { {name: "Best Colors Evar", enabled: true, colors: valid_colors} }
let(:valid_colors) { [
{name: '$primary_background_color', hex: 'FFBB00', opacity: '100'},
{name: '$secondary_background_color', hex: '888888', opacity: '70'}
]}
describe "new" do
it "can take colors" do
c = described_class.new(valid_params)
c.colors.should have(valid_colors.size).colors
c.colors.first.should be_a(ColorSchemeColor)
expect {
c.save.should == true
}.to change { ColorSchemeColor.count }.by(valid_colors.size)
end
end
describe "destroy" do
it "also destroys old versions" do
c1 = described_class.create(valid_params.merge(version: 2))
c2 = described_class.create(valid_params.merge(versioned_id: c1.id, version: 1))
other = described_class.create(valid_params)
expect {
c1.destroy
}.to change { described_class.count }.by(-2)
end
end
describe "#enabled" do
it "returns the base color scheme when there is no enabled record" do
described_class.enabled.id.should == 1
end
it "returns the enabled color scheme" do
c = described_class.create(valid_params.merge(enabled: true))
described_class.enabled.id.should == c.id
end
end
end

View File

@ -0,0 +1,116 @@
require 'spec_helper'
describe ColorSchemeRevisor do
let(:color) { Fabricate.build(:color_scheme_color, hex: 'FFFFFF', color_scheme: nil) }
let(:color_scheme) { Fabricate(:color_scheme, enabled: false, created_at: 1.day.ago, updated_at: 1.day.ago, color_scheme_colors: [color]) }
let(:valid_params) { { name: color_scheme.name, enabled: color_scheme.enabled, colors: nil } }
describe "revise" do
it "does nothing if there are no changes" do
expect {
described_class.revise(color_scheme, valid_params.merge(colors: nil))
}.to_not change { color_scheme.reload.updated_at }
end
it "can change the name" do
described_class.revise(color_scheme, valid_params.merge(name: "Changed Name"))
color_scheme.reload.name.should == "Changed Name"
end
it "can enable and disable" do
described_class.revise(color_scheme, valid_params.merge(enabled: true))
color_scheme.reload.should be_enabled
described_class.revise(color_scheme, valid_params.merge(enabled: false))
color_scheme.reload.should_not be_enabled
end
it "can change colors" do
described_class.revise(color_scheme, valid_params.merge(colors: [
{name: color.name, hex: 'BEEF99', opacity: 99}
]))
color_scheme.reload
color_scheme.colors.size.should == 1
color_scheme.colors.first.hex.should == 'BEEF99'
color_scheme.colors.first.opacity.should == 99
end
it "disables other color scheme before enabling" do
prev_enabled = Fabricate(:color_scheme, enabled: true)
described_class.revise(color_scheme, valid_params.merge(enabled: true))
prev_enabled.reload.enabled.should == false
color_scheme.reload.enabled.should == true
end
describe "versions" do
it "doesn't create a new version if colors is not given" do
expect {
described_class.revise(color_scheme, valid_params.merge(name: "Changed Name"))
}.to_not change { color_scheme.reload.version }
end
it "creates a new version if colors have changed" do
old_hex, old_opacity = color.hex, color.opacity
expect {
described_class.revise(color_scheme, valid_params.merge(colors: [
{name: color.name, hex: 'BEEF99', opacity: 99}
]))
}.to change { color_scheme.reload.version }.by(1)
old_version = ColorScheme.where(versioned_id: color_scheme.id, version: color_scheme.version - 1).first
old_version.should_not be_nil
old_version.colors.count.should == color_scheme.colors.count
old_version.colors_by_name[color.name].hex.should == old_hex
old_version.colors_by_name[color.name].opacity.should == old_opacity
color_scheme.colors_by_name[color.name].hex.should == 'BEEF99'
color_scheme.colors_by_name[color.name].opacity.should == 99
end
it "doesn't create a new version if colors have not changed" do
expect {
described_class.revise(color_scheme, valid_params.merge(colors: [
{name: color.name, hex: color.hex, opacity: color.opacity}
]))
}.to_not change { color_scheme.reload.version }
end
end
end
describe "revert" do
context "when there are no previous versions" do
it "does nothing" do
expect {
described_class.revert(color_scheme).should == color_scheme
}.to_not change { color_scheme.reload.version }
end
end
context 'when there are previous versions' do
let(:new_color_params) { {name: color.name, hex: 'BEEF99', opacity: 99} }
before do
@prev_hex, @prev_opacity = color.hex, color.opacity
described_class.revise(color_scheme, valid_params.merge(colors: [ new_color_params ]))
end
it "reverts the colors to the previous version" do
color_scheme.colors_by_name[new_color_params[:name]].hex.should == new_color_params[:hex]
expect {
described_class.revert(color_scheme)
}.to change { color_scheme.reload.version }.by(-1)
color_scheme.colors.size.should == 1
color_scheme.colors.first.hex.should == @prev_hex
color_scheme.colors.first.opacity.should == @prev_opacity
color_scheme.colors_by_name[new_color_params[:name]].hex.should == @prev_hex
color_scheme.colors_by_name[new_color_params[:name]].opacity.should == @prev_opacity
end
it "destroys the old version's record" do
expect {
described_class.revert(color_scheme)
}.to change { ColorScheme.count }.by(-1)
color_scheme.reload.previous_version.should be_nil
end
end
end
end