From ce7be5f2d6943f19083c96a3273f01c21cd102a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 21 Feb 2019 20:02:38 +0100 Subject: [PATCH] Change the way to retrieve notifiable body Using 'try' method to get notifiable is not working with translations anymore. It was returning 'nil' always even when body translation is populated. --- app/models/concerns/notifiable.rb | 4 ++++ app/models/notification.rb | 5 +++-- app/views/notifications/_notification.html.erb | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/models/concerns/notifiable.rb b/app/models/concerns/notifiable.rb index b10c57f70..aa7d4d3bc 100644 --- a/app/models/concerns/notifiable.rb +++ b/app/models/concerns/notifiable.rb @@ -12,6 +12,10 @@ module Notifiable end end + def notifiable_body + body if attribute_names.include?("body") + end + def notifiable_available? case self.class.name when "ProposalNotification" diff --git a/app/models/notification.rb b/app/models/notification.rb index 738e5ab5f..e5a8e2b95 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -11,8 +11,9 @@ class Notification < ApplicationRecord scope :recent, -> { order(id: :desc) } scope :for_render, -> { includes(:notifiable) } - delegate :notifiable_title, :notifiable_available?, :check_availability, - :linkable_resource, to: :notifiable, allow_nil: true + delegate :notifiable_title, :notifiable_body, :notifiable_available?, + :check_availability, :linkable_resource, + to: :notifiable, allow_nil: true def mark_as_read update(read_at: Time.current) diff --git a/app/views/notifications/_notification.html.erb b/app/views/notifications/_notification.html.erb index cdbad536f..ca14385da 100644 --- a/app/views/notifications/_notification.html.erb +++ b/app/views/notifications/_notification.html.erb @@ -3,7 +3,7 @@ <% locals = { notification: notification, timestamp: notification.timestamp, title: notification.notifiable_title, - body: notification.notifiable.try(:body) } %> + body: notification.notifiable_body } %> <% link_text = render partial: "/notifications/notification_body", locals: locals %> <%= link_to_if notification.link.present?, link_text, notification.link %> <% else %>