Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send link with the text_bindings in notifications when link_to is set #17913

Merged
merged 1 commit into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is okay, but I just wanted to call out what looks like a slight change in behavior.

Because you are moving which key to link to to the notification type, the :link key is getting moved up as a sibling to the relation keys.

Prior to this change it was possible to set a link for each of :initiator, :subject and, :cause, but now we can only set it to one as determined by the notification type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, the design here was for up to 3 links for a notification with the links in the notification text itself, however, UX designed the visual part differently so only one link is allowed.

: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