Skip to content

Commit

Permalink
Merge pull request #1368 from alphagov/add-comment-to-controls
Browse files Browse the repository at this point in the history
Add comments to controls
  • Loading branch information
csutter authored Feb 11, 2025
2 parents e2f5337 + dbd6d64 commit 9b6798e
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/controls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def control_params
params.expect(
control: [
:display_name,
:comment,
:action_type,
{ action_attributes: %i[boost_factor filter_expression] },
],
Expand Down
1 change: 1 addition & 0 deletions app/models/control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Control < ApplicationRecord
accepts_nested_attributes_for :action, update_only: true

validates :display_name, presence: true
validates :comment, presence: true

# Returns a representation of this Control as a Discovery Engine control resource
def to_discovery_engine_control
Expand Down
10 changes: 10 additions & 0 deletions app/views/controls/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@

<%= render "#{control.action.to_partial_path}_fields", action: control.action %>

<%= render "govuk_publishing_components/components/textarea", {
label: {
text: t_model_attr(:comment)
},
id: "control_comment",
name: "control[comment]",
value: control.comment,
error_items: error_items(control, :comment)
} %>

<%= render "govuk_publishing_components/components/button", {
text: t("common.buttons.save", model_name: t_model_name),
} %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ en:
other: external links
attributes:
control:
comment: Comment
display_name: Name
action: Action
name: Google Cloud identifier
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20250210143408_add_comment_to_controls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddCommentToControls < ActiveRecord::Migration[8.0]
def up
add_column :controls, :comment, :text,
comment: "A descriptive comment about why this control exists"

execute "UPDATE controls SET comment = '' WHERE comment IS NULL"
change_column_null :controls, :comment, false
end

def down
remove_column :controls, :comment
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2025_01_28_074343) do
ActiveRecord::Schema[8.0].define(version: 2025_02_10_143408) do
create_table "control_boost_actions", charset: "utf8mb3", force: :cascade do |t|
t.string "filter_expression", null: false
t.float "boost_factor", null: false
Expand All @@ -31,6 +31,7 @@
t.string "display_name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "comment", null: false, comment: "A descriptive comment about why this control exists"
t.index ["action_type", "action_id"], name: "index_controls_on_action_type_and_action_id", unique: true
end

Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

factory :control, traits: %i[remote_synchronizable] do
display_name { "Control" }
comment { "This is a nice control." }

action factory: :control_boost_action

Expand Down
11 changes: 11 additions & 0 deletions spec/models/control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
end
end

context "without a comment" do
before do
control.comment = nil
end

it "is invalid" do
expect(control).to be_invalid
expect(control.errors).to be_of_kind(:comment, :blank)
end
end

context "without an action" do
before do
control.action = nil
Expand Down
4 changes: 4 additions & 0 deletions spec/system/controls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ def and_i_submit_the_form_with_boost_control_details
fill_in "Name", with: "My boost control"
fill_in "Filter expression", with: "is_cool = 1"
fill_in "Boost factor", with: "0.42"
fill_in "Comment", with: "This is going to be great."

click_button "Save control"
end

def and_i_submit_the_form_with_filter_control_details
fill_in "Name", with: "My filter control"
fill_in "Filter expression", with: "is_cool = 1"
fill_in "Comment", with: "This is going to be great."

click_button "Save control"
end
Expand Down Expand Up @@ -118,13 +120,15 @@ def and_i_submit_the_form_with_updated_boost_control_details
fill_in "Name", with: "My updated boost control"
fill_in "Filter expression", with: "is_really_cool = 999"
fill_in "Boost factor", with: "0.999"
fill_in "Comment", with: "Now with 999 more BOOST!"

click_button "Save control"
end

def and_i_submit_the_form_with_updated_filter_control_details
fill_in "Name", with: "My updated filter control"
fill_in "Filter expression", with: "is_really_cool = 999"
fill_in "Comment", with: "Now with 999 more FILTER!"

click_button "Save control"
end
Expand Down

0 comments on commit 9b6798e

Please sign in to comment.