From 5052ec1c6fbb7095d7e37d664b97445945b2599d Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 12 Nov 2023 07:12:20 +0000 Subject: [PATCH] Fix AnnouncementsController crashes when not logged in `@unread_announcements` becomes `false` when not logged in. Then it will be crashed on rendering view. This error was found via `Ruby::IncompatibleAssignment` error from Steep :-) Note: The UnreachableBranch is not emmited by default. Locally, I enabled the "strict" settings of Steep to find this bug. --- app/controllers/announcements_controller.rb | 4 ++-- .../app/controllers/announcements_controller.rbs | 11 +++++++++++ .../app/controllers/announcements_controller.rbs | 10 ---------- 3 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 sig/handwritten/app/controllers/announcements_controller.rbs diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 60cbaf26..1202ff44 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -2,12 +2,12 @@ class AnnouncementsController < ApplicationController def index @event = Event.find_by!(slug: params[:event_slug]) @announcements = Announcement.where(event: @event).published.order(id: :desc, published_at: :desc) - @unread_announcements = logged_in? && current_user!.unread_announcements + @unread_announcements = logged_in? ? current_user!.unread_announcements : nil end def show @event = Event.find_by!(slug: params[:event_slug]) @announcement = Announcement.where(event: @event).published.with_rich_text_content_and_embeds.find_by!(id: params[:id]) - @unread_announcement = logged_in? && current_user!.unread_announcements.find_by(announcement: @announcement) + @unread_announcement = logged_in? ? current_user!.unread_announcements.find_by(announcement: @announcement) : nil end end diff --git a/sig/handwritten/app/controllers/announcements_controller.rbs b/sig/handwritten/app/controllers/announcements_controller.rbs new file mode 100644 index 00000000..4f7d070f --- /dev/null +++ b/sig/handwritten/app/controllers/announcements_controller.rbs @@ -0,0 +1,11 @@ +class AnnouncementsController < ApplicationController + @event: Event + + @announcements: Announcement::ActiveRecord_Relation + + @unread_announcements: UnreadAnnouncement::ActiveRecord_Associations_CollectionProxy? + + @announcement: Announcement + + @unread_announcement: UnreadAnnouncement? +end diff --git a/sig/prototype/app/controllers/announcements_controller.rbs b/sig/prototype/app/controllers/announcements_controller.rbs index 6d67071d..4aaf9c89 100644 --- a/sig/prototype/app/controllers/announcements_controller.rbs +++ b/sig/prototype/app/controllers/announcements_controller.rbs @@ -1,14 +1,4 @@ class AnnouncementsController < ApplicationController - @event: untyped - - @announcements: untyped - - @unread_announcements: untyped - - @announcement: untyped - - @unread_announcement: untyped - def index: () -> untyped def show: () -> untyped