Skip to content

Commit

Permalink
Extract seeding of ColorSchemes, add to db seed
Browse files Browse the repository at this point in the history
  • Loading branch information
rossta committed Sep 5, 2024
1 parent 65848aa commit ce60f7e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
37 changes: 37 additions & 0 deletions app/models/color_schemes/seed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class ColorSchemes::Seed
def seed_all
seed_1500
seed_ui
end

def seed_ui
hex_json_uicolors = JSON.parse(File.read("./script/colors/data/uicolors-palette-hex.json"))
hex_json_uicolors.each do |name, weights|
custom_name = "Custom #{name.titleize}"
ColorScheme.find_or_create_by!(name: custom_name) do |cs|
weights.each do |weight, css|
cs.set_weight(weight, css)
end
end
end
end

def seed_1500
# Generate ColorScheme rows from precalcated JSON
# "hex json" files are expected to represent JSON objects of color scales: { name: { weight: color, ... }, ...

if !File.exist?("./script/colors/tmp/1500-palette-hex.json")
puts "Run `rake color_schemes:generate_1500` first to generate 1500-palette-hex.json"
exit
end

hex_json_1500 = JSON.parse(File.read("./script/colors/tmp/1500-palette-hex.json"))
hex_json_1500.each do |name, weights|
ColorScheme.find_or_create_by!(name: name) do |cs|
weights.each do |weight, css|
cs.set_weight(weight, css)
end
end
end
end
end
1 change: 1 addition & 0 deletions db/seeds/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
end

ColorScheme.find_or_create_default
ColorSchemes::Seed.new.seed_ui

START_COUNT_NEWSLETTERS = 5
fill_count = START_COUNT_NEWSLETTERS - Newsletter.count
Expand Down
27 changes: 1 addition & 26 deletions lib/tasks/color_schemes/seed.rake
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
namespace :color_schemes do
desc "Seed color schemes from JSON files"
task seed: :environment do
# Generate ColorScheme rows from precalcated JSON
# "hex json" files are expected to represent JSON objects of color scales: { name: { weight: color, ... }, ...

if !File.exist?("./script/colors/tmp/1500-palette-hex.json")
puts "Run `rake color_schemes:generate_1500` first to generate 1500-palette-hex.json"
exit
end

hex_json_1500 = JSON.parse(File.read("./script/colors/tmp/1500-palette-hex.json"))
hex_json_1500.each do |name, weights|
ColorScheme.find_or_create_by!(name: name) do |cs|
weights.each do |weight, css|
cs.set_weight(weight, css)
end
end
end

hex_json_uicolors = JSON.parse(File.read("./script/colors/data/uicolors-palette-hex.json"))
hex_json_uicolors.each do |name, weights|
custom_name = "Custom #{name.titleize}"
ColorScheme.find_or_create_by!(name: custom_name) do |cs|
weights.each do |weight, css|
cs.set_weight(weight, css)
end
end
end
ColorSchemes::Seed.new.seed_all
end

task :generate_1500 do
Expand Down

0 comments on commit ce60f7e

Please sign in to comment.