activity_notification 0.0.9 → 0.0.10

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.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +5 -0
  4. data/.yardopts +3 -0
  5. data/Gemfile +5 -0
  6. data/Gemfile.lock +50 -44
  7. data/README.md +242 -81
  8. data/Rakefile +13 -13
  9. data/activity_notification.gemspec +6 -8
  10. data/app/controllers/activity_notification/notifications_controller.rb +89 -11
  11. data/app/controllers/activity_notification/notifications_with_devise_controller.rb +12 -3
  12. data/app/mailers/activity_notification/mailer.rb +3 -0
  13. data/gemfiles/Gemfile.rails-4.2 +13 -0
  14. data/gemfiles/Gemfile.rails-4.2.lock +190 -0
  15. data/gemfiles/Gemfile.rails-5.0 +14 -0
  16. data/gemfiles/Gemfile.rails-5.0.lock +201 -0
  17. data/lib/activity_notification.rb +10 -6
  18. data/lib/activity_notification/apis/notification_api.rb +137 -27
  19. data/lib/activity_notification/common.rb +48 -24
  20. data/lib/activity_notification/config.rb +68 -10
  21. data/lib/activity_notification/controllers/store_controller.rb +13 -5
  22. data/lib/activity_notification/helpers/polymorphic_helpers.rb +17 -3
  23. data/lib/activity_notification/helpers/view_helpers.rb +161 -45
  24. data/lib/activity_notification/mailers/helpers.rb +121 -83
  25. data/lib/activity_notification/models/concerns/notifiable.rb +162 -69
  26. data/lib/activity_notification/models/concerns/notifier.rb +2 -0
  27. data/lib/activity_notification/models/concerns/target.rb +124 -25
  28. data/lib/activity_notification/models/notification.rb +168 -4
  29. data/lib/activity_notification/rails/routes.rb +50 -48
  30. data/lib/activity_notification/renderable.rb +106 -26
  31. data/lib/activity_notification/roles/acts_as_notifiable.rb +99 -26
  32. data/lib/activity_notification/roles/acts_as_notifier.rb +3 -0
  33. data/lib/activity_notification/roles/acts_as_target.rb +70 -0
  34. data/lib/activity_notification/version.rb +1 -1
  35. data/lib/generators/activity_notification/active_record/migration_generator.rb +3 -1
  36. data/lib/generators/activity_notification/controllers_generator.rb +5 -0
  37. data/lib/generators/activity_notification/install_generator.rb +7 -3
  38. data/lib/generators/activity_notification/models/notification_generator.rb +4 -2
  39. data/lib/generators/activity_notification/views_generator.rb +20 -0
  40. data/spec/concerns/apis/notification_api_spec.rb +105 -36
  41. data/spec/concerns/common_spec.rb +1 -1
  42. data/spec/concerns/models/notifiable_spec.rb +2 -2
  43. data/spec/concerns/models/notifier_spec.rb +1 -1
  44. data/spec/concerns/models/target_spec.rb +9 -8
  45. data/spec/controllers/notifications_controller_shared_examples.rb +101 -28
  46. data/spec/controllers/notifications_with_devise_controller_spec.rb +14 -4
  47. data/spec/helpers/view_helpers_spec.rb +3 -3
  48. data/spec/mailers/mailer_spec.rb +1 -1
  49. data/spec/models/notification_spec.rb +57 -3
  50. data/spec/rails_app/app/models/article.rb +1 -2
  51. data/spec/rails_app/app/models/comment.rb +8 -6
  52. data/spec/rails_app/app/models/user.rb +1 -1
  53. data/spec/rails_app/app/views/layouts/_header.html.erb +2 -0
  54. data/spec/rails_app/config/application.rb +3 -1
  55. data/spec/rails_app/config/environment.rb +12 -2
  56. data/spec/rails_app/config/environments/test.rb +11 -2
  57. data/spec/roles/acts_as_notifiable_spec.rb +2 -2
  58. data/spec/roles/acts_as_notifier_spec.rb +1 -1
  59. data/spec/roles/acts_as_target_spec.rb +3 -3
  60. data/spec/spec_helper.rb +6 -0
  61. metadata +35 -40
  62. data/spec/rails_app/app/models/concerns/.keep +0 -0
