Skip to content

Commit

Permalink
Merge pull request #1254 from alphagov/controls-mvp-1
Browse files Browse the repository at this point in the history
Add basic scaffolding for Boosts
  • Loading branch information
csutter authored Jul 15, 2024
2 parents 2876976 + 58dad81 commit 1082dcd
Show file tree
Hide file tree
Showing 17 changed files with 565 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@
.js-hidden {
display: none;
}

// Display filter expressions with less large font size
.app-filter-expression {
font-size: 16px;
}
51 changes: 51 additions & 0 deletions app/controllers/boosts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class BoostsController < ApplicationController
def index
@boosts = Boost.order(:name)
end

def show
@boost = Boost.find(params[:id])
end

def new
@boost = Boost.new
end

def create
@boost = Boost.new(boost_params)
if @boost.save
redirect_to @boost, notice: "Boost created successfully"
else
render "new", status: :unprocessable_entity
end
end

def edit
@boost = Boost.find(params[:id])
end

def update
@boost = Boost.find(params[:id])
if @boost.update(boost_params)
redirect_to @boost, notice: "Boost updated successfully"
else
render "edit", status: :unprocessable_entity
end
end

def destroy
@boost = Boost.find(params[:id])

if @boost.destroy
redirect_to boosts_path, notice: "Boost deleted successfully"
else
redirect_to @boost, alert: "Boost could not be deleted. Try again later."
end
end

private

