diff --git a/app/controllers/campaigns/new_controller.rb b/app/controllers/campaigns/edit_controller.rb
similarity index 61%
rename from app/controllers/campaigns/new_controller.rb
rename to app/controllers/campaigns/edit_controller.rb
index 5e3392e56f..3715bd1612 100644
--- a/app/controllers/campaigns/new_controller.rb
+++ b/app/controllers/campaigns/edit_controller.rb
@@ -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
@@ -18,6 +16,12 @@ 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
@@ -25,9 +29,7 @@ def update
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
@@ -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
diff --git a/app/controllers/campaigns_controller.rb b/app/controllers/campaigns_controller.rb
index 489ded091d..6a8c69dd5e 100644
--- a/app/controllers/campaigns_controller.rb
+++ b/app/controllers/campaigns_controller.rb
@@ -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
diff --git a/app/models/campaign.rb b/app/models/campaign.rb
index dda5fbb6ec..ede63bbb68 100644
--- a/app/models/campaign.rb
+++ b/app/models/campaign.rb
@@ -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
diff --git a/app/views/campaigns/edit/confirm.html.erb b/app/views/campaigns/edit/confirm.html.erb
new file mode 100644
index 0000000000..8c66d284b0
--- /dev/null
+++ b/app/views/campaigns/edit/confirm.html.erb
@@ -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("
").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 %>
diff --git a/app/views/campaigns/new/dates.html.erb b/app/views/campaigns/edit/dates.html.erb
similarity index 68%
rename from app/views/campaigns/new/dates.html.erb
rename to app/views/campaigns/edit/dates.html.erb
index 4e06dad2af..e823d97d43 100644
--- a/app/views/campaigns/new/dates.html.erb
+++ b/app/views/campaigns/edit/dates.html.erb
@@ -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 %>
diff --git a/app/views/campaigns/new/details.html.erb b/app/views/campaigns/edit/details.html.erb
similarity index 80%
rename from app/views/campaigns/new/details.html.erb
rename to app/views/campaigns/edit/details.html.erb
index cbbe5651ee..0f01a796b8 100644
--- a/app/views/campaigns/new/details.html.erb
+++ b/app/views/campaigns/edit/details.html.erb
@@ -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| %>
diff --git a/app/views/campaigns/edit/vaccines.html.erb b/app/views/campaigns/edit/vaccines.html.erb
new file mode 100644
index 0000000000..74e20535a9
--- /dev/null
+++ b/app/views/campaigns/edit/vaccines.html.erb
@@ -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 %>
diff --git a/app/views/campaigns/index.html.erb b/app/views/campaigns/index.html.erb
index af47aa4a4b..4a7dd62786 100644
--- a/app/views/campaigns/index.html.erb
+++ b/app/views/campaigns/index.html.erb
@@ -28,9 +28,7 @@
<% end %>
<% row.with_cell do %>
Vaccines
- <%= campaign.vaccines
- .map { |vaccine| vaccine.brand }
- .join("
").html_safe %>
+ <%= campaign.vaccines.map(&:brand).join("
").html_safe %>
<% end %>
<% end %>
<% end %>
diff --git a/app/views/campaigns/new/confirm.html.erb b/app/views/campaigns/new/confirm.html.erb
deleted file mode 100644
index 0e992abce2..0000000000
--- a/app/views/campaigns/new/confirm.html.erb
+++ /dev/null
@@ -1,45 +0,0 @@
-<% content_for :before_main do %>
- <%= render AppBacklinkComponent.new(
- href: previous_wizard_path,
- name: "#{@previous_step} page of programme creation",
- ) %>
-<% end %>
-
-<%= form_with model: @campaign, url: wizard_path, method: :put do |f| %>
- <%= f.govuk_error_summary %>
-
- <%= h1 "Check and confirm" %>
-
- <%= render AppCardComponent.new do |card| %>
- <% card.with_heading { "New programme details" } %>
-
- <%= govuk_summary_list do |summary_list| %>
- <%= summary_list.with_row do |row| %>
- <%= row.with_key { "Name" } %>
- <%= row.with_value { @campaign.name } %>
- <% end %>
-
- <%= summary_list.with_row do |row| %>
- <%= row.with_key { "Type" } %>
- <%= row.with_value { @campaign.human_enum_name(:type) } %>
- <% end %>
-
- <%= summary_list.with_row do |row| %>
- <%= row.with_key { "Academic year" } %>
- <%= row.with_value { campaign_academic_year(@campaign) } %>
- <% end %>
-
- <%= summary_list.with_row do |row| %>
- <%= row.with_key { "Start date" } %>
- <%= row.with_value { @campaign.start_date.to_fs(:long) } %>
- <% end %>
-
- <%= summary_list.with_row do |row| %>
- <%= row.with_key { "End date" } %>
- <%= row.with_value { @campaign.end_date.to_fs(:long) } %>
- <% end %>
- <% end %>
- <% end %>
-
- <%= f.govuk_submit "Save changes" %>
-<% end %>
diff --git a/app/views/campaigns/show.html.erb b/app/views/campaigns/show.html.erb
index a7e645085a..f6275f3392 100644
--- a/app/views/campaigns/show.html.erb
+++ b/app/views/campaigns/show.html.erb
@@ -27,3 +27,9 @@
href: campaign_immunisation_imports_path(@campaign),
)
end %>
+
+