Skip to content

Commit

Permalink
Fix AnnouncementsController crashes when not logged in
Browse files Browse the repository at this point in the history
`@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.
  • Loading branch information
tk0miya committed Nov 12, 2023
1 parent 7628769 commit 5052ec1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/controllers/announcements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions sig/handwritten/app/controllers/announcements_controller.rbs
Original file line number Diff line number Diff line change
@@ -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
10 changes: 0 additions & 10 deletions sig/prototype/app/controllers/announcements_controller.rbs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 5052ec1

Please sign in to comment.