simple_notifications 1.1.7 → 1.1.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90315568f413c25998d564ad043f6895501daa5b727aff493d455ac5e26161af
4
- data.tar.gz: fa75064e52508aca390b5e27b3fb2240b01852e77d8bd7aaac4cb4caac7f9e50
3
+ metadata.gz: ac34511b2c3da57df59fcf6a5d31d4a3e108d45a93b56d78acae05b032303311
4
+ data.tar.gz: 3daf3195e073ed5117b8bda4f5cc14fa00b404b68934fbb7ff7348bea357c308
5
5
  SHA512:
6
- metadata.gz: b27dab03d2c9cd13721808afd29a97e1408cc2229621601fabeaa604d2cbb0bde72f71e9d8f45271ffa328cced8ed78302630dcf0e25d38508597ef6ddf9ddb6
7
- data.tar.gz: e6950c143a3b4098ab13ffce25bef39e44e0a89a5334f12dc617b388b0117277da94f2d09898490baa2411485d88ed2065815c7f48b0a6b1e7112a38f661df44
6
+ metadata.gz: 4571f8e0080aa3592c5c30a22b09fe6a0f3d4336144268616bd3b922e6ecec64ffa3b71049628ac5faa6ee35c286876e7295aebe0f732a467d72f42c006a8360
7
+ data.tar.gz: eef1b1bbe353c76239379b739c73763b794c455e0388a10d233961a081a8a69b68870eede03f0bbdc39bd6995ee7ad2c349e6e26e92dcd5cd46a8944c2be09c8
@@ -3,6 +3,7 @@ class CreateSimpleNotifications < ActiveRecord::Migration[5.2]
3
3
  create_table(:simple_notifications) do |t|
4
4
  t.references :sender, polymorphic: true
5
5
  t.references :entity, polymorphic: true
6
+ t.string :action
6
7
  t.string :message
7
8
 
8
9
  t.timestamps
@@ -1,33 +1,42 @@
1
1
  module SimpleNotifications
2
2
  module Base
3
- mattr_accessor :options, :notified_flag
4
-
5
- #Example
6
- #notify(sender: :author, receivers: :followers)
7
- #notify sender: :product_class,
8
- # receivers: :variants,
9
- # action: [:follow, :update, :create, :destroy]
10
- def notify(options = {})
11
- @@options = options
12
- @@options[:entity_class] = self
13
- @@options[:callbacks] = []
14
- [:create, :update, :destroy].each {|_method| @@options[:callbacks] << @@options[:actions].delete(_method)}
15
- @@options[:callbacks].compact!
3
+ mattr_accessor :options, :active
4
+
5
+ # notify sender: :product_class,
6
+ # receivers: :variants,
7
+ # action: [:follow, :update, :create, :destroy]
8
+ def acts_as_notifier(options = {})
9
+ Base.options = options
10
+ raise 'SimpleNotifications::ActionsNotAvailable' if Base.options[:actions].nil? || Base.options[:actions].empty?
11
+
12
+ # Default Assumptions
13
+ Base.options[:sender] ||= :user # Default Assumption: [:user] association is available.
14
+ Base.options[:entity_class] = self
15
+
16
+ # Filter system defined callback actions
17
+ # After this
18
+ # Base.options[:callbacks] -> will have all system defined actions only like (create/update/destroy)
19
+ # Base.options[:actions] -> will have user defined actions only.
20
+ Base.options[:callbacks] = []
21
+ %i[create update destroy].each {|method| Base.options[:callbacks] << Base.options[:actions].delete(method)}
22
+ Base.options[:callbacks].compact!
23
+
16
24
  open_sender_class
17
25
  open_receiver_class
18
26
  open_notified_class
19
- @@notified_flag = true
27
+
28
+ Base.active = true
20
29
  end
21
30
 
22
- #Getter to check if model is enabled with notifications
23
- def notified?
24
- !!@@notified_flag
31
+ # Getter to check if model is enabled with notifications
32
+ def notifications_active?
33
+ !!Base.active
25
34
  end
26
35
 
27
36
  # Opening the class which is defined as sender.
28
37
  def open_sender_class
29
38
  # Define association for sender model
30
- sender_class(@@options[:sender]).class_eval do
39
+ sender_class(Base.options[:sender]).class_eval do
31
40
  has_many :sent_notifications, class_name: 'SimpleNotifications::Record', as: :sender
32
41
  end
33
42
  end
@@ -35,7 +44,7 @@ module SimpleNotifications
35
44
  # Opening the classes which are defined as receivers.
