15 changed files with 308 additions and 28 deletions
@ -1,19 +1,77 @@ |
|||||||
class NotificationsController < ApplicationController |
class NotificationsController < ApplicationController |
||||||
|
before_action :authenticate_user!, except: %i[show] |
||||||
|
before_action :authenticate_scope!, except: %i[show dismiss] |
||||||
|
|
||||||
def dismiss |
def dismiss |
||||||
if current_user.blank? |
@notification = Notification.find_by(id: params[:notification_id]) |
||||||
redirect_to root_path |
@notification.mark_as_read! for: current_user |
||||||
else |
redirect_back(fallback_location: root_path) |
||||||
current_user.newest_active_unread_notification.mark_as_read! for: current_user if current_user.newest_active_unread_notification.present? |
|
||||||
redirect_back(fallback_location: root_path) |
|
||||||
end |
|
||||||
end |
end |
||||||
|
|
||||||
def show |
def show |
||||||
@notification = current_user&.newest_active_unread_notification || Notification.newest_active_unauthenticated_notification |
@notification = Notification.find_by(id: params[:id]) |
||||||
if @notification&.page_content |
if @notification&.page_content && (@notification&.show_on_unauthenticated_pages || current_user) |
||||||
render "show" |
render "show" |
||||||
else |
else |
||||||
redirect_back(fallback_location: root_path) |
redirect_back(fallback_location: root_path) |
||||||
end |
end |
||||||
end |
end |
||||||
|
|
||||||
|
def new |
||||||
|
@notification = Notification.new |
||||||
|
end |
||||||
|
|
||||||
|
def create |
||||||
|
@notification = Notification.new(notification_params) |
||||||
|
|
||||||
|
if @notification.errors.empty? && @notification.save |
||||||
|
redirect_to notification_check_answers_path(@notification) |
||||||
|
else |
||||||
|
render :new, status: :unprocessable_entity |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def check_answers |
||||||
|
@notification = Notification.find_by(id: params[:notification_id]) |
||||||
|
render_not_found and return unless @notification |
||||||
|
|
||||||
|
render "notifications/check_answers" |
||||||
|
end |
||||||
|
|
||||||
|
def edit |
||||||
|
@notification = Notification.find_by(id: params[:id]) |
||||||
|
end |
||||||
|
|
||||||
|
def update |
||||||
|
@notification = Notification.find_by(id: params[:id]) |
||||||
|
render_not_found and return unless @notification |
||||||
|
|
||||||
|
start_now = params[:notification][:start_now] |
||||||
|
|
||||||
|
update_params = notification_params.except(:start_now) |
||||||
|
update_params[:start_date] = Time.zone.now if start_now |
||||||
|
|
||||||
|
if @notification.errors.empty? && @notification.update(update_params) |
||||||
|
if start_now |
||||||
|
flash[:notice] = "The notification has been created" |
||||||
|
redirect_to root_path |
||||||
|
else |
||||||
|
redirect_to notification_check_answers_path(@notification) |
||||||
|
end |
||||||
|
elsif start_now |
||||||
|
render :check_answers, status: :unprocessable_entity |
||||||
|
else |
||||||
|
render :edit, status: :unprocessable_entity |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def notification_params |
||||||
|
params.require(:notification).permit(:title, :show_on_unauthenticated_pages, :start_now) |
||||||
|
end |
||||||
|
|
||||||
|
def authenticate_scope! |
||||||
|
render_not_found unless current_user.support? |
||||||
|
end |
||||||
end |
end |
||||||
|
|||||||
@ -0,0 +1,29 @@ |
|||||||
|
<div class="govuk-grid-row"> |
||||||
|
<div class="govuk-grid-column-two-thirds"> |
||||||
|
<%= f.govuk_error_summary %> |
||||||
|
|
||||||
|
<% content_for :page_title, "Create a new notification" %> |
||||||
|
|
||||||
|
<% content_for :before_content do %> |
||||||
|
<%= govuk_back_link(href: :back) %> |
||||||
|
<% end %> |
||||||
|
|
||||||
|
<h1 class="govuk-heading-l"> |
||||||
|
<%= content_for(:page_title) %> |
||||||
|
</h1> |
||||||
|
|
||||||
|
<span class="govuk-caption-m govuk-!-margin-bottom-6">This notification will be visible to all users until you delete it</span> |
||||||
|
|
||||||
|
<%= f.govuk_text_field :title, |
||||||
|
label: { text: "Title", size: "m" }, |
||||||
|
hint: { text: "Use markdown for links to existing pages" } %> |
||||||
|
|
||||||
|
<%= f.govuk_check_boxes_fieldset :show_on_unauthenticated_pages, multiple: false, legend: { text: "Display Options" } do %> |
||||||
|
<%= f.govuk_check_box :show_on_unauthenticated_pages, 1, 0, multiple: false, label: { text: "Show this notification on unauthenticated pages, for example the start page" } %> |
||||||
|
<% end %> |
||||||
|
|
||||||
|
<span class="govuk-caption-m govuk-!-margin-bottom-6">View <%= govuk_link_to "markdown syntax guide", "https://www.markdownguide.org/basic-syntax/" %></span> |
||||||
|
|
||||||
|
<%= f.govuk_submit "Continue" %> |
||||||
|
</div> |
||||||
|
</div> |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
<% content_for :title, "Create a notification" %> |
||||||
|
|
||||||
|
<div class="govuk-grid-row"> |
||||||
|
<div class="govuk-grid-column-three-quarters-from-desktop"> |
||||||
|
<% content_for :before_content do %> |
||||||
|
<%= govuk_back_link(href: :back) %> |
||||||
|
<% end %> |
||||||
|
|
||||||
|
<h1 class="govuk-heading-l"> |
||||||
|
<span class="govuk-caption-l">Create a notification</span> |
||||||
|
Check your answers |
||||||
|
</h1> |
||||||
|
|
||||||
|
<span class="govuk-caption-m govuk-!-margin-bottom-6">This notification will be visible to all users until you delete it</span> |
||||||
|
|
||||||
|
<%= form_for(@notification, method: :patch) do |f| %> |
||||||
|
<%= f.govuk_error_summary %> |
||||||
|
|
||||||
|
<div class="govuk-summary-card"> |
||||||
|
<div class="govuk-summary-card__content"> |
||||||
|
<%= govuk_summary_list do |summary_list| %> |
||||||
|
<% summary_list.with_row do |row| %> |
||||||
|
<% row.with_key { "Title" } %> |
||||||
|
<% row.with_value do %> |
||||||
|
<%== @notification.rendered_title %> |
||||||
|
<% end %> |
||||||
|
<% row.with_action(text: "Change", href: edit_notification_path(@notification)) %> |
||||||
|
<% end %> |
||||||
|
<% summary_list.with_row do |row| %> |
||||||
|
<% row.with_key { "Show on unauthenticated pages?" } %> |
||||||
|
<% row.with_value { @notification.show_on_unauthenticated_pages ? "Yes" : "No" } %> |
||||||
|
<% row.with_action(text: "Change", href: edit_notification_path(@notification)) %> |
||||||
|
<% end %> |
||||||
|
<% end %> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
|
||||||
|
<%= f.hidden_field :start_now, value: true %> |
||||||
|
<%= f.govuk_submit "Create notification" %> |
||||||
|
<% end %> |
||||||
|
</div> |
||||||
|
</div> |
||||||
@ -0,0 +1,3 @@ |
|||||||
|
<%= form_for(@notification, as: :notification, method: :patch) do |f| %> |
||||||
|
<% render partial: "form", locals: { notification: @notification, f: } %> |
||||||
|
<% end %> |
||||||
@ -0,0 +1,3 @@ |
|||||||
|
<%= form_for(@notification, as: :notification, method: :post) do |f| %> |
||||||
|
<% render partial: "form", locals: { notification: @notification, f: } %> |
||||||
|
<% end %> |
||||||
@ -0,0 +1,110 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe NotificationsController, type: :request do |
||||||
|
context "when user is signed in as a support user" do |
||||||
|
let(:support_user) { create(:user, :support) } |
||||||
|
|
||||||
|
before do |
||||||
|
allow(support_user).to receive(:need_two_factor_authentication?).and_return(false) |
||||||
|
sign_in support_user |
||||||
|
end |
||||||
|
|
||||||
|
describe "#create" do |
||||||
|
let(:request) { post notifications_path, params: params } |
||||||
|
|
||||||
|
context "with valid parameters" do |
||||||
|
let(:params) { { "notification": { title: "Test Create", show_on_unauthenticated_pages: true } } } |
||||||
|
|
||||||
|
it "creates a new notification with no start date set" do |
||||||
|
request |
||||||
|
notification = Notification.find_by(title: "Test Create") |
||||||
|
expect(notification.show_on_unauthenticated_pages).to be_truthy |
||||||
|
expect(notification.start_date).to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "redirects to check answers page" do |
||||||
|
request |
||||||
|
notification = Notification.find_by(title: "Test Create") |
||||||
|
expect(response).to redirect_to(notification_check_answers_path(notification)) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "with invalid parameters" do |
||||||
|
let(:params) { { "notification": { title: "", show_on_unauthenticated_pages: true } } } |
||||||
|
|
||||||
|
it "gives an error response" do |
||||||
|
request |
||||||
|
expect(response).to have_http_status(:unprocessable_entity) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe "#update" do |
||||||
|
let(:notification) { create(:notification, title: "Initial Title", start_date: nil, end_date: nil) } |
||||||
|
let(:request) { patch notification_path(notification), params: params } |
||||||
|
|
||||||
|
context "when start_now is set to true" do |
||||||
|
let(:params) { { "notification": { start_now: true } } } |
||||||
|
|
||||||
|
it "sets the start date on the notification" do |
||||||
|
request |
||||||
|
notification.reload |
||||||
|
expect(notification.start_date).not_to be_nil |
||||||
|
expect(notification.start_date).to be < Time.zone.now |
||||||
|
end |
||||||
|
|
||||||
|
it "redirects to the home page" do |
||||||
|
request |
||||||
|
expect(response).to redirect_to(root_path) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when start_now is not set" do |
||||||
|
let(:params) { { "notification": { title: "Updated Title", show_on_unauthenticated_pages: true } } } |
||||||
|
|
||||||
|
it "sets the relevant values on the notification" do |
||||||
|
request |
||||||
|
notification.reload |
||||||
|
expect(notification.title).to eql("Updated Title") |
||||||
|
expect(notification.start_date).to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "redirects to check answers" do |
||||||
|
request |
||||||
|
expect(response).to redirect_to(notification_check_answers_path(notification)) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when user is signed in as a non-support user" do |
||||||
|
let(:user) { create(:user, :data_coordinator) } |
||||||
|
|
||||||
|
before do |
||||||
|
sign_in user |
||||||
|
end |
||||||
|
|
||||||
|
describe "#create" do |
||||||
|
let(:request) { post notifications_path, params: { "notification": { title: "Test Create", show_on_unauthenticated_pages: true } } } |
||||||
|
|
||||||
|
it "returns not found" do |
||||||
|
request |
||||||
|
expect(response).to have_http_status(:not_found) |
||||||
|
end |
||||||
|
|
||||||
|
it "does not create a notification" do |
||||||
|
expect { request }.not_to change(Notification, :count) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe "#update" do |
||||||
|
let(:notification) { create(:notification) } |
||||||
|
let(:request) { patch notification_path(notification), params: { "notification": { title: "Test Create", show_on_unauthenticated_pages: true } } } |
||||||
|
|
||||||
|
it "returns not found" do |
||||||
|
request |
||||||
|
expect(response).to have_http_status(:not_found) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
Loading…
Reference in new issue