activity_notification 0.0.10 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -0
  3. data/Gemfile +6 -3
  4. data/Gemfile.lock +74 -58
  5. data/README.md +53 -26
  6. data/activity_notification.gemspec +2 -0
  7. data/app/controllers/activity_notification/notifications_controller.rb +4 -4
  8. data/app/mailers/activity_notification/mailer.rb +9 -3
  9. data/app/views/activity_notification/mailer/default/default.html.erb +9 -4
  10. data/app/views/activity_notification/mailer/default/default.text.erb +10 -0
  11. data/app/views/activity_notification/notifications/default/_default.html.erb +173 -34
  12. data/app/views/activity_notification/notifications/default/_index.html.erb +119 -11
  13. data/app/views/activity_notification/notifications/default/destroy.js.erb +2 -2
  14. data/app/views/activity_notification/notifications/default/index.html.erb +25 -14
  15. data/app/views/activity_notification/notifications/default/open.js.erb +2 -2
  16. data/app/views/activity_notification/notifications/default/open_all.js.erb +2 -2
  17. data/app/views/activity_notification/notifications/default/show.html.erb +1 -1
  18. data/gemfiles/Gemfile.rails-4.2 +0 -2
  19. data/gemfiles/Gemfile.rails-4.2.lock +3 -3
  20. data/gemfiles/Gemfile.rails-5.0 +1 -3
  21. data/lib/activity_notification.rb +10 -9
  22. data/lib/activity_notification/apis/notification_api.rb +108 -14
  23. data/lib/activity_notification/common.rb +11 -2
  24. data/lib/activity_notification/config.rb +13 -13
  25. data/lib/activity_notification/helpers/view_helpers.rb +26 -4
  26. data/lib/activity_notification/mailers/helpers.rb +8 -4
  27. data/lib/activity_notification/models.rb +4 -0
  28. data/lib/activity_notification/models/concerns/group.rb +32 -0
  29. data/lib/activity_notification/models/concerns/notifiable.rb +60 -32
  30. data/lib/activity_notification/models/concerns/notifier.rb +17 -1
  31. data/lib/activity_notification/models/concerns/target.rb +114 -55
  32. data/lib/activity_notification/models/notification.rb +26 -25
  33. data/lib/activity_notification/rails.rb +1 -0
  34. data/lib/activity_notification/renderable.rb +8 -3
  35. data/lib/activity_notification/roles/acts_as_common.rb +28 -0
  36. data/lib/activity_notification/roles/acts_as_group.rb +38 -0
  37. data/lib/activity_notification/roles/acts_as_notifiable.rb +56 -22
  38. data/lib/activity_notification/roles/acts_as_notifier.rb +25 -2
  39. data/lib/activity_notification/roles/acts_as_target.rb +27 -9
  40. data/lib/activity_notification/version.rb +1 -1
  41. data/lib/generators/templates/activity_notification.rb +1 -1
  42. data/spec/concerns/apis/notification_api_spec.rb +361 -2
  43. data/spec/concerns/common_spec.rb +36 -0
  44. data/spec/concerns/models/group_spec.rb +61 -0
  45. data/spec/concerns/models/notifiable_spec.rb +37 -0
  46. data/spec/concerns/models/notifier_spec.rb +48 -0
  47. data/spec/concerns/models/target_spec.rb +81 -31
  48. data/spec/factories/dummy/dummy_group.rb +4 -0
  49. data/spec/helpers/view_helpers_spec.rb +13 -0
  50. data/spec/mailers/mailer_spec.rb +8 -1
  51. data/spec/models/dummy/dummy_group_spec.rb +6 -0
  52. data/spec/rails_app/app/assets/stylesheets/application.css +15 -0
  53. data/spec/rails_app/app/assets/stylesheets/reset.css +85 -0
  54. data/spec/rails_app/app/assets/stylesheets/style.css +244 -0
  55. data/spec/rails_app/app/controllers/articles_controller.rb +1 -1
  56. data/spec/rails_app/app/models/admin.rb +2 -1
  57. data/spec/rails_app/app/models/article.rb +9 -2
  58. data/spec/rails_app/app/models/comment.rb +8 -2
  59. data/spec/rails_app/app/models/dummy/dummy_group.rb +4 -0
  60. data/spec/rails_app/app/models/user.rb +8 -3
  61. data/spec/rails_app/app/views/articles/_form.html.erb +14 -10
  62. data/spec/rails_app/app/views/articles/edit.html.erb +8 -6
  63. data/spec/rails_app/app/views/articles/index.html.erb +59 -67
  64. data/spec/rails_app/app/views/articles/new.html.erb +7 -5
  65. data/spec/rails_app/app/views/articles/show.html.erb +47 -36
  66. data/spec/rails_app/app/views/layouts/_header.html.erb +36 -9
  67. data/spec/rails_app/app/views/layouts/application.html.erb +8 -6
  68. data/spec/rails_app/config/environments/development.rb +9 -0
  69. data/spec/rails_app/config/initializers/activity_notification.rb +1 -1
  70. data/spec/rails_app/db/schema.rb +14 -20
  71. data/spec/rails_app/db/seeds.rb +5 -5
  72. data/spec/rails_app/lib/mailer_previews/mailer_preview.rb +13 -0
  73. data/spec/roles/acts_as_group_spec.rb +32 -0
  74. data/spec/roles/acts_as_notifiable_spec.rb +1 -1
  75. data/spec/roles/acts_as_notifier_spec.rb +15 -0
  76. data/spec/roles/acts_as_target_spec.rb +1 -1
  77. metadata +52 -2
