activity_notification 1.0.2 → 1.1.0

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 (105) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +33 -0
  3. data/.rubocop.yml +1157 -0
  4. data/.yardopts +3 -0
  5. data/CHANGELOG.md +25 -0
  6. data/Gemfile.lock +15 -17
  7. data/README.md +154 -27
  8. data/activity_notification.gemspec +1 -1
  9. data/app/controllers/activity_notification/notifications_controller.rb +30 -104
  10. data/app/controllers/activity_notification/notifications_with_devise_controller.rb +1 -33
  11. data/app/controllers/activity_notification/subscriptions_controller.rb +184 -0
  12. data/app/controllers/activity_notification/subscriptions_with_devise_controller.rb +6 -0
  13. data/app/mailers/activity_notification/mailer.rb +3 -3
  14. data/app/views/activity_notification/notifications/default/_index.html.erb +3 -0
  15. data/app/views/activity_notification/notifications/default/index.html.erb +8 -10
  16. data/app/views/activity_notification/notifications/default/show.html.erb +24 -2
  17. data/app/views/activity_notification/subscriptions/default/_form.html.erb +52 -0
  18. data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +89 -0
  19. data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +73 -0
  20. data/app/views/activity_notification/subscriptions/default/_subscriptions.html.erb +13 -0
  21. data/app/views/activity_notification/subscriptions/default/create.js.erb +5 -0
  22. data/app/views/activity_notification/subscriptions/default/destroy.js.erb +5 -0
  23. data/app/views/activity_notification/subscriptions/default/index.html.erb +197 -0
  24. data/app/views/activity_notification/subscriptions/default/show.html.erb +177 -0
  25. data/app/views/activity_notification/subscriptions/default/subscribe.js.erb +8 -0
  26. data/app/views/activity_notification/subscriptions/default/subscribe_to_email.js.erb +6 -0
  27. data/app/views/activity_notification/subscriptions/default/unsubscribe.js.erb +8 -0
  28. data/app/views/activity_notification/subscriptions/default/unsubscribe_to_email.js.erb +6 -0
  29. data/gemfiles/Gemfile.rails-4.2.lock +18 -20
  30. data/gemfiles/Gemfile.rails-5.0.lock +18 -20
  31. data/lib/activity_notification.rb +7 -3
  32. data/lib/activity_notification/apis/notification_api.rb +95 -61
  33. data/lib/activity_notification/apis/subscription_api.rb +51 -0
  34. data/lib/activity_notification/common.rb +1 -1
  35. data/lib/activity_notification/config.rb +65 -17
  36. data/lib/activity_notification/controllers/common_controller.rb +118 -0
  37. data/lib/activity_notification/controllers/devise_authentication_controller.rb +41 -0
  38. data/lib/activity_notification/helpers/view_helpers.rb +131 -3
  39. data/lib/activity_notification/mailers/helpers.rb +6 -8
  40. data/lib/activity_notification/models/concerns/notifiable.rb +45 -27
  41. data/lib/activity_notification/models/concerns/subscriber.rb +149 -0
  42. data/lib/activity_notification/models/concerns/target.rb +100 -66
  43. data/lib/activity_notification/models/notification.rb +7 -5
  44. data/lib/activity_notification/models/subscription.rb +93 -0
  45. data/lib/activity_notification/rails/routes.rb +148 -33
  46. data/lib/activity_notification/renderable.rb +3 -4
  47. data/lib/activity_notification/roles/acts_as_notifiable.rb +14 -1
  48. data/lib/activity_notification/roles/acts_as_target.rb +11 -8
  49. data/lib/activity_notification/version.rb +1 -1
  50. data/lib/generators/activity_notification/controllers_generator.rb +2 -2
  51. data/lib/generators/activity_notification/install_generator.rb +0 -1
  52. data/lib/generators/activity_notification/migration/migration_generator.rb +8 -2
  53. data/lib/generators/activity_notification/models_generator.rb +53 -0
  54. data/lib/generators/activity_notification/views_generator.rb +7 -7
  55. data/lib/generators/templates/activity_notification.rb +17 -3
  56. data/lib/generators/templates/controllers/notifications_controller.rb +18 -17
  57. data/lib/generators/templates/controllers/notifications_with_devise_controller.rb +18 -17
  58. data/lib/generators/templates/controllers/subscriptions_controller.rb +79 -0
  59. data/lib/generators/templates/controllers/subscriptions_with_devise_controller.rb +87 -0
  60. data/lib/generators/templates/migrations/migration.rb +57 -0
  61. data/lib/generators/templates/models/README +10 -0
  62. data/lib/generators/templates/{notification → models}/notification.rb +1 -3
  63. data/lib/generators/templates/models/subscription.rb +4 -0
  64. data/spec/concerns/apis/notification_api_spec.rb +48 -11
  65. data/spec/concerns/apis/subscription_api_spec.rb +167 -0
  66. data/spec/concerns/models/notifiable_spec.rb +60 -0
  67. data/spec/concerns/models/subscriber_spec.rb +525 -0
  68. data/spec/concerns/models/target_spec.rb +271 -42
  69. data/spec/controllers/common_controller_spec.rb +25 -0
  70. data/spec/controllers/dummy_common_controller.rb +5 -0
  71. data/spec/controllers/notifications_controller_shared_examples.rb +2 -6
  72. data/spec/controllers/subscriptions_controller_shared_examples.rb +735 -0
  73. data/spec/controllers/subscriptions_controller_spec.rb +12 -0
  74. data/spec/controllers/subscriptions_with_devise_controller_spec.rb +91 -0
  75. data/spec/factories/dummy/dummy_subscriber.rb +4 -0
  76. data/spec/factories/subscriptions.rb +8 -0
  77. data/spec/generators/controllers_generator_spec.rb +25 -2
  78. data/spec/generators/migration/migration_generator_spec.rb +3 -3
  79. data/spec/generators/models_generator_spec.rb +96 -0
  80. data/spec/generators/views_generator_spec.rb +84 -0
  81. data/spec/helpers/view_helpers_spec.rb +143 -0
  82. data/spec/mailers/mailer_spec.rb +5 -4
  83. data/spec/models/dummy/dummy_subscriber_spec.rb +5 -0
  84. data/spec/models/notification_spec.rb +7 -7
  85. data/spec/models/subscription_spec.rb +158 -0
  86. data/spec/rails_app/app/controllers/users/notifications_controller.rb +67 -0
  87. data/spec/rails_app/app/controllers/users/notifications_with_devise_controller.rb +75 -0
  88. data/spec/rails_app/app/controllers/users/subscriptions_controller.rb +79 -0
  89. data/spec/rails_app/app/controllers/users/subscriptions_with_devise_controller.rb +87 -0
  90. data/spec/rails_app/app/models/admin.rb +1 -0
  91. data/spec/rails_app/app/models/dummy/dummy_subscriber.rb +4 -0
  92. data/spec/rails_app/app/models/user.rb +2 -1
  93. data/spec/rails_app/app/views/activity_notification/mailer/dummy_subscribers/test_key.text.erb +1 -0
  94. data/spec/rails_app/app/views/articles/index.html.erb +6 -0
  95. data/spec/rails_app/config/initializers/activity_notification.rb +17 -3
  96. data/spec/rails_app/config/routes.rb +2 -2
  97. data/spec/rails_app/db/migrate/20160715050420_create_activity_notification_tables.rb +33 -0
  98. data/spec/rails_app/db/schema.rb +18 -0
  99. data/spec/roles/acts_as_notifiable_spec.rb +1 -1
  100. data/spec/roles/acts_as_target_spec.rb +1 -1
  101. metadata +70 -11
  102. data/lib/generators/activity_notification/notification/notification_generator.rb +0 -20
  103. data/lib/generators/templates/active_record/migration.rb +0 -18
  104. data/spec/generators/notification/notification_generator_spec.rb +0 -41
  105. data/spec/rails_app/db/migrate/20160715050420_create_notifications.rb +0 -18
