Skip to content

Commit 658fda0

Browse files
committed
Refactoring: checkbox -> rails_flag_checkbox
1 parent 791eae8 commit 658fda0

27 files changed

+667
-197
lines changed

app/javascript/controllers/check_box_controller.js

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
// Handles the Rails flag checkboxes in the UI
4+
export default class extends Controller {
5+
static targets = ["output"]
6+
7+
connect() {
8+
this.groupElement = this.element.closest('ul')
9+
if (!this.groupElement) return
10+
this.update()
11+
}
12+
13+
update() {
14+
const checkboxes = this.groupElement.querySelectorAll('input[type="checkbox"]')
15+
const selectedValues = Array.from(checkboxes)
16+
.filter(checkbox => {
17+
const displayWhen = checkbox.dataset.displayWhen || 'checked'
18+
const isChecked = checkbox.checked
19+
return (displayWhen === 'checked' && isChecked) ||
20+
(displayWhen === 'unchecked' && !isChecked)
21+
})
22+
.map(checkbox => checkbox.dataset.commandOutput)
23+
.filter(Boolean)
24+
25+
const outputElement = document.getElementById('rails-flags')
26+
outputElement.textContent = selectedValues.join(" ")
27+
}
28+
}

app/models/concerns/group_behavior.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module GroupBehavior
24
extend ActiveSupport::Concern
35

@@ -15,13 +17,13 @@ def stimulus_attributes
1517
}
1618
when "generic_checkbox"
1719
{
18-
controller: "check-box",
19-
"check-box-generated-output-outlet": "#rails-flags"
20+
controller: "rails-flag-checkbox",
21+
"rails-flag-checkbox-generated-output-outlet": "#rails-flags"
2022
}
2123
when "custom_ingredient_checkbox"
2224
{
23-
controller: "check-box",
24-
"check-box-generated-output-outlet": "#custom_ingredients"
25+
controller: "rails-flag-checkbox",
26+
"rails-flag-checkbox-generated-output-outlet": "#custom_ingredients"
2527
}
2628
else
2729
{}

app/models/element.rb

+17-2
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ class Element < ApplicationRecord
3232
before_save :set_command_line_value
3333

3434
delegated_type :variant, types: %w[
35-
Element::Checkbox
35+
Element::RailsFlagCheckbox
3636
Element::RadioButton
3737
Element::TextField
3838
Element::Unclassified
39-
]
39+
], dependent: :destroy
4040

4141
belongs_to :sub_group
42+
has_one :group, through: :sub_group
4243

4344
validates :label, presence: true, uniqueness: { scope: :sub_group_id }
45+
validate :unique_label_within_group
4446

4547
def self.null
4648
new(variant: Element::Null.new)
@@ -59,4 +61,17 @@ def displayed?
5961
def set_command_line_value
6062
self.command_line_value = generate_command_line_value
6163
end
64+
65+
def unique_label_within_group
66+
return unless group
67+
68+
duplicate = group.sub_groups.joins(:elements)
69+
.where.not(elements: { id: id })
70+
.where(elements: { label: label })
71+
.exists?
72+
73+
if duplicate
74+
errors.add(:label, "must be unique within the group")
75+
end
76+
end
6277
end

app/models/element/checkbox.rb app/models/element/rails_flag_checkbox.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# == Schema Information
22
#
3-
# Table name: element_checkboxes
3+
# Table name: element_rails_flag_checkboxes
44
#
55
# id :integer not null, primary key
66
# checked :boolean
@@ -24,21 +24,20 @@
2424
# Examples:
2525
#
2626
# A checkbox for "--no-test"
27-
# Element::Checkbox.create!(
27+
# Element::RailsFlagCheckbox.create!(
2828
# default: true, # Starts checked
2929
# display_when: "unchecked", # Only outputs when unchecked
3030
# # When unchecked, it would output "--no-test"
3131
# )
3232

3333
# # A checkbox for "--debug"
34-
# Element::Checkbox.create!(
34+
# Element::RailsFlagCheckbox.create!(
3535
# default: false, # Starts unchecked
3636
# display_when: "checked", # Only outputs when checked
3737
# # When checked, it would output "--debug"
3838
# )
3939

40-
41-
class Element::Checkbox < ApplicationRecord
40+
class Element::RailsFlagCheckbox < ApplicationRecord
4241
has_one :element, as: :variant
4342

4443
def displayed?

app/models/generated_app.rb

-8
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
class GeneratedApp < ApplicationRecord
3737
include HasGenerationLifecycle
3838

39-
attr_accessor :update_recipe
40-
4139
broadcasts_to ->(generated_app) { [ :generated_apps, generated_app.user_id ] }
4240
broadcasts_to ->(generated_app) { [ :notification_badge, generated_app.user_id ] }
4341

@@ -85,12 +83,6 @@ def apply_ingredient!(ingredient, configuration = {})
8583
configuration: configuration
8684
)
8785

88-
# Apply to recipe if update_recipe is true
89-
if update_recipe
90-
logger.info("Applying recipe change")
91-
recipe_change.apply!
92-
end
93-
9486
template_path = DataRepository.new(user: user).template_path(ingredient)
9587

9688
require "rails/generators"

