activity_notification 1.4.4 → 2.2.4

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 (271) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +22 -0
  3. data/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
  4. data/.github/pull_request_template.md +13 -0
  5. data/.github/workflows/build.yml +116 -0
  6. data/.gitignore +15 -3
  7. data/CHANGELOG.md +200 -1
  8. data/Gemfile +17 -2
  9. data/Procfile +2 -0
  10. data/README.md +168 -1033
  11. data/Rakefile +19 -10
  12. data/activity_notification.gemspec +14 -9
  13. data/app/channels/activity_notification/notification_api_channel.rb +12 -0
  14. data/app/channels/activity_notification/notification_api_with_devise_channel.rb +46 -0
  15. data/app/channels/activity_notification/notification_channel.rb +37 -0
  16. data/app/channels/activity_notification/notification_with_devise_channel.rb +51 -0
  17. data/app/controllers/activity_notification/apidocs_controller.rb +75 -0
  18. data/app/controllers/activity_notification/notifications_api_controller.rb +143 -0
  19. data/app/controllers/activity_notification/notifications_api_with_devise_controller.rb +7 -0
  20. data/app/controllers/activity_notification/notifications_controller.rb +60 -54
  21. data/app/controllers/activity_notification/subscriptions_api_controller.rb +197 -0
  22. data/app/controllers/activity_notification/subscriptions_api_with_devise_controller.rb +7 -0
  23. data/app/controllers/activity_notification/subscriptions_controller.rb +83 -73
  24. data/app/jobs/activity_notification/notify_all_job.rb +25 -0
  25. data/app/jobs/activity_notification/notify_job.rb +26 -0
  26. data/app/jobs/activity_notification/notify_to_job.rb +25 -0
  27. data/app/views/activity_notification/notifications/default/_default.html.erb +23 -23
  28. data/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb +19 -19
  29. data/app/views/activity_notification/notifications/default/_index.html.erb +3 -3
  30. data/app/views/activity_notification/notifications/default/index.html.erb +60 -7
  31. data/app/views/activity_notification/notifications/default/open.js.erb +2 -2
  32. data/app/views/activity_notification/notifications/default/open_all.js.erb +2 -2
  33. data/app/views/activity_notification/notifications/default/show.html.erb +2 -2
  34. data/app/views/activity_notification/optional_targets/default/action_cable_channel/_default.html.erb +176 -0
  35. data/app/views/activity_notification/optional_targets/default/base/_default.text.erb +1 -1
  36. data/app/views/activity_notification/optional_targets/default/slack/_default.text.erb +1 -1
  37. data/app/views/activity_notification/subscriptions/default/_form.html.erb +2 -2
  38. data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +5 -33
  39. data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +8 -8
  40. data/app/views/activity_notification/subscriptions/default/index.html.erb +13 -9
  41. data/app/views/activity_notification/subscriptions/default/show.html.erb +3 -3
  42. data/app/views/activity_notification/subscriptions/default/subscribe.js.erb +1 -1
  43. data/app/views/activity_notification/subscriptions/default/subscribe_to_email.js.erb +1 -1
  44. data/app/views/activity_notification/subscriptions/default/subscribe_to_optional_target.js.erb +1 -1
  45. data/app/views/activity_notification/subscriptions/default/unsubscribe.js.erb +1 -1
  46. data/app/views/activity_notification/subscriptions/default/unsubscribe_to_email.js.erb +1 -1
  47. data/app/views/activity_notification/subscriptions/default/unsubscribe_to_optional_target.js.erb +1 -1
  48. data/bin/_dynamodblocal +4 -0
  49. data/bin/bundle_update.sh +7 -0
  50. data/bin/deploy_on_heroku.sh +16 -0
  51. data/bin/install_dynamodblocal.sh +5 -0
  52. data/bin/start_dynamodblocal.sh +47 -0
  53. data/bin/stop_dynamodblocal.sh +34 -0
  54. data/docs/CODE_OF_CONDUCT.md +76 -0
  55. data/docs/CONTRIBUTING.md +36 -0
  56. data/docs/Functions.md +1146 -0
  57. data/docs/Setup.md +817 -0
  58. data/docs/Testing.md +148 -0
  59. data/gemfiles/Gemfile.rails-5.0 +8 -1
  60. data/gemfiles/Gemfile.rails-5.1 +7 -1
  61. data/gemfiles/Gemfile.rails-5.2 +24 -0
  62. data/gemfiles/Gemfile.rails-6.0 +23 -0
  63. data/gemfiles/Gemfile.rails-6.1 +22 -0
  64. data/gemfiles/Gemfile.rails-7.0 +25 -0
  65. data/lib/activity_notification/apis/notification_api.rb +356 -159
  66. data/lib/activity_notification/apis/subscription_api.rb +98 -59
  67. data/lib/activity_notification/apis/swagger.rb +6 -0
  68. data/lib/activity_notification/common.rb +18 -7
  69. data/lib/activity_notification/config.rb +176 -30
  70. data/lib/activity_notification/controllers/common_api_controller.rb +30 -0
  71. data/lib/activity_notification/controllers/common_controller.rb +47 -27
  72. data/lib/activity_notification/controllers/concerns/swagger/error_responses.rb +55 -0
  73. data/lib/activity_notification/controllers/concerns/swagger/notifications_api.rb +273 -0
  74. data/lib/activity_notification/controllers/concerns/swagger/notifications_parameters.rb +92 -0
  75. data/lib/activity_notification/controllers/concerns/swagger/subscriptions_api.rb +405 -0
  76. data/lib/activity_notification/controllers/concerns/swagger/subscriptions_parameters.rb +50 -0
  77. data/lib/activity_notification/controllers/devise_authentication_controller.rb +22 -5
  78. data/lib/activity_notification/gem_version.rb +14 -0
  79. data/lib/activity_notification/helpers/errors.rb +6 -0
  80. data/lib/activity_notification/helpers/view_helpers.rb +118 -28
  81. data/lib/activity_notification/mailers/helpers.rb +19 -12
  82. data/lib/activity_notification/models/concerns/notifiable.rb +142 -55
  83. data/lib/activity_notification/models/concerns/subscriber.rb +28 -13
  84. data/lib/activity_notification/models/concerns/swagger/error_schema.rb +36 -0
  85. data/lib/activity_notification/models/concerns/swagger/notification_schema.rb +209 -0
  86. data/lib/activity_notification/models/concerns/swagger/subscription_schema.rb +162 -0
  87. data/lib/activity_notification/models/concerns/target.rb +131 -32
  88. data/lib/activity_notification/models/notification.rb +1 -0
  89. data/lib/activity_notification/models/subscription.rb +1 -0
  90. data/lib/activity_notification/models.rb +23 -1
  91. data/lib/activity_notification/optional_targets/action_cable_api_channel.rb +69 -0
  92. data/lib/activity_notification/optional_targets/action_cable_channel.rb +68 -0
  93. data/lib/activity_notification/optional_targets/base.rb +9 -15
  94. data/lib/activity_notification/orm/active_record/notification.rb +23 -34
  95. data/lib/activity_notification/orm/active_record/subscription.rb +1 -1
  96. data/lib/activity_notification/orm/active_record.rb +1 -1
  97. data/lib/activity_notification/orm/dynamoid/extension.rb +262 -0
  98. data/lib/activity_notification/orm/dynamoid/notification.rb +224 -0
  99. data/lib/activity_notification/orm/dynamoid/subscription.rb +82 -0
  100. data/lib/activity_notification/orm/dynamoid.rb +530 -0
  101. data/lib/activity_notification/orm/mongoid/notification.rb +29 -28
  102. data/lib/activity_notification/orm/mongoid/subscription.rb +3 -3
  103. data/lib/activity_notification/orm/mongoid.rb +33 -1
  104. data/lib/activity_notification/rails/routes.rb +273 -60
  105. data/lib/activity_notification/renderable.rb +22 -7
  106. data/lib/activity_notification/roles/acts_as_notifiable.rb +64 -1
  107. data/lib/activity_notification/roles/acts_as_target.rb +99 -9
  108. data/lib/activity_notification/version.rb +1 -1
  109. data/lib/activity_notification.rb +14 -0
  110. data/lib/generators/activity_notification/controllers_generator.rb +2 -1
  111. data/lib/generators/templates/activity_notification.rb +61 -7
  112. data/lib/generators/templates/controllers/README +2 -2
  113. data/lib/generators/templates/controllers/notifications_api_controller.rb +31 -0
  114. data/lib/generators/templates/controllers/notifications_api_with_devise_controller.rb +31 -0
  115. data/lib/generators/templates/controllers/notifications_controller.rb +1 -37
  116. data/lib/generators/templates/controllers/notifications_with_devise_controller.rb +1 -45
  117. data/lib/generators/templates/controllers/subscriptions_api_controller.rb +61 -0
  118. data/lib/generators/templates/controllers/subscriptions_api_with_devise_controller.rb +61 -0
  119. data/lib/generators/templates/controllers/subscriptions_controller.rb +14 -37
  120. data/lib/generators/templates/controllers/subscriptions_with_devise_controller.rb +14 -45
  121. data/lib/generators/templates/migrations/migration.rb +5 -5
  122. data/lib/generators/templates/models/README +8 -4
  123. data/lib/generators/templates/models/notification.rb +1 -1
  124. data/lib/generators/templates/models/subscription.rb +1 -1
  125. data/lib/tasks/activity_notification_tasks.rake +14 -4
  126. data/package.json +8 -0
  127. data/spec/channels/notification_api_channel_shared_examples.rb +59 -0
  128. data/spec/channels/notification_api_channel_spec.rb +49 -0
  129. data/spec/channels/notification_api_with_devise_channel_spec.rb +76 -0
  130. data/spec/channels/notification_channel_shared_examples.rb +59 -0
  131. data/spec/channels/notification_channel_spec.rb +48 -0
  132. data/spec/channels/notification_with_devise_channel_spec.rb +97 -0
  133. data/spec/concerns/apis/notification_api_spec.rb +177 -12
  134. data/spec/concerns/apis/subscription_api_spec.rb +146 -4
  135. data/spec/concerns/common_spec.rb +25 -3
  136. data/spec/concerns/models/notifiable_spec.rb +161 -11
  137. data/spec/concerns/models/subscriber_spec.rb +253 -79
  138. data/spec/concerns/models/target_spec.rb +180 -47
  139. data/spec/concerns/renderable_spec.rb +35 -16
  140. data/spec/config_spec.rb +52 -1
  141. data/spec/controllers/controller_spec_utility.rb +100 -0
  142. data/spec/controllers/notifications_api_controller_shared_examples.rb +506 -0
  143. data/spec/controllers/notifications_api_controller_spec.rb +19 -0
  144. data/spec/controllers/notifications_api_with_devise_controller_spec.rb +60 -0
  145. data/spec/controllers/notifications_controller_shared_examples.rb +55 -76
  146. data/spec/controllers/notifications_controller_spec.rb +1 -2
  147. data/spec/controllers/notifications_with_devise_controller_spec.rb +14 -8
  148. data/spec/controllers/subscriptions_api_controller_shared_examples.rb +750 -0
  149. data/spec/controllers/subscriptions_api_controller_spec.rb +19 -0
  150. data/spec/controllers/subscriptions_api_with_devise_controller_spec.rb +60 -0
  151. data/spec/controllers/subscriptions_controller_shared_examples.rb +99 -121
  152. data/spec/controllers/subscriptions_controller_spec.rb +1 -2
  153. data/spec/controllers/subscriptions_with_devise_controller_spec.rb +14 -8
  154. data/spec/factories/notifications.rb +1 -1
  155. data/spec/factories/subscriptions.rb +3 -3
  156. data/spec/factories/users.rb +3 -3
  157. data/spec/generators/migration/migration_generator_spec.rb +29 -4
  158. data/spec/helpers/view_helpers_spec.rb +31 -21
  159. data/spec/jobs/notify_all_job_spec.rb +23 -0
  160. data/spec/jobs/notify_job_spec.rb +23 -0
  161. data/spec/jobs/notify_to_job_spec.rb +23 -0
  162. data/spec/mailers/mailer_spec.rb +42 -1
  163. data/spec/models/dummy/dummy_group_spec.rb +4 -0
  164. data/spec/models/dummy/dummy_notifiable_spec.rb +4 -0
  165. data/spec/models/dummy/dummy_notifier_spec.rb +4 -0
  166. data/spec/models/dummy/dummy_subscriber_spec.rb +3 -0
  167. data/spec/models/dummy/dummy_target_spec.rb +4 -0
  168. data/spec/models/notification_spec.rb +181 -45
  169. data/spec/models/subscription_spec.rb +77 -27
  170. data/spec/optional_targets/action_cable_api_channel_spec.rb +34 -0
  171. data/spec/optional_targets/action_cable_channel_spec.rb +41 -0
  172. data/spec/optional_targets/amazon_sns_spec.rb +0 -2
  173. data/spec/optional_targets/slack_spec.rb +0 -2
  174. data/spec/orm/dynamoid_spec.rb +115 -0
  175. data/spec/rails_app/Rakefile +9 -0
  176. data/spec/rails_app/app/assets/config/manifest.js +3 -0
  177. data/spec/rails_app/app/assets/javascripts/application.js +2 -1
  178. data/spec/rails_app/app/assets/javascripts/cable.js +12 -0
  179. data/spec/rails_app/app/controllers/admins_controller.rb +21 -0
  180. data/spec/rails_app/app/controllers/application_controller.rb +1 -1
  181. data/spec/rails_app/app/controllers/articles_controller.rb +6 -1
  182. data/spec/rails_app/app/controllers/comments_controller.rb +3 -1
  183. data/spec/rails_app/app/controllers/spa_controller.rb +7 -0
  184. data/spec/rails_app/app/controllers/users/notifications_controller.rb +0 -65
  185. data/spec/rails_app/app/controllers/users/notifications_with_devise_controller.rb +0 -73
  186. data/spec/rails_app/app/controllers/users/subscriptions_controller.rb +0 -77
  187. data/spec/rails_app/app/controllers/users/subscriptions_with_devise_controller.rb +0 -85
  188. data/spec/rails_app/app/controllers/users_controller.rb +26 -0
  189. data/spec/rails_app/app/javascript/App.vue +40 -0
  190. data/spec/rails_app/app/javascript/components/DeviseTokenAuth.vue +82 -0
  191. data/spec/rails_app/app/javascript/components/Top.vue +98 -0
  192. data/spec/rails_app/app/javascript/components/notifications/Index.vue +200 -0
  193. data/spec/rails_app/app/javascript/components/notifications/Notification.vue +133 -0
  194. data/spec/rails_app/app/javascript/components/notifications/NotificationContent.vue +122 -0
  195. data/spec/rails_app/app/javascript/components/subscriptions/Index.vue +279 -0
  196. data/spec/rails_app/app/javascript/components/subscriptions/NewSubscription.vue +112 -0
  197. data/spec/rails_app/app/javascript/components/subscriptions/NotificationKey.vue +141 -0
  198. data/spec/rails_app/app/javascript/components/subscriptions/Subscription.vue +226 -0
  199. data/spec/rails_app/app/javascript/config/development.js +5 -0
  200. data/spec/rails_app/app/javascript/config/environment.js +7 -0
  201. data/spec/rails_app/app/javascript/config/production.js +5 -0
  202. data/spec/rails_app/app/javascript/config/test.js +5 -0
  203. data/spec/rails_app/app/javascript/packs/application.js +18 -0
  204. data/spec/rails_app/app/javascript/packs/spa.js +14 -0
  205. data/spec/rails_app/app/javascript/router/index.js +73 -0
  206. data/spec/rails_app/app/javascript/store/index.js +37 -0
  207. data/spec/rails_app/app/models/admin.rb +15 -10
  208. data/spec/rails_app/app/models/article.rb +25 -20
  209. data/spec/rails_app/app/models/comment.rb +27 -62
  210. data/spec/rails_app/app/models/dummy/dummy_base.rb +1 -0
  211. data/spec/rails_app/app/models/dummy/dummy_group.rb +9 -0
  212. data/spec/rails_app/app/models/dummy/dummy_notifiable.rb +1 -0
  213. data/spec/rails_app/app/models/dummy/dummy_notifiable_target.rb +27 -0
  214. data/spec/rails_app/app/models/dummy/dummy_notifier.rb +1 -0
  215. data/spec/rails_app/app/models/dummy/dummy_subscriber.rb +1 -0
  216. data/spec/rails_app/app/models/dummy/dummy_target.rb +1 -0
  217. data/spec/rails_app/app/models/user.rb +44 -18
  218. data/spec/rails_app/app/views/activity_notification/notifications/default/article/_update.html.erb +146 -0
  219. data/spec/rails_app/app/views/activity_notification/notifications/users/overridden/custom/_test.html.erb +1 -0
  220. data/spec/rails_app/app/views/activity_notification/optional_targets/admins/amazon_sns/comment/_default.text.erb +1 -1
  221. data/spec/rails_app/app/views/articles/index.html.erb +68 -20
  222. data/spec/rails_app/app/views/articles/show.html.erb +1 -1
  223. data/spec/rails_app/app/views/layouts/_header.html.erb +9 -3
  224. data/spec/rails_app/app/views/spa/index.html.erb +2 -0
  225. data/spec/rails_app/babel.config.js +72 -0
  226. data/spec/rails_app/bin/webpack +18 -0
  227. data/spec/rails_app/bin/webpack-dev-server +18 -0
  228. data/spec/rails_app/config/application.rb +26 -6
  229. data/spec/rails_app/config/cable.yml +8 -0
  230. data/spec/rails_app/config/database.yml +1 -1
  231. data/spec/rails_app/config/dynamoid.rb +13 -0
  232. data/spec/rails_app/config/environment.rb +5 -1
  233. data/spec/rails_app/config/environments/development.rb +5 -0
  234. data/spec/rails_app/config/environments/production.rb +7 -1
  235. data/spec/rails_app/config/environments/test.rb +7 -11
  236. data/spec/rails_app/config/initializers/activity_notification.rb +63 -9
  237. data/spec/rails_app/config/initializers/copy_it.aws.rb.template +6 -0
  238. data/spec/rails_app/config/initializers/devise_token_auth.rb +55 -0
  239. data/spec/rails_app/config/initializers/mysql.rb +9 -0
  240. data/spec/rails_app/config/locales/activity_notification.en.yml +10 -4
  241. data/spec/rails_app/config/routes.rb +42 -1
  242. data/spec/rails_app/config/webpack/development.js +5 -0
  243. data/spec/rails_app/config/webpack/environment.js +7 -0
  244. data/spec/rails_app/config/webpack/loaders/vue.js +6 -0
  245. data/spec/rails_app/config/webpack/production.js +5 -0
  246. data/spec/rails_app/config/webpack/test.js +5 -0
  247. data/spec/rails_app/config/webpacker.yml +97 -0
  248. data/spec/rails_app/db/migrate/{20160715050433_create_test_tables.rb → 20160716000000_create_test_tables.rb} +1 -1
  249. data/spec/rails_app/db/migrate/{20160715050420_create_activity_notification_tables.rb → 20181209000000_create_activity_notification_tables.rb} +3 -3
  250. data/spec/rails_app/db/migrate/20191201000000_add_tokens_to_users.rb +10 -0
  251. data/spec/rails_app/db/schema.rb +46 -43
  252. data/spec/rails_app/db/seeds.rb +28 -4
  253. data/spec/rails_app/lib/custom_optional_targets/raise_error.rb +14 -0
  254. data/spec/rails_app/lib/mailer_previews/mailer_preview.rb +14 -4
  255. data/spec/rails_app/package.json +23 -0
  256. data/spec/rails_app/postcss.config.js +12 -0
  257. data/spec/roles/acts_as_group_spec.rb +0 -2
  258. data/spec/roles/acts_as_notifiable_spec.rb +80 -20
  259. data/spec/roles/acts_as_notifier_spec.rb +0 -2
  260. data/spec/roles/acts_as_target_spec.rb +1 -5
  261. data/spec/spec_helper.rb +13 -11
  262. data/spec/version_spec.rb +31 -0
  263. metadata +306 -53
  264. data/.travis.yml +0 -85
  265. data/Gemfile.lock +0 -234
  266. data/gemfiles/Gemfile.rails-4.2 +0 -17
  267. data/gemfiles/Gemfile.rails-4.2.lock +0 -225
  268. data/gemfiles/Gemfile.rails-5.0.lock +0 -234
  269. data/gemfiles/Gemfile.rails-5.1.lock +0 -234
  270. data/spec/rails_app/app/views/activity_notification/notifications/users/overriden/custom/_test.html.erb +0 -1
  271. /data/spec/rails_app/app/{models → assets/images}/.keep +0 -0