@@ -24,12 +24,13 @@ module ActivityNotification
24
24
  # @param [Symbol, String, Class] target_type Type of target
25
25
  # @param [Object] notifiable Notifiable instance
26
26
  # @param [Hash] options Options for notifications
27
- # @option options [String] :key (notifiable.default_notification_key) Key of the notification
28
- # @option options [Object] :group (nil) Group unit of the notifications
29
- # @option options [Object] :notifier (nil) Notifier of the notifications
30
- # @option options [Hash] :parameters ({}) Additional parameters of the notifications
31
- # @option options [Boolean] :send_email (true) If it sends notification email
32
- # @option options [Boolean] :send_later (true) If it sends notification email asynchronously
27
+ # @option options [String] :key (notifiable.default_notification_key) Key of the notification
28
+ # @option options [Object] :group (nil) Group unit of the notifications
29
+ # @option options [ActiveSupport::Duration] :group_expiry_delay (nil) Expiry period of a notification group
30
+ # @option options [Object] :notifier (nil) Notifier of the notifications
31
+ # @option options [Hash] :parameters ({}) Additional parameters of the notifications
32
+ # @option options [Boolean] :send_email (true) Whether it sends notification email
33
+ # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
33
34
  # @return [Array<Notificaion>] Array of generated notifications
