Skip to content

Commit

Permalink
Merge pull request #17913 from skateman/notification-links
Browse files Browse the repository at this point in the history
Send link with the text_bindings in notifications when link_to is set
  • Loading branch information
carbonin authored Aug 29, 2018
2 parents 0f4f7b1 + c28d11c commit aaaae91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
14 changes: 9 additions & 5 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/models/notification_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
14 changes: 12 additions & 2 deletions spec/models/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"}) }
Expand Down

0 comments on commit aaaae91

Please sign in to comment.