@@ -5,7 +5,7 @@ module ActivityNotification
5
5
  include Common
6
6
  include NotificationApi
7
7
  self.table_name = ActivityNotification.config.table_name
8
-
8
+
9
9
  # Belongs to target instance of this notification as polymorphic association.
10
10
  # @scope instance
11
11
  # @return [Object] Target instance of this notification
@@ -32,7 +32,7 @@ module ActivityNotification
32
32
  # Only group owner instance has :group_members value.
33
33
  # Group member instance has nil as :group_members association.
34
34
  # @scope instance
35
- # @return [Array<Notificaion>] Array or database query of the group member notification instances of this notification
35
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of the group member notification instances of this notification
36
36
  has_many :group_members, class_name: :Notification, foreign_key: :group_owner_id
37
37
 
38
38
  # Belongs to :otifier instance of this notification.
@@ -49,17 +49,17 @@ module ActivityNotification
49
49
 
50
50
  # Selects group owner notifications only.
51
51
  # @scope class
52
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
52
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
53
53
  scope :group_owners_only, -> { where(group_owner_id: nil) }
54
54
 
55
55
  # Selects group member notifications only.
56
56
  # @scope class
57
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
57
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
58
58
  scope :group_members_only, -> { where.not(group_owner_id: nil) }
59
59
 
60
60
  # Selects unopened notifications only.
61
61
  # @scope class
62
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
62
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
63
63
  scope :unopened_only, -> { where(opened_at: nil) }
64
64
 
65
65
  # Selects unopened notification index.
@@ -67,37 +67,37 @@ module ActivityNotification
67
67
  # @example Get unopened notificaton index of the @user
68
68
  # @notifications = @user.unopened_index
69
69
  # @scope class
70
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
70
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
71
71
  scope :unopened_index, -> { unopened_only.group_owners_only.latest_order }
72
72
 
73
73
  # Selects opened notifications only without limit.
74
74
  # Be careful to get too many records with this method.
75
75
  # @scope class
76
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
76
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
77
77
  scope :opened_only!, -> { where.not(opened_at: nil) }
78
78
 
79
79
  # Selects opened notifications only with limit.
80
80
  # @scope class
81
81
  # @param [Integer] limit Limit to query for opened notifications
82
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
82
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
83
83
  scope :opened_only, ->(limit) { opened_only!.limit(limit) }
84
84
 
85
85
  # Selects unopened notification index.
