Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# frozen_string_literal: true

class Campaigns::NewController < ApplicationController
class Campaigns::EditController < ApplicationController
include Wicked::Wizard

before_action :set_campaign
before_action :set_steps
before_action :setup_wizard

skip_after_action :verify_policy_scoped

def show
render_wizard
end
Expand All @@ -18,16 +16,20 @@ def update

@campaign.assign_attributes(wizard_step: step, **params)

if current_step?(:details) && @campaign.type_changed?
@campaign.vaccines = Vaccine.active.where(type: @campaign.type)
end

jump_to(:confirm) if @campaign.active && !current_step?(:confirm)

render_wizard(@campaign)
end

private

def set_campaign
@campaign =
policy_scope(Campaign).where(
active: params[:id] == Wicked::FINISH_STEP
).find(params[:campaign_id])
policy_scope(Campaign).includes(:vaccines).find(params[:campaign_id])
end

def set_steps
Expand All @@ -46,7 +48,11 @@ def dates_params
params.require(:campaign).permit(:start_date, :end_date)
end

def vaccines_params
params.require(:campaign).permit(vaccine_ids: [])
end

def confirm_params
{ active: true, vaccines: Vaccine.active.where(type: @campaign.type) }
{ active: true }
end
end
6 changes: 1 addition & 5 deletions app/controllers/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ def index

def create
campaign = Campaign.create!(team: current_user.team)

redirect_to campaign_new_path(
campaign_id: campaign.id,
id: campaign.wizard_steps.first
)
redirect_to campaign_edit_path(campaign, Wicked::FIRST_STEP)
end

def show
Expand Down
80 changes: 42 additions & 38 deletions app/models/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,69 +48,73 @@ class Campaign < ApplicationRecord

normalizes :name, with: ->(name) { name&.strip }

validates :name,
uniqueness: {
scope: %i[type academic_year team_id],
allow_nil: true
}

validates :academic_year,
comparison: {
greater_than_or_equal_to: 2000,
less_than_or_equal_to: Time.zone.today.year + 5,
allow_nil: true
}

validates :start_date,
comparison: {
greater_than_or_equal_to: :first_possible_start_date,
if: :academic_year,
allow_nil: true
}

validates :end_date,
comparison: {
greater_than_or_equal_to: :start_date,
less_than_or_equal_to: :last_possible_end_date,
if: -> { academic_year && start_date },
allow_nil: true
}

validate :vaccines_match_type

on_wizard_step :details do
validates :name, presence: true
validates :name,
presence: true,
uniqueness: {
scope: %i[type academic_year team_id],
allow_nil: true
}

validates :type, presence: true
validates :academic_year, presence: true

validates :academic_year,
comparison: {
greater_than_or_equal_to: 2000,
less_than_or_equal_to: Time.zone.today.year + 5
}
end

on_wizard_step :dates do
validates :start_date, presence: true
validates :end_date, presence: true
validates :start_date,
comparison: {
greater_than_or_equal_to: :first_possible_start_date,
less_than: :end_date
}

validates :end_date,
comparison: {
greater_than: :start_date,
less_than_or_equal_to: :last_possible_end_date
}
end

on_wizard_step :vaccines do
validates :vaccines, presence: true
end

on_wizard_step :confirm do
validates :active, presence: true
end

validate :vaccines_match_type

def wizard_steps
%i[details dates confirm]
[:details, :dates, (:vaccines if active), :confirm].compact
end

def vaccine_ids
@vaccine_ids ||= vaccines.map(&:id)
end

def vaccine_ids=(ids)
self.vaccines = Vaccine.where(id: ids)
end

private

def first_possible_start_date
Date.new(academic_year, 1, 1)
Date.new(academic_year || 2000, 1, 1)
end

def last_possible_end_date
Date.new(academic_year + 1, 12, 31)
Date.new((academic_year || 2000) + 1, 12, 31)
end

def vaccines_match_type
vaccine_types = vaccines.map(&:type).uniq
unless vaccine_types.empty? || vaccine_types == [type]
errors.add(:vaccines, "must match programme type")
errors.add(:vaccines, :match_type)
end
end
end
58 changes: 58 additions & 0 deletions app/views/campaigns/edit/confirm.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<% content_for :before_main do %>
<%= render AppBacklinkComponent.new(
href: @campaign.active ? campaign_path(@campaign) : previous_wizard_path,
name: @campaign.active ? @campaign.name : "#{@previous_step} page of programme creation",
) %>
<% end %>

<%= form_with model: @campaign, url: wizard_path, method: :put do |f| %>
<%= f.govuk_error_summary %>

<%= h1 @campaign.active ? "Edit programme" : "Check and confirm" %>

<%= render AppCardComponent.new do |card| %>
<% card.with_heading { @campaign.active ? "Programme details" : "New programme details" } %>

