diff --git a/app/models/notifier.rb b/app/models/notifier.rb new file mode 100644 index 000000000..7f2267aa4 --- /dev/null +++ b/app/models/notifier.rb @@ -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 diff --git a/app/models/notifier/assigned.rb b/app/models/notifier/assigned.rb new file mode 100644 index 000000000..c18d35a56 --- /dev/null +++ b/app/models/notifier/assigned.rb @@ -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 diff --git a/app/models/notifier/created.rb b/app/models/notifier/created.rb new file mode 100644 index 000000000..1bdc3fe2f --- /dev/null +++ b/app/models/notifier/created.rb @@ -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 diff --git a/test/models/notification_test.rb b/test/models/notification_test.rb index a76e08d66..449f3c08d 100644 --- a/test/models/notification_test.rb +++ b/test/models/notification_test.rb @@ -1,7 +1,4 @@ require "test_helper" class NotificationTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end end diff --git a/test/models/notifier/assigned_test.rb b/test/models/notifier/assigned_test.rb new file mode 100644 index 000000000..eafcd682a --- /dev/null +++ b/test/models/notifier/assigned_test.rb @@ -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 diff --git a/test/models/notifier/created_test.rb b/test/models/notifier/created_test.rb new file mode 100644 index 000000000..3e36cfcd6 --- /dev/null +++ b/test/models/notifier/created_test.rb @@ -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 diff --git a/test/models/notifier_test.rb b/test/models/notifier_test.rb new file mode 100644 index 000000000..62baf0c2d --- /dev/null +++ b/test/models/notifier_test.rb @@ -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