activity_notification 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,149 @@
1
+ module ActivityNotification
2
+ # Subscriber implementation included in target model to manage subscriptions, like users or administrators.
3
+ module Subscriber
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ # Has many subscription instances of this target.
7
+ # @scope instance
8
+ # @return [Array<Notificaion>] Array or database query of subscriptions of this target
9
+ has_many :subscriptions,
10
+ class_name: "::ActivityNotification::Subscription",
11
+ as: :target,
12
+ dependent: :delete_all
13
+ end
14
+
15
+ class_methods do
16
+ # Checks if the model includes subscriber and subscriber methods are available.
17
+ # Also checks if the model includes target and target methods are available, then return true.
18
+ # @return [Boolean] If the model includes target and subscriber are available
19
+ def available_as_subscriber?
20
+ available_as_target?
21
+ end
22
+ end
23
+
24
+
25
+ # Gets subscription of the target and notification key.
26
+ #
27
+ # @param [Hash] key Key of the notification for subscription
28
+ # @return [Subscription] Configured subscription instance
29
+ def find_subscription(key)
30
+ subscriptions.find_by_key(key)
31
+ end
32
+
33
+ # Gets subscription of the target and notification key.
34
+ #
35
+ # @param [Hash] key Key of the notification for subscription
36
+ # @param [Hash] subscription_params Parameters to create subscription record
37
+ # @return [Subscription] Found or created subscription instance
38
+ def find_or_create_subscription(key, subscription_params = {})
39
+ subscription = find_subscription(key)
40
+ subscription || create_subscription(subscription_params.merge(key: key))
41
+ end
42
+
43
+ # Creates new subscription of the target.
44
+ #
45
+ # @param [Hash] subscription_params Parameters to create subscription record
46
+ # @return [Subscription] Created subscription instance
47
+ def create_subscription(subscription_params = {})
48
+ created_at = Time.current
49
+ if subscription_params[:subscribing] == false && subscription_params[:subscribing_to_email].nil?
50
+ subscription_params[:subscribing_to_email] = subscription_params[:subscribing]
51
+ end
52
+ subscription = subscriptions.new(subscription_params)
53
+ subscription.subscribing ?
54
+ subscription.assign_attributes(subscribing: true, subscribed_at: created_at) :
55
+ subscription.assign_attributes(subscribing: false, unsubscribed_at: created_at)
56
+ subscription.subscribing_to_email ?
57
+ subscription.assign_attributes(subscribing_to_email: true, subscribed_to_email_at: created_at) :
58
+ subscription.assign_attributes(subscribing_to_email: false, unsubscribed_to_email_at: created_at)
59
+ subscription.save ? subscription : nil
60
+ end
61
+
62
+ # Gets configured subscription index of the target.
63
+ #
64
+ # @example Get configured subscription index of the @user
65
+ # @subscriptions = @user.subscription_index
66
+ #
67
+ # @param [Hash] options Options for subscription index
68
+ # @option options [Integer] :limit (nil) Limit to query for subscriptions
69
+ # @option options [Boolean] :reverse (false) If subscription index will be ordered as earliest first
70
+ # @option options [String] :filtered_by_key (nil) Key of the notification for filter
71
+ # @option options [Array|Hash] :custom_filter (nil) Custom subscription filter (e.g. ["created_at >= ?", time.hour.ago])
72
+ # @option options [Boolean] :with_target (false) If it includes target with subscriptions
73
+ # @return [Array<Notificaion>] Configured subscription index of the target
74
+ def subscription_index(options = {})
75
+ target_index = subscriptions.filtered_by_options(options)
76
+ target_index = options[:reverse] ? target_index.earliest_order : target_index.latest_order
77
+ target_index = target_index.includes(:target) if options[:with_target]
78
+ options[:limit].present? ? target_index.limit(options[:limit]) : target_index
79
+ end
80
+
81
+ # Gets received notification keys of the target.
82
+ #
83
+ # @example Get unconfigured notification keys of the @user
84
+ # @notification_keys = @user.notification_keys(filter: :unconfigured)
85
+ #
86
+ # @param [Hash] options Options for unconfigured notification keys
87
+ # @option options [Integer] :limit (nil) Limit to query for subscriptions
88
+ # @option options [Boolean] :reverse (false) If notification keys will be ordered as earliest first
89
+ # @option options [Symbol|String] :filter (nil) Filter option to load notification keys (Nothing as all, 'configured' with configured subscriptions or 'unconfigured' without subscriptions)
90
+ # @option options [String] :filtered_by_key (nil) Key of the notification for filter
91
+ # @option options [Array|Hash] :custom_filter (nil) Custom subscription filter (e.g. ["created_at >= ?", time.hour.ago])
92
+ # @return [Array<Notificaion>] Unconfigured notification keys of the target
93
+ def notification_keys(options = {})
94
+ subscription_keys = subscriptions.select(:key).distinct.pluck(:key)
95
+ target_notifications = notifications.filtered_by_options(options.select { |k, _| [:filtered_by_key, :custom_filter].include?(k) })
96
+ target_notifications = options[:reverse] ? target_notifications.earliest_order : target_notifications.latest_order
97
+ target_notifications = options[:limit].present? ? target_notifications.limit(options[:limit] + subscription_keys.size) : target_notifications
98
+ notification_keys = target_notifications.select(:key).distinct.pluck(:key)
99
+ notification_keys =
100
+ case options[:filter]
101
+ when :configured, 'configured'
102
+ notification_keys & subscription_keys
103
+ when :unconfigured, 'unconfigured'
104
+ notification_keys - subscription_keys
105
+ else
106
+ notification_keys
107
+ end
108
+ options[:limit].present? ? notification_keys.take(options[:limit]) : notification_keys
109
+ end
110
+
111
+ protected
112
+
113
+ # Returns if the target subscribes to the notification.
114
+ # This method can be overriden.
115
+ # @api protected
116
+ #
117
+ # @param [String] key Key of the notification
118
+ # @param [Boolean] subscribe_as_default Default subscription value to use when the subscription record does not configured
119
+ # @return [Boolean] If the target subscribes to the notification
120
+ def _subscribes_to_notification?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
121
+ evaluate_subscription(subscriptions.find_by_key(key), :subscribing, subscribe_as_default)
122
+ end
123
+
124
+ # Returns if the target subscribes to the notification email.
125
+ # This method can be overriden.
126
+ # @api protected
127
+ #
128
+ # @param [String] key Key of the notification
129
+ # @param [Boolean] subscribe_as_default Default subscription value to use when the subscription record does not configured
130
+ # @return [Boolean] If the target subscribes to the notification
131
+ def _subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
132
+ evaluate_subscription(subscriptions.find_by_key(key), :subscribing_to_email, subscribe_as_default)
133
+ end
134
+ alias_method :_subscribes_to_email?, :_subscribes_to_notification_email?
135
+
136
+ private
137
+
138
+ # Returns if the target subscribes.
139
+ # @api private
140
+ # @param [Boolean] record Subscription record
141
+ # @param [Symbol] field Evaluating subscription field of the record
142
+ # @param [Boolean] default Default subscription value to use when the subscription record does not configured
143
+ # @return [Boolean] If the target subscribes
144
+ def evaluate_subscription(record, field, default)
145
+ default ? record.blank? || record.send(field) : record.present? && record.send(field)
146
+ end
147
+
148
+ end
149
+ end
@@ -16,6 +16,7 @@ module ActivityNotification
16
16
  class_attribute :_notification_email,
17
17
  :_notification_email_allowed,
18
18
  :_batch_notification_email_allowed,
19
+ :_notification_subscription_allowed,
19
20
  :_notification_devise_resource,
20
21
  :_printable_notification_target_name
21
22
  set_target_class_defaults
@@ -34,6 +35,7 @@ module ActivityNotification
34
35
  self._notification_email = nil
35
36
  self._notification_email_allowed = ActivityNotification.config.email_enabled
36
37
  self._batch_notification_email_allowed = ActivityNotification.config.email_enabled
38
+ self._notification_subscription_allowed = ActivityNotification.config.subscription_enabled
37
39
  self._notification_devise_resource = ->(model) { model }
38
40
  self._printable_notification_target_name = :printable_name
39
41
  nil
@@ -45,6 +47,7 @@ module ActivityNotification
45
47
  # @option options [Boolean] :reverse (false) If notification index will be ordered as earliest first
46
48
  # @option options [Boolean] :with_group_members (false) If notification index will include group members
47
49
  # @option options [Boolean] :as_latest_group_member (false) If grouped notification will be shown as the latest group member (default is shown as the earliest member)
50
+ # @option options [String] :filtered_by_status (:all) Status for filter, :all, :opened and :unopened are available
48
51
  # @option options [String] :filtered_by_type (nil) Notifiable type for filter
49
52
  # @option options [Object] :filtered_by_group (nil) Group instance for filter
50
53
  # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
@@ -53,12 +56,23 @@ module ActivityNotification
53
56
  # @option options [Array|Hash] :custom_filter (nil) Custom notification filter (e.g. ["created_at >= ?", time.hour.ago])
54
57
  # @return [Array<Notificaion>] All notifications for this target type
55
58
  def all_notifications(options = {})
56
- reverse = options[:reverse] || false
57
- with_group_members = options[:with_group_members] || false
59
+ reverse = options[:reverse] || false
60
+ with_group_members = options[:with_group_members] || false
61
+ as_latest_group_member = options[:as_latest_group_member] || false
58
62
  target_notifications = Notification.filtered_by_target_type(self.name)
59
63
  .all_index!(reverse, with_group_members)
60
64
  .filtered_by_options(options)
61
- options[:limit].present? ? target_notifications.limit(options[:limit]) : target_notifications
65
+ .with_target
66
+ case options[:filtered_by_status]
67
+ when :opened, 'opened'
68
+ target_notifications = target_notifications.opened_only!
69
+ when :unopened, 'unopened'
70
+ target_notifications = target_notifications.unopened_only
71
+ end
72
+ target_notifications = target_notifications.limit(options[:limit]) if options[:limit].present?
73
+ as_latest_group_member ?
74
+ target_notifications.map{ |n| n.latest_group_member } :
75
+ target_notifications.to_a
62
76
  end
63
77
 
64
78
  # Gets all notifications for this target type grouped by targets.
@@ -73,6 +87,7 @@ module ActivityNotification
73
87
  # @option options [Boolean] :reverse (false) If notification index will be ordered as earliest first
74
88
  # @option options [Boolean] :with_group_members (false) If notification index will include group members
75
89
  # @option options [Boolean] :as_latest_group_member (false) If grouped notification will be shown as the latest group member (default is shown as the earliest member)
90
+ # @option options [String] :filtered_by_status (:all) Status for filter, :all, :opened and :unopened are available
76
91
  # @option options [String] :filtered_by_type (nil) Notifiable type for filter
77
92
  # @option options [Object] :filtered_by_group (nil) Group instance for filter
78
93
  # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
@@ -84,29 +99,6 @@ module ActivityNotification
84
99
  all_notifications(options).group_by(&:target)
85
100
  end
86
101
 
87
- # Gets all unopened notifications for this target type grouped by targets.
88
- #
89
- # @example Get all unopened notifications for users grouped by user
90
- # @unopened_notification_index_map = User.unopened_notification_index_map
91
- # @unopened_notification_index_map.each do |user, notifications|
92
- # # Do something for user and notifications
93
- # end
94
- #
95
- # @option options [Integer] :limit (nil) Limit to query for notifications
96
- # @option options [Boolean] :reverse (false) If notification index will be ordered as earliest first
97
- # @option options [Boolean] :with_group_members (false) If notification index will include group members
98
- # @option options [Boolean] :as_latest_group_member (false) If grouped notification will be shown as the latest group member (default is shown as the earliest member)
99
- # @option options [String] :filtered_by_type (nil) Notifiable type for filter
100
- # @option options [Object] :filtered_by_group (nil) Group instance for filter
101
- # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
102
- # @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
103
- # @option options [String] :filtered_by_key (nil) Key of the notification for filter
104
- # @option options [Array|Hash] :custom_filter (nil) Custom notification filter (e.g. ["created_at >= ?", time.hour.ago])
105
- # @return [Array<Notificaion>] All unopened notifications for this target type grouped by targets
106
- def unopened_notification_index_map(options = {})
107
- all_notifications(options).unopened_only.group_by(&:target)
108
- end
109
-
110
102
  # Send batch notification email to this type targets with unopened notifications.
111
103
  #
112
104
  # @example Send batch notification email to the users with unopened notifications of specified key
@@ -114,23 +106,34 @@ module ActivityNotification
114
106
  # @example Send batch notification email to the users with unopened notifications of specified key in 1 hour
115
107
  # User.send_batch_unopened_notification_email(filtered_by_key: 'this.key', custom_filter: ["created_at >= ?", time.hour.ago])
116
108
  #
117
- # @option options [Integer] :limit (nil) Limit to query for notifications
118
- # @option options [Boolean] :reverse (false) If notification index will be ordered as earliest first
119
- # @option options [Boolean] :with_group_members (false) If notification index will include group members
120
- # @option options [Boolean] :as_latest_group_member (false) If grouped notification will be shown as the latest group member (default is shown as the earliest member)
121
- # @option options [String] :filtered_by_type (nil) Notifiable type for filter
122
- # @option options [Object] :filtered_by_group (nil) Group instance for filter
123
- # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
124
- # @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
125
- # @option options [String] :filtered_by_key (nil) Key of the notification for filter
126
- # @option options [Array|Hash] :custom_filter (nil) Custom notification filter (e.g. ["created_at >= ?", time.hour.ago])
109
+ # @option options [Integer] :limit (nil) Limit to query for notifications
110
+ # @option options [Boolean] :reverse (false) If notification index will be ordered as earliest first
111
+ # @option options [Boolean] :with_group_members (false) If notification index will include group members
112
+ # @option options [Boolean] :as_latest_group_member (false) If grouped notification will be shown as the latest group member (default is shown as the earliest member)
113
+ # @option options [String] :filtered_by_type (nil) Notifiable type for filter
114
+ # @option options [Object] :filtered_by_group (nil) Group instance for filter
115
+ # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
116
+ # @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
117
+ # @option options [String] :filtered_by_key (nil) Key of the notification for filter
118
+ # @option options [Array|Hash] :custom_filter (nil) Custom notification filter (e.g. ["created_at >= ?", time.hour.ago])
119
+ # @option options [Boolean] :send_later (false) If it sends notification email asynchronously
120
+ # @option options [String, Symbol] :fallback (:batch_default) Fallback template to use when MissingTemplate is raised
121
+ # @option options [String] :batch_key (nil) Key of the batch notification email, a key of the first notification will be used if not specified
127
122
  # @return [Hash<Object, Mail::Message|ActionMailer::DeliveryJob>] Hash of target and sent email message or its delivery job
128
123
  def send_batch_unopened_notification_email(options = {})
129
- unopened_notification_index_map = unopened_notification_index_map(options)
124
+ unopened_notification_index_map = notification_index_map(options.merge(filtered_by_status: :unopened))
125
+ mailer_options = options.select { |k, _| [:send_later, :fallback, :batch_key].include?(k) }
130
126
  unopened_notification_index_map.map { |target, notifications|
131
- [target, Notification.send_batch_notification_email(target, notifications, options)]
127
+ [target, Notification.send_batch_notification_email(target, notifications, mailer_options)]
132
128
  }.to_h
133
129
  end
130
+
131
+ # Returns if subscription management is allowed for this target type.
132
+ # @return [Boolean] If subscription management is allowed for this target type
133
+ def subscription_enabled?
134
+ _notification_subscription_allowed ? true : false
135
+ end
136
+ alias_method :notification_subscription_enabled?, :subscription_enabled?
134
137
  end
135
138
 
136
139
  # Returns target email address for email notification.
@@ -154,13 +157,22 @@ module ActivityNotification
154
157
  # Returns if sending batch notification email is allowed for the target from configured field or overriden method.
155
158
  # This method is able to be overriden.
156
159
  #
157
- # @param [Object] notifiable_type Notifiable type of the notifications
158
160
  # @param [String] key Key of the notifications
159
161
  # @return [Boolean] If sending batch notification email is allowed for the target
160
- def batch_notification_email_allowed?(notifiable_type, key)
161
- resolve_value(_batch_notification_email_allowed, notifiable_type, key)
162
+ def batch_notification_email_allowed?(key)
163
+ resolve_value(_batch_notification_email_allowed, key)
162
164
  end
163
165
 
166
+ # Returns if subscription management is allowed for the target from configured field or overriden method.
167
+ # This method is able to be overriden.
168
+ #
169
+ # @param [String] key Key of the notifications
170
+ # @return [Boolean] If subscription management is allowed for the target
171
+ def subscription_allowed?(key)
172
+ resolve_value(_notification_subscription_allowed, key)
173
+ end
174
+ alias_method :notification_subscription_allowed?, :subscription_allowed?
175
+
164
176
  # Returns if current resource signed in with Devise is authenticated for the notification.
165
177
  # This method is able to be overriden.
166
178
  #
@@ -174,7 +186,7 @@ module ActivityNotification
174
186
  "with devise resource #{devise_resource.class} has been passed to #{self.class}##{__method__}. "\
175
187
  "You have to override #{self.class}##{__method__} method or set devise_resource in acts_as_target."
176
188
  end
177
- current_resource.present? and current_resource == devise_resource
189
+ current_resource.present? && current_resource == devise_resource
178
190
  end
179
191
 
180
192
  # Returns printable target model name to show in view or email.
@@ -215,13 +227,13 @@ module ActivityNotification
215
227
  _unopened_notification_index(options).present?
216
228
  end
217
229
 
218
- # Gets automatically arranged notification index of the target.
219
- # This method is the typical way to get notifications index from controller of view.
220
- # When the target have unopened notifications, it returns unopened notifications first.
230
+ # Returns automatically arranged notification index of the target.
231
+ # This method is the typical way to get notification index from controller and view.
232
+ # When the target has unopened notifications, it returns unopened notifications first.
221
233
  # Additionaly, it returns opened notifications unless unopened index size overs the limit.
222
- # @todo Is this switching the best solution?
234
+ # @todo Is this conbimned array the best solution?
223
235
  #
224
- # @example Get automatically arranged notification index of the @user
236
+ # @example Get automatically arranged notification index of @user
225
237
  # @notifications = @user.notification_index
226
238
  #
227
239
  # @param [Hash] options Options for notification index
@@ -242,9 +254,9 @@ module ActivityNotification
242
254
  options)
243
255
  end
244
256
 
245
- # Gets unopened notification index of the target.
257
+ # Returns unopened notification index of the target.
246
258
  #
247
- # @example Get unopened notification index of the @user
259
+ # @example Get unopened notification index of @user
248
260
  # @notifications = @user.unopened_notification_index
249
261
  #
250
262
  # @param [Hash] options Options for notification index
@@ -263,9 +275,9 @@ module ActivityNotification
263
275
  arrange_single_notification_index(method(:_unopened_notification_index), options)
264
276
  end
265
277
 
266
- # Gets opened notification index of the target.
278
+ # Returns opened notification index of the target.
267
279
  #
268
- # @example Get opened notification index of the @user
280
+ # @example Get opened notification index of @user
269
281
  # @notifications = @user.opened_notification_index(10)
270
282
  #
271
283
  # @param [Hash] options Options for notification index
@@ -290,12 +302,13 @@ module ActivityNotification
290
302
  #
291
303
  # @param [Object] notifiable Notifiable instance to notify
292
304
  # @param [Hash] options Options for notifications
293
- # @option options [String] :key (notifiable.default_notification_key) Key of the notification
294
- # @option options [Object] :group (nil) Group unit of the notifications
295
- # @option options [Object] :notifier (nil) Notifier of the notifications
296
- # @option options [Hash] :parameters ({}) Additional parameters of the notifications
297
- # @option options [Boolean] :send_email (true) Whether it sends notification email
298
- # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
305
+ # @option options [String] :key (notifiable.default_notification_key) Key of the notification
306
+ # @option options [Object] :group (nil) Group unit of the notifications
307
+ # @option options [ActiveSupport::Duration] :group_expiry_delay (nil) Expiry period of a notification group
308
+ # @option options [Object] :notifier (nil) Notifier of the notifications
309
+ # @option options [Hash] :parameters ({}) Additional parameters of the notifications
310
+ # @option options [Boolean] :send_email (true) Whether it sends notification email
311
+ # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
299
312
  # @return [Notification] Generated notification instance
300
313
  def notify_to(notifiable, options = {})
301
314
  Notification.notify_to(self, notifiable, options)
@@ -306,12 +319,12 @@ module ActivityNotification
306
319
  # @see NotificationApi#open_all_of
307
320
  #
308
321
  # @param [Hash] options Options for opening notifications
309
- # @option options [DateTime] :opened_at (DateTime.now) Time to set to opened_at of the notification record
310
- # @option options [String] :filtered_by_type (nil) Notifiable type for filter
311
- # @option options [Object] :filtered_by_group (nil) Group instance for filter
312
- # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
313
- # @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
314
- # @option options [String] :filtered_by_key (nil) Key of the notification for filter
322
+ # @option options [DateTime] :opened_at (Time.current) Time to set to opened_at of the notification record
323
+ # @option options [String] :filtered_by_type (nil) Notifiable type for filter
324
+ # @option options [Object] :filtered_by_group (nil) Group instance for filter
325
+ # @option options [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
326
+ # @option options [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
327
+ # @option options [String] :filtered_by_key (nil) Key of the notification for filter
315
328
  # @return [Integer] Number of opened notification records
316
329
  def open_all_notifications(options = {})
317
330
  Notification.open_all_of(self, options)
@@ -328,9 +341,9 @@ module ActivityNotification
328
341
  # @notifications = @user.notification_index_with_attributes
329
342
  #
330
343
  # @param [Hash] options Options for notification index
331
- # @option options [Boolean] :send_later (false) If it sends notification email asynchronously
332
- # @option options [String, Symbol] :fallback (:batch_default) Fallback template to use when MissingTemplate is raised
333
- # @option options [String] :batch_key (nil) Key of the batch notification email, a key of the first notification will be used if not specified
344
+ # @option options [Boolean] :send_later (false) If it sends notification email asynchronously
345
+ # @option options [String, Symbol] :fallback (:batch_default) Fallback template to use when MissingTemplate is raised
346
+ # @option options [String] :batch_key (nil) Key of the batch notification email, a key of the first notification will be used if not specified
334
347
  # @option options [Integer] :limit (nil) Limit to query for notifications
335
348
  # @option options [Boolean] :reverse (false) If notification index will be ordered as earliest first
336
349
  # @option options [Boolean] :with_group_members (false) If notification index will include group members
@@ -417,6 +430,27 @@ module ActivityNotification
417
430
  end
418
431
  end
419
432
 
433
+ # Returns if the target subscribes to the notification.
434
+ # It also returns true when the subscription management is not allowed for the target.
435
+ #
436
+ # @param [String] key Key of the notification
437
+ # @param [String] subscribe_as_default Default subscription value to use when the subscription record does not configured
438
+ # @return [Boolean] If the target subscribes the notification or the subscription management is not allowed for the target
439
+ def subscribes_to_notification?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
440
+ !subscription_allowed?(key) or _subscribes_to_notification?(key, subscribe_as_default)
441
+ end
442
+
443
+ # Returns if the target subscribes to the notification email.
444
+ # It also returns true when the subscription management is not allowed for the target.
445
+ #
446
+ # @param [String] key Key of the notification
447
+ # @param [String] subscribe_as_default Default subscription value to use when the subscription record does not configured
448
+ # @return [Boolean] If the target subscribes the notification email or the subscription management is not allowed for the target
449
+ def subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
450
+ !subscription_allowed?(key) or _subscribes_to_notification_email?(key, subscribe_as_default)
451
+ end
452
+ alias_method :subscribes_to_email?, :subscribes_to_notification_email?
453
+
420
454
 
421
455
  private
422
456
 
@@ -4,7 +4,9 @@ module ActivityNotification
4
4
  include Renderable
5
5
  include Common
6
6
  include NotificationApi
7
- self.table_name = ActivityNotification.config.table_name
7
+ # @deprecated ActivityNotification.config.table_name as of 1.1.0
8
+ self.table_name = ActivityNotification.config.table_name || ActivityNotification.config.notification_table_name
9
+ # self.table_name = ActivityNotification.config.notification_table_name
8
10
 
9
11
  # Belongs to target instance of this notification as polymorphic association.
10
12
  # @scope instance
@@ -61,10 +63,10 @@ module ActivityNotification
61
63
  # ActivityNotification::Notification.all_index!
62
64
  # is defined same as
63
65
  # ActivityNotification::Notification.group_owners_only.latest_order
66
+ # @scope class
64
67
  # @example Get all notification index of the @user
65
68
  # @notifications = @user.notifications.all_index!
66
69
  # @notifications = @user.notifications.group_owners_only.latest_order
67
- # @scope class
68
70
  # @param [Boolean] reverse If notification index will be ordered as earliest first
69
71
  # @param [Boolean] with_group_members If notification index will include group members
70
72
  # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
@@ -82,10 +84,10 @@ module ActivityNotification
82
84
  # ActivityNotification::Notification.unopened_index
83
85
  # is defined same as
84
86
  # ActivityNotification::Notification.unopened_only.group_owners_only.latest_order
87
+ # @scope class
85
88
  # @example Get unopened notificaton index of the @user
86
89
  # @notifications = @user.notifications.unopened_index
87
90
  # @notifications = @user.notifications.unopened_only.group_owners_only.latest_order
88
- # @scope class
89
91
  # @param [Boolean] reverse If notification index will be ordered as earliest first
90
92
  # @param [Boolean] with_group_members If notification index will include group members
91
93
  # @return [ActiveRecord_AssociationRelation<Notificaion>] Array or database query of filtered notifications
@@ -110,10 +112,10 @@ module ActivityNotification
110
112
  # ActivityNotification::Notification.opened_index(limit)
111
113
  # is defined same as
112
114
  # ActivityNotification::Notification.opened_only(limit).group_owners_only.latest_order
115
+ # @scope class
113
116
  # @example Get unopened notificaton index of the @user with limit 10
114
117
  # @notifications = @user.notifications.opened_index(10)
115
118
  # @notifications = @user.notifications.opened_only(10).group_owners_only.latest_order
116
- # @scope class
117
119
  # @param [Integer] limit Limit to query for opened notifications
118
120
  # @param [Boolean] reverse If notification index will be ordered as earliest first
119
121
  # @param [Boolean] with_group_members If notification index will include group members
@@ -214,7 +216,7 @@ module ActivityNotification
214
216
  if options.has_key?(:filtered_by_group)
215
217
  filtered_notifications = filtered_notifications.filtered_by_group(options[:filtered_by_group])
216
218
  end
217
- if options.has_key?(:filtered_by_group_type) and options.has_key?(:filtered_by_group_id)
219
+ if options.has_key?(:filtered_by_group_type) && options.has_key?(:filtered_by_group_id)
218
220
  filtered_notifications = filtered_notifications
219
221
  .where(group_type: options[:filtered_by_group_type], group_id: options[:filtered_by_group_id])
220
222
  end