• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

simukappu / activity_notification / 17074615695

19 Aug 2025 03:39PM UTC coverage: 99.749%. Remained the same
17074615695

push

travis-ci

simukappu
Bump version to v2.4.0

3575 of 3584 relevant lines covered (99.75%)

1533.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/activity_notification/orm/active_record/notification.rb
1
require 'activity_notification/apis/notification_api'
6✔
2

3
module ActivityNotification
6✔
4
  module ORM
6✔
5
    module ActiveRecord
6✔
6
      # Notification model implementation generated by ActivityNotification.
7
      class Notification < ::ActiveRecord::Base
6✔
8
        include Common
6✔
9
        include Renderable
6✔
10
        include NotificationApi
6✔
11
        self.table_name = ActivityNotification.config.notification_table_name
6✔
12

13
        # Belongs to target instance of this notification as polymorphic association.
14
        # @scope instance
15
        # @return [Object] Target instance of this notification
16
        belongs_to :target,        polymorphic: true
6✔
17

18
        # Belongs to notifiable instance of this notification as polymorphic association.
19
        # @scope instance
20
        # @return [Object] Notifiable instance of this notification
21
        belongs_to :notifiable,    polymorphic: true
6✔
22

23
        # Belongs to group instance of this notification as polymorphic association.
24
        # @scope instance
25
        # @return [Object] Group instance of this notification
26
        belongs_to :group,         polymorphic: true, optional: true
6✔
27

28
        # Belongs to group owner notification instance of this notification.
29
        # Only group member instance has :group_owner value.
30
        # Group owner instance has nil as :group_owner association.
31
        # @scope instance
32
        # @return [Notification] Group owner notification instance of this notification
33
        belongs_to :group_owner,   class_name: "ActivityNotification::Notification", optional: true
6✔
34

35
        # Has many group member notification instances of this notification.
36
        # Only group owner instance has :group_members value.
37
        # Group member instance has nil as :group_members association.
38
        # @scope instance
39
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of the group member notification instances of this notification
40
        has_many   :group_members, class_name: "ActivityNotification::Notification", foreign_key: :group_owner_id
6✔
41

42
        # Belongs to :notifier instance of this notification.
43
        # @scope instance
44
        # @return [Object] Notifier instance of this notification
45
        belongs_to :notifier,      polymorphic: true, optional: true
6✔
46

47
        # Serialize parameters Hash
48
        # :nocov:
49
        if Rails.gem_version >= Gem::Version.new('7.1')
✔
50
          serialize  :parameters, type: Hash, coder: YAML
✔
51
        else
52
          serialize  :parameters, Hash
✔
53
        end
54
        # :nocov:
55

56
        validates  :target,        presence: true
6✔
57
        validates  :notifiable,    presence: true
6✔
58
        validates  :key,           presence: true
6✔
59

60
        # Selects group owner notifications only.
61
        # @scope class
62
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
63
        scope :group_owners_only,                 -> { where(group_owner_id: nil) }
18,162✔
64

65
        # Selects group member notifications only.
66
        # @scope class
67
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
68
        scope :group_members_only,                -> { where.not(group_owner_id: nil) }
30✔
69

70
        # Selects unopened notifications only.
71
        # @scope class
72
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
73
        scope :unopened_only,                     -> { where(opened_at: nil) }
17,700✔
74

75
        # Selects opened notifications only without limit.
76
        # Be careful to get too many records with this method.
77
        # @scope class
78
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
79
        scope :opened_only!,                      -> { where.not(opened_at: nil) }
2,268✔
80

81
        # Selects opened notifications only with limit.
82
        # @scope class
83
        # @param [Integer] limit Limit to query for opened notifications
84
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
85
        scope :opened_only,                       ->(limit) { opened_only!.limit(limit) }
2,088✔
86

87
        # Selects group member notifications in unopened_index.
88
        # @scope class
89
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
90
        scope :unopened_index_group_members_only, -> { where(group_owner_id: unopened_index.map(&:id)) }
