generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new files for adding contract recipients
- Loading branch information
1 parent
71eccbd
commit b2af171
Showing
14 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
app/controllers/support/cases/contract_recipients_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module Support | ||
class Cases::ContractRecipientsController < Cases::ApplicationController | ||
before_action :set_current_case | ||
before_action :set_contract_recipient, only: %i[edit update destroy] | ||
|
||
before_action only: %i[new create edit update destroy] do | ||
@back_url = support_case_contract_recipients_path(@current_case) | ||
end | ||
|
||
before_action only: [:index] do | ||
@back_url = support_case_path(@current_case, anchor: "tasklist") | ||
end | ||
def index | ||
@contract_recipients = @current_case.contract_recipients.all | ||
end | ||
|
||
def new | ||
@contract_recipient = @current_case.contract_recipients.new | ||
end | ||
|
||
def create | ||
@contract_recipient = @current_case.contract_recipients.new(contract_recipient_params) | ||
|
||
if @contract_recipient.save | ||
redirect_to support_case_contract_recipients_path(case_id: @current_case), | ||
notice: I18n.t("support.contract_recipients.flash.success", name: @contract_recipient.name) | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def edit; end | ||
|
||
def update | ||
if @contract_recipient.update(contract_recipient_params) | ||
redirect_to support_case_contract_recipients_path(case_id: @current_case), | ||
notice: I18n.t("support.contract_recipients.flash.updated", name: @contract_recipient.name) | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
return unless params[:confirm] | ||
|
||
@contract_recipient.destroy! | ||
redirect_to support_case_contract_recipients_path(case_id: @current_case), | ||
notice: I18n.t("support.contract_recipients.flash.destroyed", name: @contract_recipient.name) | ||
end | ||
|
||
private | ||
|
||
def set_contract_recipient | ||
@contract_recipient = current_case.contract_recipients.find(params[:id]) | ||
end | ||
|
||
def set_current_case | ||
@current_case = Support::Case.find(params[:case_id]) | ||
end | ||
|
||
def contract_recipient_params | ||
params.require(:support_contract_recipient).permit(:first_name, :last_name, :email) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Support | ||
class ContractRecipient < ApplicationRecord | ||
belongs_to :support_case, class_name: "Support::Case" | ||
|
||
validates :email, | ||
presence: true, | ||
format: { with: URI::MailTo::EMAIL_REGEXP }, | ||
uniqueness: { case_sensitive: false, scope: :support_case_id } | ||
validates :first_name, :last_name, presence: true | ||
belongs_to :user, foreign_key: "dsi_uid", primary_key: "dfe_sign_in_uid", optional: true | ||
|
||
def name | ||
[first_name, last_name].compact_blank.join(" ") | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
app/views/support/cases/contract_recipients/_form.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<%= form_with( | ||
model: @contract_recipient, | ||
url: @contract_recipient.new_record? ? support_case_contract_recipients_path(case_id: @current_case) | ||
: support_case_contract_recipient_path(@contract_recipient, case_id: @current_case) | ||
) do |form| %> | ||
|
||
<%= form.govuk_error_summary %> | ||
|
||
<%= form.govuk_text_field :first_name, autofocus: true, width: "one-half" %> | ||
<%= form.govuk_text_field :last_name, width: "one-half" %> | ||
<%= form.govuk_text_field :email, width: "one-half" %> | ||
|
||
<div class="govuk-button-group flex-align-center"> | ||
<%= form.submit I18n.t("support.contract_recipients.submit"), class: "govuk-button" %> | ||
|
||
<% unless @contract_recipient.new_record? %> | ||
<%= link_to I18n.t("generic.button.remove"), | ||
support_case_contract_recipient_path(@contract_recipient, case_id: @current_case), | ||
method: :delete, | ||
class: "govuk-button govuk-button--secondary" %> | ||
<%end%> | ||
|
||
<%= link_to I18n.t("generic.button.cancel"), | ||
support_case_contract_recipients_path(case_id: @current_case), | ||
class: "govuk-link govuk-link--no-visited-state" %> | ||
</div> | ||
<% end %> |
15 changes: 15 additions & 0 deletions
15
app/views/support/cases/contract_recipients/destroy.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %> | ||
<h1 class="govuk-heading-l"> | ||
<%= I18n.t("support.contract_recipients.delete_confirmation", name: @contract_recipient.name) %> | ||
</h1> | ||
|
||
<div class="govuk-button-group flex-align-center"> | ||
<%= link_to I18n.t("generic.button.remove"), | ||
support_case_contract_recipient_path(@contract_recipient, case_id: @current_case, confirm: true), | ||
method: :delete, | ||
class: "govuk-button govuk-button--warning" %> | ||
|
||
<%= link_to I18n.t("generic.button.cancel"), | ||
support_case_contract_recipients_path(case_id: @current_case), | ||
class: "govuk-link govuk-link--no-visited-state" %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %> | ||
<h1 class="govuk-heading-l"><%= I18n.t("support.contract_recipients.header.edit") %></h1> | ||
|
||
<%= render "form" %> |
32 changes: 32 additions & 0 deletions
32
app/views/support/cases/contract_recipients/index.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %> | ||
<h1 class="govuk-heading-l"><%= I18n.t("support.contract_recipients.header.index") %></h1> | ||
|
||
<table class="govuk-table messages-table"> | ||
<thead class="govuk-table__head"> | ||
<tr class="govuk-table__row"> | ||
<th scope="col" class="govuk-table__header" | ||
><%= I18n.t(:name, scope: "helpers.label.support_contract_recipient") %></th> | ||
<th scope="col" class="govuk-table__header" | ||
><%= I18n.t(:email, scope: "helpers.label.support_contract_recipient") %></th> | ||
<th scope="col" class="govuk-table__header"></th> | ||
|
||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody class="govuk-table__body"> | ||
<% @contract_recipients.each do |contract_recipient| %> | ||
<div class="govuk-summary-list__row"> | ||
<tr> | ||
<td class="govuk-table__cell"><%= contract_recipient.name %></td> | ||
<td class="govuk-table__cell"><%= contract_recipient.email %></td> | ||
<td class="govuk-table__cell" | ||
><%= link_to I18n.t("generic.link.change"), | ||
edit_support_case_contract_recipient_path(contract_recipient, case_id: @current_case) %></td> | ||
</tr> | ||
</div> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<%= link_to I18n.t("generic.button.add"), | ||
new_support_case_contract_recipient_path(case_id: @current_case), class: 'govuk-button' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %> | ||
<h1 class="govuk-heading-l"><%= I18n.t("support.contract_recipients.header.new") %></h1> | ||
|
||
<%= render "form" %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class AddContractRecipients < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table "support_contract_recipients", id: :uuid do |t| | ||
t.references "support_case", type: :uuid | ||
t.string "first_name", null: false | ||
t.string "last_name", null: false | ||
t.string "email", null: false | ||
t.string "dsi_uid", default: "", null: false | ||
t.timestamps | ||
t.index %w[dsi_uid] | ||
t.index %w[email support_case_id], unique: true | ||
end | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
spec/features/support/agent_can_add_contract_recipient_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "rails_helper" | ||
|
||
describe "Agent can add contract recipient", :js do | ||
include_context "with an agent" | ||
|
||
let(:support_case) { create(:support_case) } | ||
|
||
specify "Adding contract recipient" do | ||
visit support_case_contract_recipients_path(case_id: support_case) | ||
|
||
click_link "Add" | ||
|
||
fill_in "First name", with: "Momo" | ||
fill_in "Last name", with: "Taro" | ||
fill_in "Email address", with: "momotaro@example.com" | ||
|
||
click_button "Save changes" | ||
|
||
expect(page).to have_text("Momo Taro successfully added") | ||
expect(page).to have_text("momotaro@example.com") | ||
|
||
click_link "Change" | ||
|
||
fill_in "First name", with: "Oni" | ||
fill_in "Last name", with: "Baba" | ||
fill_in "Email address", with: "onibaba@example.com" | ||
|
||
click_button "Save changes" | ||
|
||
expect(page).to have_text("Oni Baba successfully updated") | ||
expect(page).to have_text("onibaba@example.com") | ||
|
||
click_link "Change" | ||
click_link "Remove" | ||
|
||
expect(page).to have_text("Are you sure you want to remove Oni Baba") | ||
|
||
click_link "Remove" | ||
|
||
expect(page).to have_text("Oni Baba successfully removed") | ||
expect(page).not_to have_text("onibaba@example.com") | ||
end | ||
end |