@@ -12,22 +12,24 @@ module ActivityNotification
12
12
  include Rails.application.routes.url_helpers
13
13
 
14
14
  # Has many notification instances for this notifiable.
15
- # Dependency for these notifications can be overriden from acts_as_notifiable.
15
+ # Dependency for these notifications can be overridden from acts_as_notifiable.
16
16
  # @scope instance
17
17
  # @return [Array<Notificaion>, Mongoid::Criteria<Notificaion>] Array or database query of notifications for this notifiable
18
18
  has_many_records :generated_notifications_as_notifiable,
19
19
  class_name: "::ActivityNotification::Notification",
20
20
  as: :notifiable
21
21
 
22
- class_attribute :_notification_targets,
23
- :_notification_group,
24
- :_notification_group_expiry_delay,
25
- :_notifier,
26
- :_notification_parameters,
27
- :_notification_email_allowed,
28
- :_notifiable_path,
29
- :_printable_notifiable_name,
30
- :_optional_targets
22
+ class_attribute :_notification_targets,
23
+ :_notification_group,
24
+ :_notification_group_expiry_delay,
25
+ :_notifier,
26
+ :_notification_parameters,
27
+ :_notification_email_allowed,
28
+ :_notifiable_action_cable_allowed,
29
+ :_notifiable_action_cable_api_allowed,
30
+ :_notifiable_path,
31
+ :_printable_notifiable_name,
32
+ :_optional_targets
31
33
  set_notifiable_class_defaults