@@ -4,27 +4,31 @@ module ActivityNotification
4
4
  # Accepts Symbols, which it will send against context.
5
5
  # Accepts Procs, which it will execute with controller and context.
6
6
  # Both Symbols and Procs will be passed arguments of this method.
7
+ # Also accepts Hash of these Symbols or Procs.
8
+ # If any other value will be passed, returns original value.
9
+ #
10
+ # @param [Object] context Context to resolve parameter, which is usually target or notificable model
11
+ # @param [Symbol, Proc, Hash, Object] thing Symbol or Proc to resolve parameter
12
+ # @param [Array] args Arguments to pass to thing as method
13
+ # @return [Object] Resolved parameter value
7
14
  def self.resolve_value(context, thing, *args)
8
15
  case thing
9
16
  when Symbol
10
- begin
11
- context.__send__(thing, ActivityNotification.get_controller, *args)
12
- rescue ArgumentError
13
- begin
14
- context.__send__(thing, ActivityNotification.get_controller)
15
- rescue ArgumentError
16
- context.__send__(thing)
17
- end
17
+ symbol_method = context.method(thing)
18
+ if symbol_method.arity > 1
19
+ symbol_method.call(ActivityNotification.get_controller, *args)
20
+ elsif symbol_method.arity > 0
21
+ symbol_method.call(ActivityNotification.get_controller)
22
+ else
23
+ symbol_method.call
18
24
  end
19
25
  when Proc
20
- begin
26
+ if thing.arity > 2
21
27
  thing.call(ActivityNotification.get_controller, context, *args)
22
- rescue ArgumentError
23
- begin
24
- thing.call(ActivityNotification.get_controller, context)
25
- rescue ArgumentError
26
- thing.call(context)
27
- end
28
+ elsif thing.arity > 1
29
+ thing.call(ActivityNotification.get_controller, context)
30
+ else
31
+ thing.call(context)
28
32
  end
29
33
  when Hash
30
34
  thing.dup.tap do |hash|
@@ -37,24 +41,34 @@ module ActivityNotification
37
41
  end
38
42
  end
39
43
 
44
+ # Common module included in target and notifiable model.
45
+ # Provides methods to resolve parameters from configured field or defined method.
46
+ # Also provides methods to convert into resource name or class name as string.
40
47
  module Common
41
48
 
42
49
  # Used to transform value from metadata to data which belongs model instance.
43
- # Accepts Symbols, which it will send against this instance.
50
+ # Accepts Symbols, which it will send against this instance,
44
51
  # Accepts Procs, which it will execute with this instance.
45
52
  # Both Symbols and Procs will be passed arguments of this method.
53
+ # Also accepts Hash of these Symbols or Procs.
54
+ # If any other value will be passed, returns original value.
55
+ #
56
+ # @param [Symbol, Proc, Hash, Object] thing Symbol or Proc to resolve parameter
57
+ # @param [Array] args Arguments to pass to thing as method
58
+ # @return [Object] Resolved parameter value
46
59
  def resolve_value(thing, *args)
47
60
  case thing
48
61
  when Symbol
49
- begin
50
- __send__(thing, *args)
51
- rescue ArgumentError
52
- __send__(thing)
62
+ symbol_method = method(thing)
63
+ if symbol_method.arity > 0
64
+ symbol_method.call(*args)
65
+ else
66
+ symbol_method.call
53
67
  end
54
68
  when Proc
55
- begin
69
+ if thing.arity > 1
56
70
  thing.call(self, *args)
57
- rescue ArgumentError
71
+ else
58
72
  thing.call(self)
59
73
  end
60
74
  when Hash
@@ -68,24 +82,34 @@ module ActivityNotification
68
82
  end
69
83
  end
70
84
 
85
+ # Convets to class name.
86
+ # @return [String] Class name
71
87
  def to_class_name