12,348✔
91

92
        # Selects group member notifications in opened_index.
93
        # @scope class
94
        # @param [Integer] limit Limit to query for opened notifications
95
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
96
        scope :opened_index_group_members_only,   ->(limit) { where(group_owner_id: opened_index(limit).map(&:id)) }
720✔
97

98
        # Selects notifications within expiration.
99
        # @scope class
100
        # @param [ActiveSupport::Duration] expiry_delay Expiry period of notifications
101
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
102
        scope :within_expiration_only,            ->(expiry_delay) { where("created_at > ?", expiry_delay.ago) }
42✔
103

104
        # Selects group member notifications with specified group owner ids.
105
        # @scope class
106
        # @param [Array<String>] owner_ids Array of group owner ids
107
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
108
        scope :group_members_of_owner_ids_only,   ->(owner_ids) { where(group_owner_id: owner_ids) }
798✔
109

110
        # Selects filtered notifications by target instance.
111
        #   ActivityNotification::Notification.filtered_by_target(@user)
112
        # is the same as
113
        #   @user.notifications
114
        # @scope class
115
        # @param [Object] target Target instance for filter
116
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
117
        scope :filtered_by_target,                ->(target) { where(target: target) }
804✔
118

119
        # Selects filtered notifications by notifiable instance.
120
        # @example Get filtered unopened notificatons of the @user for @comment as notifiable
121
        #   @notifications = @user.notifications.unopened_only.filtered_by_instance(@comment)
122
        # @scope class
123
        # @param [Object] notifiable Notifiable instance for filter
124
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
125
        scope :filtered_by_instance,              ->(notifiable) { where(notifiable: notifiable) }
348✔
126

127
        # Selects filtered notifications by group instance.
128
        # @example Get filtered unopened notificatons of the @user for @article as group
129
        #   @notifications = @user.notifications.unopened_only.filtered_by_group(@article)
130
        # @scope class
131
        # @param [Object] group Group instance for filter
132
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of filtered notifications
133
        scope :filtered_by_group,                 ->(group) { where(group: group) }
894✔
134

135
        # Selects filtered notifications later than specified time.
136
        # @example Get filtered unopened notificatons of the @user later than @notification
137
        #   @notifications = @user.notifications.unopened_only.later_than(@notification.created_at)
138
        # @scope class
139
        # @param [Time] Created time of the notifications for filter
140
        # @return [ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>] Database query of filtered notifications
141
        scope :later_than,                        ->(created_time) { where('created_at > ?', created_time) }
174✔
142

143
        # Selects filtered notifications earlier than specified time.
144
        # @example Get filtered unopened notificatons of the @user earlier than @notification
145
        #   @notifications = @user.notifications.unopened_only.earlier_than(@notification.created_at)
146
        # @scope class
147
        # @param [Time] Created time of the notifications for filter
148
        # @return [ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>] Database query of filtered notifications
149
        scope :earlier_than,                      ->(created_time) { where('created_at < ?', created_time) }
216✔
150

151
        # Includes target instance with query for notifications.
152
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with target
153
        scope :with_target,                       -> { includes(:target) }
1,374✔
154

155
        # Includes notifiable instance with query for notifications.
156
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with notifiable
157
        scope :with_notifiable,                   -> { includes(:notifiable) }
762✔
158

159
        # Includes group instance with query for notifications.
160
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with group
161
        scope :with_group,                        -> { includes(:group) }
24✔
162

163
        # Includes group owner instances with query for notifications.
164
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with group owner
165
        scope :with_group_owner,                  -> { includes(:group_owner) }
12✔
166

167
        # Includes group member instances with query for notifications.
168
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with group members
169
        scope :with_group_members,                -> { includes(:group_members) }
12✔
170

171
        # Includes notifier instance with query for notifications.
172
        # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with notifier
173
        scope :with_notifier,                     -> { includes(:notifier) }
762✔
174

175
        # Raise DeleteRestrictionError for notifications.
176
        # @param [String] error_text Error text for raised exception