86
86
  # Defined same as `opened_only(limit).group_owners_only.latest_order`.
87
87
  # @scope class
88
88
  # @param [Integer] limit Limit to query for opened notifications
89
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
89
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
90
90
  scope :opened_index, ->(limit) { opened_only(limit).group_owners_only.latest_order }
91
91
 
92
92
  # Selects group member notifications in unopened_index.
93
93
  # @scope class
94
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
95
- scope :unopened_index_group_members_only, -> { where(group_owner_id: unopened_index.pluck(:id)) }
94
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
95
+ scope :unopened_index_group_members_only, -> { where(group_owner_id: unopened_index.map(&:id)) }
96
96
 
97
97
  # Selects group member notifications in opened_index.
98
98
  # @scope class
99
99
  # @param [Integer] limit Limit to query for opened notifications
100
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
100
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
101
101
  scope :opened_index_group_members_only, ->(limit) { where(group_owner_id: opened_index(limit).map(&:id)) }
102
102
 
103
103
  # Selects filtered notifications by target instance.
@@ -106,7 +106,7 @@ module ActivityNotification
106
106
  # @user.notifications
107
107
  # @scope class
108
108
  # @param [Object] target Target instance for filter
109
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
109
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
110
110
  scope :filtered_by_target, ->(target) { where(target: target) }
111
111
 
112
112
  # Selects filtered notifications by notifiable instance.
@@ -114,7 +114,7 @@ module ActivityNotification
114
114
  # @notifications = @user.notifications.unopened_only.filtered_by_instance(@comment)
115
115
  # @scope class
116
116
  # @param [Object] notifiable Notifiable instance for filter
117
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
117
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
118
118
  scope :filtered_by_instance, ->(notifiable) { where(notifiable: notifiable) }
119
119
 
120
120
  # Selects filtered notifications by notifiable_type.
@@ -122,7 +122,7 @@ module ActivityNotification
122
122
  # @notifications = @user.notifications.unopened_only.filtered_by_type('Comment')
123
123
  # @scope class
124
124
  # @param [String] notifiable_type Notifiable type for filter
125
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
125
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
126
126
  scope :filtered_by_type, ->(notifiable_type) { where(notifiable_type: notifiable_type) }
127
127
 
128
128
  # Selects filtered notifications by group instance.
@@ -130,7 +130,7 @@ module ActivityNotification
130
130
  # @notifications = @user.notifications.unopened_only.filtered_by_group(@article)
131
131
  # @scope class
132
132
  # @param [Object] group Group instance for filter
133
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
133
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
134
134
  scope :filtered_by_group, ->(group) { where(group: group) }
135
135
 
136
136
  # Selects filtered notifications by key.
@@ -138,7 +138,7 @@ module ActivityNotification
138
138
  # @notifications = @user.notifications.unopened_only.filtered_by_key('comment.reply')
139
139
  # @scope class
140
140
  # @param [String] key Key of the notification for filter
141
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
141
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
142
142
  scope :filtered_by_key, ->(key) { where(key: key) }
143
143
 
144
144
  # Selects filtered notifications by notifiable_type, group or key with filter options.
@@ -159,9 +159,9 @@ module ActivityNotification
159
159
  # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
160
160
  # @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
161
161
  # @option options [String] :filtered_by_key (nil) Key of the notification for filter
162
- # @return [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
162
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
163
163
  scope :filtered_by_options, ->(options = {}) {
164
- options = options.with_indifferent_access
164
+ options = ActivityNotification.cast_to_indifferent_hash(options)
165
165
  filtered_notifications = all
166
166
  if options.has_key?(:filtered_by_type)
167
167
  filtered_notifications = filtered_notifications.filtered_by_type(options[:filtered_by_type])
@@ -180,27 +180,27 @@ module ActivityNotification
180
180
  }
181
181
 
182
182
  # Includes target instance with query for notifications.
183
- # @return [ActiveRecord_AssociationRelation] Database query of notifications with target
183
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with target
184
184
  scope :with_target, -> { includes(:target) }
185
185
 
186
186
  # Includes notifiable instance with query for notifications.
187
- # @return [ActiveRecord_AssociationRelation] Database query of notifications with notifiable
187
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with notifiable
188
188
  scope :with_notifiable, -> { includes(:notifiable) }
189
189
 
190
190
  # Includes group instance with query for notifications.
191
- # @return [ActiveRecord_AssociationRelation] Database query of notifications with group
191
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with group
192
192
  scope :with_group, -> { includes(:group) }
193
193
 
194
194
  # Includes notifier instance with query for notifications.
195
- # @return [ActiveRecord_AssociationRelation] Database query of notifications with notifier
195
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications with notifier
196
196
  scope :with_notifier, -> { includes(:notifier) }
197
197
 
198
198
  # Orders by latest (newest) first as created_at: :desc.
199
- # @return [ActiveRecord_AssociationRelation] Database query of notifications ordered by latest first
199
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications ordered by latest first
200
200
  scope :latest_order, -> { order(created_at: :desc) }
201
201
 
202
202
  # Orders by earliest (older) first as created_at: :asc.
203
- # @return [ActiveRecord_AssociationRelation] Database query of notifications ordered by earliest first
203
+ # @return [ActiveRecord_AssociationRelation<Notificaion>] Database query of notifications ordered by earliest first
204
204
  scope :earliest_order, -> { order(created_at: :asc) }
205
205
 
206
206
  # Returns latest notification instance.
@@ -210,5 +210,6 @@ module ActivityNotification
210
210
  # Returns earliest notification instance.
211
211
  # @return [Notification] Earliest notification instance
212
212
  scope :earliest, -> { earliest_order.first }
213
+
213
214
  end
214
215
  end
@@ -2,5 +2,6 @@ require 'activity_notification/rails/routes'
2
2
 
3
3
  module ActivityNotification #:nodoc:
4
4
  class Engine < ::Rails::Engine #:nodoc:
5
+ require 'jquery-rails'
5
6
  end
6
7
  end
@@ -21,7 +21,12 @@ module ActivityNotification
21
21
  k.push('text')
22
22
  k = k.join('.')
23
23
 
24
- I18n.t(k, (parameters.merge(params) || {}).merge(group_member_count: group_member_count))
24
+ I18n.t(k, (parameters.merge(params) || {}).merge(
25
+ group_member_count: group_member_count,
26
+ group_notification_count: group_notification_count,
27
+ group_member_notifier_count: group_member_notifier_count,
28
+ group_notifier_count: group_notifier_count
29
+ ))
25
30
  end
26
31
 
27
32
  # Renders notification from views.
@@ -134,7 +139,7 @@ module ActivityNotification
134
139
  # @option params [String] :partial (self.key.gsub('.', '/')) Root path of partial template
135
140
  # @option params [String] :layout (nil) Layout template name
136
141
  # @option params [String] :layout_root ('layouts') Root path of layout template
137
- # @option params [String] :fallback (nil) Fallback template to use when MissingTemplate is raised. Set :text to use i18n text as fallback.
142
+ # @option params [String, Symbol] :fallback (nil) Fallback template to use when MissingTemplate is raised. Set :text to use i18n text as fallback.
138
143
  # @option params [Hash] others Parameters to be set as locals
139
144
  # @return [String] Rendered view or text as string
140
145
  def render(context, params = {})
@@ -207,7 +212,7 @@ module ActivityNotification
207
212
  # @param [Hash] params Parameters to prepare
208
213
  # @return [Hash] Prepared parameters
209
214
  def prepare_parameters(params)
210
- @prepared_params ||= self.parameters.with_indifferent_access.merge(params)
215
+ @prepared_params ||= ActivityNotification.cast_to_indifferent_hash(parameters).merge(params)
211
216
  end
212
217
 
213
218
 