72
88
  self.class.name
73
89
  end
74
90
 
91
+ # Convets to singularized model name (resource name).
92
+ # @return [String] Singularized model name (resource name)
75
93
  def to_resource_name
76
94
  self.class.name.demodulize.singularize.underscore
77
95
  end
78
96
 
97
+ # Convets to pluralized model name (resources name).
98
+ # @return [String] Pluralized model name (resources name)
79
99
  def to_resources_name
80
100
  self.class.name.demodulize.pluralize.underscore
81
101
  end
82
102
 
83
- #TODO Is it the best solution?
103
+ # Convets to printable model type name to be humanized.
104
+ # @return [String] Printable model type name
105
+ # @todo Is this the best to make readable?
84
106
  def printable_type
85
107
  "#{self.class.name.demodulize.humanize}"
86
108
  end
87
109
 
88
- #TODO Is it the best solution?
110
+ # Convets to printable model name to show in view or email.
111
+ # @return [String] Printable model name
112
+ # @todo Is this the best to make readable?
89
113
  def printable_name
90
114
  "#{self.printable_type} (#{id})"
91
115
  end
@@ -1,21 +1,79 @@
1
1
  module ActivityNotification
2
2
  # Class used to initialize configuration object.
3
3
  class Config
4
- attr_accessor :enabled,
5
- :table_name,
6
- :email_enabled,
7
- :mailer_sender,
8
- :mailer,
9
- :parent_mailer,
10
- :parent_controller,
11
- :opened_limit
4
+ # @overload enabled
5
+ # @return [Boolean] Whether ActivityNotification is enabled.
6
+ # @overload enabled=(value)
7
+ # Sets the enabled
8
+ # @param [Boolean] enabled The new enabled
9
+ # @return [Boolean] Whether ActivityNotification is enabled.
10
+ attr_accessor :enabled
12
11
 
12
+ # @overload table_name
13
+ # @return [String] Table name to store notifications.
14
+ # @overload table_name=(value)
15
+ # Sets the table_name
16
+ # @param [String] table_name The new table_name
17
+ # @return [String] Table name to store notifications.
18
+ attr_accessor :table_name
19
+
20
+ # @overload email_enabled
21
+ # @return [Boolean] Whether activity_notification sends notification email.
22
+ # @overload email_enabled=(value)
23
+ # Sets the email_enabled
24
+ # @param [Boolean] email_enabled The new email_enabled
25
+ # @return [Boolean] Whether activity_notification sends notification email.
26
+ attr_accessor :email_enabled
27
+
28
+ # @overload mailer_sender
29
+ # @return [String] Email address as sender of notification email.
30
+ # @overload mailer_sender=(value)
31
+ # Sets the mailer_sender
32
+ # @param [String] mailer_sender The new mailer_sender
33
+ # @return [String] Email address as sender of notification email.
34
+ attr_accessor :mailer_sender
35
+
36
+ # @overload mailer
37
+ # @return [String] Mailer class for email notification.
38
+ # @overload mailer=(value)
39
+ # Sets the mailer
40
+ # @param [String] mailer The new mailer
41
+ # @return [String] Mailer class for email notification.
42
+ attr_accessor :mailer
43
+
44
+ # @overload parent_mailer
45
+ # @return [String] Base mailer class for email notification.
46
+ # @overload parent_mailer=(value)
47
+ # Sets the parent_mailer
48
+ # @param [String] parent_mailer The new parent_mailer
49
+ # @return [String] Base mailer class for email notification.
50
+ attr_accessor :parent_mailer
51
+
52
+ # @overload parent_controller
53
+ # @return [String] Base controller class for notifications_controller.
54
+ # @overload parent_controller=(value)
55
+ # Sets the parent_controller
56
+ # @param [String] parent_controller The new parent_controller
57
+ # @return [String] Base controller class for notifications_controller.
58
+ attr_accessor :parent_controller
59
+
60
+ # @overload opened_limit
61
+ # @return [Integer] Default limit to query for opened notifications.
62
+ # @overload opened_limit=(value)
63
+ # Sets the opened_limit
64
+ # @param [Integer] opened_limit The new opened_limit
65
+ # @return [Integer] Default limit to query for opened notifications.
66
+ attr_accessor :opened_limit
67
+
68
+ # Initialize configuration for ActivityNotification.
69
+ # These configuration can be overriden in initializer.
70
+ # @return [Config] A new instance of Config
13
71
  def initialize
