Skip to content

Commit 22cf3cb

Browse files
committed
Add ColorScale model and seed default color scale
1 parent edff3d8 commit 22cf3cb

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

app/models/color_scale.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
class ColorScale < ApplicationRecord
4+
APP_DEFAULT = {
5+
name: "Custom Cerulean Blue",
6+
weights: {
7+
"50": "#f1f5fd",
8+
"100": "#dfe8fa",
9+
"200": "#c7d7f6",
10+
"300": "#a1beef",
11+
"400": "#749be6",
12+
"500": "#537ade",
13+
"600": "#3e5dd2",
14+
"700": "#354bc0",
15+
"800": "#313f9c",
16+
"900": "#2c397c",
17+
"950": "#1f244c"
18+
}
19+
}.freeze
20+
21+
VALID_WEIGHTS = %w[50 100 200 300 400 500 600 700 800 900 950].freeze
22+
23+
def self.find_or_create_default
24+
find_or_create_by(name: APP_DEFAULT[:name]) do |cs|
25+
APP_DEFAULT[:weights].each do |weight, value|
26+
cs.set_weight(weight, value)
27+
end
28+
end
29+
end
30+
31+
def set_weight(weight, value)
32+
raise ArgumentError, "Invalid weight: #{weight}" unless VALID_WEIGHTS.include?(weight.to_s)
33+
34+
send(:"weight_#{weight}=", value)
35+
end
36+
37+
def weights
38+
VALID_WEIGHTS.each_with_object({}) do |weight, hash|
39+
hash[weight] = send(:"weight_#{weight}")
40+
end
41+
end
42+
43+
def to_css
44+
weights.map { |weight, value| "--color-#{name.dasherize}-#{weight}: #{value};" }.join("\n")
45+
end
46+
end

db/seeds/development.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
u.password = password
77
u.password_confirmation = password
88
end
9+
10+
ColorScale.find_or_create_default

db/seeds/production.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
u.password = password
77
u.password_confirmation = password
88
end
9+
10+
ColorScale.find_or_create_default

spec/factories/color_scales.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require "faker"
2+
3+
FactoryBot.define do
4+
factory :color_scale do
5+
name { Faker::Color.color_name.titleize }
6+
weight_50 { Faker::Color.hex_color }
7+
weight_100 { Faker::Color.hex_color }
8+
weight_200 { Faker::Color.hex_color }
9+
weight_300 { Faker::Color.hex_color }
10+
weight_400 { Faker::Color.hex_color }
11+
weight_500 { Faker::Color.hex_color }
12+
weight_600 { Faker::Color.hex_color }
13+
weight_700 { Faker::Color.hex_color }
14+
weight_800 { Faker::Color.hex_color }
15+
weight_900 { Faker::Color.hex_color }
16+
weight_950 { Faker::Color.hex_color }
17+
end
18+
end

spec/models/color_scale_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require "rails_helper"
2+
3+
RSpec.describe ColorScale, type: :model do
4+
describe "self.find_or_create_default" do
5+
it "should be idempotent" do
6+
expect {
7+
ColorScale.find_or_create_default
8+
}.to change(ColorScale, :count).by(1)
9+
10+
expect {
11+
ColorScale.find_or_create_default
12+
}.not_to change(ColorScale, :count)
13+
end
14+
end
15+
16+
describe "#weights" do
17+
it "should return a hash of all weights" do
18+
color_scale = FactoryBot.build(:color_scale)
19+
20+
expect(color_scale.weights).to eq(
21+
"50" => color_scale.weight_50,
22+
"100" => color_scale.weight_100,
23+
"200" => color_scale.weight_200,
24+
"300" => color_scale.weight_300,
25+
"400" => color_scale.weight_400,
26+
"500" => color_scale.weight_500,
27+
"600" => color_scale.weight_600,
28+
"700" => color_scale.weight_700,
29+
"800" => color_scale.weight_800,
30+
"900" => color_scale.weight_900,
31+
"950" => color_scale.weight_950
32+
)
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)