Skip to content

Commit 207e412

Browse files
committed
Extract seeding of ColorSchemes, add to db seed
1 parent 3a197f7 commit 207e412

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
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

db/seeds/development.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
end
1616

1717
ColorScheme.find_or_create_default
18+
ColorSchemes::Seed.new.seed_ui
1819

1920
START_COUNT_NEWSLETTERS = 5
2021
fill_count = START_COUNT_NEWSLETTERS - Newsletter.count

lib/tasks/color_schemes/seed.rake

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +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-
hex_json_1500.each do |name, weights|
14-
ColorScheme.find_or_create_by!(name: name) do |cs|
15-
weights.each do |weight, css|
16-
cs.set_weight(weight, css)
17-
end
18-
end
19-
end
20-
21-
hex_json_uicolors = JSON.parse(File.read("./script/colors/data/uicolors-palette-hex.json"))
22-
hex_json_uicolors.each do |name, weights|
23-
custom_name = "Custom #{name.titleize}"
24-
ColorScheme.find_or_create_by!(name: custom_name) do |cs|
25-
weights.each do |weight, css|
26-
cs.set_weight(weight, css)
27-
end
28-
end
29-
end
4+
ColorSchemes::Seed.new.seed_all
305
end
316

327
task :generate_1500 do

0 commit comments

Comments
 (0)