14
72
  @enabled = true
15
- @table_name = "notifications"
73
+ @table_name = 'notifications'
16
74
  @email_enabled = false
17
75
  @mailer_sender = nil
18
- @mailer = "ActivityNotification::Mailer"
76
+ @mailer = 'ActivityNotification::Mailer'
19
77
  @parent_mailer = 'ActionMailer::Base'
20
78
  @parent_controller = 'ApplicationController'
21
79
  @opened_limit = 10
@@ -1,17 +1,22 @@
1
1
  module ActivityNotification
2
2
  class << self
3
3
  # Setter for remembering controller instance
4
+ #
5
+ # @param [NotificationsController, NotificationsWithDeviseController] controller Controller instance to set
6
+ # @return [NotificationsController, NotificationsWithDeviseController]] Controller instance to be set
4
7
  def set_controller(controller)
5
8
  Thread.current[:activity_notification_controller] = controller
6
9
  end
7
10
 
8
11
  # Getter for accessing the controller instance
12
+ #
13
+ # @return [NotificationsController, NotificationsWithDeviseController]] Controller instance to be set
9
14
  def get_controller
10
15
  Thread.current[:activity_notification_controller]
11
16
  end
12
17
  end
13
18
 
14
- # Module included in controllers to allow p_a access to controller instance
19
+ # Module included in controllers to allow ActivityNotification access to controller instance
15
20
  module StoreController
16
21
  extend ActiveSupport::Concern
17
22
 
@@ -20,11 +25,14 @@ module ActivityNotification
20
25
  around_filter :store_controller_for_activity_notification unless respond_to?(:around_action)
21
26
  end
22
27
 
28
+ # Sets controller as around action to use controller instance in models or helpers
23
29
  def store_controller_for_activity_notification
24
- ActivityNotification.set_controller(self)
25
- yield
26
- ensure
27
- ActivityNotification.set_controller(nil)
30
+ begin
31
+ ActivityNotification.set_controller(self)
32
+ yield
33
+ ensure
34
+ ActivityNotification.set_controller(nil)
35
+ end
28
36
  end
29
37
  end
30
38
  end
@@ -1,25 +1,39 @@
1
1
  module ActivityNotification
2
+ # Provides extension of String class to help polymorphic implementation.
2
3
  module PolymorphicHelpers
3
4
  extend ActiveSupport::Concern
4
5
 
5
6
  included do
6
7
  class ::String
8
+ # Convets to model instance.
9
+ # @return [Object] Model instance
7
10
  def to_model_name
8
11
  singularize.camelize
9
12
  end
10
-
13
+
14
+ # Convets to model class.
15
+ # @return [Class] Model class
11
16
  def to_model_class
12
17
  to_model_name.classify.constantize
13
18
  end
14
-
19
+
20
+ # Convets to singularized model name (resource name).
21
+ # @return [String] Singularized model name (resource name)
15
22
  def to_resource_name
16
23
  singularize.underscore
17
24
  end
18
-
25
+
26
+ # Convets to pluralized model name (resources name).
27
+ # @return [String] Pluralized model name (resources name)
19
28
  def to_resources_name
20
29
  pluralize.underscore
21
30
  end
22
31
 
32
+ # Convets to boolean.
33
+ # Returns true for 'true', '1', 'yes', 'on' and 't'.
34
+ # Returns false for 'false', '0', 'no', 'off' and 'f'.
35
+ # @param [Boolean] default Default value to return when the String is not interpretable
36
+ # @return [Boolean] Convered boolean value
23
37
  def to_boolean(default = nil)
24
38
  return true if ['true', '1', 'yes', 'on', 't'].include? self