177
        # @raise [ActiveRecord::DeleteRestrictionError] DeleteRestrictionError from used ORM
178
        # @return [void]
179
        def self.raise_delete_restriction_error(error_text)
6✔
180
          raise ::ActiveRecord::DeleteRestrictionError.new(error_text)
6✔
181
        end
182

183
        protected
6✔
184

185
          # Returns count of group members of the unopened notification.
186
          # This method is designed to cache group by query result to avoid N+1 call.
187
          # @api protected
188
          #
189
          # @return [Integer] Count of group members of the unopened notification
190
          def unopened_group_member_count
6✔
191
            # Cache group by query result to avoid N+1 call
192
            unopened_group_member_counts = target.notifications
7,110✔
193
                                                 .unopened_index_group_members_only
194
                                                 .group(:group_owner_id)
195
                                                 .count
196
            unopened_group_member_counts[id] || 0
7,110✔
197
          end
198

199
          # Returns count of group members of the opened notification.
200
          # This method is designed to cache group by query result to avoid N+1 call.
201
          # @api protected
202
          #
203
          # @param [Integer] limit Limit to query for opened notifications
204
          # @return [Integer] Count of group members of the opened notification
205
          def opened_group_member_count(limit = ActivityNotification.config.opened_index_limit)
6✔
206
            # Cache group by query result to avoid N+1 call
207
            opened_group_member_counts   = target.notifications
378✔
208
                                                 .opened_index_group_members_only(limit)
209
                                                 .group(:group_owner_id)
210
                                                 .count
211
            count = opened_group_member_counts[id] || 0
378✔
212
            count > limit ? limit : count
378✔
213
          end
214

215
          # Returns count of group member notifiers of the unopened notification not including group owner notifier.
216
          # This method is designed to cache group by query result to avoid N+1 call.
217
          # @api protected
218
          #
219
          # @return [Integer] Count of group member notifiers of the unopened notification
220
          def unopened_group_member_notifier_count
6✔
221
            # Cache group by query result to avoid N+1 call
222
            unopened_group_member_notifier_counts = target.notifications
5,226✔
223
                                                          .unopened_index_group_members_only
224
                                                          .includes(:group_owner)
225
                                                          .where("group_owners_#{self.class.table_name}.notifier_type = #{self.class.table_name}.notifier_type")
226
                                                          .where.not("group_owners_#{self.class.table_name}.notifier_id = #{self.class.table_name}.notifier_id")
227
                                                          .references(:group_owner)
228
                                                          .group(:group_owner_id, :notifier_type)
229
                                                          .count("distinct #{self.class.table_name}.notifier_id")
230
            unopened_group_member_notifier_counts[[id, notifier_type]] || 0
5,226✔
231
          end
232

233
          # Returns count of group member notifiers of the opened notification not including group owner notifier.
234
          # This method is designed to cache group by query result to avoid N+1 call.
235
          # @api protected
236
          #
237
          # @param [Integer] limit Limit to query for opened notifications
238
          # @return [Integer] Count of group member notifiers of the opened notification
239
          def opened_group_member_notifier_count(limit = ActivityNotification.config.opened_index_limit)
6✔
240
            # Cache group by query result to avoid N+1 call
241
            opened_group_member_notifier_counts   = target.notifications
324✔
242
                                                          .opened_index_group_members_only(limit)
243
                                                          .includes(:group_owner)
244
                                                          .where("group_owners_#{self.class.table_name}.notifier_type = #{self.class.table_name}.notifier_type")
245
                                                          .where.not("group_owners_#{self.class.table_name}.notifier_id = #{self.class.table_name}.notifier_id")
246
                                                          .references(:group_owner)
247
                                                          .group(:group_owner_id, :notifier_type)
248
                                                          .count("distinct #{self.class.table_name}.notifier_id")
249
            count = opened_group_member_notifier_counts[[id, notifier_type]] || 0
324✔
250
            count > limit ? limit : count
324✔
251
          end
252

253
      end
254
    end
255
  end
256
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc