notification-handler 1.0.0.beta6 → 1.0.0.beta7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f39ea79cb44c6242c64d15816329a2c505a5b2421686c4efe106ff286e027d15
4
- data.tar.gz: 760d3aec4e2b75787db310fbf7cfd430213997d94f66403411b1ace527e0ddf1
3
+ metadata.gz: 9547328112aaa356ee69213fd899699b8b3637eacae843b554f797e40a42b01b
4
+ data.tar.gz: 81e7724af9478908387dffe2d4f73779302bbe739e3eb6bca1dd3f2474983954
5
5
  SHA512:
6
- metadata.gz: bdd9699f6be18a889bc4c3a1b1c0418b608d14dba21fbf4ecddab4098c8e99bd630cc0ea7341564a492fc541f6a9dcb372ce45e7f8a0fc962c63b308652a36cc
7
- data.tar.gz: 421ee72029f64b8f04eb1b3d747a2b59c969622c098a37edaa97c4c996695bc7ec575ca270159e66dbee9a0b2d5a192672c2e0f31013311cd84319da5f985f70
6
+ metadata.gz: 6de83feb349ba94456ad089694621944cd48533a11ba76b2c7d2d65b9c0a824b2df770a75ad42a505afac679a20b0e51f54e22ec1343040fdf236a7fbac1f65b
7
+ data.tar.gz: ced24de953f5b28e256a2ecfb558bdb9751a15388cbf0b8f666b193a79a1baa550fe496224fc1e129dcef1671ca6ab38090a2956cc3564e81348f9a439047490
data/CHANGELOG.md ADDED
@@ -0,0 +1,42 @@
1
+ # Changelog
2
+
3
+ ### master
4
+
5
+ * nothing yet
6
+
7
+ ### 1.0.0.beta6 - 2017-12-28
8
+
9
+ * bugfixes
10
+ * fix lib loading problems
11
+
12
+ ### 1.0.0.beta5 - 2017-12-28
13
+
14
+ * bugfixes
15
+ * fix generators
16
+ * fix lib loading problems
17
+ * fix syntax errors
18
+
19
+ ### 1.0.0.beta4 - 2017-12-27
20
+
21
+ * features
22
+ * add caching functionality
23
+ * add `read` attribute to `Notification` instances
24
+ * add `notify` method to notification targets
25
+
26
+ ### 1.0.0.beta3 - 2017-12-25
27
+
28
+ * features
29
+ * allow `Group` object definition in configuration
30
+
31
+ ### 1.0.0.beta2 - 2017-12-23
32
+
33
+ * features
34
+ * add `NotificationHandler::Group` class for target grouping notifications
35
+ * add `metadata` hash to `Notification` objects
36
+ * enhancements
37
+ * add custom `NotificationHandler::Notification` class
38
+ * drop Rails dependency
39
+
40
+ ### 1.0.0.beta1 - 2017-12-22
41
+
42
+ * initial release
data/README.md CHANGED
@@ -1,17 +1,23 @@
1
- # NotificationsRails
1
+ # NotificationHandler
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/notifications-rails.svg)](https://badge.fury.io/rb/notifications-rails) <img src="https://travis-ci.org/jonhue/notifications-rails.svg?branch=master" />
3
+ [![Gem Version](https://badge.fury.io/rb/notification-handler.svg)](https://badge.fury.io/rb/notification-handler) <img src="https://travis-ci.org/jonhue/notifications-rails.svg?branch=master" />
4
4
 
5
- The most powerful notification solution for Rails. NotificationsRails simplifies the handling, rendering, user-integration and cross-platform pushing of notifications through its simple API.
6
-
7
- It integrates with the [Native](https://github.com/NativeGap/native-rails) gem to allow you to create a cross platform Rails app.
5
+ Create and modify your notifications through a simple API.
8
6
 
9
7
  ---
10
8
 
11
9
  ## Table of Contents
12
10
 
13
- * [Philosophy](#philosophy)
14
11
  * [Installation](#installation)
12
+ * [Usage](#usage)
13
+ * [`Notification` API](#notification-api)
14
+ * [`notification_target`](#notification_target)
15
+ * [`notification_object`](#notification_object)
16
+ * [Groups](#groups)
17
+ * [Defining a group](#defining-a-group)
18
+ * [Using a group](#using-a-group)
19
+ * [Caching](#caching)
20
+ * [Configuration](#configuration)
15
21
  * [To Do](#to-do)
16
22
  * [Contributing](#contributing)
17
23
  * [Contributors](#contributors)
@@ -20,49 +26,194 @@ It integrates with the [Native](https://github.com/NativeGap/native-rails) gem t
20
26
 
21
27
  ---
22
28
 
23
- ## Philosophy
29
+ ## Installation
30
+
31
+ NotificationHandler works with Rails 5 onwards. You can add it to your `Gemfile` with:
32
+
33
+ ```ruby
34
+ gem 'notification-handler'
35
+ ```
24
36
 
25
- NotificationsRails has been built with modularity in mind. It currently consists of four components each of which bringing one essential functionality to the notification-integration in your Rails app.
37
+ And then execute:
26
38
 
27
- **[NotificationHandler](notification-handler):** Create and modify your notifications through a simple API.
39
+ $ bundle
28
40
 
29
- **[NotificationRenderer](notification-renderer):** Render your notifications on multiple platforms by specifying notification types.
41
+ Or install it yourself as:
30
42
 
31
- **[NotificationPusher](notification-pusher):** Push your notifications to various services. Including [Email](notification-pusher-actionmailer), [ActionCable](notification-pusher-actioncable), [OneSignal](notification-pusher-onesignal).
43
+ $ gem install notification-handler
32
44
 
33
- **[NotificationSettings](notification-settings):** Integrates with your authentication solution to craft a personalized user notification platform.
45
+ If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
34
46
 
35
- You may just add the components you actually need, or instead use this gem to bundle everything for a complete notification solution.
47
+ ```ruby
48
+ gem 'notification-handler', github: 'jonhue/notifications-rails'
49
+ ```
50
+
51
+ Now run the generator:
52
+
53
+ $ rails g notification_handler:install
54
+
55
+ To wrap things up, migrate the changes to your database:
56
+
57
+ $ rails db:migrate
36
58
 
37
59
  ---
38
60
 
39
- ## Installation
61
+ ## Usage
62
+
63
+ ### `Notification` API
40
64
 
41
- NotificationsRails works with Rails 5 onwards. You can add it to your `Gemfile` with:
65
+ You can use all the ActiveRecord methods you know and love on your `Notification` class. So creating a new notification is dead simple:
42
66
 
43
67
  ```ruby
44
- gem 'notifications-rails'
68
+ notification = Notification.new
45
69
  ```
46
70
 
47
- And then execute:
71
+ Every `Notification` object has a `target` record. This target record is the object, that this notification belongs to (or targets). Usually it's a user, but it can be a record of any class:
48
72
 
49
- $ bundle
73
+ ```ruby
74
+ notification.target = User.first
75
+ ```
50
76
 
51
- Or install it yourself as:
77
+ To store information in your `Notification` record you can use the `metadata` attribute that gets serialized as a `Hash`:
52
78
 
53
- $ gem install notifications-rails
79
+ ```ruby
80
+ notification.metadata = {
81
+ title: 'My first notification',
82
+ content: "It looks great, doesn't it?"
83
+ }
84
+ ```
54
85
 
55
- If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
86
+ Another form of adding information is by associating an object to the notification. This can be a record of any class you like:
87
+
88
+ ```ruby
89
+ notification.object = Recipe.first
90
+ ```
91
+
92
+ The `read` attribute determines whether a notification has been seen or not:
56
93
 
57
94
  ```ruby
58
- gem 'notifications-rails', github: 'jonhue/notifications-rails'
95
+ notification.read = true
96
+ notification.read? # true
97
+ notification.unread? # false
59
98
  ```
60
99
 
100
+ You can use scopes to filter for read or unread notifications:
101
+
102
+ ```ruby
103
+ # Return all read notifications
104
+ Notification.read
105
+
106
+ # Return all unread notifications
107
+ Notification.unread
108
+
109
+ # Number of unread notifications
110
+ Notification.unread.count
111
+ ```
112
+
113
+ ### `notification_target`
114
+
115
+ To use records of an ActiveRecord class as notification targets, add the following to your class:
116
+
117
+ ```ruby
118
+ class User < ApplicationRecord
119
+ notification_target
120
+ end
121
+ ```
122
+
123
+ Now belonging notifications are easy to access:
124
+
125
+ ```ruby
126
+ notifications = User.first.notifications
127
+ ```
128
+
129
+ You can create a notification from a `target`:
130
+
131
+ ```ruby
132
+ User.first.notify object: Recipe.first
133
+ ```
134
+
135
+ ...
136
+
137
+ ### `notification_object`
138
+
139
+ When using records of an ActiveRecord class as notification objects, add this to your class:
140
+
141
+ ```ruby
142
+ class Recipe < ApplicationRecord
143
+ notification_object
144
+ end
145
+ ```
146
+
147
+ Now associated notifications are easy to access:
148
+
149
+ ```ruby
150
+ notifications = Recipe.first.belonging_notifications
151
+ ```
152
+
153
+ ...
154
+
155
+ ### Groups
156
+
157
+ Groups are a powerful way to bulk-create notifications for multiple objects that don't necessarily have a common class.
158
+
159
+ #### Defining a group
160
+
161
+ You define groups in your `NotificationHandler` configuration:
162
+
163
+ ```ruby
164
+ NotificationHandler.configure do |config|
165
+ config.define_group :subscribers, User.where(subscriber: true)
166
+ end
167
+ ```
168
+
169
+ When creating a notification for the group `:subscribers`, one notification will be added for every target that fulfills this scope: `User.where(subscriber: true)`. You can also target objects from different classes:
170
+
171
+ ```ruby
172
+ NotificationHandler.configure do |config|
173
+ config.define_group :subscribers, User.where(subscriber: true) + Admin.all
174
+ end
175
+ ```
176
+
177
+ #### Using a group
178
+
179
+ Bulk-creation of notifications for a certain group is fairly simple:
180
+
181
+ ```ruby
182
+ notification = Notification.create object: Recipe.first, group: :subscribers
183
+ ```
184
+
185
+ **Note:** You are not able to set the `target` attribute when a `group` has been specified.
186
+
187
+ ### Caching
188
+
189
+ You can cache the amount of unread and read notifications for notification targets by settings the [`cache`](#configuration) configuration option to `true`.
190
+
191
+ Then add the following columns to the database tables of ActiveRecord classes acting as notification targets:
192
+
193
+ ```ruby
194
+ add_column :user, :read_notification_count, :integer
195
+ add_column :user, :unread_notification_count, :integer
196
+ ```
197
+
198
+ ---
199
+
200
+ ## Configuration
201
+
202
+ You can configure NotificationHandler by passing a block to `configure`. This can be done in `config/initializers/notification-handler.rb`:
203
+
204
+ ```ruby
205
+ NotificationHandler.configure do |config|
206
+ config.cache = true
207
+ end
208
+ ```
209
+
210
+ **`cache`** Cache amount of unread and read notifications for notification targets. Takes a boolean. Defaults to `false`.
211
+
61
212
  ---
62
213
 
63
214
  ## To Do
64
215
 
65
- [Here](https://github.com/jonhue/notifications-rails/projects) is the full list of current projects.
216
+ [Here](https://github.com/jonhue/notifications-rails/projects/2) is the full list of current projects.
66
217
 
67
218
  To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/notifications-rails/issues/new).
68
219
 
@@ -70,9 +221,9 @@ To propose your ideas, initiate the discussion by adding a [new issue](https://g
70
221
 
71
222
  ## Contributing
72
223
 
73
- We hope that you will consider contributing to NotificationsRails. Please read this short overview for some information about how to get started:
224
+ We hope that you will consider contributing to NotificationHandler. Please read this short overview for some information about how to get started:
74
225
 
75
- [Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
226
+ [Learn more about contributing to this repository](https://github.com/jonhue/notifications-rails/blob/master/CONTRIBUTING.md), [Code of Conduct](https://github.com/jonhue/notifications-rails/blob/master/CODE_OF_CONDUCT.md)
76
227
 
77
228
  ### Contributors
78
229
 
@@ -82,7 +233,7 @@ https://github.com/jonhue/notifications-rails/graphs/contributors
82
233
 
83
234
  ### Semantic Versioning
84
235
 
85
- NotificationsRails follows Semantic Versioning 2.0 as defined at http://semver.org.
236
+ NotificationHandler follows Semantic Versioning 2.0 as defined at http://semver.org.
86
237
 
87
238
  ## License
88
239
 
@@ -0,0 +1,46 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module NotificationHandler
5
+
6
+ class InstallGenerator < Rails::Generators::Base
7
+
8
+ include Rails::Generators::Migration
9
+
10
+ source_root File.join File.dirname(__FILE__), '../templates/install'
11
+ desc 'Install NotificationHandler'
12
+
13
+ def self.next_migration_number dirname
14
+ if ActiveRecord::Base.timestamped_migrations
15
+ Time.now.utc.strftime '%Y%m%d%H%M%S'
16
+ else
17
+ "%.3d" % ( current_migration_number(dirname) + 1 )
18
+ end
19
+ end
20
+
21
+ def create_initializer
22
+ template 'initializer.rb', 'config/initializers/notification-handler.rb'
23
+ end
24
+
25
+ def create_notifications_migration_file
26
+ migration_template 'notifications_migration.rb.erb', 'db/migrate/notification_handler_migration.rb', migration_version: migration_version
27
+ end
28
+ def create_notification_model
29
+ template 'notification_model.rb', 'app/models/notification.rb'
30
+ end
31
+
32
+ def show_readme
33
+ readme 'README.md'
34
+ end
35
+
36
+ private
37
+
38
+ def migration_version
39
+ if Rails.version >= '5.0.0'
40
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1 @@
1
+ Now run `rails db:migrate` to add NotificationHandler to your database.
@@ -0,0 +1,11 @@
1
+ NotificationHandler.configure do |config|
2
+
3
+ # Cache amount of unread and read notifications for notification targets
4
+ # Learn more: https://github.com/jonhue/notifications-rails/tree/master/notification-handler#caching
5
+ # config.cache = false
6
+
7
+ # Groups are a powerful way to bulk-create notifications for multiple objects that don't necessarily have a common class.
8
+ # Learn more: https://github.com/jonhue/notifications-rails/tree/master/notification-handler#groups
9
+ # config.define_group :subscribers, User.where(subscriber: true)
10
+
11
+ end
@@ -0,0 +1,2 @@
1
+ class Notification < NotificationHandler::Notification
2
+ end
@@ -0,0 +1,16 @@
1
+ class NotificationHandlerMigration < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :notifications do |t|
4
+
5
+ t.references :target, polymorphic: true, index: true
6
+ t.references :object, polymorphic: true, index: true
7
+
8
+ t.boolean :read, default: false, null: false, index: true
9
+
10
+ t.text :metadata
11
+
12
+ t.timestamps
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ module NotificationHandler
2
+
3
+ class << self
4
+ attr_accessor :configuration
5
+ end
6
+
7
+ def self.configure
8
+ self.configuration ||= Configuration.new
9
+ yield configuration
10
+ end
11
+
12
+ class Configuration
13
+
14
+ attr_accessor :cache
15
+
16
+ def initialize
17
+ @cache = false
18
+ end
19
+
20
+ def define_group name, target_scope
21
+ ::NotificationHandler::Group.new name: name.to_sym, target_scope: target_scope
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails/railtie'
2
+
3
+ module NotificationHandler
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,17 @@
1
+ module NotificationHandler
2
+ class Group
3
+
4
+ attr_accessor :name
5
+ attr_accessor :target_scope
6
+
7
+ def initialize name, target_scope
8
+ @name = name
9
+ @target_scope = target_scope
10
+ end
11
+
12
+ def find_by_name name
13
+ ObjectSpace.each_object(NotificationHandler::Group).select { |group| group.name == name.to_sym }
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ module NotificationHandler
2
+ module NotificationLibrary
3
+
4
+ attr_accessor :group
5
+
6
+ before_validation :create_for_group
7
+ after_commit :cache
8
+
9
+ def read?
10
+ self.read
11
+ end
12
+ def unread?
13
+ !self.read
14
+ end
15
+
16
+ private
17
+
18
+ def create_for_group
19
+ unless self.group.nil?
20
+ target_scope = NotificationHandler::Group.find_by_name(self.group).last.target_scope
21
+ target_scope&.each do |target|
22
+ notification = self.dup
23
+ notification.target = target
24
+ notification.group = nil
25
+ notification.save
26
+ end
27
+ return false
28
+ end
29
+ end
30
+
31
+ def cache
32
+ if self.read_changed?
33
+ self.target.read_notification_count = self.target.notifications.read.count
34
+ self.target.unread_notification_count = self.target.notifications.unread.count
35
+ self.target.save!
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,8 @@
1
+ module NotificationHandler
2
+ module NotificationScopes
3
+
4
+ scope :read, -> { where(read: true) }
5
+ scope :unread, -> { where(read: false) }
6
+
7
+ end
8
+ end
@@ -0,0 +1,25 @@
1
+ module NotificationHandler
2
+ module Object
3
+
4
+ def self.included base
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def notification_object
10
+ has_many :belonging_notifications, as: :object, class_name: 'Notification', dependent: :destroy
11
+ include NotificationHandler::Object::InstanceMethods
12
+
13
+ extend NotificationSettings::Object if defined?(NotificationSettings)
14
+ extend NotificationSettings::Subscribable if defined?(NotificationSettings)
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+
20
+ # ...
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/railtie'
2
+ require 'active_record'
3
+
4
+ module NotificationHandler
5
+ class Railtie < Rails::Railtie
6
+
7
+ initializer 'notification-handler.active_record' do
8
+ ActiveSupport.on_load :active_record do
9
+ include NotificationHandler::Target
10
+ include NotificationHandler::Object
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ module NotificationHandler
2
+ module Target
3
+
4
+ def self.included base
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def notification_target
10
+ has_many :notifications, as: :target, dependent: :destroy
11
+ include NotificationHandler::Target::InstanceMethods
12
+
13
+ extend NotificationSettings::Target if defined?(NotificationSettings)
14
+ extend NotificationSettings::Subscriber if defined?(NotificationSettings)
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+
20
+ def notify options = {}
21
+ options[:target] = self
22
+ Notification.create options
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module NotificationHandler
2
+
3
+ require 'notification_handler/configuration'
4
+
5
+ require 'notification_handler/engine'
6
+
7
+ autoload :Group, 'notification_handler/group'
8
+
9
+ autoload :Target, 'notification_handler/target'
10
+ autoload :Object, 'notification_handler/object'
11
+ autoload :NotificationLibrary, 'notification_handler/notification_library'
12
+ autoload :NotificationScopes, 'notification_handler/notification_scopes'
13
+
14
+ require 'notification_handler/railtie'
15
+
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notification-handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta6
4
+ version: 1.0.0.beta7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Hübotter
@@ -72,8 +72,23 @@ executables: []
72
72
  extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
+ - CHANGELOG.md
75
76
  - LICENSE
76
77
  - README.md
78
+ - lib/generators/notification_handler/install_generator.rb
79
+ - lib/generators/templates/install/README.md
80
+ - lib/generators/templates/install/initializer.rb
81
+ - lib/generators/templates/install/notification_model.rb
82
+ - lib/generators/templates/install/notifications_migration.rb.erb
83
+ - lib/notification_handler.rb
84
+ - lib/notification_handler/configuration.rb
85
+ - lib/notification_handler/engine.rb
86
+ - lib/notification_handler/group.rb
87
+ - lib/notification_handler/notification_library.rb
88
+ - lib/notification_handler/notification_scopes.rb
89
+ - lib/notification_handler/object.rb
90
+ - lib/notification_handler/railtie.rb
91
+ - lib/notification_handler/target.rb
77
92
  homepage: https://github.com/jonhue/notifications-rails/tree/master/notification-handler
78
93
  licenses:
79
94
  - MIT
@@ -81,7 +96,7 @@ metadata: {}
81
96
  post_install_message:
82
97
  rdoc_options: []
83
98
  require_paths:
84
- - lib/notification_handler.rb
99
+ - lib
85
100
  required_ruby_version: !ruby/object:Gem::Requirement
86
101
  requirements:
87
102
  - - ">="