25
39
  return false if ['false', '0', 'no', 'off', 'f'].include? self
@@ -1,9 +1,20 @@
1
- # Provides a shortcut from views to the rendering method.
2
1
  module ActivityNotification
2
+ # Provides a shortcut from views to the rendering method.
3
3
  # Module extending ActionView::Base and adding `render_notification` helper.
4
4
  module ViewHelpers
5
- # View helper for rendering an notification, calls {ActivityNotification::Notification#render} internally.
6
- def render_notification notifications, options = {}
5
+ # View helper for rendering an notification, calls {Notification#render} internally.
6
+ # @see Notification#render
7
+ #
8
+ # @param [Notification, Array<Notificaion>] Array or single instance of notifications to render
9
+ # @param [Hash] options Options for rendering notifications
10
+ # @option options [String, Symbol] :target (nil) Target type name to find template or i18n text
11
+ # @option options [String] :partial ("activity_notification/notifications/#{target}", controller.target_view_path, 'activity_notification/notifications/default') Partial template name
12
+ # @option options [String] :partial_root (self.key.gsub('.', '/')) Root path of partial template
13
+ # @option options [String] :layout (nil) Layout template name
14
+ # @option options [String] :layout_root ('layouts') Root path of layout template
15
+ # @option options [String] :fallback (nil) Fallback template to use when MissingTemplate is raised. Set :text to use i18n text as fallback.
16
+ # @return [String] Rendered view or text as string
17
+ def render_notification(notifications, options = {})
7
18
  if notifications.is_a? ActivityNotification::Notification
8
19
  notifications.render self, options
9
20
  elsif notifications.respond_to?(:map)
@@ -13,92 +24,197 @@ module ActivityNotification
13
24
  end
14
25
  alias_method :render_notifications, :render_notification
15
26
 
16
- # View helper for rendering embedded partial template of target's notifications
27
+ # View helper for rendering on notifications of the target to embedded partial template.
28
+ # It calls {Notification#render} to prepare view as `content_for :index_content`
29
+ # and render partial index calling `yield :index_content` internally.
30
+ # For example, this method can be used for notification index as dropdown in common header.
31
+ # @todo Show examples
32
+ #
33
+ # @param [Object] target Target instance of the rendering notifications
34
+ # @param [Hash] options Options for rendering notifications
35
+ # @option options [String, Symbol] :target (nil) Target type name to find template or i18n text
36
+ # @option options [String] :partial_root ("activity_notification/notifications/#{target.to_resources_name}", 'activity_notification/notifications/default') Root path of partial template
37
+ # @option options [String] :notification_partial ("activity_notification/notifications/#{target.to_resources_name}", controller.target_view_path, 'activity_notification/notifications/default') Partial template name of the notification index content
38
+ # @option options [String] :layout_root ('layouts') Root path of layout template
39
+ # @option options [String] :notification_layout (nil) Layout template name of the notification index content
40
+ # @option options [String] :fallback (nil) Fallback template to use when MissingTemplate is raised. Set :text to use i18n text as fallback.
41
+ # @option options [String] :partial ('index') Partial template name of the partial index
42
+ # @option options [String] :layout (nil) Layout template name of the partial index
43
+ # @return [String] Rendered view or text as string
17
44
  def render_notification_of target, options = {}
18
45
  return unless target.is_a? ActivityNotification::Target
19
46
 
20
- partial_path = options.delete(:partial) || "index"
21
- partial_root = options[:partial_root] ||
22
- "activity_notification/notifications/#{target.to_resources_name}"
23
- partial = select_path(partial_path, partial_root)
24
- layout = options[:layout].present? ?
25
- select_path(options.delete(:layout), (options[:layout_root] || "layouts")) :
26
- nil
27
- locals = (options[:locals] || {}).merge(target: target)
28
-
29
47
  # Prepare content for notifications index
