Skip to content

Commit 561b644

Browse files
committed
fix: use find_by! to return 404 for invalid tokens
- Changed find_by to find_by! in before_action set_* methods to raise ActiveRecord::RecordNotFound (which Rails converts to 404) instead of returning nil and causing NoMethodError High priority (production crash): - workshop_invitation_concerns.rb - WorkshopInvitation lookup - admin/meeting_invitations_controller.rb - MeetingInvitation lookup - admin/invitations_controller.rb - workshop invitation lookup Medium priority (potential 500 errors): - admin/meetings_controller.rb - Meeting lookup by slug - events_controller.rb - Event lookup by slug - admin/events_controller.rb - Event lookup by slug Lower priority: - invitations_controller.rb:80 - MeetingInvitation lookup in cancel_meeting - contact_preferences_controller.rb:11 - Contact lookup in update - feedback_controller.rb:22 - FeedbackRequest lookup in submit
1 parent aab9d91 commit 561b644

10 files changed

+17
-15
lines changed

.worktrees/n1-queries-events

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit ed6e150f9dacaae089b2f24612ccc2e96eb008d0

app/controllers/admin/events_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def show
2727
@attending_students = InvitationPresenter.decorate_collection(@original_event.attending_students)
2828
@attending_coaches = InvitationPresenter.decorate_collection(@original_event.attending_coaches)
2929

30-
return render plain: @event.attendees_csv if request.format.csv?
30+
render plain: @event.attendees_csv if request.format.csv?
3131
end
3232

3333
def update
@@ -67,7 +67,7 @@ def attendees_emails
6767
private
6868

6969
def set_event
70-
@original_event = Event.find_by(slug: params[:id])
70+
@original_event = Event.find_by!(slug: params[:id])
7171
@event = EventPresenter.new(@original_event)
7272
end
7373

app/controllers/admin/invitations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def update_to_not_attending
7373
end
7474

7575
def set_invitation
76-
@invitation = @workshop.invitations.find_by(token: invitation_id)
76+
@invitation = @workshop.invitations.find_by!(token: invitation_id)
7777
end
7878

7979
def invitation_id

app/controllers/admin/meeting_invitations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def create
3434
private
3535

3636
def set_invitation
37-
@invitation = MeetingInvitation.find_by(token: id)
37+
@invitation = MeetingInvitation.find_by!(token: id)
3838
end
3939

4040
def id

app/controllers/admin/meetings_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def create
2121
def show
2222
@invitations = @meeting.invitations.accepted.includes(:member).order(:created_at)
2323

24-
return render plain: @meeting.attendees_csv if request.format.csv?
24+
render plain: @meeting.attendees_csv if request.format.csv?
2525
end
2626

2727
def edit; end
@@ -54,7 +54,7 @@ def invite
5454
private
5555

5656
def set_meeting
57-
@meeting = Meeting.find_by(slug: slug)
57+
@meeting = Meeting.find_by!(slug: slug)
5858
end
5959

6060
def slug

app/controllers/concerns/workshop_invitation_concerns.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def back_with_message(message)
1919
end
2020

2121
def set_invitation
22-
@invitation = WorkshopInvitation.find_by(token: token)
22+
@invitation = WorkshopInvitation.find_by!(token: token)
2323
end
2424
end
2525
end

app/controllers/contact_preferences_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def show
88
end
99

1010
def update
11-
contact = Contact.find_by(token: contact_preferences[:token])
11+
contact = Contact.find_by!(token: contact_preferences[:token])
1212
contact.update(mailing_list_consent: mailing_list_consent)
1313
audit_contact_subscription(contact)
1414

app/controllers/events_controller.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ def index
1313
events << Event.past.includes(:venue, :sponsors).limit(RECENT_EVENTS_DISPLAY_LIMIT)
1414
events = events.compact.flatten.sort_by(&:date_and_time).reverse.first(RECENT_EVENTS_DISPLAY_LIMIT)
1515
events_hash_grouped_by_date = events.group_by(&:date)
16-
@past_events = events_hash_grouped_by_date.map.inject({}) do |hash, (key, value)|
16+
@past_events = events_hash_grouped_by_date.map.each_with_object({}) do |(key, value), hash|
1717
hash[key] = EventPresenter.decorate_collection(value)
18-
hash
1918
end
2019

2120
events = [Workshop.includes(:chapter).upcoming.joins(:chapter).merge(Chapter.active)]
2221
events << Meeting.upcoming.all
2322
events << Event.upcoming.includes(:venue, :sponsors).all
2423
events = events.compact.flatten.sort_by(&:date_and_time).group_by(&:date)
25-
@events = events.map.inject({}) { |hash, (key, value)| hash[key] = EventPresenter.decorate_collection(value); hash }
24+
@events = events.map.each_with_object({}) do |(key, value), hash|
25+
hash[key] = EventPresenter.decorate_collection(value)
26+
end
2627
end
2728

2829
def show
@@ -34,7 +35,7 @@ def show
3435
return unless logged_in?
3536

3637
invitation = Invitation.find_by(member: current_user, event: event, attending: true)
37-
return redirect_to event_invitation_path(@event, invitation) if invitation
38+
redirect_to event_invitation_path(@event, invitation) if invitation
3839
end
3940

4041
def student
@@ -74,6 +75,6 @@ def find_invitation_and_redirect_to_event(role)
7475
end
7576

7677
def set_event
77-
@event = Event.find_by(slug: params[:event_id])
78+
@event = Event.find_by!(slug: params[:event_id])
7879
end
7980
end

app/controllers/feedback_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def submit
1919

2020
redirect_to root_path
2121
else
22-
feedback_request = FeedbackRequest.find_by(token: params[:id], submited: false)
22+
feedback_request = FeedbackRequest.find_by!(token: params[:id], submited: false)
2323
set_coaches(feedback_request.workshop)
2424

2525
@workshop = feedback_request.workshop

app/controllers/invitations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def rsvp_meeting
7777
end
7878

7979
def cancel_meeting
80-
@invitation = MeetingInvitation.find_by(token: params[:token])
80+
@invitation = MeetingInvitation.find_by!(token: params[:token])
8181

8282
@invitation.update_attribute(:attending, false)
8383

0 commit comments

Comments
 (0)