simple_notifications 1.0.3 → 1.0.4
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/README.md +10 -1
- data/lib/generators/simple_notifications/copy_models/USAGE +10 -0
- data/lib/generators/simple_notifications/copy_models/copy_models_generator.rb +13 -0
- data/lib/generators/simple_notifications/install/copy_models_generator.rb +13 -0
- data/lib/simple_notifications/app/models/delivery.rb +13 -2
- data/lib/simple_notifications/app/models/simple_notification.rb +27 -9
- data/lib/simple_notifications/base.rb +15 -4
- data/lib/simple_notifications/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 637fcdccc7f7369aee6e40e286087ac074f5a373fcea81268620e110405542a8
|
4
|
+
data.tar.gz: 04046a239fffa98a0dc094cf5efeaed8cb259b5459e234ab1fa54c5e26f78834
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c95d4692facc8a87180ccbdeaff38cefc1e418c36974fef15a07815ecf729eff29f67a8411c66a88ec5263e8dcbf98b0a5ce169c7ddbb7d51d956b75a8f0663
|
7
|
+
data.tar.gz: d91290995506e71974a2aead19fe7f30c46cf9c2f63c5729974dbb4a35f8be61020f9945d115fd52a36d829a733159c4d3e0542123acffac6d842cee11aabf3c
|
data/README.md
CHANGED
@@ -39,7 +39,10 @@ rails db:migrate
|
|
39
39
|
Add following line to the model for which notifications functionality is required
|
40
40
|
|
41
41
|
```ruby
|
42
|
-
notify sender: :author,
|
42
|
+
notify sender: :author,
|
43
|
+
receivers: :followers,
|
44
|
+
before_notify: proc {puts 'Before Notifications actions'},
|
45
|
+
after_notify: proc {puts 'Deliver Push notifications'}
|
43
46
|
```
|
44
47
|
Or you can provide ActiveRecord::Base object or ActiveRecord::Relation objects as
|
45
48
|
|
@@ -120,6 +123,12 @@ Post.create(content: '123', notify: false)
|
|
120
123
|
Post.create(content: '123', message: 'My custom notification message')
|
121
124
|
```
|
122
125
|
|
126
|
+
### Generators
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
rails generate simple_notifications:copy_models
|
130
|
+
```
|
131
|
+
|
123
132
|
## Contributing
|
124
133
|
|
125
134
|
Bug reports and pull requests are welcome on GitHub at https://github.com/aashishgarg/simple_notifications.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Description:
|
2
|
+
1. Creates an initializer file for simple_notifications.
|
3
|
+
2. Creates a migration file file for recording the data of notifications receiver and sender.
|
4
|
+
|
5
|
+
Example:
|
6
|
+
rails generate simple_notifications:install
|
7
|
+
|
8
|
+
This will create:
|
9
|
+
config/simple_notifications.rb
|
10
|
+
db/migrate/{time_stamp}_create_simple_notifications.rb
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SimpleNotifications
|
2
|
+
class CopyModelsGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../../../simple_notifications/app/models', __dir__)
|
4
|
+
|
5
|
+
def copy_notification_model
|
6
|
+
copy_file "simple_notification.rb", "app/models/simple_notification.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
def copy_delivery_model
|
10
|
+
copy_file "delivery.rb", "app/models/delivery.rb"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SimpleNotifications
|
2
|
+
class CopyModelsGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../../../lib/simple_notifications/app/models', __dir__)
|
4
|
+
|
5
|
+
def copy_notification_model
|
6
|
+
copy_file "simple_notifications.rb", "app/models/simple_notifications.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
def copy_delivery_model
|
10
|
+
copy_file "deliver.rb", "app/models/deliver.rb"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -2,8 +2,19 @@ module SimpleNotifications
|
|
2
2
|
class Delivery < ActiveRecord::Base
|
3
3
|
self.table_name = 'deliveries'
|
4
4
|
|
5
|
-
#
|
6
|
-
belongs_to :simple_notification,
|
5
|
+
# Associations
|
6
|
+
belongs_to :simple_notification,
|
7
|
+
class_name: 'SimpleNotifications::Record',
|
8
|
+
inverse_of: :deliveries
|
7
9
|
belongs_to :receiver, polymorphic: true
|
10
|
+
|
11
|
+
# Callbacks
|
12
|
+
after_create_commit :after_create_actions
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def after_create_actions
|
17
|
+
|
18
|
+
end
|
8
19
|
end
|
9
20
|
end
|
@@ -2,28 +2,46 @@ module SimpleNotifications
|
|
2
2
|
class Record < ActiveRecord::Base
|
3
3
|
self.table_name = 'simple_notifications'
|
4
4
|
|
5
|
-
#
|
5
|
+
# Class Attribute Accessors
|
6
|
+
cattr_accessor :before_notify, :after_notify
|
7
|
+
|
8
|
+
# Associations
|
6
9
|
belongs_to :sender, polymorphic: true
|
7
10
|
belongs_to :entity, polymorphic: true
|
8
|
-
has_many :deliveries, class_name: 'SimpleNotifications::Delivery',
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
has_many :
|
13
|
-
|
11
|
+
has_many :deliveries, class_name: 'SimpleNotifications::Delivery',
|
12
|
+
inverse_of: :simple_notification,
|
13
|
+
foreign_key: :simple_notification_id,
|
14
|
+
dependent: :destroy
|
15
|
+
has_many :read_deliveries, -> {where(is_read: true)},
|
16
|
+
class_name: 'SimpleNotifications::Delivery',
|
17
|
+
inverse_of: :simple_notification,
|
18
|
+
foreign_key: :simple_notification_id,
|
19
|
+
dependent: :destroy
|
20
|
+
has_many :unread_deliveries, -> {where(is_read: false)},
|
21
|
+
class_name: 'SimpleNotifications::Delivery',
|
22
|
+
inverse_of: :simple_notification,
|
23
|
+
foreign_key: :simple_notification_id,
|
24
|
+
dependent: :destroy
|
14
25
|
|
26
|
+
# Scopes
|
15
27
|
scope :read, -> {where(is_read: true)}
|
16
28
|
scope :unread, -> {where.not(is_read: true)}
|
17
29
|
|
18
|
-
#
|
30
|
+
# Validations
|
19
31
|
validates :message, presence: true, length: {minimum: 1, maximum: 199}
|
20
32
|
|
21
|
-
#
|
33
|
+
# Callbacks
|
34
|
+
before_create :before_actions
|
22
35
|
after_create_commit :after_actions
|
23
36
|
|
24
37
|
private
|
25
38
|
|
39
|
+
def before_actions
|
40
|
+
SimpleNotifications::Record.before_notify.call if !!SimpleNotifications::Record.before_notify
|
41
|
+
end
|
42
|
+
|
26
43
|
def after_actions
|
44
|
+
SimpleNotifications::Record.after_notify.call if !!SimpleNotifications::Record.after_notify
|
27
45
|
end
|
28
46
|
end
|
29
47
|
end
|
@@ -7,12 +7,14 @@ module SimpleNotifications
|
|
7
7
|
!!@@notified_flag
|
8
8
|
end
|
9
9
|
|
10
|
-
#
|
11
|
-
#
|
10
|
+
#Example
|
11
|
+
#notify(sender: :author, receivers: :followers)
|
12
12
|
def notify(options = {})
|
13
13
|
@@entity_class = self
|
14
14
|
@@sender = options[:sender]
|
15
15
|
@@receivers = options[:receivers]
|
16
|
+
SimpleNotifications::Record.before_notify = options[:before_notify] if !!options[:before_notify]
|
17
|
+
SimpleNotifications::Record.after_notify = options[:after_notify] if !!options[:after_notify]
|
16
18
|
|
17
19
|
open_sender_class
|
18
20
|
open_receiver_class
|
@@ -21,6 +23,7 @@ module SimpleNotifications
|
|
21
23
|
@@notified_flag = true
|
22
24
|
end
|
23
25
|
|
26
|
+
# Opening the class which is defined as sender.
|
24
27
|
def open_sender_class
|
25
28
|
# Define association for sender model
|
26
29
|
sender_class(@@sender).class_eval do
|
@@ -28,6 +31,7 @@ module SimpleNotifications
|
|
28
31
|
end
|
29
32
|
end
|
30
33
|
|
34
|
+
# Opening the classes which are defined as receivers.
|
31
35
|
def open_receiver_class
|
32
36
|
# Define association for receiver model
|
33
37
|
[receivers_class(@@receivers)].flatten.each do |base|
|
@@ -38,6 +42,7 @@ module SimpleNotifications
|
|
38
42
|
end
|
39
43
|
end
|
40
44
|
|
45
|
+
# Opening the class on which the notify functionality is applied.
|
41
46
|
def open_notified_class
|
42
47
|
class_eval do
|
43
48
|
attr_accessor :message, :notify
|
@@ -45,6 +50,7 @@ module SimpleNotifications
|
|
45
50
|
has_many :notifications, class_name: 'SimpleNotifications::Record', as: :entity
|
46
51
|
has_many :notifiers, through: :notifications, source: :sender, source_type: sender_class(@@sender).to_s
|
47
52
|
|
53
|
+
# Opening the Notification class.
|
48
54
|
SimpleNotifications::Record.class_eval do
|
49
55
|
[@@entity_class.receivers_class(@@receivers)].flatten.each do |receiver_class|
|
50
56
|
has_many "#{receiver_class.name.downcase}_receivers".to_sym,
|
@@ -60,12 +66,13 @@ module SimpleNotifications
|
|
60
66
|
source: "#{receiver_class.name.downcase}_receivers".to_sym
|
61
67
|
end
|
62
68
|
|
63
|
-
# has_many :notificants, through: :notifications, source: :receivers
|
64
69
|
has_many :read_deliveries, through: :notifications, source: :read_deliveries
|
65
70
|
has_many :unread_deliveries, through: :notifications, source: :unread_deliveries
|
71
|
+
# has_many :notificants, through: :notifications, source: :receivers
|
66
72
|
# has_many :read_notificants, through: :read_deliveries, source: :receiver, source_type: 'User'
|
67
73
|
# has_many :unread_notificants, through: :unread_deliveries, source: :receiver, source_type: 'User'
|
68
74
|
|
75
|
+
# Callbacks
|
69
76
|
after_create_commit :create_notification, if: proc {@notify.nil? || !!@notify}
|
70
77
|
after_update_commit :update_notification, if: proc {@notify.nil? || !!@notify}
|
71
78
|
|
@@ -74,7 +81,9 @@ module SimpleNotifications
|
|
74
81
|
!notifications.blank?
|
75
82
|
end
|
76
83
|
|
77
|
-
#
|
84
|
+
#Example
|
85
|
+
#post.notify(sender: :author, receivers: :followers, message: 'My Custom logic message')
|
86
|
+
#post.create(content: '', notify: false) -> It does not create the notification.
|
78
87
|
def notify(options = {})
|
79
88
|
raise 'SimpleNotification::SenderReceiverError' unless options[:sender] && options[:receivers]
|
80
89
|
@message = options[:message] if options[:message]
|
@@ -133,6 +142,7 @@ module SimpleNotifications
|
|
133
142
|
end
|
134
143
|
end
|
135
144
|
|
145
|
+
# Provides the class of Sender
|
136
146
|
def sender_class(sender)
|
137
147
|
if sender.kind_of? Symbol
|
138
148
|
reflections[sender.to_s].klass
|
@@ -143,6 +153,7 @@ module SimpleNotifications
|
|
143
153
|
end
|
144
154
|
end
|
145
155
|
|
156
|
+
# Provides the classes of Receivers
|
146
157
|
def receivers_class(receivers)
|
147
158
|
if receivers.kind_of? Symbol
|
148
159
|
reflections[receivers.to_s].klass
|
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.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ashish Garg
|
@@ -71,7 +71,10 @@ files:
|
|
71
71
|
- Rakefile
|
72
72
|
- bin/console
|
73
73
|
- bin/setup
|
74
|
+
- lib/generators/simple_notifications/copy_models/USAGE
|
75
|
+
- lib/generators/simple_notifications/copy_models/copy_models_generator.rb
|
74
76
|
- lib/generators/simple_notifications/install/USAGE
|
77
|
+
- lib/generators/simple_notifications/install/copy_models_generator.rb
|
75
78
|
- lib/generators/simple_notifications/install/install_generator.rb
|
76
79
|
- lib/generators/simple_notifications/install/templates/initializer.rb
|
77
80
|
- lib/generators/simple_notifications/install/templates/migration.rb
|