Skip to content

Commit c94b6d4

Browse files
committed
Extract color theme model
1 parent 524dad5 commit c94b6d4

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
class ColorThemesController < ApplicationController
22
def show
3-
@color_scale = params[:id] ? ColorScale.find(params[:id]) : ColorScale.find_or_create_default
4-
curated_color_scales = ColorScale.cached_curated
3+
show_params = params[:color_theme] ? params.require(:color_theme).permit(:color_scale_id) : {}
4+
color_scale_id = show_params[:color_scale_id]
5+
@color_scale = color_scale_id ? ColorScale.find(color_scale_id) : ColorScale.find_or_create_default
6+
@color_theme = ColorTheme.new(color_scale: @color_scale)
57

68
render ColorThemes::ShowView.new(
7-
color_scale: @color_scale,
8-
curated_color_scales: curated_color_scales,
9-
selected: params[:id].present?
9+
color_theme: @color_theme,
10+
curated_color_scales: ColorScale.cached_curated,
11+
selected: color_scale_id.present?
1012
)
1113
end
1214
end

app/models/color_theme.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class ColorTheme
2+
include ActiveModel::Model
3+
4+
attr_accessor :color_scale
5+
6+
def initialize(color_scale:)
7+
@color_scale = color_scale
8+
end
9+
end

app/views/color_themes/show_view.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ 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: [], selected: false)
8-
@color_scale = color_scale
7+
def initialize(color_theme:, curated_color_scales: [], selected: false)
8+
@color_theme = color_theme
9+
@color_scale = color_theme.color_scale
910
@curated_color_scales = curated_color_scales
1011
@selected = selected
1112
end
@@ -17,9 +18,10 @@ def view_template
1718

1819
p { "You can preview and save your desired color theme for this site. We have curated some options for you below." }
1920

20-
form_with(url: url_for, method: :get) do |f|
21+
form_with(model: @color_theme, url: url_for, method: :get) do |f|
2122
fieldset(class: "flex items-center") do
22-
f.select :id,
23+
f.select(
24+
:color_scale_id,
2325
@curated_color_scales.map { |cs| [cs.display_name, cs.id] },
2426
{
2527
prompt: "Pick one!",
@@ -29,9 +31,10 @@ def view_template
2931
# https://stackoverflow.com/questions/68624668/how-can-i-submit-a-form-on-input-change-with-turbo-streams
3032
onchange: "this.form.requestSubmit()",
3133
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"
34+
)
3235

3336
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"
37+
link_to "I feel lucky!", url_for(color_theme: {color_scale_id: @curated_color_scales.sample.id}), class: "button primary mr-2"
3538
if @selected
3639
span(class: "mr-2 text-small") { unsafe_raw "&bull;" }
3740
link_to "Reset to default", url_for, class: "button secondary"

0 commit comments

Comments
 (0)