-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HasNotifications module and default to json for sqlite
- Loading branch information
Showing
9 changed files
with
99 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
module Noticed | ||
class Engine < ::Rails::Engine | ||
initializer "noticed.has_notifications" do | ||
ActiveSupport.on_load(:active_record) do | ||
include Noticed::HasNotifications | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Noticed | ||
module HasNotifications | ||
# Defines a method for the association and a before_destory callback to remove notifications | ||
# where this record is a param | ||
# | ||
# class User < ApplicationRecord | ||
# has_noticed_notifications | ||
# has_noticed_notifications param_name: :owner, destroy: false, model: "Notification" | ||
# end | ||
# | ||
# @user.notifications_as_user | ||
# @user.notifications_as_owner | ||
|
||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def has_noticed_notifications(param_name: model_name.singular, **options) | ||
model = options.fetch(:model_name, "Notification").constantize | ||
|
||
define_method "notifications_as_#{param_name}" do | ||
model.where(params: {param_name.to_sym => self}) | ||
end | ||
|
||
if options.fetch(:destroy, true) | ||
before_destroy do | ||
send("notifications_as_#{param_name}").destroy_all | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "test_helper" | ||
|
||
class HasNotificationsTest < ActiveSupport::TestCase | ||
class DatabaseDelivery < Noticed::Base | ||
deliver_by :database | ||
end | ||
|
||
test "has_noticed_notifications" do | ||
assert User.respond_to?(:has_noticed_notifications) | ||
end | ||
|
||
test "noticed notifications association" do | ||
assert user.respond_to?(:notifications_as_user) | ||
end | ||
|
||
test "noticed notifications with custom name" do | ||
assert user.respond_to?(:notifications_as_owner) | ||
end | ||
|
||
test "deletes notifications with matching param" do | ||
DatabaseDelivery.with(user: user).deliver(users(:two)) | ||
|
||
assert_difference "Notification.count", -1 do | ||
user.destroy | ||
end | ||
end | ||
|
||
test "doesn't delete notifications when disabled" do | ||
DatabaseDelivery.with(owner: user).deliver(users(:two)) | ||
|
||
assert_no_difference "Notification.count" do | ||
user.destroy | ||
end | ||
end | ||
end |