<%= govuk_summary_list do |summary_list| %>
<%= summary_list.with_row do |row| %>
<%= row.with_key { "Name" } %>
<%= row.with_value { @campaign.name } %>
<%= row.with_action(text: "Change", href: campaign_edit_path(@campaign, :details), visually_hidden_text: "name") %>
<% end %>

<%= summary_list.with_row do |row| %>
<%= row.with_key { "Programme type" } %>
<%= row.with_value { @campaign.human_enum_name(:type) } %>
<%= row.with_action(text: "Change", href: campaign_edit_path(@campaign, :details), visually_hidden_text: "programme type") %>
<% end %>

<%= summary_list.with_row do |row| %>
<%= row.with_key { "Academic year" } %>
<%= row.with_value { campaign_academic_year(@campaign) } %>
<%= row.with_action(text: "Change", href: campaign_edit_path(@campaign, :details), visually_hidden_text: "academic year") %>
<% end %>

<%= summary_list.with_row do |row| %>
<%= row.with_key { "Start date" } %>
<%= row.with_value { @campaign.start_date.to_fs(:long) } %>
<%= row.with_action(text: "Change", href: campaign_edit_path(@campaign, :dates), visually_hidden_text: "start date") %>
<% end %>

<%= summary_list.with_row do |row| %>
<%= row.with_key { "End date" } %>
<%= row.with_value { @campaign.end_date.to_fs(:long) } %>
<%= row.with_action(text: "Change", href: campaign_edit_path(@campaign, :dates), visually_hidden_text: "end date") %>
<% end %>

<% if @campaign.active %>
<%= summary_list.with_row do |row| %>
<%= row.with_key { "Vaccines" } %>
<%= row.with_value { @campaign.vaccines.map(&:brand).join("<br>").html_safe } %>
<%= row.with_action(text: "Change", href: campaign_edit_path(@campaign, :vaccines), visually_hidden_text: "vaccines") %>
<% end %>
<% end %>
<% end %>
<% end %>

<%= f.govuk_submit "Save changes" %>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% content_for :before_main do %>
<%= render AppBacklinkComponent.new(
href: previous_wizard_path,
name: "#{@previous_step} page of programme creation",
href: @campaign.active ? campaign_edit_path(@campaign, :confirm) : previous_wizard_path,
name: @campaign.active ? "edit programme" : "#{@previous_step} page of programme creation",
) %>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<% content_for :before_main do %>
<%= render AppBacklinkComponent.new(href: campaigns_path, name: "programme") %>
<%= render AppBacklinkComponent.new(
href: @campaign.active ? campaign_edit_path(@campaign, :confirm) : campaigns_path,
name: @campaign.active ? "edit programme" : "vaccination programmes",
) %>
<% end %>

<%= form_with model: @campaign, url: wizard_path, method: :put do |f| %>
Expand Down
18 changes: 18 additions & 0 deletions app/views/campaigns/edit/vaccines.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% content_for :before_main do %>
<%= render AppBacklinkComponent.new(
href: @campaign.active ? campaign_edit_path(@campaign, :confirm) : previous_wizard_path,
name: @campaign.active ? "edit programme" : "#{@previous_step} page of programme creation",
) %>
<% end %>

<% title = "Which vaccine(s) does this programme administer?" %>

<% content_for :page_title, title %>

<%= form_with model: @campaign, url: wizard_path, method: :put do |f| %>
<%= f.govuk_error_summary %>

<%= f.govuk_collection_check_boxes :vaccine_ids, Vaccine.where(type: @campaign.type), :id, :brand, legend: { text: title, tag: "h1", size: "l" } %>

<%= f.govuk_submit %>
<% end %>
4 changes: 1 addition & 3 deletions app/views/campaigns/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
<% end %>
<% row.with_cell do %>
<span class="nhsuk-table-responsive__heading">Vaccines</span>
<%= campaign.vaccines
.map { |vaccine| vaccine.brand }
.join("<br>").html_safe %>
<%= campaign.vaccines.map(&:brand).join("<br>").html_safe %>
<% end %>
<% end %>
<% end %>
Expand Down
45 changes: 0 additions & 45 deletions app/views/campaigns/new/confirm.html.erb

This file was deleted.

6 changes: 6 additions & 0 deletions app/views/campaigns/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@
href: campaign_immunisation_imports_path(@campaign),
)
end %>

<ul class="app-action-list">
<li class="app-action-list__item">
<%= govuk_link_to "Edit programme", campaign_edit_path(@campaign, :confirm) %>
</li>
</ul>
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ en:
type:
blank: Choose a programme type
inclusion: Choose a programme type
vaccines:
blank: Choose the vaccines this programme administers
match_type: Vaccines must be suitable for the programme type
consent:
attributes:
new_or_existing_parent:
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
resources :campaigns, only: %i[index create show] do
get "sessions", on: :member

resource :new,
controller: "campaigns/new",
resource :edit,
controller: "campaigns/edit",
only: %i[show update],
path: "new/:id"
path: "edit/:id"

resources :immunisation_imports,
path: "immunisation-imports",
Expand Down
Loading