Skip to content

Commit b81d880

Browse files
committed
Extract seeding of ColorSchemes, add to db seed
1 parent 7673653 commit b81d880

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

app/models/color_schemes/seed.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class ColorSchemes::Seed
2+
def seed_all
3+
seed_1500
4+
seed_ui
5+
end
6+
7+
def seed_ui
8+
hex_json_uicolors = JSON.parse(File.read("./script/colors/data/uicolors-palette-hex.json"))
9+
hex_json_uicolors.each do |name, weights|
10+
custom_name = "Custom #{name.titleize}"
11+
ColorScheme.find_or_create_by!(name: custom_name) do |cs|
12+
weights.each do |weight, css|
13+
cs.set_weight(weight, css)
14+
end
15+
end
16+
end
17+
end
18+
19+
def seed_1500
20+
# Generate ColorScheme rows from precalcated JSON
21+
# "hex json" files are expected to represent JSON objects of color scales: { name: { weight: color, ... }, ...
22+
23+
if !File.exist?("./script/colors/tmp/1500-palette-hex.json")
24+
puts "Run `rake color_schemes:generate_1500` first to generate 1500-palette-hex.json"
25+
exit
26+
end
27+
28+
hex_json_1500 = JSON.parse(File.read("./script/colors/tmp/1500-palette-hex.json"))
29+
hex_json_1500.each do |name, weights|
30+
ColorScheme.find_or_create_by!(name: name) do |cs|
31+
weights.each do |weight, css|
32+
cs.set_weight(weight, css)
33+
end
34+
end
35+
end
36+
end
37+
end

lib/tasks/color_schemes/seed.rake

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
namespace :color_schemes do
22
desc "Seed color schemes from JSON files"
33
task seed: :environment do
4-
# Generate ColorScheme rows from precalcated JSON
5-
# "hex json" files are expected to represent JSON objects of color scales: { name: { weight: color, ... }, ...
6-
7-
if !File.exist?("./script/colors/tmp/1500-palette-hex.json")
8-
puts "Run `rake color_schemes:generate_1500` first to generate 1500-palette-hex.json"
9-
exit
10-
end
11-
12-
hex_json_1500 = JSON.parse(File.read("./script/colors/tmp/1500-palette-hex.json"))
13-
ColorScheme.bulk_load(hex_json_1500)
14-
15-
hex_json_uicolors = JSON.parse(File.read("./script/colors/data/uicolors-palette-hex.json"))
16-
ColorSchme.bulk_load(hex_json_uicolors) { |name| "Custom #{name.titleize}" }
4+
ColorSchemes::Seed.new.seed_all
175
end
186

197
task :generate_1500 do

0 commit comments

Comments
 (0)