def boost_params
params.require(:boost).permit(:name, :active, :boost_amount, :filter)
end
end
5 changes: 5 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ def navigation_items
href: recommended_links_path,
active: controller.controller_name == "recommended_links",
},
{
text: "Boosts",
href: boosts_path,
active: controller.controller_name == "boosts",
},
{
text: current_user.name,
href: Plek.new.external_url_for("signon"),
Expand Down
13 changes: 13 additions & 0 deletions app/helpers/boosts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module BoostsHelper
def boost_status_tag(boost)
if boost.active?
content_tag(:span, "Active", class: "govuk-tag govuk-tag--blue")
else
content_tag(:span, "Not active", class: "govuk-tag govuk-tag--grey")
end
end

def filter_expression_code(boost)
content_tag(:code, boost.filter, class: "app-filter-expression")
end
end
6 changes: 6 additions & 0 deletions app/models/boost.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Boost < ApplicationRecord
validates :name, :filter, presence: true
validates :boost_amount, numericality: {
greater_than_or_equal_to: -1.0, less_than_or_equal_to: 1.0
}
end
59 changes: 59 additions & 0 deletions app/views/boosts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<%= form_for(boost) do |f| %>
<% if boost.errors.any? %>
<%= render "govuk_publishing_components/components/error_summary", {
id: "error-summary",
title: "There is a problem",
description: "The boost could not be updated because some fields are missing or incorrect.",
items: error_summary_items(boost)
} %>
<% end %>

<%= render "govuk_publishing_components/components/input", {
label: {
text: Boost.human_attribute_name(:name)
},
id: "boost_name",
name: "boost[name]",
value: boost.name,
error_items: error_items(boost, :name)
} %>

<%= render "govuk_publishing_components/components/input", {
label: {
text: Boost.human_attribute_name(:boost_amount)
},
hint: "A value between -1.0 and 1.0 used to influence the order of search results.",
id: "boost_boost_amount",
name: "boost[boost_amount]",
value: boost.boost_amount,
error_items: error_items(boost, :boost_amount)
} %>

<%= render "govuk_publishing_components/components/textarea", {
label: {
text: Boost.human_attribute_name(:filter)
},
hint: "Determines which documents are affected by this boost. See #{link_to 'Vertex AI Search filter syntax', 'https://cloud.google.com/retail/docs/filter-and-order#filter', class: 'govuk-link'} for details.".html_safe,
id: "boost_filter",
name: "boost[filter]",
value: boost.filter,
error_items: error_items(boost, :filter)
} %>

<%= hidden_field_tag "boost[active]", "0" %>
<%= render "govuk_publishing_components/components/checkboxes", {
name: "boost[active]",
items: [
{
label: Boost.human_attribute_name(:active),
hint: "Boosts that are not active will not affect search results.",
value: "1",
checked: boost.active?
}
]
} %>

<%= render "govuk_publishing_components/components/button", {
text: "Save"
} %>
<% end %>
19 changes: 19 additions & 0 deletions app/views/boosts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%= render "govuk_publishing_components/components/breadcrumbs", {
breadcrumbs: [
{
title: "Boosts",
url: boosts_path
},
{
title: @boost.name,
url: boost_path(@boost)
},
{
title: "Edit"
}
]
} %>

<%= render "common/page_title", title: @boost.name %>

<%= render "form", boost: @boost %>
42 changes: 42 additions & 0 deletions app/views/boosts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%= render "common/page_title", title: "Boosts" %>

<div class="actions">
<%= render "govuk_publishing_components/components/button", {
text: "New boost",
href: new_boost_path,
inline_layout: true
} %>
</div>

<div class="govuk-!-margin-top-6 app-table__container" data-module="filterable-table">
<% if @boosts.any? %>
<%= render "govuk_publishing_components/components/input", {
label: {
text: "Filter boosts"
},
name: "table-filter",
type: "search",
search_icon: true,
tabindex: 0
} %>

<div>
<%= GovukPublishingComponents::AppHelpers::TableHelper.helper(self) do |t| %>
<%= t.head do %>
<%= t.header Boost.human_attribute_name(:name) %>
<%= t.header "Status" %>
<% end %>
<%= t.body do %>
<% @boosts.each do |boost| %>
<%= t.row do %>
<%= t.cell link_to(boost.name, boost_path(boost), class:'govuk-link') %>
<%= t.cell boost_status_tag(boost) %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
<% else %>
<p class="govuk-body">No boosts have been set up yet.</p>
<% end %>
</div>
15 changes: 15 additions & 0 deletions app/views/boosts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= render "govuk_publishing_components/components/breadcrumbs", {
breadcrumbs: [
{
title: "Boosts",
url: boosts_path
},
{
title: "New boost"
}
]
} %>

<%= render "common/page_title", title: "New boost" %>

<%= render "form", boost: @boost %>
47 changes: 47 additions & 0 deletions app/views/boosts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<%= render "govuk_publishing_components/components/breadcrumbs", {
breadcrumbs: [
{
title: "Boosts",
url: boosts_path
},
{
title: @boost.name,
}
]
} %>

<%= render "common/page_title", title: @boost.name %>

<div class="actions">
<%= render "govuk_publishing_components/components/button", {
text: "Edit boost",
href: edit_boost_path(@boost),
inline_layout: true
} %>
<%= delete_button "Delete boost", boost_path(@boost), is_inline: true %>
</div>

<%= render "govuk_publishing_components/components/summary_list", {
items: [
{
field: "Status",
value: boost_status_tag(@boost)
},
{
field: Boost.human_attribute_name(:boost_amount),
value: @boost.boost_amount
},
{
field: Boost.human_attribute_name(:filter),
value: filter_expression_code(@boost)
},
{
field: Boost.human_attribute_name(:created_at),
value: @boost.created_at.to_formatted_s(:govuk_date)
},
{
field: Boost.human_attribute_name(:updated_at),
value: @boost.updated_at.to_formatted_s(:govuk_date)
}
]
} %>
10 changes: 9 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@
# available at https://guides.rubyonrails.org/i18n.html.

en:
hello: "Hello world"
activerecord:
attributes:
boost:
name: Name
active: Activate boost
boost_amount: Boost amount
filter: Filter expression
created_at: Created
updated_at: Updated
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
GovukHealthcheck::ActiveRecord,
)

resources :boosts
resources :recommended_links, path: "/recommended-links"

root "recommended_links#index"
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20240619100722_create_boosts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateBoosts < ActiveRecord::Migration[7.1]
def change
create_table :boosts do |t|
t.string :name, null: false
t.boolean :active, null: false
t.float :boost_amount, null: false
t.text :filter, null: false

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_04_11_142717) do
ActiveRecord::Schema[7.1].define(version: 2024_06_19_100722) do
create_table "boosts", charset: "utf8mb3", force: :cascade do |t|
t.string "name", null: false
t.boolean "active", null: false
t.float "boost_amount", null: false
t.text "filter", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "recommended_links", charset: "utf8mb3", force: :cascade do |t|
t.string "title"
t.string "link"
Expand Down
7 changes: 7 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
FactoryBot.define do
factory :boost do
name { "Boost" }
active { true }
boost_amount { 0.5 }
filter { 'document_type: ANY("press_release")' }
end

factory :recommended_link do
title { "Tax online" }
link { "https://www.tax.service.gov.uk/" }
Expand Down
Loading

0 comments on commit 1082dcd

Please sign in to comment.