36
45
  def open_receiver_class
37
46
  # Define association for receiver model
38
- [receivers_class(@@options[:receivers])].flatten.each do |base|
47
+ [receivers_class(Base.options[:receivers])].flatten.each do |base|
39
48
  base.class_eval do
40
49
  has_many :deliveries, class_name: 'SimpleNotifications::Delivery', as: :receiver
41
50
  has_many :received_notifications, through: :deliveries, source: :simple_notification
@@ -46,22 +55,54 @@ module SimpleNotifications
46
55
  # Opening the class on which the notify functionality is applied.
47
56
  def open_notified_class
48
57
  class_eval do
49
- prepend NotificationActions
58
+ prepend NotificationActions # For adding all user defined methods at the last level of present class
50
59
  attr_accessor :message, :notify_flag
51
60
 
61
+ # -------------------------------------------------- #
52
62
  # Define association for the notified model
63
+ # -------------------------------------------------- #
53
64
  has_many :notifications, class_name: 'SimpleNotifications::Record', as: :entity
54
- has_many :notifiers, through: :notifications, source: :sender, source_type: sender_class(@@options[:sender]).to_s
65
+ has_many :notifiers, through: :notifications, source: :sender,
66
+ source_type: sender_class(Base.options[:sender]).to_s
55
67
  has_many :read_deliveries, through: :notifications, source: :read_deliveries
56
68
  has_many :unread_deliveries, through: :notifications, source: :unread_deliveries
57
69
 
70
+ # -------------------------------------------------- #
71
+ # For defining the System defined callbacks
72
+ # -------------------------------------------------- #
73
+
58
74
  # Callbacks
59
- after_create_commit :create_notification, if: proc {@@options[:callbacks].include?(:create)}
60
- after_update_commit :update_notification, if: proc {@@options[:callbacks].include?(:update)}
61
- after_destroy_commit :destroy_notification, if: proc {@@options[:callbacks].include?(:destroy)}
75
+ # after_create_commit :create_notification
76
+ Base.options[:callbacks].each do |callback|
77
+ send("after_#{callback}_commit", "#{callback}_notification")
78
+ end
79
+
80
+ def create_notification
81
+ notify(sender: get_obj(Base.options[:sender]),
82
+ receivers: get_obj(Base.options[:receivers]),
83
+ action: 'create',
84
+ message: default_message(self, get_obj(Base.options[:sender]), 'created'))
85
+ end
86
+
87
+ def update_notification
88
+ notify(sender: get_obj(Base.options[:sender]),
89
+ receivers: get_obj(Base.options[:receivers]),
90
+ action: 'update',
91
+ message: default_message(self, get_obj(Base.options[:sender]), 'updated'))
92
+ end
93
+
94
+ def destroy_notification
95
+ notify(sender: get_obj(Base.options[:sender]),
96
+ receivers: get_obj(Base.options[:receivers]),
97
+ action: 'destroy',
98
+ message: default_message(self, get_obj(Base.options[:sender]), 'destroyed'))
99
+ end
62
100
 
101
+ # -------------------------------------------------- #
102
+ # For defining the callbacks for user defined actions
103
+ # -------------------------------------------------- #
63
104
  NotificationActions.module_eval do
64
- @@options[:actions].each do |action|
105
+ Base.options[:actions].each do |action|
65
106
  define_method(action) do
66
107
  run_callbacks action do
67
108
  super()
@@ -72,27 +113,35 @@ module SimpleNotifications
72
113
  end
73
114
 
74
115
  define_method("after_#{action}".to_sym) do
75
- self.notify(sender: @@options[:sender], receivers: @@options[:receivers], message: default_message(self, @@options[:sender], action.to_s))
116
+ notify(sender: Base.options[:sender],
117
+ receivers: Base.options[:receivers],
118
+ action: action,
119
+ message: default_message(self, Base.options[:sender], action.to_s))
76
120
  end
77
121
  end
78
122
  end
79
123
 
80
- @@options[:actions].each do |action|
124
+ Base.options[:actions].each do |action|
81
125
  define_model_callbacks action
82
126
  send("before_#{action}", "before_#{action}".to_sym)
83
127
  send("after_#{action}", "after_#{action}".to_sym)
84
128
  end
85
129
 
