diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 6691d472c..d0181ead6 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -1,16 +1,21 @@ class NotificationsController < ApplicationController before_action :authenticate_user!, except: %i[show] before_action :authenticate_scope!, except: %i[show dismiss] + before_action :find_notification, except: %i[new create] + + rescue_from ActiveRecord::RecordNotFound, with: :render_not_found def dismiss - @notification = Notification.find_by(id: params[:notification_id]) @notification.mark_as_read! for: current_user redirect_back(fallback_location: root_path) end def show - @notification = current_user&.support? ? Notification.find_by(id: params[:id]) : Notification.active.find_by(id: params[:id]) - if @notification&.show_additional_page && (@notification&.show_on_unauthenticated_pages || current_user) + if !@notification.show_on_unauthenticated_pages && !current_user + render_not_found and return + end + + if @notification.show_additional_page render "show" else redirect_back(fallback_location: root_path) @@ -31,21 +36,7 @@ class NotificationsController < ApplicationController 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] if @notification.errors.empty? && @notification.update(notification_model_params) @@ -62,6 +53,12 @@ class NotificationsController < ApplicationController end end + def delete + @notification.update!(end_date: Time.zone.now) + flash[:notice] = "The notification has been deleted" + redirect_to root_path + end + private def notification_params @@ -84,4 +81,13 @@ private model_params end + + def find_notification + id = params[:id] || params[:notification_id] + @notification = current_user&.support? ? Notification.find_by(id:) : Notification.active.find_by(id:) + + raise ActiveRecord::RecordNotFound unless @notification + + @notification + end end diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 67206001c..318918134 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -27,11 +27,18 @@ module NotificationsHelper render_normal_markdown(title) end - def render_for_page(title, page_content) - content = page_content - unless /\A\s*#[^#]/.match?(page_content) - content = "# #{title}\n#{page_content}" - end + def render_for_page(notification) + content_includes_own_title = /\A\s*#[^#]/.match?(notification.page_content) + return render_normal_markdown(notification.page_content) if content_includes_own_title + + content = "# #{notification.title}\n#{notification.page_content}" + render_normal_markdown(content) + end + + def render_for_home(notification) + return render_normal_markdown(notification.title) unless notification.show_additional_page + + content = "#{notification.title} \n[#{notification.link_text}](#{notification_path(notification)})" render_normal_markdown(content) end diff --git a/app/presenters/homepage_presenter.rb b/app/presenters/homepage_presenter.rb index 1cb784e3a..3b3314667 100644 --- a/app/presenters/homepage_presenter.rb +++ b/app/presenters/homepage_presenter.rb @@ -2,7 +2,7 @@ class HomepagePresenter include Rails.application.routes.url_helpers include CollectionTimeHelper - attr_reader :current_year_in_progress_lettings_data, :current_year_completed_lettings_data, :current_year_in_progress_sales_data, :current_year_completed_sales_data, :last_year_in_progress_lettings_data, :last_year_completed_lettings_data, :last_year_in_progress_sales_data, :last_year_completed_sales_data, :incomplete_schemes_data + attr_reader :current_year_in_progress_lettings_data, :current_year_completed_lettings_data, :current_year_in_progress_sales_data, :current_year_completed_sales_data, :last_year_in_progress_lettings_data, :last_year_completed_lettings_data, :last_year_in_progress_sales_data, :last_year_completed_sales_data, :incomplete_schemes_data, :active_notifications def initialize(user) @user = user @@ -25,6 +25,7 @@ class HomepagePresenter path: schemes_path(status: [:incomplete], owning_organisation_select: "all"), } end + @active_notifications = Notification.active if @user.support? end def title_text_for_user diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index bb4792e68..e55dc6c99 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -7,9 +7,7 @@ <% if @current_user.support? %> -
<%== I18n.t("active_notifications", count: active_notifications.count) %>
+ <% active_notifications.each do |notification| %> +<%== render_for_summary("**Notification:** #{@notification.title}") %>
+ +Users will no longer see this notification.
+ + <%= govuk_warning_text(text: "You will not be able to undo this action.") %> + + +