activity_notification 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +5 -0
- data/.yardopts +3 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +50 -44
- data/README.md +242 -81
- data/Rakefile +13 -13
- data/activity_notification.gemspec +6 -8
- data/app/controllers/activity_notification/notifications_controller.rb +89 -11
- data/app/controllers/activity_notification/notifications_with_devise_controller.rb +12 -3
- data/app/mailers/activity_notification/mailer.rb +3 -0
- data/gemfiles/Gemfile.rails-4.2 +13 -0
- data/gemfiles/Gemfile.rails-4.2.lock +190 -0
- data/gemfiles/Gemfile.rails-5.0 +14 -0
- data/gemfiles/Gemfile.rails-5.0.lock +201 -0
- data/lib/activity_notification.rb +10 -6
- data/lib/activity_notification/apis/notification_api.rb +137 -27
- data/lib/activity_notification/common.rb +48 -24
- data/lib/activity_notification/config.rb +68 -10
- data/lib/activity_notification/controllers/store_controller.rb +13 -5
- data/lib/activity_notification/helpers/polymorphic_helpers.rb +17 -3
- data/lib/activity_notification/helpers/view_helpers.rb +161 -45
- data/lib/activity_notification/mailers/helpers.rb +121 -83
- data/lib/activity_notification/models/concerns/notifiable.rb +162 -69
- data/lib/activity_notification/models/concerns/notifier.rb +2 -0
- data/lib/activity_notification/models/concerns/target.rb +124 -25
- data/lib/activity_notification/models/notification.rb +168 -4
- data/lib/activity_notification/rails/routes.rb +50 -48
- data/lib/activity_notification/renderable.rb +106 -26
- data/lib/activity_notification/roles/acts_as_notifiable.rb +99 -26
- data/lib/activity_notification/roles/acts_as_notifier.rb +3 -0
- data/lib/activity_notification/roles/acts_as_target.rb +70 -0
- data/lib/activity_notification/version.rb +1 -1
- data/lib/generators/activity_notification/active_record/migration_generator.rb +3 -1
- data/lib/generators/activity_notification/controllers_generator.rb +5 -0
- data/lib/generators/activity_notification/install_generator.rb +7 -3
- data/lib/generators/activity_notification/models/notification_generator.rb +4 -2
- data/lib/generators/activity_notification/views_generator.rb +20 -0
- data/spec/concerns/apis/notification_api_spec.rb +105 -36
- data/spec/concerns/common_spec.rb +1 -1
- data/spec/concerns/models/notifiable_spec.rb +2 -2
- data/spec/concerns/models/notifier_spec.rb +1 -1
- data/spec/concerns/models/target_spec.rb +9 -8
- data/spec/controllers/notifications_controller_shared_examples.rb +101 -28
- data/spec/controllers/notifications_with_devise_controller_spec.rb +14 -4
- data/spec/helpers/view_helpers_spec.rb +3 -3
- data/spec/mailers/mailer_spec.rb +1 -1
- data/spec/models/notification_spec.rb +57 -3
- data/spec/rails_app/app/models/article.rb +1 -2
- data/spec/rails_app/app/models/comment.rb +8 -6
- data/spec/rails_app/app/models/user.rb +1 -1
- data/spec/rails_app/app/views/layouts/_header.html.erb +2 -0
- data/spec/rails_app/config/application.rb +3 -1
- data/spec/rails_app/config/environment.rb +12 -2
- data/spec/rails_app/config/environments/test.rb +11 -2
- data/spec/roles/acts_as_notifiable_spec.rb +2 -2
- data/spec/roles/acts_as_notifier_spec.rb +1 -1
- data/spec/roles/acts_as_target_spec.rb +3 -3
- data/spec/spec_helper.rb +6 -0
- metadata +35 -40
- data/spec/rails_app/app/models/concerns/.keep +0 -0
@@ -1,55 +1,96 @@
|
|
1
1
|
module ActivityNotification
|
2
|
+
# Target implementation included in target model to notify, like users or administrators.
|
2
3
|
module Target
|
3
4
|
extend ActiveSupport::Concern
|
4
5
|
included do
|
5
6
|
include Common
|
7
|
+
|
8
|
+
# Has many notification instances of this target.
|
9
|
+
# @scope instance
|
10
|
+
# @return [Array<Notificaion>] Array or database query of notifications of this target
|
6
11
|
has_many :notifications,
|
7
12
|
class_name: "::ActivityNotification::Notification",
|
8
13
|
as: :target
|
14
|
+
|
9
15
|
class_attribute :_notification_email, :_notification_email_allowed, :_notification_devise_resource
|
10
16
|
set_target_class_defaults
|
11
17
|
end
|
12
18
|
|
13
19
|
class_methods do
|
20
|
+
# Checks if the model includes target and target methods are available.
|
21
|
+
# @return [Boolean] Always true
|
14
22
|
def available_as_target?
|
15
23
|
true
|
16
24
|
end
|
17
25
|
|
26
|
+
# Sets default values to target class fields.
|
27
|
+
# @return [Nil] nil
|
18
28
|
def set_target_class_defaults
|
19
29
|
self._notification_email = nil
|
20
30
|
self._notification_email_allowed = ActivityNotification.config.email_enabled
|
21
31
|
self._notification_devise_resource = ->(model) { model }
|
32
|
+
nil
|
22
33
|
end
|
23
34
|
end
|
24
35
|
|
36
|
+
# Returns target email address for email notification.
|
37
|
+
# This method is able to be overriden.
|
38
|
+
#
|
39
|
+
# @return [String] Target email address
|
25
40
|
def mailer_to
|
26
41
|
resolve_value(_notification_email)
|
27
42
|
end
|
28
43
|
|
44
|
+
# Returns if sending notification email is allowed for the target from configured field or overriden method.
|
45
|
+
# This method is able to be overriden.
|
46
|
+
#
|
47
|
+
# @param [Object] notifiable Notifiable instance of the notification
|
48
|
+
# @param [String] key Key of the notification
|
49
|
+
# @return [Boolean] If sending notification email is allowed for the target
|
29
50
|
def notification_email_allowed?(notifiable, key)
|
30
51
|
resolve_value(_notification_email_allowed, notifiable, key)
|
31
52
|
end
|
32
53
|
|
54
|
+
# Returns if current resource signed in with Devise is authenticated for the notification.
|
55
|
+
# This method is able to be overriden.
|
56
|
+
#
|
57
|
+
# @param [Object] current_resource Current resource signed in with Devise
|
58
|
+
# @return [Boolean] If current resource signed in with Devise is authenticated for the notification
|
33
59
|
def authenticated_with_devise?(current_resource)
|
34
60
|
devise_resource = resolve_value(_notification_devise_resource)
|
35
61
|
unless current_resource.instance_of? devise_resource.class
|
36
62
|
raise TypeError,
|
37
|
-
"Defferent type of current resource #{current_resource.class
|
38
|
-
"with devise resource #{devise_resource.class
|
63
|
+
"Defferent type of current resource #{current_resource.class} "\
|
64
|
+
"with devise resource #{devise_resource.class} has been passed to #{self.class}##{__method__}. "\
|
39
65
|
"You have to override #{self.class}##{__method__} method or set devise_resource in acts_as_target."
|
40
66
|
end
|
41
67
|
current_resource == devise_resource
|
42
68
|
end
|
43
69
|
|
70
|
+
# Returns count of unopened notifications of the target.
|
71
|
+
#
|
72
|
+
# @return [Integer] Count of unopened notifications of the target
|
44
73
|
def unopened_notification_count
|
45
74
|
unopened_notification_index.count
|
46
75
|
end
|
47
76
|
|
77
|
+
# Returns if the target has unopened notifications.
|
78
|
+
#
|
79
|
+
# @return [Boolean] If the target has unopened notifications
|
48
80
|
def has_unopened_notifications?
|
49
81
|
unopened_notification_index.exists?
|
50
82
|
end
|
51
83
|
|
52
|
-
#
|
84
|
+
# Gets automatically arranged notification index of the target.
|
85
|
+
# When the target has unopened notifications, returns unopened index with unopened_notification_index.
|
86
|
+
# Otherwise, returns opened index with opened_notification_index.
|
87
|
+
# @todo Is this switching the best solution?
|
88
|
+
#
|
89
|
+
# @example Get automatically arranged notification index of the @user
|
90
|
+
# @notifications = @user.notification_index
|
91
|
+
#
|
92
|
+
# @param [Integer] limit Limit to query for notifications
|
93
|
+
# @return [Array<Notificaion>] Notification index of the target
|
53
94
|
def notification_index(limit = nil)
|
54
95
|
# When the target have unopened notifications
|
55
96
|
notifications.unopened_index.exists? ?
|
@@ -61,31 +102,68 @@ module ActivityNotification
|
|
61
102
|
opened_notification_index
|
62
103
|
end
|
63
104
|
|
105
|
+
# Gets unopened notification index of the target.
|
106
|
+
#
|
107
|
+
# @example Get unopened notification index of the @user
|
108
|
+
# @notifications = @user.unopened_notification_index
|
109
|
+
#
|
110
|
+
# @param [Integer] limit Limit to query for notifications
|
111
|
+
# @return [Array<Notificaion>] Unopened notification index of the target
|
64
112
|
def unopened_notification_index(limit = nil)
|
65
113
|
limit.present? ?
|
66
114
|
notifications.unopened_index.limit(limit) :
|
67
115
|
notifications.unopened_index
|
68
116
|
end
|
69
117
|
|
118
|
+
# Gets opened notification index of the target.
|
119
|
+
#
|
120
|
+
# @example Get opened notification index of the @user
|
121
|
+
# @notifications = @user.opened_notification_index(10)
|
122
|
+
#
|
123
|
+
# @param [Integer] limit Limit to query for notifications
|
124
|
+
# @return [Array<Notificaion>] Opened notification index of the target
|
70
125
|
def opened_notification_index(limit = ActivityNotification.config.opened_limit)
|
71
126
|
notifications.opened_index(limit)
|
72
127
|
end
|
73
128
|
|
74
|
-
|
75
|
-
#
|
76
|
-
|
129
|
+
# Generates notifications to this target.
|
130
|
+
# This method calls NotificationApi#notify_to internally with self target instance.
|
131
|
+
# @see NotificationApi#notify_to
|
132
|
+
#
|
133
|
+
# @param [Object] notifiable Notifiable instance to notify
|
134
|
+
# @param [Hash] options Options for notifications
|
135
|
+
# @option options [String] :key (notifiable.default_notification_key) Key of the notification
|
136
|
+
# @option options [Object] :group (nil) Group unit of the notifications
|
137
|
+
# @option options [Object] :notifier (nil) Notifier of the notifications
|
138
|
+
# @option options [Hash] :parameters ({}) Additional parameters of the notifications
|
139
|
+
# @option options [Boolean] :send_email (true) Whether it sends notification email
|
140
|
+
# @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
|
141
|
+
# @return [Notification] Generated notification instance
|
77
142
|
def notify_to(notifiable, options = {})
|
78
143
|
Notification.notify_to(self, notifiable, options)
|
79
144
|
end
|
80
145
|
|
81
|
-
|
82
|
-
|
146
|
+
# Opens all notifications of this target.
|
147
|
+
# This method calls NotificationApi#open_all_of internally with self target instance.
|
148
|
+
# @see NotificationApi#open_all_of
|
149
|
+
#
|
150
|
+
# @param [Hash] options Options for opening notifications
|
151
|
+
# @option options [DateTime] :opened_at (DateTime.now) Time to set to opened_at of the notification record
|
152
|
+
# @return [Integer] Number of opened notification records
|
153
|
+
# @todo Add filter option
|
154
|
+
def open_all_notifications(options = {})
|
155
|
+
Notification.open_all_of(self, options)
|
83
156
|
end
|
84
157
|
|
85
158
|
|
86
|
-
#
|
87
|
-
|
88
|
-
#
|
159
|
+
# Gets automatically arranged notification index of the target with included attributes like target, notifiable, group and notifier.
|
160
|
+
# This method is the typical way to get notifications index from controller of view.
|
161
|
+
#
|
162
|
+
# @example Get automatically arranged notification index of the @user with included attributes
|
163
|
+
# @notifications = @user.notification_index_with_attributes
|
164
|
+
#
|
165
|
+
# @param [Integer] limit Limit to query for notifications
|
166
|
+
# @return [Array<Notificaion>] Notification index of the target with attributes
|
89
167
|
def notification_index_with_attributes(limit = nil)
|
90
168
|
# When the target have unopened notifications
|
91
169
|
unopened_notification_index.exists? ?
|
@@ -97,25 +175,46 @@ module ActivityNotification
|
|
97
175
|
opened_notification_index_with_attributes
|
98
176
|
end
|
99
177
|
|
178
|
+
# Gets unopened notification index of the target with included attributes like target, notifiable, group and notifier.
|
179
|
+
#
|
180
|
+
# @example Get unopened notification index of the @user with included attributes
|
181
|
+
# @notifications = @user.unopened_notification_index_with_attributes
|
182
|
+
#
|
183
|
+
# @param [Integer] limit Limit to query for notifications
|
184
|
+
# @return [Array<Notificaion>] Unopened notification index of the target with attributes
|
100
185
|
def unopened_notification_index_with_attributes(limit = nil)
|
101
|
-
|
102
|
-
Notification.group_member_exists?(unopened_notification_index(limit)) ?
|
103
|
-
unopened_notification_index(limit).with_target.with_notifiable.with_group.with_notifier :
|
104
|
-
unopened_notification_index(limit).with_target.with_notifiable.with_notifier
|
105
|
-
else
|
106
|
-
notifications.none
|
107
|
-
end
|
186
|
+
include_attributes unopened_notification_index(limit)
|
108
187
|
end
|
109
188
|
|
189
|
+
# Gets opened notification index of the target with including attributes like target, notifiable, group and notifier.
|
190
|
+
#
|
191
|
+
# @example Get opened notification index of the @user with included attributes
|
192
|
+
# @notifications = @user.opened_notification_index_with_attributes(10)
|
193
|
+
#
|
194
|
+
# @param [Integer] limit Limit to query for notifications
|
195
|
+
# @return [Array<Notificaion>] Opened notification index of the target with attributes
|
110
196
|
def opened_notification_index_with_attributes(limit = ActivityNotification.config.opened_limit)
|
111
|
-
|
112
|
-
Notification.group_member_exists?(opened_notification_index(limit)) ?
|
113
|
-
opened_notification_index(limit).with_target.with_notifiable.with_group.with_notifier :
|
114
|
-
opened_notification_index(limit).with_target.with_notifiable.with_notifier
|
115
|
-
else
|
116
|
-
notifications.none
|
117
|
-
end
|
197
|
+
include_attributes opened_notification_index(limit)
|
118
198
|
end
|
119
199
|
|
200
|
+
private
|
201
|
+
|
202
|
+
# Includes attributes like target, notifiable, group or notifier from the notification index.
|
203
|
+
# When group member exists in the notification index, group will be included in addition to target, notifiable and or notifier.
|
204
|
+
# Otherwise, target, notifiable and or notifier will be include without group.
|
205
|
+
# @api private
|
206
|
+
#
|
207
|
+
# @param [Array<Notificaion>] notification_index Notification index
|
208
|
+
# @return [Array<Notificaion>] Notification index with attributes
|
209
|
+
def include_attributes(notification_index)
|
210
|
+
if notification_index.present?
|
211
|
+
Notification.group_member_exists?(notification_index) ?
|
212
|
+
notification_index.with_target.with_notifiable.with_group.with_notifier :
|
213
|
+
notification_index.with_target.with_notifiable.with_notifier
|
214
|
+
else
|
215
|
+
notifications.none
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
120
219
|
end
|
121
220
|
end
|
@@ -1,17 +1,43 @@
|
|
1
1
|
module ActivityNotification
|
2
|
+
# Notification model implementation generated by ActivityNotification.
|
2
3
|
class Notification < ActiveRecord::Base
|
3
4
|
include Renderable
|
4
5
|
include Common
|
5
6
|
include NotificationApi
|
6
7
|
self.table_name = ActivityNotification.config.table_name
|
7
8
|
|
9
|
+
# Belongs to target instance of this notification as polymorphic association.
|
10
|
+
# @scope instance
|
11
|
+
# @return [Object] Target instance of this notification
|
8
12
|
belongs_to :target, polymorphic: true
|
13
|
+
|
14
|
+
# Belongs to notifiable instance of this notification as polymorphic association.
|
15
|
+
# @scope instance
|
16
|
+
# @return [Object] Notifiable instance of this notification
|
9
17
|
belongs_to :notifiable, polymorphic: true
|
18
|
+
|
19
|
+
# Belongs to group instance of this notification as polymorphic association.
|
20
|
+
# @scope instance
|
21
|
+
# @return [Object] Group instance of this notification
|
10
22
|
belongs_to :group, polymorphic: true
|
23
|
+
|
24
|
+
# Belongs to group owner notification instance of this notification.
|
25
|
+
# Only group member instance has :group_owner value.
|
26
|
+
# Group owner instance has nil as :group_owner association.
|
27
|
+
# @scope instance
|
28
|
+
# @return [Notification] Group owner notification instance of this notification
|
11
29
|
belongs_to :group_owner, class_name: :Notification
|
12
|
-
|
13
|
-
|
14
|
-
|
30
|
+
|
31
|
+
# Has many group member notification instances of this notification.
|
32
|
+
# Only group owner instance has :group_members value.
|
33
|
+
# Group member instance has nil as :group_members association.
|
34
|
+
# @scope instance
|
35
|
+
# @return [Array<Notificaion>] Array or database query of the group member notification instances of this notification
|
36
|
+
has_many :group_members, class_name: :Notification, foreign_key: :group_owner_id
|
37
|
+
|
38
|
+
# Belongs to :otifier instance of this notification.
|
39
|
+
# @scope instance
|
40
|
+
# @return [Object] Notifier instance of this notification
|
15
41
|
belongs_to :notifier, polymorphic: true
|
16
42
|
|
17
43
|
# Serialize parameters Hash
|
@@ -21,30 +47,168 @@ module ActivityNotification
|
|
21
47
|
validates :notifiable, presence: true
|
22
48
|
validates :key, presence: true
|
23
49
|
|
50
|
+
# Selects group owner notifications only.
|
51
|
+
# @scope class
|
52
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
24
53
|
scope :group_owners_only, -> { where(group_owner_id: nil) }
|
54
|
+
|
55
|
+
# Selects group member notifications only.
|
56
|
+
# @scope class
|
57
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
25
58
|
scope :group_members_only, -> { where.not(group_owner_id: nil) }
|
59
|
+
|
60
|
+
# Selects unopened notifications only.
|
61
|
+
# @scope class
|
62
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
26
63
|
scope :unopened_only, -> { where(opened_at: nil) }
|
64
|
+
|
65
|
+
# Selects unopened notification index.
|
66
|
+
# Defined same as `unopened_only.group_owners_only.latest_order`.
|
67
|
+
# @example Get unopened notificaton index of the @user
|
68
|
+
# @notifications = @user.unopened_index
|
69
|
+
# @scope class
|
70
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
27
71
|
scope :unopened_index, -> { unopened_only.group_owners_only.latest_order }
|
72
|
+
|
73
|
+
# Selects opened notifications only without limit.
|
74
|
+
# Be careful to get too many records with this method.
|
75
|
+
# @scope class
|
76
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
28
77
|
scope :opened_only!, -> { where.not(opened_at: nil) }
|
78
|
+
|
79
|
+
# Selects opened notifications only with limit.
|
80
|
+
# @scope class
|
81
|
+
# @param [Integer] limit Limit to query for opened notifications
|
82
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
29
83
|
scope :opened_only, ->(limit) { opened_only!.limit(limit) }
|
84
|
+
|
85
|
+
# Selects unopened notification index.
|
86
|
+
# Defined same as `opened_only(limit).group_owners_only.latest_order`.
|
87
|
+
# @scope class
|
88
|
+
# @param [Integer] limit Limit to query for opened notifications
|
89
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
30
90
|
scope :opened_index, ->(limit) { opened_only(limit).group_owners_only.latest_order }
|
91
|
+
|
92
|
+
# Selects group member notifications in unopened_index.
|
93
|
+
# @scope class
|
94
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
31
95
|
scope :unopened_index_group_members_only, -> { where(group_owner_id: unopened_index.pluck(:id)) }
|
32
|
-
scope :opened_index_group_members_only, ->(limit) { where(group_owner_id: opened_index(limit).pluck(:id)) }
|
33
96
|
|
97
|
+
# Selects group member notifications in opened_index.
|
98
|
+
# @scope class
|
99
|
+
# @param [Integer] limit Limit to query for opened notifications
|
100
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
101
|
+
scope :opened_index_group_members_only, ->(limit) { where(group_owner_id: opened_index(limit).map(&:id)) }
|
102
|
+
|
103
|
+
# Selects filtered notifications by target instance.
|
104
|
+
# ActivityNotification::Notification.filtered_by_target(@user)
|
105
|
+
# is the same as
|
106
|
+
# @user.notifications
|
107
|
+
# @scope class
|
108
|
+
# @param [Object] target Target instance for filter
|
109
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
34
110
|
scope :filtered_by_target, ->(target) { where(target: target) }
|
111
|
+
|
112
|
+
# Selects filtered notifications by notifiable instance.
|
113
|
+
# @example Get filtered unopened notificatons of the @user for @comment as notifiable
|
114
|
+
# @notifications = @user.notifications.unopened_only.filtered_by_instance(@comment)
|
115
|
+
# @scope class
|
116
|
+
# @param [Object] notifiable Notifiable instance for filter
|
117
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
35
118
|
scope :filtered_by_instance, ->(notifiable) { where(notifiable: notifiable) }
|
119
|
+
|
120
|
+
# Selects filtered notifications by notifiable_type.
|
121
|
+
# @example Get filtered unopened notificatons of the @user for Comment notifiable class
|
122
|
+
# @notifications = @user.notifications.unopened_only.filtered_by_type('Comment')
|
123
|
+
# @scope class
|
124
|
+
# @param [String] notifiable_type Notifiable type for filter
|
125
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
36
126
|
scope :filtered_by_type, ->(notifiable_type) { where(notifiable_type: notifiable_type) }
|
127
|
+
|
128
|
+
# Selects filtered notifications by group instance.
|
129
|
+
# @example Get filtered unopened notificatons of the @user for @article as group
|
130
|
+
# @notifications = @user.notifications.unopened_only.filtered_by_group(@article)
|
131
|
+
# @scope class
|
132
|
+
# @param [Object] group Group instance for filter
|
133
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
37
134
|
scope :filtered_by_group, ->(group) { where(group: group) }
|
135
|
+
|
136
|
+
# Selects filtered notifications by key.
|
137
|
+
# @example Get filtered unopened notificatons of the @user with key 'comment.reply'
|
138
|
+
# @notifications = @user.notifications.unopened_only.filtered_by_key('comment.reply')
|
139
|
+
# @scope class
|
140
|
+
# @param [String] key Key of the notification for filter
|
141
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
38
142
|
scope :filtered_by_key, ->(key) { where(key: key) }
|
39
143
|
|
144
|
+
# Selects filtered notifications by notifiable_type, group or key with filter options.
|
145
|
+
# @example Get filtered unopened notificatons of the @user for Comment notifiable class
|
146
|
+
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_type: 'Comment' })
|
147
|
+
# @example Get filtered unopened notificatons of the @user for @article as group
|
148
|
+
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_group: @article })
|
149
|
+
# @example Get filtered unopened notificatons of the @user for Article instance id=1 as group
|
150
|
+
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_group_type: 'Article', filtered_by_group_id: '1' })
|
151
|
+
# @example Get filtered unopened notificatons of the @user with key 'comment.reply'
|
152
|
+
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_key: 'comment.reply' })
|
153
|
+
# @example Get filtered unopened notificatons of the @user for Comment notifiable class with key 'comment.reply'
|
154
|
+
# @notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_type: 'Comment', filtered_by_key: 'comment.reply' })
|
155
|
+
# @scope class
|
156
|
+
# @param [Hash] options Options for filter
|
157
|
+
# @option options [String] :filtered_by_type (nil) Notifiable type for filter
|
158
|
+
# @option options [Object] :filtered_by_group (nil) Group instance for filter
|
159
|
+
# @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
|
160
|
+
# @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
|
161
|
+
# @option options [String] :filtered_by_key (nil) Key of the notification for filter
|
162
|
+
# @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
|
163
|
+
scope :filtered_by_options, ->(options = {}) {
|
164
|
+
options = options.with_indifferent_access
|
165
|
+
filtered_notifications = all
|
166
|
+
if options.has_key?(:filtered_by_type)
|
167
|
+
filtered_notifications = filtered_notifications.filtered_by_type(options[:filtered_by_type])
|
168
|
+
end
|
169
|
+
if options.has_key?(:filtered_by_group)
|
170
|
+
filtered_notifications = filtered_notifications.filtered_by_group(options[:filtered_by_group])
|
171
|
+
end
|
172
|
+
if options.has_key?(:filtered_by_group_type) and options.has_key?(:filtered_by_group_id)
|
173
|
+
filtered_notifications = filtered_notifications
|
174
|
+
.where(group_type: options[:filtered_by_group_type], group_id: options[:filtered_by_group_id])
|
175
|
+
end
|
176
|
+
if options.has_key?(:filtered_by_key)
|
177
|
+
filtered_notifications = filtered_notifications.filtered_by_key(options[:filtered_by_key])
|
178
|
+
end
|
179
|
+
filtered_notifications
|
180
|
+
}
|
181
|
+
|
182
|
+
# Includes target instance with query for notifications.
|
183
|
+
# @return [ActiveRecord_AssociationRelation] Database query of notifications with target
|
40
184
|
scope :with_target, -> { includes(:target) }
|
185
|
+
|
186
|
+
# Includes notifiable instance with query for notifications.
|
187
|
+
# @return [ActiveRecord_AssociationRelation] Database query of notifications with notifiable
|
41
188
|
scope :with_notifiable, -> { includes(:notifiable) }
|
189
|
+
|
190
|
+
# Includes group instance with query for notifications.
|
191
|
+
# @return [ActiveRecord_AssociationRelation] Database query of notifications with group
|
42
192
|
scope :with_group, -> { includes(:group) }
|
193
|
+
|
194
|
+
# Includes notifier instance with query for notifications.
|
195
|
+
# @return [ActiveRecord_AssociationRelation] Database query of notifications with notifier
|
43
196
|
scope :with_notifier, -> { includes(:notifier) }
|
44
197
|
|
198
|
+
# Orders by latest (newest) first as created_at: :desc.
|
199
|
+
# @return [ActiveRecord_AssociationRelation] Database query of notifications ordered by latest first
|
45
200
|
scope :latest_order, -> { order(created_at: :desc) }
|
201
|
+
|
202
|
+
# Orders by earliest (older) first as created_at: :asc.
|
203
|
+
# @return [ActiveRecord_AssociationRelation] Database query of notifications ordered by earliest first
|
46
204
|
scope :earliest_order, -> { order(created_at: :asc) }
|
205
|
+
|
206
|
+
# Returns latest notification instance.
|
207
|
+
# @return [Notification] Latest notification instance
|
47
208
|
scope :latest, -> { latest_order.first }
|
209
|
+
|
210
|
+
# Returns earliest notification instance.
|
211
|
+
# @return [Notification] Earliest notification instance
|
48
212
|
scope :earliest, -> { earliest_order.first }
|
49
213
|
end
|
50
214
|
end
|