86
- #Example
87
- #post.notify(sender: :author, receivers: :followers, message: 'My Custom logic message')
88
- #post.create(content: '', notify: false) -> It does not create the notification.
130
+ # -------------------------------------------------- #
131
+ # Example
132
+ # post.notify(sender: :author, receivers: :followers, message: 'My Custom logic message')
133
+ # post.create(content: '', notify: false) -> It does not create the notification.
89
134
  def notify(options = {})
90
- options[:sender] ||= @@options[:sender]
91
- options[:receivers] ||= @@options[:receivers]
135
+ options[:sender] ||= Base.options[:sender]
136
+ options[:receivers] ||= Base.options[:receivers]
92
137
  if notify_flag.nil? || (!notify_flag.nil? && !!notify_flag)
93
- raise 'SimpleNotification::SenderReceiverError' unless @@options[:sender] && @@options[:receivers]
138
+ raise 'SimpleNotification::SenderReceiverError' unless Base.options[:sender] && Base.options[:receivers]
139
+
94
140
  @message = options[:message] if options[:message]
95
- notification = notifications.build(entity: self, sender: get_obj(options[:sender]), message: default_message(self, get_obj(options[:sender]), 'created'))
141
+ notification = notifications.build(entity: self,
142
+ sender: get_obj(options[:sender]),
143
+ action: '',
144
+ message: default_message(self, get_obj(options[:sender]), 'notified'))
96
145
  [get_obj(options[:receivers])].flatten.each {|receiver| notification.deliveries.build(receiver: receiver)}
97
146
  notification.save
98
147
  end
@@ -109,21 +158,22 @@ module SimpleNotifications
109
158
 
110
159
  def notificants
111
160
  SimpleNotifications::Record.includes(deliveries: :receiver)
112
- .collect(&:deliveries).flatten
113
- .collect(&:receiver)
161
+ .collect(&:deliveries).flatten
162
+ .collect(&:receiver)
114
163
  end
115
164
 
116
165
  def read_marked_notificants
117
166
  SimpleNotifications::Record.includes(deliveries: :receiver)
118
- .collect(&:deliveries).flatten
119
- .select{|record| record.is_read}
120
- .collect(&:receiver)
167
+ .collect(&:deliveries).flatten
168
+ .select(&:is_read)
169
+ .collect(&:receiver)
121
170
  end
122
171
 
123
172
  def unread_marked_notificants
124
- SimpleNotifications::Record.includes(deliveries: :receiver)
125
- .collect(&:deliveries).flatten
126
- .select{|record| !record.is_read}.collect(&:receiver)
173
+ SimpleNotifications::Record
174
+ .includes(deliveries: :receiver)
175
+ .collect(&:deliveries).flatten
176
+ .reject {|record| record.is_read}.collect(&:receiver)
127
177
  end
128
178
 
129
179
  # Mark notifications in read mode.
@@ -141,7 +191,9 @@ module SimpleNotifications
141
191
  end
142
192
 
143
193
  def default_message(entity, sender, action)
144
- @message || (method(@@options[:notify_message]).call if !!@@options[:notify_message]) || "#{get_obj(sender).class.name} #{action} #{entity.class.name} #{entity.name}."
194
+ @message ||
195
+ (method(Base.options[:notify_message]).call if !!Base.options[:notify_message]) ||
196
+ "#{get_obj(sender).class.name} #{action} #{entity.class.name} #{entity.name}"
145
197
  end
146
198
 
147
199
  private
@@ -149,18 +201,6 @@ module SimpleNotifications
149
201
  def get_obj(sender_or_receivers)
150
202
  sender_or_receivers.kind_of?(Symbol) ? send(sender_or_receivers) : sender_or_receivers
151
203
  end
152
-
153
- def create_notification
154
- notify({sender: get_obj(@@options[:sender]), receivers: get_obj(@@options[:receivers]), message: default_message(self, get_obj(@@options[:sender]), 'created')})
155
- end
156
-
157
- def update_notification
158
- notify({sender: get_obj(@@options[:sender]), receivers: get_obj(@@options[:receivers]), message: default_message(self, get_obj(@@options[:sender]), 'updated')})
159
- end
160
-
161
- def destroy_notification
162
- notify({sender: get_obj(@@options[:sender]), receivers: get_obj(@@options[:receivers]), message: default_message(self, get_obj(@@options[:sender]), 'deleted')})
163
- end
164
204
  end
165
205
  end
166
206
 
@@ -1,3 +1,3 @@
1
1
  module SimpleNotifications
2
- VERSION = "1.1.7"
2
+ VERSION = '1.1.8'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_notifications
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.7
4
+ version: 1.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashish Garg