app/services/ingredient_ui_creator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def create_element(sub_group)
3535
label: ingredient.name,
3636
description: ingredient.description,
3737
position: last_position + 1,
38-
variant: Element::Checkbox.create!(
38+
variant: Element::RailsFlagCheckbox.create!(
3939
checked: false,
4040
display_when: "checked"
4141
)

app/services/ingredient_ui_destroyer.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def call
2323
element.destroy
2424

2525
# If this was the last element in the sub_group, delete the sub_group
26-
if sub_group.elements.empty?
26+
if sub_group.elements.reload.empty?
2727
sub_group.destroy
28-
end
2928

30-
# If this was the last sub_group in the group, delete the group
31-
if group.sub_groups.empty?
32-
group.destroy
29+
# If this was the last sub_group in the group, delete the group
30+
if group.sub_groups.reload.empty?
31+
group.destroy
32+
end
3333
end
3434
end
3535
end

app/views/components/pages/groups/sub_groups/elements/component.rb

+3-9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def view_template
2424
action: "change->radio-button-choice#update"
2525
}
2626
)
27-
when "Element::Checkbox"
28-
render Pages::Groups::SubGroups::Elements::Checkbox::Component.new(
27+
when "Element::RailsFlagCheckbox"
28+
render Pages::Groups::SubGroups::Elements::RailsFlagCheckbox::Component.new(
2929
label: element.label,
3030
description: element.description,
3131
image_path: element.image_path,
@@ -34,16 +34,10 @@ def view_template
3434
command_line_value: element.command_line_value,
3535
display_when: element.variant.display_when,
3636
data: {
37-
action: "change->check-box#update",
37+
action: "change->rails-flag-checkbox#update",
3838
**group_stimulus_attributes
3939
}
4040
)
41-
# when "Element::TextField"
42-
# render Pages::Groups::SubGroups::Elements::TextField::Component.new(
43-
# label: element.label,
44-
# description: element.description,
45-
# name: element.label,
46-
# )
4741
else
4842
raise "Unknown element variant_type: #{element.variant_type}"
4943
end

app/views/components/pages/groups/sub_groups/elements/checkbox/component.rb app/views/components/pages/groups/sub_groups/elements/rails_flag_checkbox/component.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Pages
44
module Groups
55
module SubGroups
66
module Elements
7-
module Checkbox
7+
module RailsFlagCheckbox
88
class Component < ApplicationComponent
99
include Phlex::Rails::Helpers::ImageTag
1010

@@ -19,13 +19,13 @@ def initialize(label:, description:, image_path:, name:, command_line_value:, ch
1919
@display_when = display_when
2020

2121
# Extract the outlet from data if present, defaulting to #rails-flags
22-
@outlet = @data.delete("check-box-generated-output-outlet") || "#rails-flags"
22+
@outlet = @data.delete("rails-flag-checkbox-generated-output-outlet") || "#rails-flags"
2323
end
2424

2525
def view_template
26-
li(data_controller: "check-box",
26+
li(data_controller: "rails-flag-checkbox",
2727
data: {
28-
"check-box-generated-output-outlet": @outlet,
28+
"rails-flag-checkbox-generated-output-outlet": @outlet,
2929
**@data.except("action")
3030
},
3131
class: "menu-card-row text-ruby") do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RenameElementCheckboxesToRailsFlagCheckboxes < ActiveRecord::Migration[7.1]
2+
def change
3+
rename_table :element_checkboxes, :element_rails_flag_checkboxes
4+
end
5+
end

db/schema.rb

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

design/landing-page-image.jpg

327 KB
Loading

test/components/pages/groups/component_test.rb

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1+
# frozen_string_literal: true
2+
13
require "test_helper"
4+
require "support/phlex_component_test_case"
25

36
module Pages
47
module Groups
5-
class ComponentTest < ActiveSupport::TestCase
6-
include Phlex::Testing::ViewHelper
7-
8-
setup do
9-
@databases_group = groups(:databases)
10-
@dev_env_group = groups(:dev_env)
8+
class ComponentTest < PhlexComponentTestCase
9+
test "renders group with stimulus attributes" do
10+
group = Group.new(title: "Test Group", behavior_type: "generic_checkbox")
11+
group.stubs(:stimulus_attributes).returns({
12+
controller: "rails-flag-checkbox",
13+
"rails-flag-checkbox-generated-output-outlet": "#rails-flags"
14+
})
15+
group.stubs(:sub_groups).returns([])
16+
17+
component = Component.new(group: group)
18+
html = component.render_in(view_context)
19+
20+
assert_includes html, 'data-controller="rails-flag-checkbox"'
21+
assert_includes html, 'data-rails-flag-checkbox-generated-output-outlet="#rails-flags"'
1122
end
1223

13-
test "renders database choice group with correct stimulus attributes" do
14-
component = Component.new(group: @databases_group)
15-
16-
Pages::Groups::SubGroups::Elements::RadioButton::Component.any_instance
17-
.stubs(:image_tag)
18-
.returns("image_tag_stub")
24+
private
1925

20-
html = render(component)
21-
22-
assert_includes html, 'data-controller="radio-button-choice"'
23-
assert_includes html, 'data-radio-button-choice-generated-output-outlet="#database-choice"'
24-
assert_includes html, 'data-output-prefix="-d"'
26+
def view_context
27+
controller.view_context
2528
end
2629

27-
test "renders generic checkbox group with correct stimulus attributes" do
28-
@dev_env_group.update!(behavior_type: "generic_checkbox")
29-
component = Component.new(group: @dev_env_group)
30-
31-
html = render(component)
32-
33-
assert_includes html, 'data-controller="check-box"'
34-
assert_includes html, 'data-check-box-generated-output-outlet="#rails-flags"'
30+
def controller
31+
@controller ||= ApplicationController.new.tap do |c|
32+
c.request = ActionDispatch::TestRequest.create
33+
c.response = ActionDispatch::TestResponse.new
34+
end
3535
end
3636
end
3737
end

0 commit comments

Comments
 (0)