notification-renderer 1.0.0.beta6 → 1.0.0.beta7

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: c1d9ee72295fa4ef67073e39179419f4e29146d7955928d9a577e08b5a8c0e41
4
- data.tar.gz: a8ab724ed8b15b547e2d54ba6291ccc9d49fa3c62efc86588688c391b15c5c5e
3
+ metadata.gz: b85c93e55a50084e3e1d1f31af5d942a21455285303ef5aae231c9c7deae3a52
4
+ data.tar.gz: 80e6f088d3631eb327715a5318d46bf62e23d6ed728b5bb6b779e27a5af1c481
5
5
  SHA512:
6
- metadata.gz: 5b588ad303c2fd11d7c6ed436c176bdae38a14bd72b428ca6e1dc0654fe93e9c962d658a71f7166c4fedc768a5e466d11b62535eab4eb2de8b7bc2af34e9ad79
7
- data.tar.gz: 44b03932d0eaec4d98815d6d230376f8acd3ea20f19b9f8d29b6b63e4177e081b9a094474976508512fd9268b6ba62a0e6578598372b94c72d39890cae3e1eef
6
+ metadata.gz: f87e98fba149abd4b3f7b18c1e67ee29d0f4d2c18b9f094c88f905b73f3acaad708d85108aa245ef33ad003ef7b91ecb539fedeeaf807f53e844a794e1c09478
7
+ data.tar.gz: de08d08a118aef28227ef0cbd01cdea5244b01ce52bdcea4e98098d59769b599e2931c409924f56a83ba7852477a0b48fab43ccd40894d9d0253eb14ed952088
data/CHANGELOG.md ADDED
@@ -0,0 +1,35 @@
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 helpers
23
+ * add `default_renderer` and `auto_read` configuration options
24
+ * add grouping functionality
25
+ * enhancements
26
+ * allow defining renderers for `type` generator
27
+
28
+ ### 1.0.0.beta3 - 2017-12-25
29
+
30
+ * features
31
+ * introduce `type` generator
32
+
33
+ ### 1.0.0.beta2 - 2017-12-23
34
+
35
+ * initial release
data/README.md CHANGED
@@ -1,17 +1,24 @@
1
- # NotificationsRails
1
+ # NotificationRenderer
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-renderer.svg)](https://badge.fury.io/rb/notification-renderer) <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
+ Render your notifications on multiple platforms by specifying notification types.
8
6
 
9
7
  ---
10
8
 
11
9
  ## Table of Contents
12
10
 
13
- * [Philosophy](#philosophy)
14
11
  * [Installation](#installation)
12
+ * [Usage](#usage)
13
+ * [Types](#types)
14
+ * [Generating a new type](#generating-a-new-type)
15
+ * [Using a type](#using-a-type)
16
+ * [Renderers](#renderers)
17
+ * [View helpers](#view-helpers)
18
+ * [`render_notification`](#render_notification)
19
+ * [`render_notifications`](#render_notifications)
20
+ * [Grouping](#grouping)
21
+ * [Configuration](#configuration)
15
22
  * [To Do](#to-do)
16
23
  * [Contributing](#contributing)
17
24
  * [Contributors](#contributors)
@@ -20,49 +27,217 @@ It integrates with the [Native](https://github.com/NativeGap/native-rails) gem t
20
27
 
21
28
  ---
22
29
 
23
- ## Philosophy
30
+ ## Installation
31
+
32
+ NotificationRenderer works with Rails 5 onwards. You can add it to your `Gemfile` with:
33
+
34
+ ```ruby
35
+ gem 'notification-renderer'
36
+ ```
24
37
 
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.
38
+ And then execute:
26
39
 
27
- **[NotificationHandler](notification-handler):** Create and modify your notifications through a simple API.
40
+ $ bundle
28
41
 
29
- **[NotificationRenderer](notification-renderer):** Render your notifications on multiple platforms by specifying notification types.
42
+ Or install it yourself as:
30
43
 
31
- **[NotificationPusher](notification-pusher):** Push your notifications to various services. Including [Email](notification-pusher-actionmailer), [ActionCable](notification-pusher-actioncable), [OneSignal](notification-pusher-onesignal).
44
+ $ gem install notification-renderer
32
45
 
33
- **[NotificationSettings](notification-settings):** Integrates with your authentication solution to craft a personalized user notification platform.
46
+ If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
34
47
 
35
- You may just add the components you actually need, or instead use this gem to bundle everything for a complete notification solution.
48
+ ```ruby
49
+ gem 'notification-renderer', github: 'jonhue/notifications-rails'
50
+ ```
51
+
52
+ Now run the generator:
53
+
54
+ $ rails g notification_renderer:install
55
+
56
+ To wrap things up, migrate the changes to your database:
57
+
58
+ $ rails db:migrate
36
59
 
37
60
  ---
38
61
 
39
- ## Installation
62
+ ## Usage
63
+
64
+ ### Types
65
+
66
+ NotificationRenderer uses templates to render your notifications.
67
+
68
+ The `type` of a notification determines which template gets utilized for rendering. Each notification type has multiple templates each of which responsible for rendering a notification in another scenario. The default template for a given type is `index`.
69
+
70
+ #### Generating a new type
71
+
72
+ This gem comes with a generator to make adding new types a whole lot easier. Run in your terminal:
73
+
74
+ $ rails g notification_renderer:type -t notification
40
75
 
41
- NotificationsRails works with Rails 5 onwards. You can add it to your `Gemfile` with:
76
+ This will create the following structure in your application:
77
+
78
+ * `views`
79
+ * `notifications`
80
+ * `notification`
81
+ * `_index.html.erb`
82
+
83
+ You can also customize the generated templates (renderers):
84
+
85
+ $ rails g notification_renderer:type -t notification -r index feed
86
+
87
+ This command will also create a custom renderer called `feed` for the notification type `notification`:
88
+
89
+ * `views`
90
+ * `notifications`
91
+ * `notification`
92
+ * `_feed.html.erb`
93
+ * `_index.html.erb`
94
+
95
+ #### Using a type
96
+
97
+ You are able to specify the `type` of a `Notification` record:
42
98
 
43
99
  ```ruby
44
- gem 'notifications-rails'
100
+ notification = Notification.create target: User.first, object: Recipe.first, type: 'notification'
45
101
  ```
46
102
 
47
- And then execute:
103
+ **Note:** The `type` attribute of any new `Notification` record will default to the [`default_type` configuration](#configuration).
48
104
 
49
- $ bundle
105
+ You can also scope records by their type:
50
106
 
51
- Or install it yourself as:
107
+ ```ruby
108
+ # Return records with `'notification'` as type
109
+ Notification.notification_type
52
110
 
53
- $ gem install notifications-rails
111
+ # Return records with `'follow'` as type
112
+ Notification.follow_type
113
+ ```
54
114
 
55
- If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
115
+ ### Renderers
116
+
117
+ In your renderers you can access the `Notification` record through the `notification` variable. This is how a renderer could look like:
118
+
119
+ ```erb
120
+ <%= notification.target.name %> commented on <%= notification.object.article.title %>.
121
+ ```
122
+
123
+ ### View helpers
124
+
125
+ NotificationRenderer introduces some view helpers to assist you in embedding notifications.
126
+
127
+ #### `render_notification`
128
+
129
+ `render_notification` renders a single `Notification` record:
130
+
131
+ ```erb
132
+ <%= render_notification Notification.first %>
133
+ ```
134
+
135
+ Rendering a notification will set its `read` attribute to `true`. This behavior can be [configured](#configuration).
136
+
137
+ You can also specify a renderer. It defaults to `'index'`.
138
+
139
+ ```erb
140
+ <%= render_notification Notification.first, 'feed' %>
141
+ ```
142
+
143
+ #### `render_notifications`
144
+
145
+ `render_notifications` takes an ActiveRecord array of `Notification` records and renders each of them in order:
146
+
147
+ ```erb
148
+ <%= render_notifications Notification.all %>
149
+ ```
150
+
151
+ You can also specify a renderer. It defaults to `'index'`.
152
+
153
+ ```erb
154
+ <%= render_notifications Notification.all, 'feed' %>
155
+ ```
156
+
157
+ It wraps the rendered notifications in a `div`:
158
+
159
+ ```html
160
+ <div class="notification-renderer notifications">
161
+ <!-- ... -->
162
+ </div>
163
+ ```
164
+
165
+ ### Grouping
166
+
167
+ You can group any ActiveRecord array of `Notification` records by an attribute value:
56
168
 
57
169
  ```ruby
58
- gem 'notifications-rails', github: 'jonhue/notifications-rails'
170
+ Notification.grouping('object.article')
171
+ Notification.grouping('metadata[:title]')
172
+ ```
173
+
174
+ When rendering notifications you often want to group them by the object they belong to. This is how to group notifications by the associated object:
175
+
176
+ ```erb
177
+ <%= render_notifications_grouped Notification.all, 'object', 'feed' %>
59
178
  ```
60
179
 
180
+ You can also group notifications by nested attributes:
181
+
182
+ ```erb
183
+ <%= render_notifications_grouped Notification.all, 'object.article' %>
184
+ <%= render_notifications_grouped Notification.all, 'metadata[:title]' %>
185
+ ```
186
+
187
+ It is also possible to group notifications for just one object:
188
+
189
+ ```erb
190
+ <%= render_notifications_grouped Notification.where(object_id: 1, object_type: 'Comment'), 'object' %>
191
+ ```
192
+
193
+ This will render the last notification for every group and pass the attributes value grouped by to your renderer:
194
+
195
+ ```erb
196
+ <!-- View -->
197
+
198
+ <%= render_notifications_grouped Notification.all, 'object.article' %>
199
+
200
+
201
+ <!-- Renderer -->
202
+
203
+ <% if notification_grouped? %>
204
+ <%= notification.target.name %> and <%= (notification_count - 1).to_s %> others commented on <%= attribute.title %>.
205
+ <% else %>
206
+ <%= notification.target.name %> commented on <%= notification.object.article.title %>.
207
+ <% end %>
208
+ ```
209
+
210
+ Grouping makes the following two methods available in your renderer:
211
+
212
+ **`attribute`** The value of the attribute used for grouping.
213
+
214
+ **`notification_count`** The notification count for the group of the current notification.
215
+
216
+ You may check whether a template is being used for grouping by using the `notification_grouped?` helper method.
217
+
218
+ ---
219
+
220
+ ## Configuration
221
+
222
+ You can configure NotificationRenderer by passing a block to `configure`. This can be done in `config/initializers/notification-renderer.rb`:
223
+
224
+ ```ruby
225
+ NotificationRenderer.configure do |config|
226
+ config.default_type = 'notification'
227
+ end
228
+ ```
229
+
230
+ **`default_type`** Choose your default notification type. Takes a string. Defaults to `'notification'`.
231
+
232
+ **`default_renderer`** Choose your default renderer. Takes a string. Defaults to `'index'`.
233
+
234
+ **`auto_read`** Automatically mark rendered notifications as read. Takes a boolean. Defaults to `true`.
235
+
61
236
  ---
62
237
 
63
238
  ## To Do
64
239
 
65
- [Here](https://github.com/jonhue/notifications-rails/projects) is the full list of current projects.
240
+ [Here](https://github.com/jonhue/notifications-rails/projects/7) is the full list of current projects.
66
241
 
67
242
  To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/notifications-rails/issues/new).
68
243
 
@@ -70,9 +245,9 @@ To propose your ideas, initiate the discussion by adding a [new issue](https://g
70
245
 
71
246
  ## Contributing
72
247
 
73
- We hope that you will consider contributing to NotificationsRails. Please read this short overview for some information about how to get started:
248
+ We hope that you will consider contributing to NotificationRenderer. Please read this short overview for some information about how to get started:
74
249
 
75
- [Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
250
+ [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
251
 
77
252
  ### Contributors
78
253
 
@@ -82,7 +257,7 @@ https://github.com/jonhue/notifications-rails/graphs/contributors
82
257
 
83
258
  ### Semantic Versioning
84
259
 
85
- NotificationsRails follows Semantic Versioning 2.0 as defined at http://semver.org.
260
+ NotificationRenderer follows Semantic Versioning 2.0 as defined at http://semver.org.
86
261
 
87
262
  ## License
88
263
 
@@ -0,0 +1,47 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module NotificationRenderer
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 NotificationRenderer'
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-renderer.rb'
23
+ end
24
+
25
+ def create_notifications_migration_file
26
+ migration_template 'notifications_migration.rb.erb', 'db/migrate/notification_renderer_migration.rb', migration_version: migration_version
27
+ end
28
+
29
+ def create_templates
30
+ system 'rails g notification_renderer:type -t notification'
31
+ end
32
+
33
+ def show_readme
34
+ readme 'README.md'
35
+ end
36
+
37
+ private
38
+
39
+ def migration_version
40
+ if Rails.version >= '5.0.0'
41
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module NotificationRenderer
5
+
6
+ class TypeGenerator < Rails::Generators::Base
7
+
8
+ source_root File.join File.dirname(__FILE__), '../templates/type'
9
+ desc 'Create a new notification type'
10
+ class_option :type, desc: 'Specify the notification type', type: :string, default: NotificationRenderer.configuration.default_type, aliases: '-t'
11
+ class_option :renderers, desc: 'Specify the renderer templates', type: :string, default: NotificationRenderer.configuration.default_renderer, aliases: '-r'
12
+
13
+ def create_templates
14
+ options[:renderers].split(' ')&.each do |template|
15
+ template '_template.html', "app/views/notifications/#{options[:type]}/_#{template}.html.erb"
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1 @@
1
+ Now run `rails db:migrate` to add NotificationRenderer to your database.
@@ -0,0 +1,12 @@
1
+ NotificationRenderer.configure do |config|
2
+
3
+ # Choose your default notification type. Takes a string.
4
+ # config.default_type = 'notification'
5
+
6
+ # Choose your default renderer. Takes a string.
7
+ # config.default_renderer = 'index'
8
+
9
+ # Automatically mark rendered notifications as read. Takes a boolean.
10
+ # config.auto_read = true
11
+
12
+ end
@@ -0,0 +1,5 @@
1
+ class NotificationRendererMigration < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ add_column :notifications, :type, :string, index: true
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ <!--
2
+ In your renderers you can access the `Notification` record through the `notification` variable. This is how a renderer could look like:
3
+
4
+ <%#= notification.target.name %> commented on <%#= notification.object.article.title %>
5
+
6
+ Learn more: https://github.com/jonhue/notifications-rails/tree/master/notification-renderer#renderers
7
+ -->
@@ -0,0 +1,26 @@
1
+ module NotificationRenderer
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 :default_type
15
+ attr_accessor :default_renderer
16
+ attr_accessor :auto_read
17
+
18
+ def initialize
19
+ @default_type = 'notification'
20
+ @default_renderer = 'index'
21
+ @auto_read = true
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails/railtie'
2
+
3
+ module NotificationRenderer
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ module NotificationRenderer
2
+ module NotificationLibrary
3
+
4
+ def self.grouping group_by
5
+ group_by{ |notification| notification.send(group_by) }
6
+ end
7
+
8
+ def type
9
+ self[:type] || NotificationRenderer.configuration.default_type
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module NotificationRenderer
2
+ module NotificationScopes
3
+
4
+ def method_missing m, *args
5
+ if m.to_s[/(.+)_type/]
6
+ where type: $1.singularize.classify
7
+ else
8
+ super
9
+ end
10
+ end
11
+
12
+ def respond_to? m, include_private = false
13
+ super || m.to_s[/(.+)_type/]
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ module NotificationRenderer
2
+
3
+ require 'notification_renderer/configuration'
4
+
5
+ require 'notification_renderer/engine'
6
+
7
+ autoload :NotificationLibrary, 'notification_renderer/notification_library'
8
+ autoload :NotificationScopes, 'notification_renderer/notification_scopes'
9
+
10
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notification-renderer
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
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: 1.0.0.beta6
47
+ version: 1.0.0.beta7
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: 1.0.0.beta6
54
+ version: 1.0.0.beta7
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -87,8 +87,20 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
+ - CHANGELOG.md
90
91
  - LICENSE
91
92
  - README.md
93
+ - lib/generators/notification_renderer/install_generator.rb
94
+ - lib/generators/notification_renderer/type_generator.rb
95
+ - lib/generators/templates/install/README.md
96
+ - lib/generators/templates/install/initializer.rb
97
+ - lib/generators/templates/install/notifications_migration.rb.erb
98
+ - lib/generators/templates/type/_template.html
99
+ - lib/notification_renderer.rb
100
+ - lib/notification_renderer/configuration.rb
101
+ - lib/notification_renderer/engine.rb
102
+ - lib/notification_renderer/notification_library.rb
103
+ - lib/notification_renderer/notification_scopes.rb
92
104
  homepage: https://github.com/jonhue/notifications-rails/tree/master/notification-renderer
93
105
  licenses:
94
106
  - MIT
@@ -96,7 +108,7 @@ metadata: {}
96
108
  post_install_message:
97
109
  rdoc_options: []
98
110
  require_paths:
99
- - lib/notification_renderer.rb
111
+ - lib
100
112
  required_ruby_version: !ruby/object:Gem::Requirement
101
113
  requirements:
102
114
  - - ">="