@@ -0,0 +1,28 @@
1
+ module ActivityNotification
2
+ # Common module included in acts_as module.
3
+ # Provides methods to extract parameters.
4
+ module ActsAsCommon
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ protected
9
+ # Sets acts_as parameters.
10
+ # @api protected
11
+ def set_acts_as_parameters(option_list, options, field_prefix = "")
12
+ option_list.map { |key|
13
+ options[key] ?
14
+ [key, self.send("_#{field_prefix}#{key}=".to_sym, options.delete(key))] : [nil, nil]
15
+ }.to_h.delete_if { |k, _| k.nil? }
16
+ end
17
+
18
+ # Sets acts_as parameters for target.
19
+ # @api protected
20
+ def set_acts_as_parameters_for_target(target_type, option_list, options, field_prefix = "")
21
+ option_list.map { |key|
22
+ options[key] ?
23
+ [key, self.send("_#{field_prefix}#{key}".to_sym).store(target_type.to_sym, options.delete(key))] : [nil, nil]
24
+ }.to_h.delete_if { |k, _| k.nil? }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ module ActivityNotification
2
+ # Manages to add all required configurations to group models of notification.
3
+ module ActsAsGroup
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ # Adds required configurations to group models.
8
+ #
9
+ # == Parameters:
10
+ # * :printable_name or :printable_notification_group_name
11
+ # * Printable notification group name.
12
+ # This parameter is a optional since `ActivityNotification::Common.printable_name` is used as default value.
13
+ # :printable_name is the same option as :printable_notification_group_name
14
+ # @example Define printable name with article title
15
+ # # app/models/article.rb
16
+ # class Article < ActiveRecord::Base
17
+ # acts_as_notification_group printable_name: ->(article) { "article \"#{article.title}\"" }
18
+ # end
19
+ #
20
+ # @param [Hash] options Options for notifier model configuration
21
+ # @option options [Symbol, Proc, String] :printable_name (ActivityNotification::Common.printable_name) Printable notifier target name
22
+ # @return [Hash] Configured parameters as notifier model
23
+ def acts_as_group(options = {})
24
+ include Group
25
+
26
+ options[:printable_notification_group_name] ||= options.delete(:printable_name)
27
+ set_acts_as_parameters([:printable_notification_group_name], options)
28
+ end
29
+ alias_method :acts_as_notification_group, :acts_as_group
30
+
31
+ # Returns array of available notification group options in acts_as_group.
32
+ # @return [Array<Symbol>] Array of available notification group options
33
+ def available_group_options
34
+ [:printable_notification_group_name, :printable_name].freeze
35
+ end
36
+ end
37
+ end
38
+ end
@@ -18,6 +18,7 @@ module ActivityNotification
18
18
  # acts_as_notifiable :users, targets: User.all
19
19
  # end
20
20
  # @example Notify to author and users commented to the article, except comment owner self
21
+ # # app/models/comment.rb
21
22
  # class Comment < ActiveRecord::Base
22
23
  # belongs_to :article
23
24
  # belongs_to :user
@@ -32,6 +33,7 @@ module ActivityNotification
32
33
  # Notifications will be bundled by this group (and target, notifiable_type, key).
33
34
  # This parameter is a optional.
34
35
  # @example All *unopened* notifications to the same target will be grouped by `article`
36
+ # # app/models/comment.rb
35
37
  # class Comment < ActiveRecord::Base
36
38
  # belongs_to :article
37
39
  # acts_as_notifiable :users, targets: User.all, group: :article
@@ -42,6 +44,7 @@ module ActivityNotification
42
44
  # This will be stored as notifier with notification record.
43
45
  # This parameter is a optional.
44
46
  # @example Set comment owner self as notifier
47
+ # # app/models/comment.rb
45
48
  # class Comment < ActiveRecord::Base
46
49
  # belongs_to :article
47
50
  # belongs_to :user
@@ -54,10 +57,12 @@ module ActivityNotification
54
57
  # You can use these additional parameters in your notification view or i18n text.
55
58
  # This parameter is a optional.
56
59
  # @example Set constant values as additional parameter
60
+ # # app/models/comment.rb
57
61
  # class Comment < ActiveRecord::Base
