Skip to content

Commit 524dad5

Browse files
committed
Add cached id query for color scale default and reset button
1 parent 03bcfb5 commit 524dad5

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

app/controllers/color_themes_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ def show
55

66
render ColorThemes::ShowView.new(
77
color_scale: @color_scale,
8-
curated_color_scales: curated_color_scales
8+
curated_color_scales: curated_color_scales,
9+
selected: params[:id].present?
910
)
1011
end
1112
end

app/models/color_scale.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ def self.curated
2828
end
2929

3030
def self.cached_curated
31-
cached_ids = Rails.cache.read("curated_color_scale_ids")
31+
cache_key = "curated_color_scale_ids"
32+
cached_ids = Rails.cache.read(cache_key)
3233

3334
return where(id: cached_ids) if cached_ids
3435

3536
curated.tap do |collection|
36-
Rails.cache.write("curated_color_scale_ids", collection.map(&:id))
37+
Rails.cache.write(cache_key, collection.map(&:id))
3738
end
3839
end
3940

@@ -45,6 +46,17 @@ def self.find_or_create_default
4546
end
4647
end
4748

49+
def self.cached_default
50+
cache_key = "default_color_scale_id"
51+
cached_id = Rails.cache.read(cache_key)
52+
53+
return where(id: cached_id).first if cached_id
54+
55+
where(name: APP_DEFAULT[:name]).first.tap do |cs|
56+
Rails.cache.write("default_color_scale_id", cs.id)
57+
end
58+
end
59+
4860
def set_weight(weight, value)
4961
raise ArgumentError, "Invalid weight: #{weight}" unless VALID_WEIGHTS.include?(weight.to_s)
5062

app/views/color_themes/show_view.rb

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,62 @@ class ColorThemes::ShowView < ApplicationView
44
include Phlex::Rails::Helpers::FormWith
55
include Phlex::Rails::Helpers::LinkTo
66

7-
def initialize(color_scale:, curated_color_scales: [])
7+
def initialize(color_scale:, curated_color_scales: [], selected: false)
88
@color_scale = color_scale
99
@curated_color_scales = curated_color_scales
10+
@selected = selected
1011
end
1112

1213
def view_template
1314
render Pages::Header.new(title: "Settings: Color Theme")
1415
div(class: "section-content container py-gap") do
15-
h3 { @color_scale.display_name }
16-
div(class: "color-theme color-theme:#{@color_scale.name.parameterize}") do
17-
@color_scale.weights.each do |weight, color|
18-
div(class: "color-swatch color-swatch__weight:#{weight}", style: "background-color: #{color.hex}") do
19-
div(class: "color-swatch__weight") { weight }
20-
div(class: "color-swatch__color") { color.hex.delete("#").upcase }
21-
end
22-
end
23-
end
24-
2516
h3 { "Want to try a different color theme?" }
17+
2618
p { "You can preview and save your desired color theme for this site. We have curated some options for you below." }
2719

28-
form_with(url: color_theme_path, method: :get) do |f|
20+
form_with(url: url_for, method: :get) do |f|
2921
fieldset(class: "flex items-center") do
3022
f.select :id,
3123
@curated_color_scales.map { |cs| [cs.display_name, cs.id] },
3224
{
33-
prompt: "Pick one!"
25+
prompt: "Pick one!",
26+
selected: (@selected ? @color_scale.id : nil)
3427
},
28+
# requestSubmit and Turbo
3529
# https://stackoverflow.com/questions/68624668/how-can-i-submit-a-form-on-input-change-with-turbo-streams
3630
onchange: "this.form.requestSubmit()",
3731
class: "mr-2 bg-gray-50 border border-gray-300 text-small rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
3832

39-
whitespace
40-
span(class: "mr-2 font-small") { "OR" }
41-
whitespace
42-
link_to "I feel lucky!", color_theme_path(id: @curated_color_scales.sample.id), class: "button primary"
33+
span(class: "mr-2 text-small") { "OR" }
34+
link_to "I feel lucky!", url_for(id: @curated_color_scales.sample.id), class: "button primary mr-2"
35+
if @selected
36+
span(class: "mr-2 text-small") { unsafe_raw "&bull;" }
37+
link_to "Reset to default", url_for, class: "button secondary"
38+
end
4339
end
4440
end
41+
42+
h3 { display_name }
43+
div(class: "color-theme color-theme:#{@color_scale.name.parameterize}") do
44+
@color_scale.weights.each do |weight, color|
45+
div(class: "color-swatch color-swatch__weight:#{weight}", style: "background-color: #{color.hex}") do
46+
div(class: "color-swatch__weight") { weight }
47+
div(class: "color-swatch__color") { color.hex.delete("#").upcase }
48+
end
49+
end
50+
end
51+
end
52+
end
53+
54+
private
55+
56+
def default_color_scale? = @color_scale.id == ColorScale.cached_default.id
57+
58+
def display_name
59+
if default_color_scale?
60+
"#{@color_scale.display_name} (default)"
61+
else
62+
@color_scale.display_name
4563
end
4664
end
4765
end

0 commit comments

Comments
 (0)