activity_notification 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +15 -5
- data/CHANGELOG.md +13 -2
- data/Gemfile +1 -1
- data/Gemfile.lock +78 -71
- data/README.md +64 -43
- data/activity_notification.gemspec +5 -4
- data/app/controllers/activity_notification/notifications_controller.rb +1 -1
- data/app/controllers/activity_notification/subscriptions_controller.rb +1 -1
- data/gemfiles/Gemfile.rails-4.2 +1 -1
- data/gemfiles/Gemfile.rails-4.2.lock +71 -62
- data/gemfiles/Gemfile.rails-5.0 +1 -1
- data/gemfiles/Gemfile.rails-5.0.lock +74 -67
- data/lib/activity_notification.rb +9 -2
- data/lib/activity_notification/apis/notification_api.rb +142 -76
- data/lib/activity_notification/apis/subscription_api.rb +72 -0
- data/lib/activity_notification/config.rb +12 -0
- data/lib/activity_notification/models/concerns/notifiable.rb +56 -6
- data/lib/activity_notification/models/concerns/notifier.rb +8 -1
- data/lib/activity_notification/models/concerns/subscriber.rb +13 -10
- data/lib/activity_notification/models/concerns/target.rb +7 -5
- data/lib/activity_notification/models/notification.rb +2 -270
- data/lib/activity_notification/models/subscription.rb +3 -101
- data/lib/activity_notification/optional_targets/base.rb +5 -1
- data/lib/activity_notification/orm/active_record.rb +16 -0
- data/lib/activity_notification/orm/active_record/notification.rb +252 -0
- data/lib/activity_notification/orm/active_record/subscription.rb +52 -0
- data/lib/activity_notification/orm/mongoid.rb +63 -0
- data/lib/activity_notification/orm/mongoid/notification.rb +255 -0
- data/lib/activity_notification/orm/mongoid/subscription.rb +73 -0
- data/lib/activity_notification/rails/routes.rb +7 -3
- data/lib/activity_notification/renderable.rb +5 -1
- data/lib/activity_notification/roles/acts_as_notifiable.rb +4 -18
- data/lib/activity_notification/version.rb +1 -1
- data/lib/generators/activity_notification/install_generator.rb +3 -5
- data/lib/generators/templates/activity_notification.rb +9 -4
- data/spec/concerns/apis/notification_api_spec.rb +27 -14
- data/spec/concerns/models/notifiable_spec.rb +10 -8
- data/spec/concerns/models/subscriber_spec.rb +12 -12
- data/spec/concerns/models/target_spec.rb +61 -51
- data/spec/controllers/subscriptions_controller_shared_examples.rb +0 -1
- data/spec/helpers/view_helpers_spec.rb +24 -7
- data/spec/models/notification_spec.rb +63 -52
- data/spec/models/subscription_spec.rb +6 -3
- data/spec/optional_targets/amazon_sns_spec.rb +8 -5
- data/spec/optional_targets/base_spec.rb +3 -1
- data/spec/optional_targets/slack_spec.rb +5 -5
- data/spec/rails_app/app/models/comment.rb +1 -1
- data/spec/rails_app/app/views/activity_notification/notifications/users/overriden/custom/_test.html.erb +1 -0
- data/spec/rails_app/config/application.rb +7 -0
- data/spec/rails_app/config/initializers/activity_notification.rb +5 -0
- data/spec/rails_app/config/mongoid.yml +13 -0
- data/spec/rails_app/db/seeds.rb +1 -1
- data/spec/rails_app/lib/custom_optional_targets/console_output.rb +7 -4
- data/spec/rails_app/lib/custom_optional_targets/wrong_target.rb +3 -0
- data/spec/roles/acts_as_notifiable_spec.rb +77 -16
- data/spec/spec_helper.rb +7 -0
- metadata +38 -14
@@ -2,11 +2,18 @@ module ActivityNotification
|
|
2
2
|
# Notifier implementation included in notifier model to be notified, like users or administrators.
|
3
3
|
module Notifier
|
4
4
|
extend ActiveSupport::Concern
|
5
|
+
|
5
6
|
included do
|
6
7
|
include Common
|
7
|
-
|
8
|
+
include Association
|
9
|
+
|
10
|
+
# Has many sent notification instances from this notifier.
|
11
|
+
# @scope instance
|
12
|
+
# @return [Array<Notificaion>, Mongoid::Criteria<Notificaion>] Array or database query of sent notifications from this notifier
|
13
|
+
has_many_records :sent_notifications,
|
8
14
|
class_name: "::ActivityNotification::Notification",
|
9
15
|
as: :notifier
|
16
|
+
|
10
17
|
class_attribute :_printable_notifier_name
|
11
18
|
set_notifier_class_defaults
|
12
19
|
end
|
@@ -2,11 +2,14 @@ module ActivityNotification
|
|
2
2
|
# Subscriber implementation included in target model to manage subscriptions, like users or administrators.
|
3
3
|
module Subscriber
|
4
4
|
extend ActiveSupport::Concern
|
5
|
+
|
5
6
|
included do
|
7
|
+
include Association
|
8
|
+
|
6
9
|
# Has many subscription instances of this target.
|
7
10
|
# @scope instance
|
8
|
-
# @return [Array<
|
9
|
-
|
11
|
+
# @return [Array<Subscription>, Mongoid::Criteria<Subscription>] Array or database query of subscriptions of this target
|
12
|
+
has_many_records :subscriptions,
|
10
13
|
class_name: "::ActivityNotification::Subscription",
|
11
14
|
as: :target,
|
12
15
|
dependent: :delete_all
|
@@ -27,7 +30,7 @@ module ActivityNotification
|
|
27
30
|
# @param [Hash] key Key of the notification for subscription
|
28
31
|
# @return [Subscription] Configured subscription instance
|
29
32
|
def find_subscription(key)
|
30
|
-
subscriptions.
|
33
|
+
subscriptions.where(key: key).first
|
31
34
|
end
|
32
35
|
|
33
36
|
# Gets subscription of the target and notification key.
|
@@ -87,8 +90,8 @@ module ActivityNotification
|
|
87
90
|
def subscription_index(options = {})
|
88
91
|
target_index = subscriptions.filtered_by_options(options)
|
89
92
|
target_index = options[:reverse] ? target_index.earliest_order : target_index.latest_order
|
90
|
-
target_index = target_index.
|
91
|
-
options[:limit].present? ? target_index.limit(options[:limit]) : target_index
|
93
|
+
target_index = target_index.with_target if options[:with_target]
|
94
|
+
options[:limit].present? ? target_index.limit(options[:limit]).to_a : target_index.to_a
|
92
95
|
end
|
93
96
|
|
94
97
|
# Gets received notification keys of the target.
|
@@ -104,11 +107,11 @@ module ActivityNotification
|
|
104
107
|
# @option options [Array|Hash] :custom_filter (nil) Custom subscription filter (e.g. ["created_at >= ?", time.hour.ago])
|
105
108
|
# @return [Array<Notificaion>] Unconfigured notification keys of the target
|
106
109
|
def notification_keys(options = {})
|
107
|
-
subscription_keys = subscriptions.
|
110
|
+
subscription_keys = subscriptions.uniq_keys
|
108
111
|
target_notifications = notifications.filtered_by_options(options.select { |k, _| [:filtered_by_key, :custom_filter].include?(k) })
|
109
112
|
target_notifications = options[:reverse] ? target_notifications.earliest_order : target_notifications.latest_order
|
110
113
|
target_notifications = options[:limit].present? ? target_notifications.limit(options[:limit] + subscription_keys.size) : target_notifications
|
111
|
-
notification_keys = target_notifications.
|
114
|
+
notification_keys = target_notifications.uniq_keys
|
112
115
|
notification_keys =
|
113
116
|
case options[:filter]
|
114
117
|
when :configured, 'configured'
|
@@ -131,7 +134,7 @@ module ActivityNotification
|
|
131
134
|
# @param [Boolean] subscribe_as_default Default subscription value to use when the subscription record does not configured
|
132
135
|
# @return [Boolean] If the target subscribes to the notification
|
133
136
|
def _subscribes_to_notification?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
|
134
|
-
evaluate_subscription(subscriptions.
|
137
|
+
evaluate_subscription(subscriptions.where(key: key).first, :subscribing?, subscribe_as_default)
|
135
138
|
end
|
136
139
|
|
137
140
|
# Returns if the target subscribes to the notification email.
|
@@ -142,7 +145,7 @@ module ActivityNotification
|
|
142
145
|
# @param [Boolean] subscribe_as_default Default subscription value to use when the subscription record does not configured
|
143
146
|
# @return [Boolean] If the target subscribes to the notification
|
144
147
|
def _subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
|
145
|
-
evaluate_subscription(subscriptions.
|
148
|
+
evaluate_subscription(subscriptions.where(key: key).first, :subscribing_to_email?, subscribe_as_default)
|
146
149
|
end
|
147
150
|
alias_method :_subscribes_to_email?, :_subscribes_to_notification_email?
|
148
151
|
|
@@ -156,7 +159,7 @@ module ActivityNotification
|
|
156
159
|
# @return [Boolean] If the target subscribes to the specified optional target
|
157
160
|
def _subscribes_to_optional_target?(key, optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
|
158
161
|
_subscribes_to_notification?(key, subscribe_as_default) &&
|
159
|
-
evaluate_subscription(subscriptions.
|
162
|
+
evaluate_subscription(subscriptions.where(key: key).first, :subscribing_to_optional_target?, subscribe_as_default, optional_target_name, subscribe_as_default)
|
160
163
|
end
|
161
164
|
|
162
165
|
private
|
@@ -2,13 +2,15 @@ module ActivityNotification
|
|
2
2
|
# Target implementation included in target model to notify, like users or administrators.
|
3
3
|
module Target
|
4
4
|
extend ActiveSupport::Concern
|
5
|
+
|
5
6
|
included do
|
6
7
|
include Common
|
8
|
+
include Association
|
7
9
|
|
8
10
|
# Has many notification instances of this target.
|
9
11
|
# @scope instance
|
10
|
-
# @return [Array<Notificaion>] Array or database query of notifications of this target
|
11
|
-
|
12
|
+
# @return [Array<Notificaion>, Mongoid::Criteria<Notificaion>] Array or database query of notifications of this target
|
13
|
+
has_many_records :notifications,
|
12
14
|
class_name: "::ActivityNotification::Notification",
|
13
15
|
as: :target,
|
14
16
|
dependent: :delete_all
|
@@ -180,9 +182,9 @@ module ActivityNotification
|
|
180
182
|
# @return [Boolean] If current resource signed in with Devise is authenticated for the notification
|
181
183
|
def authenticated_with_devise?(current_resource)
|
182
184
|
devise_resource = resolve_value(_notification_devise_resource)
|
183
|
-
unless current_resource.blank? or current_resource.
|
185
|
+
unless current_resource.blank? or current_resource.is_a? devise_resource.class
|
184
186
|
raise TypeError,
|
185
|
-
"
|
187
|
+
"Different type of current resource #{current_resource.class} "\
|
186
188
|
"with devise resource #{devise_resource.class} has been passed to #{self.class}##{__method__}. "\
|
187
189
|
"You have to override #{self.class}##{__method__} method or set devise_resource in acts_as_target."
|
188
190
|
end
|
@@ -581,7 +583,7 @@ module ActivityNotification
|
|
581
583
|
end
|
582
584
|
else
|
583
585
|
# Otherwise, return opened notifications
|
584
|
-
loading_opened_index_method.call(options)
|
586
|
+
loading_opened_index_method.call(options).to_a
|
585
587
|
end
|
586
588
|
end
|
587
589
|
|
@@ -1,273 +1,5 @@
|
|
1
1
|
module ActivityNotification
|
2
|
-
# Notification model implementation
|
3
|
-
class Notification <
|
4
|
-
include Renderable
|
5
|
-
include Common
|
6
|
-
include NotificationApi
|
7
|
-
# @deprecated ActivityNotification.config.table_name as of 1.1.0
|
8
|
-
self.table_name = ActivityNotification.config.table_name || ActivityNotification.config.notification_table_name
|
9
|
-
# self.table_name = ActivityNotification.config.notification_table_name
|
10
|
-
|
11
|
-
# Belongs to target instance of this notification as polymorphic association.
|
12
|
-
# @scope instance
|
13
|
-
# @return [Object] Target instance of this notification
|
14
|
-
belongs_to :target, polymorphic: true
|
15
|
-
|
16
|
-
# Belongs to notifiable instance of this notification as polymorphic association.
|
17
|
-
# @scope instance
|
18
|
-
# @return [Object] Notifiable instance of this notification
|
19
|
-
belongs_to :notifiable, polymorphic: true
|
20
|
-
|
21
|
-
# Belongs to group instance of this notification as polymorphic association.
|
22
|
-
# @scope instance
|
23
|
-
# @return [Object] Group instance of this notification
|
24
|
-
belongs_to :group, polymorphic: true
|
25
|
-
|
26
|
-
# Belongs to group owner notification instance of this notification.
|
27
|
-
# Only group member instance has :group_owner value.
|
28
|
-
# Group owner instance has nil as :group_owner association.
|
29
|
-
# @scope instance
|
30
|
-
# @return [Notification] Group owner notification instance of this notification
|
31
|
-
belongs_to :group_owner, class_name: :Notification
|
32
|
-
|
33
|
-
# Has many group member notification instances of this notification.
|
34
|
-
# Only group owner instance has :group_members value.
|
35
|
-
# Group member instance has nil as :group_members association.
|
36
|
-
# @scope instance
|
37
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of the group member notification instances of this notification
|
38
|
-
has_many :group_members, class_name: :Notification, foreign_key: :group_owner_id
|
39
|
-
|
40
|
-
# Belongs to :otifier instance of this notification.
|
41
|
-
# @scope instance
|
42
|
-
# @return [Object] Notifier instance of this notification
|
43
|
-
belongs_to :notifier, polymorphic: true
|
44
|
-
|
45
|
-
# Serialize parameters Hash
|
46
|
-
serialize :parameters, Hash
|
47
|
-
|
48
|
-
validates :target, presence: true
|
49
|
-
validates :notifiable, presence: true
|
50
|
-
validates :key, presence: true
|
51
|
-
|
52
|
-
# Selects group owner notifications only.
|
53
|
-
# @scope class
|
54
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
55
|
-
scope :group_owners_only, -> { where(group_owner_id: nil) }
|
56
|
-
|
57
|
-
# Selects group member notifications only.
|
58
|
-
# @scope class
|
59
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
60
|
-
scope :group_members_only, -> { where.not(group_owner_id: nil) }
|
61
|
-
|
62
|
-
# Selects all notification index.
|
63
|
-
# ActivityNotification::Notification.all_index!
|
64
|
-
# is defined same as
|
65
|
-
# ActivityNotification::Notification.group_owners_only.latest_order
|
66
|
-
# @scope class
|
67
|
-
# @example Get all notification index of the @user
|
68
|
-
# @notifications = @user.notifications.all_index!
|
69
|
-
# @notifications = @user.notifications.group_owners_only.latest_order
|
70
|
-
# @param [Boolean] reverse If notification index will be ordered as earliest first
|
71
|
-
# @param [Boolean] with_group_members If notification index will include group members
|
72
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
73
|
-
scope :all_index!, ->(reverse = false, with_group_members = false) {
|
74
|
-
target_index = with_group_members ? self : group_owners_only
|
75
|
-
reverse ? target_index.earliest_order : target_index.latest_order
|
76
|
-
}
|
77
|
-
|
78
|
-
# Selects unopened notifications only.
|
79
|
-
# @scope class
|
80
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
81
|
-
scope :unopened_only, -> { where(opened_at: nil) }
|
82
|
-
|
83
|
-
# Selects unopened notification index.
|
84
|
-
# ActivityNotification::Notification.unopened_index
|
85
|
-
# is defined same as
|
86
|
-
# ActivityNotification::Notification.unopened_only.group_owners_only.latest_order
|
87
|
-
# @scope class
|
88
|
-
# @example Get unopened notificaton index of the @user
|
89
|
-
# @notifications = @user.notifications.unopened_index
|
90
|
-
# @notifications = @user.notifications.unopened_only.group_owners_only.latest_order
|
91
|
-
# @param [Boolean] reverse If notification index will be ordered as earliest first
|
92
|
-
# @param [Boolean] with_group_members If notification index will include group members
|
93
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
94
|
-
scope :unopened_index, ->(reverse = false, with_group_members = false) {
|
95
|
-
target_index = with_group_members ? unopened_only : unopened_only.group_owners_only
|
96
|
-
reverse ? target_index.earliest_order : target_index.latest_order
|
97
|
-
}
|
98
|
-
|
99
|
-
# Selects opened notifications only without limit.
|
100
|
-
# Be careful to get too many records with this method.
|
101
|
-
# @scope class
|
102
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
103
|
-
scope :opened_only!, -> { where.not(opened_at: nil) }
|
104
|
-
|
105
|
-
# Selects opened notifications only with limit.
|
106
|
-
# @scope class
|
107
|
-
# @param [Integer] limit Limit to query for opened notifications
|
108
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
109
|
-
scope :opened_only, ->(limit) { opened_only!.limit(limit) }
|
110
|
-
|
111
|
-
# Selects unopened notification index.
|
112
|
-
# ActivityNotification::Notification.opened_index(limit)
|
113
|
-
# is defined same as
|
114
|
-
# ActivityNotification::Notification.opened_only(limit).group_owners_only.latest_order
|
115
|
-
# @scope class
|
116
|
-
# @example Get unopened notificaton index of the @user with limit 10
|
117
|
-
# @notifications = @user.notifications.opened_index(10)
|
118
|
-
# @notifications = @user.notifications.opened_only(10).group_owners_only.latest_order
|
119
|
-
# @param [Integer] limit Limit to query for opened notifications
|
120
|
-
# @param [Boolean] reverse If notification index will be ordered as earliest first
|
121
|
-
# @param [Boolean] with_group_members If notification index will include group members
|
122
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
123
|
-
scope :opened_index, ->(limit, reverse = false, with_group_members = false) {
|
124
|
-
target_index = with_group_members ? opened_only(limit) : opened_only(limit).group_owners_only
|
125
|
-
reverse ? target_index.earliest_order : target_index.latest_order
|
126
|
-
}
|
127
|
-
|
128
|
-
# Selects group member notifications in unopened_index.
|
129
|
-
# @scope class
|
130
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
131
|
-
scope :unopened_index_group_members_only, -> { where(group_owner_id: unopened_index.map(&:id)) }
|
132
|
-
|
133
|
-
# Selects group member notifications in opened_index.
|
134
|
-
# @scope class
|
135
|
-
# @param [Integer] limit Limit to query for opened notifications
|
136
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
137
|
-
scope :opened_index_group_members_only, ->(limit) { where(group_owner_id: opened_index(limit).map(&:id)) }
|
138
|
-
|
139
|
-
# Selects filtered notifications by target instance.
|
140
|
-
# ActivityNotification::Notification.filtered_by_target(@user)
|
141
|
-
# is the same as
|
142
|
-
# @user.notifications
|
143
|
-
# @scope class
|
144
|
-
# @param [Object] target Target instance for filter
|
145
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
146
|
-
scope :filtered_by_target, ->(target) { where(target: target) }
|
147
|
-
|
148
|
-
# Selects filtered notifications by target_type.
|
149
|
-
# @example Get filtered unopened notificatons of User as target type
|
150
|
-
# @notifications = ActivityNotification.Notification.unopened_only.filtered_by_target_type('User')
|
151
|
-
# @scope class
|
152
|
-
# @param [String] target_type Target type for filter
|
153
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
154
|
-
scope :filtered_by_target_type, ->(target_type) { where(target_type: target_type) }
|
155
|
-
|
156
|
-
# Selects filtered notifications by notifiable instance.
|
157
|
-
# @example Get filtered unopened notificatons of the @user for @comment as notifiable
|
158
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_instance(@comment)
|
159
|
-
# @scope class
|
160
|
-
# @param [Object] notifiable Notifiable instance for filter
|
161
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
162
|
-
scope :filtered_by_instance, ->(notifiable) { where(notifiable: notifiable) }
|
163
|
-
|
164
|
-
# Selects filtered notifications by notifiable_type.
|
165
|
-
# @example Get filtered unopened notificatons of the @user for Comment notifiable class
|
166
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_type('Comment')
|
167
|
-
# @scope class
|
168
|
-
# @param [String] notifiable_type Notifiable type for filter
|
169
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
170
|
-
scope :filtered_by_type, ->(notifiable_type) { where(notifiable_type: notifiable_type) }
|
171
|
-
|
172
|
-
# Selects filtered notifications by group instance.
|
173
|
-
# @example Get filtered unopened notificatons of the @user for @article as group
|
174
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_group(@article)
|
175
|
-
# @scope class
|
176
|
-
# @param [Object] group Group instance for filter
|
177
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
178
|
-
scope :filtered_by_group, ->(group) { where(group: group) }
|
179
|
-
|
180
|
-
# Selects filtered notifications by key.
|
181
|
-
# @example Get filtered unopened notificatons of the @user with key 'comment.reply'
|
182
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_key('comment.reply')
|
183
|
-
# @scope class
|
184
|
-
# @param [String] key Key of the notification for filter
|
185
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
186
|
-
scope :filtered_by_key, ->(key) { where(key: key) }
|
187
|
-
|
188
|
-
# Selects filtered notifications by notifiable_type, group or key with filter options.
|
189
|
-
# @example Get filtered unopened notificatons of the @user for Comment notifiable class
|
190
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_type: 'Comment' })
|
191
|
-
# @example Get filtered unopened notificatons of the @user for @article as group
|
192
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_group: @article })
|
193
|
-
# @example Get filtered unopened notificatons of the @user for Article instance id=1 as group
|
194
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_group_type: 'Article', filtered_by_group_id: '1' })
|
195
|
-
# @example Get filtered unopened notificatons of the @user with key 'comment.reply'
|
196
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_key: 'comment.reply' })
|
197
|
-
# @example Get filtered unopened notificatons of the @user for Comment notifiable class with key 'comment.reply'
|
198
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_type: 'Comment', filtered_by_key: 'comment.reply' })
|
199
|
-
# @example Get custom filtered notificatons of the @user
|
200
|
-
# @notifications = @user.notifications.unopened_only.filtered_by_options({ custom_filter: ["created_at >= ?", time.hour.ago] })
|
201
|
-
# @scope class
|
202
|
-
# @param [Hash] options Options for filter
|
203
|
-
# @option options [String] :filtered_by_type (nil) Notifiable type for filter
|
204
|
-
# @option options [Object] :filtered_by_group (nil) Group instance for filter
|
205
|
-
# @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
|
206
|
-
# @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
|
207
|
-
# @option options [String] :filtered_by_key (nil) Key of the notification for filter
|
208
|
-
# @option options [Array|Hash] :custom_filter (nil) Custom notification filter (e.g. ["created_at >= ?", time.hour.ago])
|
209
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
210
|
-
scope :filtered_by_options, ->(options = {}) {
|
211
|
-
options = ActivityNotification.cast_to_indifferent_hash(options)
|
212
|
-
filtered_notifications = all
|
213
|
-
if options.has_key?(:filtered_by_type)
|
214
|
-
filtered_notifications = filtered_notifications.filtered_by_type(options[:filtered_by_type])
|
215
|
-
end
|
216
|
-
if options.has_key?(:filtered_by_group)
|
217
|
-
filtered_notifications = filtered_notifications.filtered_by_group(options[:filtered_by_group])
|
218
|
-
end
|
219
|
-
if options.has_key?(:filtered_by_group_type) && options.has_key?(:filtered_by_group_id)
|
220
|
-
filtered_notifications = filtered_notifications
|
221
|
-
.where(group_type: options[:filtered_by_group_type], group_id: options[:filtered_by_group_id])
|
222
|
-
end
|
223
|
-
if options.has_key?(:filtered_by_key)
|
224
|
-
filtered_notifications = filtered_notifications.filtered_by_key(options[:filtered_by_key])
|
225
|
-
end
|
226
|
-
if options.has_key?(:custom_filter)
|
227
|
-
filtered_notifications = filtered_notifications.where(options[:custom_filter])
|
228
|
-
end
|
229
|
-
filtered_notifications
|
230
|
-
}
|
231
|
-
|
232
|
-
# Includes target instance with query for notifications.
|
233
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with target
|
234
|
-
scope :with_target, -> { includes(:target) }
|
235
|
-
|
236
|
-
# Includes notifiable instance with query for notifications.
|
237
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with notifiable
|
238
|
-
scope :with_notifiable, -> { includes(:notifiable) }
|
239
|
-
|
240
|
-
# Includes group instance with query for notifications.
|
241
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with group
|
242
|
-
scope :with_group, -> { includes(:group) }
|
243
|
-
|
244
|
-
# Includes group owner instances with query for notifications.
|
245
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with group owner
|
246
|
-
scope :with_group_owner, -> { includes(:group_owner) }
|
247
|
-
|
248
|
-
# Includes group member instances with query for notifications.
|
249
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with group members
|
250
|
-
scope :with_group_members, -> { includes(:group_members) }
|
251
|
-
|
252
|
-
# Includes notifier instance with query for notifications.
|
253
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with notifier
|
254
|
-
scope :with_notifier, -> { includes(:notifier) }
|
255
|
-
|
256
|
-
# Orders by latest (newest) first as created_at: :desc.
|
257
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications ordered by latest first
|
258
|
-
scope :latest_order, -> { order(created_at: :desc) }
|
259
|
-
|
260
|
-
# Orders by earliest (older) first as created_at: :asc.
|
261
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications ordered by earliest first
|
262
|
-
scope :earliest_order, -> { order(created_at: :asc) }
|
263
|
-
|
264
|
-
# Returns latest notification instance.
|
265
|
-
# @return [Notification] Latest notification instance
|
266
|
-
scope :latest, -> { latest_order.first }
|
267
|
-
|
268
|
-
# Returns earliest notification instance.
|
269
|
-
# @return [Notification] Earliest notification instance
|
270
|
-
scope :earliest, -> { earliest_order.first }
|
271
|
-
|
2
|
+
# Notification model implementation with ORM.
|
3
|
+
class Notification < inherit_orm("Notification")
|
272
4
|
end
|
273
5
|
end
|
@@ -1,104 +1,6 @@
|
|
1
1
|
module ActivityNotification
|
2
|
-
# Subscription model implementation
|
3
|
-
class Subscription <
|
4
|
-
include SubscriptionApi
|
5
|
-
self.table_name = ActivityNotification.config.subscription_table_name
|
6
|
-
|
7
|
-
# Belongs to target instance of this subscription as polymorphic association.
|
8
|
-
# @scope instance
|
9
|
-
# @return [Object] Target instance of this subscription
|
10
|
-
belongs_to :target, polymorphic: true
|
11
|
-
|
12
|
-
# Serialize parameters Hash
|
13
|
-
serialize :optional_targets, Hash
|
14
|
-
|
15
|
-
validates :target, presence: true
|
16
|
-
validates :key, presence: true
|
17
|
-
validates_inclusion_of :subscribing, in: [true, false]
|
18
|
-
validates_inclusion_of :subscribing_to_email, in: [true, false]
|
19
|
-
validate :subscribing_to_email_cannot_be_true_when_subscribing_is_false
|
20
|
-
validates :subscribed_at, presence: true, if: :subscribing
|
21
|
-
validates :unsubscribed_at, presence: true, unless: :subscribing
|
22
|
-
validates :subscribed_to_email_at, presence: true, if: :subscribing_to_email
|
23
|
-
validates :unsubscribed_to_email_at, presence: true, unless: :subscribing_to_email
|
24
|
-
validate :subscribing_to_optional_target_cannot_be_true_when_subscribing_is_false
|
25
|
-
|
26
|
-
# Selects filtered subscriptions by target instance.
|
27
|
-
# ActivityNotification::Subscription.filtered_by_target(@user)
|
28
|
-
# is the same as
|
29
|
-
# @user.subscriptions
|
30
|
-
# @scope class
|
31
|
-
# @param [Object] target Target instance for filter
|
32
|
-
# @return [ActiveRecord_AssociationRelation<Subscription>] Array or database query of filtered subscriptions
|
33
|
-
scope :filtered_by_target, ->(target) { where(target: target) }
|
34
|
-
|
35
|
-
# Selects filtered subscriptions by key.
|
36
|
-
# @example Get filtered subscriptions of the @user with key 'comment.reply'
|
37
|
-
# @subscriptions = @user.subscriptions.filtered_by_key('comment.reply')
|
38
|
-
# @scope class
|
39
|
-
# @param [String] key Key of the subscription for filter
|
40
|
-
# @return [ActiveRecord_AssociationRelation<Subscription>] Array or database query of filtered subscriptions
|
41
|
-
scope :filtered_by_key, ->(key) { where(key: key) }
|
42
|
-
|
43
|
-
# Selects filtered subscriptions by key with filter options.
|
44
|
-
# @example Get filtered subscriptions of the @user with key 'comment.reply'
|
45
|
-
# @subscriptions = @user.subscriptions.filtered_by_key('comment.reply')
|
46
|
-
# @example Get custom filtered subscriptions of the @user
|
47
|
-
# @subscriptions = @user.subscriptions.filtered_by_options({ custom_filter: ["created_at >= ?", time.hour.ago] })
|
48
|
-
# @scope class
|
49
|
-
# @param [Hash] options Options for filter
|
50
|
-
# @option options [String] :filtered_by_key (nil) Key of the subscription for filter
|
51
|
-
# @option options [Array|Hash] :custom_filter (nil) Custom subscription filter (e.g. ["created_at >= ?", time.hour.ago])
|
52
|
-
# @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered subscriptions
|
53
|
-
scope :filtered_by_options, ->(options = {}) {
|
54
|
-
options = ActivityNotification.cast_to_indifferent_hash(options)
|
55
|
-
filtered_subscriptions = all
|
56
|
-
if options.has_key?(:filtered_by_key)
|
57
|
-
filtered_subscriptions = filtered_subscriptions.filtered_by_key(options[:filtered_by_key])
|
58
|
-
end
|
59
|
-
if options.has_key?(:custom_filter)
|
60
|
-
filtered_subscriptions = filtered_subscriptions.where(options[:custom_filter])
|
61
|
-
end
|
62
|
-
filtered_subscriptions
|
63
|
-
}
|
64
|
-
|
65
|
-
# Orders by latest (newest) first as created_at: :desc.
|
66
|
-
# @return [ActiveRecord_AssociationRelation<Subscription>] Database query of subscriptions ordered by latest first
|
67
|
-
scope :latest_order, -> { order(created_at: :desc) }
|
68
|
-
|
69
|
-
# Orders by earliest (older) first as created_at: :asc.
|
70
|
-
# @return [ActiveRecord_AssociationRelation<Subscription>] Database query of subscriptions ordered by earliest first
|
71
|
-
scope :earliest_order, -> { order(created_at: :asc) }
|
72
|
-
|
73
|
-
# Orders by latest (newest) first as subscribed_at: :desc.
|
74
|
-
# @return [ActiveRecord_AssociationRelation<Subscription>] Database query of subscriptions ordered by latest subscribed_at first
|
75
|
-
scope :latest_subscribed_order, -> { order(subscribed_at: :desc) }
|
76
|
-
|
77
|
-
# Orders by earliest (older) first as subscribed_at: :asc.
|
78
|
-
# @return [ActiveRecord_AssociationRelation<Subscription>] Database query of subscriptions ordered by earliest subscribed_at first
|
79
|
-
scope :earliest_subscribed_order, -> { order(subscribed_at: :asc) }
|
80
|
-
|
81
|
-
# Orders by key name as key: :asc.
|
82
|
-
# @return [ActiveRecord_AssociationRelation<Subscription>] Database query of subscriptions ordered by key name
|
83
|
-
scope :key_order, -> { order(key: :asc) }
|
84
|
-
|
85
|
-
private
|
86
|
-
|
87
|
-
# Validates subscribing_to_email cannot be true when subscribing isfalse.
|
88
|
-
def subscribing_to_email_cannot_be_true_when_subscribing_is_false
|
89
|
-
if !subscribing && subscribing_to_email?
|
90
|
-
errors.add(:subscribing_to_email, "cannot be true when subscribing is false")
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# Validates subscribing_to_optional_target cannot be true when subscribing isfalse.
|
95
|
-
def subscribing_to_optional_target_cannot_be_true_when_subscribing_is_false
|
96
|
-
optional_target_names.each do |optional_target_name|
|
97
|
-
if !subscribing && subscribing_to_optional_target?(optional_target_name)
|
98
|
-
errors.add(:optional_targets, "#Subscription.to_optional_target_key(optional_target_name) cannot be true when subscribing is false")
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
2
|
+
# Subscription model implementation with ORM.
|
3
|
+
class Subscription < inherit_orm("Subscription")
|
103
4
|
end
|
104
5
|
end
|
6
|
+
|