32
34
  end
33
35
 
@@ -47,21 +49,23 @@ module ActivityNotification
47
49
  # Sets default values to notifiable class fields.
48
50
  # @return [NilClass] nil
49
51
  def set_notifiable_class_defaults
50
- self._notification_targets = {}
51
- self._notification_group = {}
52
- self._notification_group_expiry_delay = {}
53
- self._notifier = {}
54
- self._notification_parameters = {}
55
- self._notification_email_allowed = {}
56
- self._notifiable_path = {}
57
- self._printable_notifiable_name = {}
58
- self._optional_targets = {}
52
+ self._notification_targets = {}
53
+ self._notification_group = {}
54
+ self._notification_group_expiry_delay = {}
55
+ self._notifier = {}
56
+ self._notification_parameters = {}
57
+ self._notification_email_allowed = {}
58
+ self._notifiable_action_cable_allowed = {}
59
+ self._notifiable_action_cable_api_allowed = {}
60
+ self._notifiable_path = {}
61
+ self._printable_notifiable_name = {}
62
+ self._optional_targets = {}
59
63
  nil
60
64
  end
61
65
  end
62
66
 
63
- # Returns notification targets from configured field or overriden method.
64
- # This method is able to be overriden.
67
+ # Returns notification targets from configured field or overridden method.
68
+ # This method is able to be overridden.
65
69
  #
66
70
  # @param [String] target_type Target type to notify
67
71
  # @param [Hash] options Options for notifications
@@ -82,8 +86,8 @@ module ActivityNotification
82
86
  resolved_parameter
83
87
  end
84
88
 
85
- # Returns group unit of the notifications from configured field or overriden method.
86
- # This method is able to be overriden.
89
+ # Returns group unit of the notifications from configured field or overridden method.
90
+ # This method is able to be overridden.
87
91
  #
88
92
  # @param [String] target_type Target type to notify
89
93
  # @param [String] key Key of the notification
@@ -96,8 +100,8 @@ module ActivityNotification
96
100
  key)
97
101
  end
98
102
 
99
- # Returns group expiry period of the notifications from configured field or overriden method.
100
- # This method is able to be overriden.
103
+ # Returns group expiry period of the notifications from configured field or overridden method.
104
+ # This method is able to be overridden.
101
105
  #
102
106
  # @param [String] target_type Target type to notify
103
107
  # @param [String] key Key of the notification
@@ -110,8 +114,8 @@ module ActivityNotification
110
114
  key)
111
115
  end
112
116
 
113
- # Returns additional notification parameters from configured field or overriden method.
114
- # This method is able to be overriden.
117
+ # Returns additional notification parameters from configured field or overridden method.
118
+ # This method is able to be overridden.
115
119
  #
116
120
  # @param [String] target_type Target type to notify
117
121
  # @param [String] key Key of the notification
@@ -124,8 +128,8 @@ module ActivityNotification
124
128
  key)
125
129
  end
126
130
 
127
- # Returns notifier of the notification from configured field or overriden method.
128
- # This method is able to be overriden.
131
+ # Returns notifier of the notification from configured field or overridden method.
132
+ # This method is able to be overridden.
129
133
  #
130
134
  # @param [String] target_type Target type to notify
131
135
  # @param [String] key Key of the notification
@@ -138,8 +142,8 @@ module ActivityNotification
138
142
  key)
139
143
  end
140
144
 
141
- # Returns if sending notification email is allowed for the notifiable from configured field or overriden method.
142
- # This method is able to be overriden.
145
+ # Returns if sending notification email is allowed for the notifiable from configured field or overridden method.
146
+ # This method is able to be overridden.
143
147
  #
144
148
  # @param [Object] target Target instance to notify
145
149
  # @param [String] key Key of the notification
@@ -152,8 +156,36 @@ module ActivityNotification
152
156
  target, key)
153
157
  end
154
158
 
155
- # Returns notifiable_path to move after opening notification from configured field or overriden method.
156
- # This method is able to be overriden.
159
+ # Returns if publishing WebSocket using ActionCable is allowed for the notifiable from configured field or overridden method.
160
+ # This method is able to be overridden.
161
+ #
162
+ # @param [Object] target Target instance to notify
163
+ # @param [String] key Key of the notification
164
+ # @return [Boolean] If publishing WebSocket using ActionCable is allowed for the notifiable
165
+ def notifiable_action_cable_allowed?(target, key = nil)
166
+ resolve_parameter(
167
+ "notifiable_action_cable_allowed_for_#{cast_to_resources_name(target.class)}?",
168
+ _notifiable_action_cable_allowed[cast_to_resources_sym(target.class)],
169
+ ActivityNotification.config.action_cable_enabled,
170
+ target, key)
171
+ end
172
+
173
+ # Returns if publishing WebSocket API using ActionCable is allowed for the notifiable from configured field or overridden method.
174
+ # This method is able to be overridden.
175
+ #
176
+ # @param [Object] target Target instance to notify
177
+ # @param [String] key Key of the notification
178
+ # @return [Boolean] If publishing WebSocket API using ActionCable is allowed for the notifiable
179
+ def notifiable_action_cable_api_allowed?(target, key = nil)
180
+ resolve_parameter(
181
+ "notifiable_action_cable_api_allowed_for_#{cast_to_resources_name(target.class)}?",
182
+ _notifiable_action_cable_api_allowed[cast_to_resources_sym(target.class)],
183
+ ActivityNotification.config.action_cable_api_enabled,
184
+ target, key)
185
+ end
186
+
187
+ # Returns notifiable_path to move after opening notification from configured field or overridden method.
188
+ # This method is able to be overridden.
157
189
  #
158
190
  # @param [String] target_type Target type to notify
159
191
  # @param [String] key Key of the notification
@@ -166,7 +198,7 @@ module ActivityNotification
166
198
  key)
167
199
  unless resolved_parameter
168
200
  begin
169
- resolved_parameter = polymorphic_path(self)
201
+ resolved_parameter = defined?(super) ? super : polymorphic_path(self)
170
202
  rescue NoMethodError, ActionController::UrlGenerationError
171
203
  raise NotImplementedError, "You have to implement #{self.class}##{__method__}, "\
172
204
  "set :notifiable_path in acts_as_notifiable or "\
@@ -186,8 +218,8 @@ module ActivityNotification
186
218
  target, key)
187
219
  end
188
220
 
189
- # Returns optional_targets of the notification from configured field or overriden method.
190
- # This method is able to be overriden.
221
+ # Returns optional_targets of the notification from configured field or overridden method.
222
+ # This method is able to be overridden.
191
223
  #
192
224
  # @param [String] target_type Target type to notify
193
225
  # @param [String] key Key of the notification
@@ -200,8 +232,8 @@ module ActivityNotification
200
232
  key)
201
233
  end
202
234
 
203
- # Returns optional_target names of the notification from configured field or overriden method.
204
- # This method is able to be overriden.
235
+ # Returns optional_target names of the notification from configured field or overridden method.
236
+ # This method is able to be overridden.
205
237
  #
206
238
  # @param [String] target_type Target type to notify
207
239
  # @param [String] key Key of the notification
@@ -249,6 +281,27 @@ module ActivityNotification
249
281
  Notification.notify(target_type, self, options)
250
282
  end
251
283
 
284
+ # Generates notifications to configured targets with notifiable model later by ActiveJob queue.
285
+ # This method calls NotificationApi#notify_later internally with self notifiable instance.
286
+ # @see NotificationApi#notify_later
287
+ #
288
+ # @param [Symbol, String, Class] target_type Type of target
289
+ # @param [Hash] options Options for notifications
290
+ # @option options [String] :key (notifiable.default_notification_key) Key of the notification
291
+ # @option options [Object] :group (nil) Group unit of the notifications
292
+ # @option options [ActiveSupport::Duration] :group_expiry_delay (nil) Expiry period of a notification group
293
+ # @option options [Object] :notifier (nil) Notifier of the notifications
294
+ # @option options [Hash] :parameters ({}) Additional parameters of the notifications
295
+ # @option options [Boolean] :send_email (true) Whether it sends notification email
296
+ # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
297
+ # @option options [Boolean] :publish_optional_targets (true) Whether it publishes notification to optional targets
298
+ # @option options [Hash<String, Hash>] :optional_targets ({}) Options for optional targets, keys are optional target name (:amazon_sns or :slack etc) and values are options
299
+ # @return [Array<Notificaion>] Array of generated notifications
300
+ def notify_later(target_type, options = {})
301
+ Notification.notify_later(target_type, self, options)
302
+ end
303
+ alias_method :notify_now, :notify
304
+
252
305
  # Generates notifications to one target.
253
306
  # This method calls NotificationApi#notify_all internally with self notifiable instance.
254
307
  # @see NotificationApi#notify_all
@@ -268,6 +321,27 @@ module ActivityNotification
268
321
  def notify_all(targets, options = {})
269
322
  Notification.notify_all(targets, self, options)
270
323
  end
324
+ alias_method :notify_all_now, :notify_all
325
+
326
+ # Generates notifications to one target later by ActiveJob queue.
327
+ # This method calls NotificationApi#notify_all_later internally with self notifiable instance.
328
+ # @see NotificationApi#notify_all_later
329
+ #
330
+ # @param [Array<Object>] targets Targets to send notifications
331
+ # @param [Hash] options Options for notifications
332
+ # @option options [String] :key (notifiable.default_notification_key) Key of the notification
333
+ # @option options [Object] :group (nil) Group unit of the notifications
334
+ # @option options [ActiveSupport::Duration] :group_expiry_delay (nil) Expiry period of a notification group
335
+ # @option options [Object] :notifier (nil) Notifier of the notifications
336
+ # @option options [Hash] :parameters ({}) Additional parameters of the notifications
337
+ # @option options [Boolean] :send_email (true) Whether it sends notification email
338
+ # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
339
+ # @option options [Boolean] :publish_optional_targets (true) Whether it publishes notification to optional targets
340
+ # @option options [Hash<String, Hash>] :optional_targets ({}) Options for optional targets, keys are optional target name (:amazon_sns or :slack etc) and values are options
341
+ # @return [Array<Notificaion>] Array of generated notifications
342
+ def notify_all_later(targets, options = {})
343
+ Notification.notify_all_later(targets, self, options)
344
+ end
271
345
 
272
346
  # Generates notifications to one target.
273
347
  # This method calls NotificationApi#notify_to internally with self notifiable instance.
@@ -288,9 +362,30 @@ module ActivityNotification
288
362
  def notify_to(target, options = {})
289
363
  Notification.notify_to(target, self, options)
290
364
  end
365
+ alias_method :notify_now_to, :notify_to
366
+
367
+ # Generates notifications to one target later by ActiveJob queue.
368
+ # This method calls NotificationApi#notify_later_to internally with self notifiable instance.
369
+ # @see NotificationApi#notify_later_to
370
+ #
371
+ # @param [Object] target Target to send notifications
372
+ # @param [Hash] options Options for notifications
373
+ # @option options [String] :key (notifiable.default_notification_key) Key of the notification
374
+ # @option options [Object] :group (nil) Group unit of the notifications
375
+ # @option options [ActiveSupport::Duration] :group_expiry_delay (nil) Expiry period of a notification group
376
+ # @option options [Object] :notifier (nil) Notifier of the notifications
377
+ # @option options [Hash] :parameters ({}) Additional parameters of the notifications
378
+ # @option options [Boolean] :send_email (true) Whether it sends notification email
379
+ # @option options [Boolean] :send_later (true) Whether it sends notification email asynchronously
380
+ # @option options [Boolean] :publish_optional_targets (true) Whether it publishes notification to optional targets
381
+ # @option options [Hash<String, Hash>] :optional_targets ({}) Options for optional targets, keys are optional target name (:amazon_sns or :slack etc) and values are options
382
+ # @return [Notification] Generated notification instance
383
+ def notify_later_to(target, options = {})
384
+ Notification.notify_later_to(target, self, options)
385
+ end
291
386
 
292
387
  # Returns default key of the notification.
293
- # This method is able to be overriden.
388
+ # This method is able to be overridden.
294
389
  # "#{to_resource_name}.default" is defined as default key.
295
390
  #
296
391
  # @return [String] Default key of the notification
@@ -299,7 +394,7 @@ module ActivityNotification
299
394
  end
300
395
 
301
396
  # Returns key of the notification for tracked notifiable creation.
302
- # This method is able to be overriden.
397
+ # This method is able to be overridden.
303
398
  # "#{to_resource_name}.create" is defined as default creation key.
304
399
  #
305
400
  # @return [String] Key of the notification for tracked notifiable creation
@@ -308,7 +403,7 @@ module ActivityNotification
308
403
  end
309
404
 
310
405
  # Returns key of the notification for tracked notifiable update.
311
- # This method is able to be overriden.
406
+ # This method is able to be overridden.
312
407
  # "#{to_resource_name}.update" is defined as default update key.
313
408
  #
314
409
  # @return [String] Key of the notification for tracked notifiable update
@@ -321,10 +416,10 @@ module ActivityNotification
321
416
  # Used to transform parameter value from configured field or defined method.
322
417
  # @api private
323
418
  #
324
- # @param [String] target_typed_method_name Method name overriden for the target type
419
+ # @param [String] target_typed_method_name Method name overridden for the target type
325
420
  # @param [Object] parameter_field Parameter Configured field in this model
326
421
  # @param [Object] default_value Default parameter value
327
- # @param [Array] args Arguments to pass to the method overriden or defined as parameter field
422
+ # @param [Array] args Arguments to pass to the method overridden or defined as parameter field
328
423
  # @return [Object] Resolved parameter value
329
424
  def resolve_parameter(target_typed_method_name, parameter_field, default_value, *args)
330
425
  if respond_to?(target_typed_method_name)
@@ -356,20 +451,12 @@ module ActivityNotification
356
451
  generated_notifications = generated_notifications_as_notifiable_for(target_type)
357
452
  case dependent
358
453
  when :restrict_with_exception
359
- ActivityNotification::Notification.raise_delete_restriction_error("generated_notifications_as_notifiable_for_#{target_type.to_s.pluralize.underscore}") unless generated_notifications.empty?
454
+ ActivityNotification::Notification.raise_delete_restriction_error("generated_notifications_as_notifiable_for_#{target_type.to_s.pluralize.underscore}") unless generated_notifications.to_a.empty?
360
455
  when :restrict_with_error
361
- unless generated_notifications.empty?
456
+ unless generated_notifications.to_a.empty?
362
457
  record = self.class.human_attribute_name("generated_notifications_as_notifiable_for_#{target_type.to_s.pluralize.underscore}").downcase
363
458
  self.errors.add(:base, :'restrict_dependent_destroy.has_many', record: record)
364
- # :skip-rails4:
365
- if Rails::VERSION::MAJOR >= 5
366
- throw(:abort)
367
- # :skip-rails4:
368
- # :skip-rails5:
369
- else
370
- false
371
- end
372
- # :skip-rails5:
459
+ throw(:abort)
373
460
  end
374
461
  when :destroy
375
462
  generated_notifications.each { |n| n.destroy }
@@ -395,7 +482,7 @@ module ActivityNotification
395
482
  # Casts to symbol of resources name.
396
483
  # @api private
397
484
  def cast_to_resources_sym(target_type)
398
- target_type.to_s.to_resources_name.to_sym
485
+ cast_to_resources_name(target_type).to_sym
399
486
  end
400
487
  end
401
488
  end
@@ -46,33 +46,48 @@ module ActivityNotification
46
46
  # Creates new subscription of the target.
47
47
  #
48
48
  # @param [Hash] subscription_params Parameters to create subscription record
49
+ # @raise [ActivityNotification::RecordInvalidError] Failed to save subscription due to model validation
49
50
  # @return [Subscription] Created subscription instance
50
51
  def create_subscription(subscription_params = {})
52
+ subscription = build_subscription(subscription_params)
53
+ raise RecordInvalidError, subscription.errors.full_messages.first unless subscription.save
54
+ subscription
55
+ end
56
+
57
+ # Builds new subscription of the target.
58
+ #
59
+ # @param [Hash] subscription_params Parameters to build subscription record
60
+ # @return [Subscription] Built subscription instance
61
+ def build_subscription(subscription_params = {})
51
62
  created_at = Time.current
52
63
  if subscription_params[:subscribing] == false && subscription_params[:subscribing_to_email].nil?
53
- subscription_params[:subscribing_to_email] = subscription_params[:subscribing]
64
+ subscription_params[:subscribing_to_email] = subscription_params[:subscribing]
65
+ elsif subscription_params[:subscribing_to_email].nil?
66
+ subscription_params[:subscribing_to_email] = ActivityNotification.config.subscribe_to_email_as_default
54
67
  end
55
- subscription = subscriptions.new(subscription_params)
68
+ subscription = Subscription.new(subscription_params)
69
+ subscription.assign_attributes(target: self)
56
70
  subscription.subscribing? ?
57
71
  subscription.assign_attributes(subscribing: true, subscribed_at: created_at) :
58
72
  subscription.assign_attributes(subscribing: false, unsubscribed_at: created_at)
59
73
  subscription.subscribing_to_email? ?
60
74
  subscription.assign_attributes(subscribing_to_email: true, subscribed_to_email_at: created_at) :
61
75
  subscription.assign_attributes(subscribing_to_email: false, unsubscribed_to_email_at: created_at)
62
- optional_targets = subscription.optional_targets
76
+ subscription.optional_targets = (subscription.optional_targets || {}).with_indifferent_access
77
+ optional_targets = {}.with_indifferent_access
63
78
  subscription.optional_target_names.each do |optional_target_name|
64
79
  optional_targets = subscription.subscribing_to_optional_target?(optional_target_name) ?
65
80
  optional_targets.merge(
66
81
  Subscription.to_optional_target_key(optional_target_name) => true,
67
- Subscription.to_optional_target_subscribed_at_key(optional_target_name) => created_at
82
+ Subscription.to_optional_target_subscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(created_at)
68
83
  ) :
69
84
  optional_targets.merge(
70
85
  Subscription.to_optional_target_key(optional_target_name) => false,
71
- Subscription.to_optional_target_unsubscribed_at_key(optional_target_name) => created_at
86
+ Subscription.to_optional_target_unsubscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(created_at)
72
87
  )
73
88
  end
74
89
  subscription.assign_attributes(optional_targets: optional_targets)
75
- subscription.save ? subscription : nil
90
+ subscription
76
91
  end
77
92
 
78
93
  # Gets configured subscription index of the target.
@@ -127,7 +142,7 @@ module ActivityNotification
127
142
  protected
128
143
 
129
144
  # Returns if the target subscribes to the notification.
130
- # This method can be overriden.
145
+ # This method can be overridden.
131
146
  # @api protected
132
147
  #
133
148
  # @param [String] key Key of the notification
@@ -138,26 +153,26 @@ module ActivityNotification
138
153
  end
139
154
 
140
155
  # Returns if the target subscribes to the notification email.
141
- # This method can be overriden.
156
+ # This method can be overridden.
142
157
  # @api protected
143
158
  #
144
159
  # @param [String] key Key of the notification
145
160
  # @param [Boolean] subscribe_as_default Default subscription value to use when the subscription record does not configured
146
161
  # @return [Boolean] If the target subscribes to the notification
147
- def _subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
162
+ def _subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_to_email_as_default)
148
163
  evaluate_subscription(subscriptions.where(key: key).first, :subscribing_to_email?, subscribe_as_default)
149
164
  end
150
165
  alias_method :_subscribes_to_email?, :_subscribes_to_notification_email?
151
166
 
152
167
  # Returns if the target subscribes to the specified optional target.
153
- # This method can be overriden.
168
+ # This method can be overridden.
154
169
  # @api protected
155
170
  #
156
171
  # @param [String] key Key of the notification
157
172
  # @param [String, Symbol] optional_target_name Class name of the optional target implementation (e.g. :amazon_sns, :slack)
158
173
  # @param [Boolean] subscribe_as_default Default subscription value to use when the subscription record does not configured
159
174
  # @return [Boolean] If the target subscribes to the specified optional target
160
- def _subscribes_to_optional_target?(key, optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
175
+ def _subscribes_to_optional_target?(key, optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_to_optional_targets_as_default)
161
176
  _subscribes_to_notification?(key, subscribe_as_default) &&
162
177
  evaluate_subscription(subscriptions.where(key: key).first, :subscribing_to_optional_target?, subscribe_as_default, optional_target_name, subscribe_as_default)
163
178
  end
@@ -172,8 +187,8 @@ module ActivityNotification
172
187
  # @param [Array] args Arguments of evaluating subscription method
173
188
  # @return [Boolean] If the target subscribes
174
189
  def evaluate_subscription(record, field, default, *args)
175
- default ? record.blank? || record.send(field, *args) : record.present? && record.send(field)
190
+ default ? record.blank? || record.send(field, *args) : record.present? && record.send(field, *args)
176
191
  end
177
192
 
178
193
  end
179
- end
194
+ end
@@ -0,0 +1,36 @@
1
+ module ActivityNotification
2
+ module Swagger::ErrorSchema #:nodoc:
3
+ extend ActiveSupport::Concern
4
+ include ::Swagger::Blocks
5
+
6
+ included do
7
+ swagger_component do
8
+ schema :Error do
9
+ key :required, [:gem, :error]
10
+ property :gem do
11
+ key :type, :string
12
+ key :description, "Name of gem generating this error"
13
+ key :default, "activity_notification"
14
+ key :example, "activity_notification"
15
+ end
16
+ property :error do
17
+ key :type, :object
18
+ key :description, "Error information"
19
+ property :code do
20
+ key :type, :integer
21
+ key :description, "Error code: default value is HTTP status code"
22
+ end
23
+ property :message do
24
+ key :type, :string
25
+ key :description, "Error message"
26
+ end
27
+ property :type do
28
+ key :type, :string
29
+ key :description, "Error type describing error message"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end