58
62
  # acts_as_notifiable :users, targets: User.all, parameters: { default_param: '1' }
59
63
  # end
60
64
  # @example Set comment body as additional parameter
65
+ # # app/models/comment.rb
61
66
  # class Comment < ActiveRecord::Base
62
67
  # acts_as_notifiable :users, targets: User.all, parameters: ->(comment, key) { body: comment.body }
63
68
  # end
@@ -69,6 +74,7 @@ module ActivityNotification
69
74
  # To use notification email, email_allowed option must return true (not nil) in both of notifiable and target model.
70
75
  # This can be also configured default option in initializer.
71
76
  # @example Enable email notification for this notifiable model
77
+ # # app/models/comment.rb
72
78
  # class Comment < ActiveRecord::Base
73
79
  # acts_as_notifiable :users, targets: User.all, email_allowed: true
74
80
  # end
@@ -78,6 +84,7 @@ module ActivityNotification
78
84
  # You can also use this notifiable_path as notifiable link in notification view.
79
85
  # This parameter is a optional since polymorphic_path is used as default value.
80
86
  # @example Redirect to parent article page from comment notifications
87
+ # # app/models/comment.rb
81
88
  # class Comment < ActiveRecord::Base
82
89
  # belongs_to :article
83
90
  # acts_as_notifiable :users, targets: User.all, notifiable_path: :article_notifiable_path
@@ -87,39 +94,66 @@ module ActivityNotification
87
94
  # end
88
95
  # end
89
96
  #
97
+ # * :printable_name or :printable_notifiable_name
98
+ # * Printable notifiable name.
99
+ # This parameter is a optional since `ActivityNotification::Common.printable_name` is used as default value.
100
+ # :printable_name is the same option as :printable_notifiable_name
101
+ # @example Define printable name with comment body
102
+ # # app/models/comment.rb
103
+ # class Comment < ActiveRecord::Base
104
+ # acts_as_notifiable :users, targets: User.all, printable_name: ->(comment) { "comment \"#{comment.body}\"" }
105
+ # end
106
+ #
107
+ # * :dependent_notifications
108
+ # * Dependency for notifications to delete generated notifications with this notifiable.
109
+ # This option is used to configure generated_notifications_as_notifiable association.
110
+ # You can use :delete_all, :destroy, or :nullify for this option.
111
+ # This parameter is a optional since no dependent option is used as default.
112
+ # @example Define :delete_all dependency to generated notifications
113
+ # # app/models/comment.rb
114
+ # class Comment < ActiveRecord::Base
115
+ # acts_as_notifiable :users, targets: User.all, dependent_notifications: :delete_all
116
+ # end
117
+ #
90
118
  # @param [Symbol] target_type Type of notification target as symbol
91
119
  # @param [Hash] options Options for notifiable model configuration
92
- # @option options [Symbol, Proc, Array] :targets (nil) Targets to send notifications
93
- # @option options [Symbol, Proc, Object] :group (nil) Group unit of the notifications
94
- # @option options [Symbol, Proc, Object] :notifier (nil) Notifier of the notifications
95
- # @option options [Symbol, Proc, Hash] :parameters ({}) Additional parameters of the notifications
96
- # @option options [Symbol, Proc, Boolean] :email_allowed (ActivityNotification.config.email_enabled) Whether activity_notification sends notification email
97
- # @option options [Symbol, Proc, String] :notifiable_path (polymorphic_path(self)) Path to redirect from open or move action of notification controller
120
+ # @option options [Symbol, Proc, Array] :targets (nil) Targets to send notifications
121
+ # @option options [Symbol, Proc, Object] :group (nil) Group unit of the notifications
122
+ # @option options [Symbol, Proc, Object] :notifier (nil) Notifier of the notifications
123
+ # @option options [Symbol, Proc, Hash] :parameters ({}) Additional parameters of the notifications
124
+ # @option options [Symbol, Proc, Boolean] :email_allowed (ActivityNotification.config.email_enabled) Whether activity_notification sends notification email
125
+ # @option options [Symbol, Proc, String] :notifiable_path (polymorphic_path(self)) Path to redirect from open or move action of notification controller
126
+ # @option options [Symbol, Proc, String] :printable_name (ActivityNotification::Common.printable_name) Printable notifiable name
127
+ # @option options [Symbol, Proc] :dependent_notifications (nil) Dependency for notifications to delete generated notifications with this notifiable
98
128
  # @return [Hash] Configured parameters as notifiable model
