Try if notifiable has notifiable methods

Why:

There are Notifications with associated `notifiables` that actually are
not anymore Notifiables (the class doesn't include the Notifiable
concern). So when Notification delegates certain "notifiable" methods
to them the is an error.

How:

Using `try` directly on the notifiable association to avoid the delegate
trap on those corner case scenarios.
This commit is contained in:
Bertocq
2018-03-05 16:44:28 +01:00
committed by decabeza
parent 7053711269
commit c0feabd71e
2 changed files with 9 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
<li id="<%= dom_id(notification) %>" class="notification <%= 'unread' if notification&.unread? %>"> <li id="<%= dom_id(notification) %>" class="notification <%= 'unread' if notification&.unread? %>">
<% if notification.try(:notifiable_available?) %> <% if notification.notifiable.try(:notifiable_available?) %>
<% locals = { notification: notification, <% locals = { notification: notification,
timestamp: notification.timestamp, timestamp: notification.timestamp,
title: notification.notifiable_title, title: notification.notifiable_title,

View File

@@ -128,6 +128,14 @@ feature "Notifications" do
expect(page).to_not have_css("#notifications") expect(page).to_not have_css("#notifications")
end end
scenario "Notification's notifiable model no longer includes Notifiable module" do
create(:notification, notifiable: create(:spending_proposal), user: user)
create(:notification, notifiable: create(:poll_question), user: user)
click_notifications_icon
expect(page).to have_content('This resource is not available anymore.', count: 2)
end
context "Admin Notifications" do context "Admin Notifications" do
let(:admin_notification) do let(:admin_notification) do
create(:admin_notification, title: 'Notification title', create(:admin_notification, title: 'Notification title',