notification-renderer 1.2.5 → 1.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +328 -328
- data/app/helpers/notification_renderer_helper.rb +80 -63
- data/lib/generators/notification_renderer/install_generator.rb +33 -36
- data/lib/generators/notification_renderer/type_generator.rb +23 -14
- data/lib/generators/templates/install/initializer.rb +12 -12
- data/lib/generators/templates/install/notifications_migration.rb.erb +5 -3
- data/lib/generators/templates/type/_template.html +7 -7
- data/lib/notification-renderer.rb +10 -10
- data/lib/notification_renderer/configuration.rb +24 -26
- data/lib/notification_renderer/engine.rb +9 -7
- data/lib/notification_renderer/notification_library.rb +49 -45
- data/lib/notification_renderer/notification_scopes.rb +24 -26
- metadata +46 -34
- data/CHANGELOG.md +0 -46
- data/lib/generators/templates/install/README.md +0 -1
@@ -1,63 +1,80 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationRendererHelper
|
4
|
+
def render_notification(notification,
|
5
|
+
renderer: default_renderer,
|
6
|
+
attributes: nil,
|
7
|
+
notifications: nil)
|
8
|
+
notification.update(read: true) if auto_read
|
9
|
+
render(
|
10
|
+
partial: "notifications/#{notification.type}/#{renderer}",
|
11
|
+
locals: {
|
12
|
+
notification: notification,
|
13
|
+
attributes: attributes,
|
14
|
+
notifications: notifications
|
15
|
+
}
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
def render_notifications(notifications, renderer: default_renderer)
|
20
|
+
content_tag :div, class: 'notification-renderer notifications' do
|
21
|
+
notifications&.each do |notification|
|
22
|
+
render_notification(notification, renderer: renderer)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def render_notifications_grouped(notifications, group_by,
|
28
|
+
renderer: default_renderer,
|
29
|
+
group_by_date: false,
|
30
|
+
group_by_type: false)
|
31
|
+
group_by.unshift(:type) if group_by_type
|
32
|
+
if group_by_date
|
33
|
+
group_by.unshift("created_at.beginning_of_#{group_by_date}")
|
34
|
+
end
|
35
|
+
|
36
|
+
content_tag :div, class: 'notification-renderer notifications' do
|
37
|
+
recursive_notification_grouping(
|
38
|
+
notifications.grouping(group_by), group_by, renderer
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def notification_grouped?
|
44
|
+
local_assigns[:attributes] && local_assigns[:notifications]
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def recursive_notification_grouping(notifications, group_by, renderer,
|
50
|
+
attributes)
|
51
|
+
i = 0
|
52
|
+
notifications.each do |k, v|
|
53
|
+
attributes[group_by[i]] = k
|
54
|
+
i += 1
|
55
|
+
recursive_render_notifications_grouped(v, renderer, attributes)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def recursive_render_notifications_grouped(notifications, renderer,
|
60
|
+
attributes)
|
61
|
+
if notifications.is_a?(Hash)
|
62
|
+
recursive_notification_grouping(
|
63
|
+
notifications, group_by, renderer, attributes
|
64
|
+
)
|
65
|
+
else
|
66
|
+
render_notification(notifications.last,
|
67
|
+
renderer: renderer,
|
68
|
+
attributes: attributes,
|
69
|
+
notifications: notifications)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def auto_read
|
74
|
+
NotificationRenderer.configuration.auto_read
|
75
|
+
end
|
76
|
+
|
77
|
+
def default_renderer
|
78
|
+
NotificationRenderer.configuration.default_renderer
|
79
|
+
end
|
80
|
+
end
|
@@ -1,47 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/generators'
|
2
4
|
require 'rails/generators/migration'
|
3
5
|
|
4
6
|
module NotificationRenderer
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
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
|
+
format('%.3d', current_migration_number(dirname) + 1)
|
18
|
+
end
|
19
|
+
end
|
5
20
|
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
21
|
+
def create_initializer
|
22
|
+
template 'initializer.rb', 'config/initializers/notification-renderer.rb'
|
23
|
+
end
|
32
24
|
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
def create_notifications_migration_file
|
26
|
+
migration_template(
|
27
|
+
'notifications_migration.rb.erb',
|
28
|
+
'db/migrate/notification_renderer_migration.rb',
|
29
|
+
migration_version: migration_version
|
30
|
+
)
|
31
|
+
end
|
36
32
|
|
37
|
-
|
33
|
+
def create_templates
|
34
|
+
system 'rails g notification_renderer:type -t notification'
|
35
|
+
end
|
38
36
|
|
39
|
-
|
40
|
-
if Rails.version >= '5.0.0'
|
41
|
-
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
42
|
-
end
|
43
|
-
end
|
37
|
+
private
|
44
38
|
|
39
|
+
def migration_version
|
40
|
+
return unless Rails.version >= '5.0.0'
|
41
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
45
42
|
end
|
46
|
-
|
43
|
+
end
|
47
44
|
end
|
@@ -1,21 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/generators'
|
2
4
|
require 'rails/generators/migration'
|
3
5
|
|
4
6
|
module NotificationRenderer
|
7
|
+
class TypeGenerator < Rails::Generators::Base
|
8
|
+
source_root(File.join(File.dirname(__FILE__), '../templates/type'))
|
9
|
+
desc 'Create a new notification type'
|
10
|
+
class_option :type,
|
11
|
+
desc: 'Specify the notification type',
|
12
|
+
type: :string,
|
13
|
+
default: NotificationRenderer.configuration.default_type,
|
14
|
+
aliases: '-t'
|
15
|
+
class_option :renderers,
|
16
|
+
desc: 'Specify the renderer templates',
|
17
|
+
type: :string,
|
18
|
+
default: NotificationRenderer.configuration.default_renderer,
|
19
|
+
aliases: '-r'
|
5
20
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
21
|
+
def create_templates
|
22
|
+
options[:renderers].split(' ')&.each do |template|
|
23
|
+
template(
|
24
|
+
'_template.html',
|
25
|
+
"app/views/notifications/#{options[:type]}/_#{template}.html.erb"
|
26
|
+
)
|
27
|
+
end
|
19
28
|
end
|
20
|
-
|
29
|
+
end
|
21
30
|
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
NotificationRenderer.configure do |config|
|
4
|
+
# Choose your default notification type. Takes a string.
|
5
|
+
# config.default_type = 'notification'
|
6
|
+
|
7
|
+
# Choose your default renderer. Takes a string.
|
8
|
+
# config.default_renderer = 'index'
|
9
|
+
|
10
|
+
# Automatically mark rendered notifications as read. Takes a boolean.
|
11
|
+
# config.auto_read = true
|
12
|
+
end
|
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class NotificationRendererMigration < ActiveRecord::Migration<%= migration_version %>
|
2
|
-
|
3
|
-
|
4
|
-
|
4
|
+
def change
|
5
|
+
add_column :notifications, :type, :string, index: true
|
6
|
+
end
|
5
7
|
end
|
@@ -1,7 +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
|
-
-->
|
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
|
+
-->
|
@@ -1,10 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationRenderer
|
4
|
+
require 'notification_renderer/configuration'
|
5
|
+
|
6
|
+
require 'notification_renderer/engine'
|
7
|
+
|
8
|
+
autoload :NotificationLibrary, 'notification_renderer/notification_library'
|
9
|
+
autoload :NotificationScopes, 'notification_renderer/notification_scopes'
|
10
|
+
end
|
@@ -1,26 +1,24 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationRenderer
|
4
|
+
class << self
|
5
|
+
attr_accessor :configuration
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.configure
|
9
|
+
self.configuration ||= Configuration.new
|
10
|
+
yield configuration
|
11
|
+
end
|
12
|
+
|
13
|
+
class Configuration
|
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
|
+
end
|
24
|
+
end
|
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/railtie'
|
4
|
+
require 'action_view'
|
5
|
+
|
6
|
+
module NotificationRenderer
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
end
|
9
|
+
end
|
@@ -1,45 +1,49 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'notification-handler'
|
4
|
+
require 'active_support'
|
5
|
+
|
6
|
+
module NotificationRenderer
|
7
|
+
module NotificationLibrary
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def grouping(group_by)
|
12
|
+
notifications = all
|
13
|
+
group_by.each do |method|
|
14
|
+
notifications = recursive_grouping(notifications, method)
|
15
|
+
end
|
16
|
+
notifications
|
17
|
+
end
|
18
|
+
|
19
|
+
def grouping_by(group_by)
|
20
|
+
all.group_by { |notification| notification.send(group_by) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def recursive_grouping(notifications, group_by)
|
24
|
+
if notifications.is_a?(Hash)
|
25
|
+
recursive_hash_grouping(notifications, group_by)
|
26
|
+
else
|
27
|
+
notifications.grouping_by(group_by)
|
28
|
+
end
|
29
|
+
notifications
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def type
|
34
|
+
self[:type] || NotificationRenderer.configuration.default_type
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def recursive_hash_grouping(notifications, group_by)
|
40
|
+
notifications.each_pair do |k, v|
|
41
|
+
if v.is_a?(Hash)
|
42
|
+
recursive_hash_grouping(v, group_by)
|
43
|
+
else
|
44
|
+
notifications[k] = v.grouping_by(group_by)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -1,26 +1,24 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'notification-handler'
|
4
|
+
require 'active_support'
|
5
|
+
|
6
|
+
module NotificationRenderer
|
7
|
+
module NotificationScopes
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def method_missing(method, *args)
|
12
|
+
if method.to_s[/(.+)_type/]
|
13
|
+
where(type: $1.singularize.classify)
|
14
|
+
else
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def respond_to_missing?(method, include_private = false)
|
20
|
+
super || method.to_s[/(.+)_type/]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|