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.
@@ -1,63 +1,80 @@
1
- module NotificationRendererHelper
2
-
3
- def render_notification notification, options = {}
4
- defaults = {
5
- renderer: NotificationRenderer.configuration.default_renderer,
6
- attributes: nil,
7
- notifications: nil
8
- }
9
- options = defaults.merge options
10
-
11
- notification.update_attributes read: true if NotificationRenderer.configuration.auto_read
12
- render "notifications/#{notification.type}/#{options[:renderer]}", notification: notification, attributes: options[:attributes], notifications: options[:notifications]
13
- end
14
-
15
- def render_notifications notifications, options = {}
16
- defaults = {
17
- renderer: NotificationRenderer.configuration.default_renderer
18
- }
19
- options = defaults.merge options
20
-
21
- content_tag :div, class: 'notification-renderer notifications' do
22
- notifications.each do |notification|
23
- render_notification notification, renderer: options[:renderer]
24
- end
25
- end
26
- end
27
-
28
- def render_notifications_grouped notifications, group_by, options = {}
29
- defaults = {
30
- renderer: NotificationRenderer.configuration.default_renderer,
31
- group_by_date: false,
32
- group_by_type: false
33
- }
34
- options = defaults.merge options
35
-
36
- group_by.unshift :type if options[:group_by_type]
37
- group_by.unshift "created_at.beginning_of_#{options[:group_by_date]}" if options[:group_by_date]
38
-
39
- content_tag :div, class: 'notification-renderer notifications' do
40
- recursive_render_notifications_grouped notifications.grouping(options[:group_by]), options
41
- end
42
- end
43
-
44
- def notification_grouped?
45
- local_assigns[:attributes] && local_assigns[:notifications]
46
- end
47
-
48
- private
49
-
50
- def recursive_render_notifications_grouped notifications, i = 0, options = {}
51
- options[:attributes] = {} unless options.has_key? :attributes
52
- notifications.each_pair do |k, v|
53
- options[:attributes][options[:group_by][i]] = k
54
- i += 1
55
- if v.is_a? Hash
56
- recursive_render_notifications_grouped v, options
57
- else
58
- render_notification v.last, renderer: options[:renderer], attributes: options[:attributes], notifications: v
59
- end
60
- end
61
- end
62
-
63
- end
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
- 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
21
+ def create_initializer
22
+ template 'initializer.rb', 'config/initializers/notification-renderer.rb'
23
+ end
32
24
 
33
- def show_readme
34
- readme 'README.md'
35
- end
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
- private
33
+ def create_templates
34
+ system 'rails g notification_renderer:type -t notification'
35
+ end
38
36
 
39
- def migration_version
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
- 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
-
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
- 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
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
- def change
3
- add_column :notifications, :type, :string, index: true
4
- end
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
- 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
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
- 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
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
- require 'rails/railtie'
2
- require 'action_view'
3
-
4
- module NotificationRenderer
5
- class Engine < ::Rails::Engine
6
- end
7
- end
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
- require 'notification-handler'
2
- require 'active_support'
3
-
4
- module NotificationRenderer
5
- module NotificationLibrary
6
-
7
- extend ActiveSupport::Concern
8
-
9
- module ClassMethods
10
-
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
- notifications.each_pair do |k, v|
26
- if v.is_a? Hash
27
- recursive_grouping v, group_by
28
- else
29
- k = v.grouping_by group_by
30
- end
31
- end
32
- else
33
- notifications.grouping_by group_by
34
- end
35
- notifications
36
- end
37
-
38
- end
39
-
40
- def type
41
- self[:type] || NotificationRenderer.configuration.default_type
42
- end
43
-
44
- end
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
- require 'notification-handler'
2
- require 'active_support'
3
-
4
- module NotificationRenderer
5
- module NotificationScopes
6
-
7
- extend ActiveSupport::Concern
8
-
9
- module ClassMethods
10
-
11
- def method_missing m, *args
12
- if m.to_s[/(.+)_type/]
13
- where type: $1.singularize.classify
14
- else
15
- super
16
- end
17
- end
18
-
19
- def respond_to? m, include_private = false
20
- super || m.to_s[/(.+)_type/]
21
- end
22
-
23
- end
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