34
35
  def notify(target_type, notifiable, options = {})
35
36
  targets = notifiable.notification_targets(target_type, options[:key])
@@ -46,12 +47,13 @@ module ActivityNotification
46
47
  # @param [Array<Object>] targets Targets to send notifications
47
48
  # @param [Object] notifiable Notifiable instance
48
49
  # @param [Hash] options Options for notifications
49
- # @option options [String] :key (notifiable.default_notification_key) Key of the notification
50
- # @option options [Object] :group (nil) Group unit of the notifications
51
- # @option options [Object] :notifier (nil) Notifier of the notifications
52
- # @option options [Hash] :parameters ({}) Additional parameters of the notifications
53
- # @option options [Boolean] :send_email (true) Whether it sends notification email
54
- # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
50
+ # @option options [String] :key (notifiable.default_notification_key) Key of the notification
51
+ # @option options [Object] :group (nil) Group unit of the notifications
52
+ # @option options [ActiveSupport::Duration] :group_expiry_delay (nil) Expiry period of a notification group
53
+ # @option options [Object] :notifier (nil) Notifier of the notifications
54
+ # @option options [Hash] :parameters ({}) Additional parameters of the notifications
55
+ # @option options [Boolean] :send_email (true) Whether it sends notification email
56
+ # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
55
57
  # @return [Array<Notificaion>] Array of generated notifications
56
58
  def notify_all(targets, notifiable, options = {})
57
59
  targets.map { |target| target.notify_to(notifiable, options) }
@@ -65,48 +67,67 @@ module ActivityNotification
65
67
  # @param [Object] target Target to send notifications
66
68
  # @param [Object] notifiable Notifiable instance
67
69
  # @param [Hash] options Options for notifications
68
- # @option options [String] :key (notifiable.default_notification_key) Key of the notification
69
- # @option options [Object] :group (nil) Group unit of the notifications
70
- # @option options [Object] :notifier (nil) Notifier of the notifications
71
- # @option options [Hash] :parameters ({}) Additional parameters of the notifications
72
- # @option options [Boolean] :send_email (true) Whether it sends notification email
73
- # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
70
+ # @option options [String] :key (notifiable.default_notification_key) Key of the notification
71
+ # @option options [Object] :group (nil) Group unit of the notifications
72
+ # @option options [ActiveSupport::Duration] :group_expiry_delay (nil) Expiry period of a notification group
73
+ # @option options [Object] :notifier (nil) Notifier of the notifications
74
+ # @option options [Hash] :parameters ({}) Additional parameters of the notifications
75
+ # @option options [Boolean] :send_email (true) Whether it sends notification email
76
+ # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
74
77
  # @return [Notification] Generated notification instance
75
78
  def notify_to(target, notifiable, options = {})
76
79
  send_email = options.has_key?(:send_email) ? options[:send_email] : true
77
80
  send_later = options.has_key?(:send_later) ? options[:send_later] : true
78
- # Store notification
79
- notification = store_notification(target, notifiable, options)
81
+ # Generate notification
82
+ notification = generate_notification(target, notifiable, options)
80
83
  # Send notification email
81
- notification.send_notification_email({ send_later: send_later }) if send_email
82
- # Return created notification
84
+ if notification.present? && send_email
85
+ notification.send_notification_email({ send_later: send_later })
86
+ end
87
+ # Return generated notification
83
88
  notification
84
89
  end
85
90
 
91
+ # Generates a notification
92
+ # @param [Object] target Target to send notification
93
+ # @param [Object] notifiable Notifiable instance
94
+ # @param [Hash] options Options for notification
95
+ # @option options [String] :key (notifiable.default_notification_key) Key of the notification
96
+ # @option options [Object] :group (nil) Group unit of the notifications
97
+ # @option options [Object] :notifier (nil) Notifier of the notifications
98
+ # @option options [Hash] :parameters ({}) Additional parameters of the notifications
99
+ def generate_notification(target, notifiable, options = {})
100
+ key = options[:key] || notifiable.default_notification_key
101
+ if target.subscribes_to_notification?(key)
102
+ # Store notification
103
+ store_notification(target, notifiable, key, options)
104
+ end
105
+ end
106
+
86
107
  # Opens all notifications of the target.
87
108
  #
88
109
  # @param [Object] target Target of the notifications to open
89
110
  # @param [Hash] options Options for opening notifications
90
- # @option options [DateTime] :opened_at (DateTime.now) Time to set to opened_at of the notification record
91
- # @option options [String] :filtered_by_type (nil) Notifiable type for filter
92
- # @option options [Object] :filtered_by_group (nil) Group instance for filter
93
- # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
94
- # @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
95
- # @option options [String] :filtered_by_key (nil) Key of the notification for filter
111
+ # @option options [DateTime] :opened_at (Time.current) Time to set to opened_at of the notification record
112
+ # @option options [String] :filtered_by_type (nil) Notifiable type for filter
113
+ # @option options [Object] :filtered_by_group (nil) Group instance for filter
114
+ # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
115
+ # @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
116
+ # @option options [String] :filtered_by_key (nil) Key of the notification for filter
96
117
  # @return [Integer] Number of opened notification records
97
118
  # @todo Add filter option
98
119
  def open_all_of(target, options = {})
99
- opened_at = options[:opened_at] || DateTime.now
120
+ opened_at = options[:opened_at] || Time.current
100
121
  target.notifications.unopened_only.filtered_by_options(options).update_all(opened_at: opened_at)
101
122
  end
102
123
 
103
124
  # Returns if group member of the notifications exists.
104
125
  # This method is designed to be called from controllers or views to avoid N+1.
105
126
  #
106
- # @param [Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>] notifications Array or database query of the notifications to test member exists
127
+ # @param [Array<Notificaion>, ActiveRecord_AssociationRelation<Notificaion>] notifications Array or database query of the notifications to test member exists
107
128
  # @return [Boolean] If group member of the notifications exists
108
129
  def group_member_exists?(notifications)
109
- notifications.present? and where(group_owner_id: notifications.map(&:id)).exists?
130
+ notifications.present? && where(group_owner_id: notifications.map(&:id)).exists?
110
131
  end
111
132
 
112
133
  # Sends batch notification email to the target.
@@ -117,16 +138,16 @@ module ActivityNotification
117
138
  # @option options [Boolean] :send_later (false) If it sends notification email asynchronously
118
139
  # @option options [String, Symbol] :fallback (:batch_default) Fallback template to use when MissingTemplate is raised
119
140
  # @option options [String] :batch_key (nil) Key of the batch notification email, a key of the first notification will be used if not specified
120
- # @return [Mail::Message|ActionMailer::DeliveryJob|NilClass] Email message or its delivery job, return NilClass for wrong target
141
+ # @return [Mail::Message, ActionMailer::DeliveryJob|NilClass] Email message or its delivery job, return NilClass for wrong target
121
142
  def send_batch_notification_email(target, notifications, options = {})
122
- return if notifications.blank?
123
- if target.batch_notification_email_allowed?(notifications.first.notifiable_type, notifications.first.key)
143
+ notifications.blank? and return
144
+ batch_key = options[:batch_key] || notifications.first.key
145
+ if target.batch_notification_email_allowed?(batch_key) &&
146
+ target.subscribes_to_notification_email?(batch_key)
124
147
  send_later = options.has_key?(:send_later) ? options[:send_later] : true
125
- if send_later
126
- Mailer.send_batch_notification_email(target, notifications, options).deliver_later
127
- else
128
- Mailer.send_batch_notification_email(target, notifications, options).deliver_now
129
- end
148
+ send_later ?
149
+ Mailer.send_batch_notification_email(target, notifications, batch_key, options).deliver_later :
150
+ Mailer.send_batch_notification_email(target, notifications, batch_key, options).deliver_now
130
151
  end
131
152
  end
132
153
 
@@ -139,24 +160,26 @@ module ActivityNotification
139
160
 
140
161
  # Stores notifications to datastore
141
162
  # @api private
142
- def store_notification(target, notifiable, options = {})
143
- target_type = target.to_class_name
144
- key = options[:key] || notifiable.default_notification_key
145
- group = options[:group] || notifiable.notification_group(target_type, key)
146
- notifier = options[:notifier] || notifiable.notifier(target_type, key)
147
- parameters = options[:parameters] || {}
163
+ def store_notification(target, notifiable, key, options = {})
164
+ target_type = target.to_class_name
165
+ group = options[:group] || notifiable.notification_group(target_type, key)
166
+ group_expiry_delay = options[:group_expiry_delay] || notifiable.notification_group_expiry_delay(target_type, key)
167
+ notifier = options[:notifier] || notifiable.notifier(target_type, key)
168
+ parameters = options[:parameters] || {}
148
169
  parameters.merge!(options.except(*available_options))
149
170
  parameters.merge!(notifiable.notification_parameters(target_type, key))
150
171
 
151
172
  # Bundle notification group by target, notifiable_type, group and key
152
173
  # Defferent notifiable.id can be made in a same group
153
- group_owner = where(target: target, notifiable_type: notifiable.to_class_name, key: key, group: group)
154
- .where(group_owner_id: nil, opened_at: nil).earliest
155
- if group.present? and group_owner.present?
156
- create(target: target, notifiable: notifiable, key: key, group: group, group_owner: group_owner, parameters: parameters, notifier: notifier)
157
- else
158
- create(target: target, notifiable: notifiable, key: key, group: group, parameters: parameters, notifier: notifier)
159
- end
174
+ group_owner_notifications = filtered_by_target(target).filtered_by_type(notifiable.to_class_name).filtered_by_key(key)
175
+ .filtered_by_group(group).group_owners_only.unopened_only
176
+ group_owner = group_expiry_delay.present? ?
177
+ group_owner_notifications.where("created_at > ?", group_expiry_delay.ago).earliest :
178
+ group_owner_notifications.earliest
179
+ notification_fields = { target: target, notifiable: notifiable, key: key, group: group, parameters: parameters, notifier: notifier }
180
+ group.present? && group_owner.present? ?
181
+ create(notification_fields.merge(group_owner: group_owner)) :
182
+ create(notification_fields)
160
183
  end
161
184
  end
162
185
 
@@ -166,27 +189,26 @@ module ActivityNotification
166
189
  # @param [Hash] options Options for notification email
167
190
  # @option options [Boolean] :send_later If it sends notification email asynchronously
168
191
  # @option options [String, Symbol] :fallback (:default) Fallback template to use when MissingTemplate is raised
169
- # @return [Mail::Message|ActionMailer::DeliveryJob] Email message or its delivery job
192
+ # @return [Mail::Message, ActionMailer::DeliveryJob] Email message or its delivery job
170
193
  def send_notification_email(options = {})
171
- if target.notification_email_allowed?(notifiable, key) and
194
+ if target.notification_email_allowed?(notifiable, key) &&
195
+ email_subscribed?(key) &&
172
196
  notifiable.notification_email_allowed?(target, key)
173
197
  send_later = options.has_key?(:send_later) ? options[:send_later] : true
174
- if send_later
175
- Mailer.send_notification_email(self, options).deliver_later
176
- else
198
+ send_later ?
199
+ Mailer.send_notification_email(self, options).deliver_later :
177
200
  Mailer.send_notification_email(self, options).deliver_now
178
- end
179
201
  end
180
202
  end
181
203
 
182
204
  # Opens the notification.
183
205
  #
184
206
  # @param [Hash] options Options for opening notifications
185
- # @option options [DateTime] :opened_at (DateTime.now) Time to set to opened_at of the notification record
186
- # @option options [Boolean] :with_members (true) If it opens notifications including group members
207
+ # @option options [DateTime] :opened_at (Time.current) Time to set to opened_at of the notification record
208
+ # @option options [Boolean] :with_members (true) If it opens notifications including group members
187
209
  # @return [Integer] Number of opened notification records
188
210
  def open!(options = {})
189
- opened_at = options[:opened_at] || DateTime.now
211
+ opened_at = options[:opened_at] || Time.current
190
212
  with_members = options.has_key?(:with_members) ? options[:with_members] : true
191
213
  update(opened_at: opened_at)
192
214
  with_members ? group_members.update_all(opened_at: opened_at) + 1 : 1
@@ -297,6 +319,18 @@ module ActivityNotification
297
319
  notifiable.notifiable_path(target_type, key)
298
320
  end
299
321
 
322
+ # Returns if the target subscribes this notification.
323
+ # @return [Boolean] If the target subscribes the notification
324
+ def subscribed?
325
+ target.subscribes_to_notification?(key)
326
+ end
327
+
328
+ # Returns if the target subscribes this notification email.
329
+ # @return [Boolean] If the target subscribes the notification
330
+ def email_subscribed?(key)
331
+ target.subscribes_to_notification_email?(key)
332
+ end
333
+
300
334
 
301
335
  protected
302
336
 
@@ -0,0 +1,51 @@
1
+ module ActivityNotification
2
+ # Defines API for subscription included in Subscription model.
3
+ module SubscriptionApi
4
+ extend ActiveSupport::Concern
5
+
6
+ # Subscribes to the notification and notification email.
7
+ #
8
+ # @param [Hash] options Options for subscribing notification
9
+ # @option options [DateTime] :subscribed_at (Time.current) Time to set to subscribed_at and subscribed_to_email_at of the subscription record
10
+ # @option options [Boolean] :with_email_subscription (true) If the subscriber also subscribes notification email
11
+ # @return [Boolean] If successfully updated subscription instance
12
+ def subscribe(options = {})
13
+ subscribed_at = options[:subscribed_at] || Time.current
14
+ with_email_subscription = options.has_key?(:with_email_subscription) ? options[:with_email_subscription] : true
15
+ with_email_subscription ?
16
+ update(subscribing: true, subscribing_to_email: true, subscribed_at: subscribed_at, subscribed_to_email_at: subscribed_at) :
17
+ update(subscribing: true, subscribed_at: subscribed_at)
18
+ end
19
+
20
+ # Unsubscribes to the notification and notification email.
21
+ #
22
+ # @param [Hash] options Options for unsubscribing notification
23
+ # @option options [DateTime] :unsubscribed_at (Time.current) Time to set to unsubscribed_at and unsubscribed_to_email_at of the subscription record
24
+ # @return [Boolean] If successfully updated subscription instance
25
+ def unsubscribe(options = {})
26
+ unsubscribed_at = options[:unsubscribed_at] || Time.current
27
+ update(subscribing: false, subscribing_to_email: false, unsubscribed_at: unsubscribed_at, unsubscribed_to_email_at: unsubscribed_at)
28
+ end
29
+
30
+ # Subscribes to the notification email.
31
+ #
32
+ # @param [Hash] options Options for subscribing notification email
33
+ # @option options [DateTime] :subscribed_to_email_at (Time.current) Time to set to subscribed_to_email_at of the subscription record
34
+ # @return [Boolean] If successfully updated subscription instance
35
+ def subscribe_to_email(options = {})
36
+ subscribed_to_email_at = options[:subscribed_to_email_at] || Time.current
37
+ update(subscribing_to_email: true, subscribed_to_email_at: subscribed_to_email_at)
38
+ end
39
+
40
+ # Unsubscribes to the notification email.
41
+ #
42
+ # @param [Hash] options Options for unsubscribing notification email
43
+ # @option options [DateTime] :subscribed_to_email_at (Time.current) Time to set to subscribed_to_email_at of the subscription record
44
+ # @return [Boolean] If successfully updated subscription instance
45
+ def unsubscribe_to_email(options = {})
46
+ unsubscribed_to_email_at = options[:unsubscribed_to_email_at] || Time.current
47
+ update(subscribing_to_email: false, unsubscribed_to_email_at: unsubscribed_to_email_at)
48
+ end
49
+
50
+ end
51
+ end
@@ -42,7 +42,7 @@ module ActivityNotification
42
42
  end
