-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
module Noticed | ||
class Engine < ::Rails::Engine | ||
isolate_namespace Noticed | ||
|
||
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,49 @@ | ||
module Noticed | ||
module HasNotifications | ||
# Defines a method for the association and a before_destroy 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) | ||
define_method :"notifications_as_#{param_name}" do | ||
model = options.fetch(:model_name, "Noticed::Event").constantize | ||
case current_adapter | ||
when "postgresql", "postgis" | ||
model.where("params @> ?", Noticed::Coder.dump(param_name.to_sym => self).to_json) | ||
when "mysql2" | ||
model.where("JSON_CONTAINS(params, ?)", Noticed::Coder.dump(param_name.to_sym => self).to_json) | ||
when "sqlite3" | ||
model.where("json_extract(params, ?) = ?", "$.#{param_name}", Noticed::Coder.dump(self).to_json) | ||
else | ||
# This will perform an exact match which isn't ideal | ||
model.where(params: {param_name.to_sym => self}) | ||
end | ||
end | ||
|
||
if options.fetch(:destroy, true) | ||
before_destroy do | ||
send(:"notifications_as_#{param_name}").destroy_all | ||
end | ||
end | ||
end | ||
end | ||
|
||
def current_adapter | ||
if ActiveRecord::Base.respond_to?(:connection_db_config) | ||
ActiveRecord::Base.connection_db_config.adapter | ||
else | ||
ActiveRecord::Base.connection_config[:adapter] | ||
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
class User < ApplicationRecord | ||
has_many :notifications, as: :recipient, dependent: :destroy, class_name: "Noticed::Notification" | ||
|
||
# Used for querying Noticed::Event where params[:user] is a User instance | ||
has_noticed_notifications | ||
has_noticed_notifications param_name: :owner, destroy: false | ||
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
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,47 @@ | ||
require "test_helper" | ||
|
||
class HasNotificationsTest < ActiveSupport::TestCase | ||
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 "association returns notifications" do | ||
assert_difference "user.notifications_as_user.count" do | ||
SimpleNotifier.with(user: user, message: "test").deliver(user) | ||
end | ||
end | ||
|
||
test "association with custom name returns notifications" do | ||
assert_difference "user.notifications_as_owner.count" do | ||
SimpleNotifier.with(owner: user, message: "test").deliver(user) | ||
end | ||
end | ||
|
||
test "deletes notifications with matching param" do | ||
SimpleNotifier.with(user: user, message: "test").deliver(users(:two)) | ||
|
||
assert_difference "Noticed::Event.count", -1 do | ||
user.destroy | ||
end | ||
end | ||
|
||
test "doesn't delete notifications when disabled" do | ||
SimpleNotifier.with(owner: user, message: "test").deliver(users(:two)) | ||
|
||
assert_no_difference "Noticed::Event.count" do | ||
user.destroy | ||
end | ||
end | ||
|
||
def user | ||
@user ||= users(:one) | ||
end | ||
end |