30
- notification_options = options.merge\
31
- target: target.to_resources_name,
32
- partial: options[:notification_partial],
33
- layout: options[:notification_layout]
34
- case options[:index_content]
35
- when :simple
36
- notification_index = target.notification_index
37
- when :none
38
- else
39
- notification_index = target.notification_index_with_attributes
40
- end
41
-
42
- if notification_index.present?
43
- content_for :notification_index do
44
- @target = target
45
- begin
46
- render_notification notification_index, notification_options
47
- rescue ActionView::MissingTemplate
48
- notification_options.delete(:target)
49
- render_notification notification_index, notification_options
50
- end
48
+ notification_options = options.merge target: target.to_resources_name,
49
+ partial: options[:notification_partial], layout: options[:notification_layout]
50
+ notification_index =
51
+ case options[:index_content]
52
+ when :simple then target.notification_index
53
+ when :none then target.notifications.none
54
+ else target.notification_index_with_attributes
51
55
  end
52
- end
56
+ prepare_content_for(target, notification_index, notification_options)
53
57
 
54
58
  # Render partial index
55
- begin
56
- render options.merge(partial: partial, layout: layout, locals: locals)
57
- rescue ActionView::MissingTemplate
58
- partial_root = "activity_notification/notifications/default"
59
- partial = select_path(partial_path, partial_root)
60
- render options.merge(partial: partial, layout: layout, locals: locals)
61
- end
59
+ render_partial_index(target, options)
62
60
  end
63
61
  alias_method :render_notifications_of, :render_notification_of
64
62
 
65
- # Url helper methods
66
- #TODO Is there any other better solution?
67
- #TODO Must handle devise namespace
63
+ # Returns notification_path for the notification
64
+ #
65
+ # @param [Notification] notification Notification instance
66
+ # @param [Hash] params Request parameters
67
+ # @return [String] notification_path for the notification
68
+ # @todo Needs any other better implementation
69
+ # @todo Must handle devise namespace
68
70
  def notification_path_for(notification, params = {})
69
71
  send("#{notification.target.to_resource_name}_notification_path", notification.target, notification, params)
70
72
  end
71
73
 
74
+ # Returns move_notification_path for the target of specified notification
75
+ #
76
+ # @param [Notification] notification Notification instance
77
+ # @param [Hash] params Request parameters
78
+ # @return [String] move_notification_path for the target
79
+ # @todo Needs any other better implementation
80
+ # @todo Must handle devise namespace
72
81
  def move_notification_path_for(notification, params = {})
73
82
  send("move_#{notification.target.to_resource_name}_notification_path", notification.target, notification, params)
74
83
  end
75
84
 
85
+ # Returns open_notification_path for the target of specified notification
86
+ #
87
+ # @param [Notification] notification Notification instance
88
+ # @param [Hash] params Request parameters
89
+ # @return [String] open_notification_path for the target
90
+ # @todo Needs any other better implementation
91
+ # @todo Must handle devise namespace
76
92
  def open_notification_path_for(notification, params = {})
77
93
  send("open_#{notification.target.to_resource_name}_notification_path", notification.target, notification, params)
78
94
  end
79
95
 
96
+ # Returns open_all_notifications_path for the target of specified notification
97
+ #
98
+ # @param [Notification] notification Notification instance
99
+ # @param [Hash] params Request parameters
100
+ # @return [String] open_all_notifications_path for the target
101
+ # @todo Needs any other better implementation
102
+ # @todo Must handle devise namespace
80
103
  def open_all_notifications_path_for(target, params = {})
81
104
  send("open_all_#{target.to_resource_name}_notifications_path", target, params)
82
105
  end
83
106
 
107
+ # Returns notification_url for the target of specified notification
108
+ #
109
+ # @param [Notification] notification Notification instance
110
+ # @param [Hash] params Request parameters
111
+ # @return [String] notification_url for the target
112
+ # @todo Needs any other better implementation
113
+ # @todo Must handle devise namespace
84
114
  def notification_url_for(notification, params = {})
85
115
  send("#{notification.target.to_resource_name}_notification_url", notification.target, notification, params)
86
116
  end
87
117
 
