From c28d11c04bf203a1cf6eea0a8acac94701a31396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Hal=C3=A1sz?= Date: Tue, 28 Aug 2018 12:33:37 +0200 Subject: [PATCH] Send link with the text_bindings in notifications when link_to is set --- app/models/notification.rb | 14 +++++++++----- app/models/notification_type.rb | 1 + spec/models/notification_spec.rb | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index 220a2ac43e6..fea567caf57 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -83,13 +83,17 @@ def backup_subject_name def text_bindings [:initiator, :subject, :cause].each_with_object(text_bindings_dynamic) do |key, result| value = public_send(key) + next unless value + + # Set the link based on the notification_type.link_to + result[:link] = { + :id => value.id, + :model => value.class.name + } if notification_type.link_to.try(:to_sym) == key + result[key] = { - :link => { - :id => value.id, - :model => value.class.name, - }, :text => value.try(:name) || value.try(:description) - } if value + } end end diff --git a/app/models/notification_type.rb b/app/models/notification_type.rb index a5dd9bf33ee..26806b61abd 100644 --- a/app/models/notification_type.rb +++ b/app/models/notification_type.rb @@ -9,6 +9,7 @@ class NotificationType < ApplicationRecord has_many :notifications validates :message, :presence => true validates :level, :inclusion => { :in => %w(success error warning info) } + validates :link_to, :inclusion => { :in => %w(subject initiator cause) }, :allow_blank => true validates :audience, :inclusion => { :in => [AUDIENCE_USER, AUDIENCE_GROUP, AUDIENCE_TENANT, AUDIENCE_GLOBAL, AUDIENCE_SUPERADMIN] } diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index c89542586e4..280508861bb 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -106,13 +106,23 @@ :level => notification.notification_type.level, :created_at => notification.created_at, :text => notification.notification_type.message, - :bindings => a_hash_including(:initiator => a_hash_including(:text => user.name, - :link => a_hash_including(:id, :model)), + :bindings => a_hash_including(:initiator => a_hash_including(:text => user.name), :extra => a_hash_including(:text => 'information') ) ) end + context 'link_to is set' do + let(:notification) do + FactoryGirl.create(:notification, :initiator => user, + :notification_type => FactoryGirl.create(:notification_type, :link_to => 'initiator')) + end + + it 'contains the link to the initiator' do + expect(notification.to_h).to include(:bindings => a_hash_including(:link => a_hash_including(:model => 'User', :id => user.id))) + end + end + context "subject text" do let(:vm) { FactoryGirl.create(:vm, :tenant => tenant) } subject { Notification.create(:type => :vm_snapshot_failure, :subject => vm, :options => {:error => "oops", :snapshot_op => "create"}) }