99
129
  def acts_as_notifiable(target_type, options = {})
100
130
  include Notifiable
101
- (
102
- [:targets, :group, :parameters, :email_allowed].map { |key|
103
- assign_parameter(target_type, key, "_notification_#{key}", options)
104
- }.to_h.merge [:notifier, :notifiable_path].map { |key|
105
- assign_parameter(target_type, key, "_#{key}", options)
106
- }.to_h
107
- ).delete_if { |k, _| k.nil? }
131
+
132
+ if [:delete_all, :destroy, :nullify].include? options[:dependent_notifications]
133
+ has_many :generated_notifications_as_notifiable,
134
+ class_name: "::ActivityNotification::Notification",
135
+ as: :notifiable,
136
+ dependent: options[:dependent_notifications]
137
+ end
138
+
139
+ options[:printable_notifiable_name] ||= options.delete(:printable_name)
140
+ set_acts_as_parameters_for_target(target_type, [:targets, :group, :parameters, :email_allowed], options, "notification_")
141
+ .merge set_acts_as_parameters_for_target(target_type, [:notifier, :notifiable_path, :printable_notifiable_name], options)
108
142
  end
109
143
 
110
144
  # Returns array of available notifiable options in acts_as_notifiable.
111
145
  # @return [Array<Symbol>] Array of available notifiable options
112
146
  def available_notifiable_options
113
- [:targets, :group, :notifier, :parameters, :email_allowed, :notifiable_path].freeze
147
+ [ :targets,
148
+ :group,
149
+ :notifier,
150
+ :parameters,
151
+ :email_allowed,
152
+ :notifiable_path,
153
+ :printable_notifiable_name, :printable_name,
154
+ :dependent_notifications
155
+ ].freeze
114
156
  end
115
-
116
- private
117
-
118
- def assign_parameter(target_type, key, field_name, options)
119
- options[key] ?
120
- [key, self.send(field_name.to_sym).store(target_type.to_sym, options.delete(key))] :
121
- [nil, nil]
122
- end
123
157
  end
124
158
  end
125
159
  end
@@ -5,9 +5,32 @@ module ActivityNotification
5
5
 
6
6
  class_methods do
7
7
  # Adds required configurations to notifier models.
8
- # @return [nil] nil
9
- def acts_as_notifier
8
+ #
9
+ # == Parameters:
10
+ # * :printable_name or :printable_notifier_name
11
+ # * Printable notifier name.
12
+ # This parameter is a optional since `ActivityNotification::Common.printable_name` is used as default value.
13
+ # :printable_name is the same option as :printable_notifier_name
14
+ # @example Define printable name with user name of name field
15
+ # # app/models/user.rb
16
+ # class User < ActiveRecord::Base
17
+ # acts_as_notifier printable_name: :name
18
+ # end
19
+ #
20
+ # @param [Hash] options Options for notifier model configuration
21
+ # @option options [Symbol, Proc, String] :printable_name (ActivityNotification::Common.printable_name) Printable notifier target name
22
+ # @return [Hash] Configured parameters as notifier model
23
+ def acts_as_notifier(options = {})
10
24
  include Notifier
25
+
26
+ options[:printable_notifier_name] ||= options.delete(:printable_name)
27
+ set_acts_as_parameters([:printable_notifier_name], options)
28
+ end
29
+
30
+ # Returns array of available notifier options in acts_as_notifier.
31
+ # @return [Array<Symbol>] Array of available notifier options
32
+ def available_notifier_options
33
+ [:printable_notifier_name, :printable_name].freeze
11
34
  end
12
35
  end
13
36
  end