118
+ # Returns move_notification_url for the target of specified notification
119
+ #
120
+ # @param [Notification] notification Notification instance
121
+ # @param [Hash] params Request parameters
122
+ # @return [String] move_notification_url for the target
123
+ # @todo Needs any other better implementation
124
+ # @todo Must handle devise namespace
88
125
  def move_notification_url_for(notification, params = {})
89
126
  send("move_#{notification.target.to_resource_name}_notification_url", notification.target, notification, params)
90
127
  end
91
128
 
129
+ # Returns open_notification_url for the target of specified notification
130
+ #
131
+ # @param [Notification] notification Notification instance
132
+ # @param [Hash] params Request parameters
133
+ # @return [String] open_notification_url for the target
134
+ # @todo Needs any other better implementation
135
+ # @todo Must handle devise namespace
92
136
  def open_notification_url_for(notification, params = {})
93
137
  send("open_#{notification.target.to_resource_name}_notification_url", notification.target, notification, params)
94
138
  end
95
139
 
140
+ # Returns open_all_notifications_url for the target of specified notification
141
+ #
142
+ # @param [Notification] notification Notification instance
143
+ # @param [Hash] params Request parameters
144
+ # @return [String] open_all_notifications_url for the target
145
+ # @todo Needs any other better implementation
146
+ # @todo Must handle devise namespace
96
147
  def open_all_notifications_url_for(target, params = {})
97
148
  send("open_all_#{target.to_resource_name}_notifications_url", target, params)
98
149
  end
99
150
 
151
+
100
152
  private
101
153
 
154
+ # Prepare content for notification index
155
+ # @api private
156
+ #
157
+ # @param [Object] target Notification target instance
158
+ # @param [Array<Notificaion>] notification_index Array notification index
159
+ # @param [Hash] params Option parameter to send render_notification
160
+ def prepare_content_for(target, notification_index, params)
161
+ content_for :notification_index do
162
+ @target = target
163
+ begin
164
+ render_notification notification_index, params
165
+ rescue ActionView::MissingTemplate
166
+ params.delete(:target)
167
+ render_notification notification_index, params
168
+ end
169
+ end
170
+ end
171
+
172
+ # Render partial index of notifications
173
+ # @api private
174
+ #
175
+ # @param [Object] target Notification target instance
176
+ # @param [Hash] params Option parameter to send render
177
+ # @return [String] Rendered partial index view as string
178
+ def render_partial_index(target, params)
179
+ index_path = params.delete(:partial)
180
+ partial = partial_index_path(target, index_path, params[:partial_root])
181
+ layout = layout_path(params.delete(:layout), params[:layout_root])
182
+ locals = (params[:locals] || {}).merge(target: target)
183
+ begin
184
+ render params.merge(partial: partial, layout: layout, locals: locals)
185
+ rescue ActionView::MissingTemplate
186
+ partial = partial_index_path(target, index_path, 'activity_notification/notifications/default')
187
+ render params.merge(partial: partial, layout: layout, locals: locals)
188
+ end
189
+ end
190
+
191
+ # Returns partial index path from options
192
+ # @api private
193
+ #
194
+ # @param [Object] target Notification target instance
195
+ # @param [String] path Partial index template name
196
+ # @param [String] root Root path of partial index template
197
+ # @return [String] Partial index template path
198
+ def partial_index_path(target, path = nil, root = nil)
199
+ path ||= 'index'
200
+ root ||= "activity_notification/notifications/#{target.to_resources_name}"
201
+ select_path(path, root)
202
+ end
203
+
204
+ # Returns layout path from options
205
+ # @api private
206
+ #
207
+ # @param [String] path Layout template name
208
+ # @param [String] root Root path of layout template
209
+ # @return [String] Layout template path
210
+ def layout_path(path = nil, root = nil)
211
+ path.nil? and return
212
+ root ||= 'layouts'
213
+ select_path(path, root)
214
+ end
215
+
216
+ # Select template path
217
+ # @api private
102
218
  def select_path(path, root)
103
219
  [root, path].map(&:to_s).join('/')
104
220
  end