43
43
 
44
44
  # Casts to indifferent hash
45
- # @param [ActionController::Parameters, Hash] new_hash
45
+ # @param [ActionController::Parameters, Hash] hash
46
46
  # @return [HashWithIndifferentAccess] Converted indifferent hash
47
47
  def self.cast_to_indifferent_hash(hash = {})
48
48
  # This is the typical (not-ActionView::TestCase) code path.
@@ -2,65 +2,110 @@ module ActivityNotification
2
2
  # Class used to initialize configuration object.
3
3
  class Config
4
4
  # @overload enabled
5
+ # Returns whether ActivityNotification is enabled
5
6
  # @return [Boolean] Whether ActivityNotification is enabled.
6
7
  # @overload enabled=(value)
7
- # Sets the enabled
8
+ # Sets whether ActivityNotification is enabled
8
9
  # @param [Boolean] enabled The new enabled
9
10
  # @return [Boolean] Whether ActivityNotification is enabled.
10
11
  attr_accessor :enabled
11
12
 
13
+ # @deprecated as of 1.1.0
12
14
  # @overload table_name
15
+ # Returns table name to store notifications
13
16
  # @return [String] Table name to store notifications.
14
17
  # @overload table_name=(value)
15
- # Sets the table_name
16
- # @param [String] table_name The new table_name
18
+ # Sets table name to store notifications
19
+ # @param [String] table_name The new notification_table_name
17
20
  # @return [String] Table name to store notifications.
18
21
  attr_accessor :table_name
19
22
 
23
+ # @overload notification_table_name
24
+ # Returns table name to store notifications
25
+ # @return [String] Table name to store notifications.
26
+ # @overload notification_table_name=(value)
27
+ # Sets table name to store notifications
28
+ # @param [String] notification_table_name The new notification_table_name
29
+ # @return [String] Table name to store notifications.
30
+ attr_accessor :notification_table_name
31
+
32
+ # @overload subscription_table_name
33
+ # Returns table name to store subscriptions
34
+ # @return [String] Table name to store subscriptions.
35
+ # @overload subscription_table_name=(value)
36
+ # Sets table name to store subscriptions
37
+ # @param [String] notification_table_name The new subscription_table_name
38
+ # @return [String] Table name to store subscriptions.
39
+ attr_accessor :subscription_table_name
40
+
20
41
  # @overload email_enabled
42
+ # Returns whether activity_notification sends notification email
21
43
  # @return [Boolean] Whether activity_notification sends notification email.
22
44
  # @overload email_enabled=(value)
23
- # Sets the email_enabled
45
+ # Sets whether activity_notification sends notification email
24
46
  # @param [Boolean] email_enabled The new email_enabled
25
47
  # @return [Boolean] Whether activity_notification sends notification email.
26
48
  attr_accessor :email_enabled
27
49
 
