Add a factory for creating notifications
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
class Notifier
|
||||
attr_reader :event
|
||||
|
||||
class << self
|
||||
def for(event)
|
||||
"Notifier::#{event.action.classify}".safe_constantize&.new(event)
|
||||
end
|
||||
end
|
||||
|
||||
def generate
|
||||
recipients.each do |recipient|
|
||||
Notification.create! user: recipient, bubble: bubble, body: body
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def initialize(event)
|
||||
@event = event
|
||||
end
|
||||
|
||||
def body
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def recipients
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def bubble
|
||||
@event.summary.message.bubble
|
||||
end
|
||||
|
||||
def creator
|
||||
@event.creator
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
class Notifier::Assigned < Notifier
|
||||
private
|
||||
def body
|
||||
"#{creator.name} assigned you: #{bubble.title}"
|
||||
end
|
||||
|
||||
def recipients
|
||||
event.assignees.without(creator)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
class Notifier::Created < Notifier
|
||||
private
|
||||
def body
|
||||
"#{creator.name} created a new item: #{bubble.title}"
|
||||
end
|
||||
|
||||
def recipients
|
||||
bubble.bucket.users.without(creator)
|
||||
end
|
||||
end
|
||||
@@ -1,7 +1,4 @@
|
||||
require "test_helper"
|
||||
|
||||
class NotificationTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
require "test_helper"
|
||||
|
||||
class Notifier::AssignedTest < ActiveSupport::TestCase
|
||||
test "generate creates a notification for each recipient" do
|
||||
assert_difference -> { Notification.count }, 1 do
|
||||
assert_difference -> { users(:kevin).notifications.count }, 1 do
|
||||
Notifier.for(events(:logo_assignment_km)).generate
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "generate does not notify for self-assignments" do
|
||||
event = EventSummary.last.events.create! action: :assigned, creator: users(:kevin), particulars: { assignee_ids: [ users(:kevin).id ] }
|
||||
|
||||
assert_no_difference -> { Notification.count } do
|
||||
Notifier.for(event).generate
|
||||
end
|
||||
end
|
||||
|
||||
test "generate populates the notification details" do
|
||||
Notifier.for(events(:logo_assignment_km)).generate
|
||||
|
||||
assert_equal "David assigned you: The logo isn't big enough", Notification.last.body
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
require "test_helper"
|
||||
|
||||
class Notifier::CreatedTest < ActiveSupport::TestCase
|
||||
test "generate creates a notification for each recipient" do
|
||||
assert_difference -> { Notification.count }, 2 do
|
||||
assert_difference -> { users(:kevin).notifications.count }, 1 do
|
||||
assert_difference -> { users(:jz).notifications.count }, 1 do
|
||||
Notifier.for(events(:logo_created)).generate
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "generate populates the notification details" do
|
||||
Notifier.for(events(:logo_created)).generate
|
||||
|
||||
assert_equal "David created a new item: The logo isn't big enough", Notification.last.body
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
require "test_helper"
|
||||
|
||||
class NotifierTest < ActiveSupport::TestCase
|
||||
test "for returns the matching notifier class for the event" do
|
||||
assert_kind_of Notifier::Created, Notifier.for(events(:logo_created))
|
||||
end
|
||||
|
||||
test "for does not raise an error when the event is not notifiable" do
|
||||
assert_nothing_raised do
|
||||
assert_no_difference -> { Notification.count } do
|
||||
Notifier.for(events(:logo_boost_dhh))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user