50
+ # @overload subscription_enabled
51
+ # Returns whether activity_notification manages subscriptions
52
+ # @return [Boolean] Whether activity_notification manages subscriptions.
53
+ # @overload subscription_enabled=(value)
54
+ # Sets whether activity_notification manages subscriptions
55
+ # @param [Boolean] subscription_enabled The new subscription_enabled
56
+ # @return [Boolean] Whether activity_notification manages subscriptions.
57
+ attr_accessor :subscription_enabled
58
+
59
+ # @overload subscribe_as_default
60
+ # Returns default subscription value to use when the subscription record does not configured
61
+ # @return [Boolean] Default subscription value to use when the subscription record does not configured.
62
+ # @overload default_subscription=(value)
63
+ # Sets default subscription value to use when the subscription record does not configured
64
+ # @param [Boolean] subscribe_as_default The new subscribe_as_default
65
+ # @return [Boolean] Default subscription value to use when the subscription record does not configured.
66
+ attr_accessor :subscribe_as_default
67
+
28
68
  # @overload mailer_sender
69
+ # Returns email address as sender of notification email
29
70
  # @return [String] Email address as sender of notification email.
30
71
  # @overload mailer_sender=(value)
31
- # Sets the mailer_sender
72
+ # Sets email address as sender of notification email
32
73
  # @param [String] mailer_sender The new mailer_sender
33
74
  # @return [String] Email address as sender of notification email.
34
75
  attr_accessor :mailer_sender
35
76
 
36
77
  # @overload mailer
78
+ # Returns mailer class for email notification
37
79
  # @return [String] Mailer class for email notification.
38
80
  # @overload mailer=(value)
39
- # Sets the mailer
81
+ # Sets mailer class for email notification
40
82
  # @param [String] mailer The new mailer
41
83
  # @return [String] Mailer class for email notification.
42
84
  attr_accessor :mailer
43
85
 
44
86
  # @overload parent_mailer
87
+ # Returns base mailer class for email notification
45
88
  # @return [String] Base mailer class for email notification.
46
89
  # @overload parent_mailer=(value)
47
- # Sets the parent_mailer
90
+ # Sets base mailer class for email notification
48
91
  # @param [String] parent_mailer The new parent_mailer
49
92
  # @return [String] Base mailer class for email notification.
50
93
  attr_accessor :parent_mailer
51
94
 
52
95
  # @overload parent_controller
96
+ # Returns base controller class for notifications_controller
53
97
  # @return [String] Base controller class for notifications_controller.
54
98
  # @overload parent_controller=(value)
55
- # Sets the parent_controller
99
+ # Sets base controller class for notifications_controller
56
100
  # @param [String] parent_controller The new parent_controller
57
101
  # @return [String] Base controller class for notifications_controller.
58
102
  attr_accessor :parent_controller
59
103
 
60
104
  # @overload opened_index_limit
105
+ # Returns default limit to query for opened notifications
61
106
  # @return [Integer] Default limit to query for opened notifications.
62
107
  # @overload opened_index_limit=(value)
63
- # Sets the opened_index_limit
108
+ # Sets default limit to query for opened notifications
64
109
  # @param [Integer] opened_index_limit The new opened_index_limit
65
110
  # @return [Integer] Default limit to query for opened notifications.
66
111
  attr_accessor :opened_index_limit
@@ -69,14 +114,17 @@ module ActivityNotification
69
114
  # These configuration can be overriden in initializer.
70
115
  # @return [Config] A new instance of Config
71
116
  def initialize
72
- @enabled = true
73
- @table_name = 'notifications'
74
- @email_enabled = false
75
- @mailer_sender = nil
76
- @mailer = 'ActivityNotification::Mailer'
77
- @parent_mailer = 'ActionMailer::Base'
78
- @parent_controller = 'ApplicationController'
79
- @opened_index_limit = 10
117
+ @enabled = true
118
+ @notification_table_name = 'notifications'
119
+ @subscription_table_name = 'subscriptions'
120
+ @email_enabled = false
121
+ @subscription_enabled = false
122
+ @subscribe_as_default = true
123
+ @mailer_sender = nil
124
+ @mailer = 'ActivityNotification::Mailer'
125
+ @parent_mailer = 'ActionMailer::Base'
126
+ @parent_controller = 'ApplicationController'
127
+ @opened_index_limit = 10
80
128
  end
81
129
 
82
130
  end