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
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
 
28
28
  s.add_development_dependency 'sqlite3', '~> 1.3.12'
29
29
  s.add_development_dependency 'rspec-rails', '~> 3.5.1'
30
- s.add_development_dependency 'factory_girl_rails', '~> 4.7.0'
30
+ s.add_development_dependency 'factory_girl_rails', '~> 4.8.0'
31
31
  s.add_development_dependency 'simplecov', '~> 0.12.0'
32
32
  s.add_development_dependency 'yard', '~> 0.9.5'
33
33
  s.add_development_dependency 'yard-activesupport-concern', '~> 0.0.1'
@@ -1,19 +1,13 @@
1
1
  module ActivityNotification
2
2
  # Controller to manage notifications.
3
3
  class NotificationsController < ActivityNotification.config.parent_controller.constantize
4
- # Include StoreController to allow ActivityNotification access to controller instance
5
- include ActivityNotification::StoreController
6
- # Include PolymorphicHelpers to resolve string extentions
7
- include ActivityNotification::PolymorphicHelpers
8
- prepend_before_action :set_target
4
+ # Include CommonController to select target and define common methods
5
+ include CommonController
9
6
  before_action :set_notification, except: [:index, :open_all]
10
- before_action :set_view_prefixes, except: [:move]
11
-
12
- DEFAULT_VIEW_DIRECTORY = "default"
13
-
7
+
14
8
  # Shows notification index of the target.
15
9
  #
16
- # GET /:target_type/:target_id/notifcations
10
+ # GET /:target_type/:target_id/notifications
17
11
  # @overload index(params)
18
12
  # @param [Hash] params Request parameter options for notification index
19
13
  # @option params [String] :filter (nil) Filter option to load notification index (Nothing as auto, 'opened' or 'unopened')
@@ -29,7 +23,7 @@ module ActivityNotification
29
23
  # @return [Responce] HTML view as default or JSON of notification index with json format parameter
30
24
  def index
31
25
  set_index_options
32
- @notifications = load_notification_index(@index_options) if params[:reload].to_s.to_boolean(true)
26
+ load_index if params[:reload].to_s.to_boolean(true)
33
27
  respond_to do |format|
34
28
  format.html # index.html.erb
35
29
  format.json { render json: @notifications.to_json(include: [:target, :notifiable, :group]) }
@@ -38,7 +32,7 @@ module ActivityNotification
38
32
 
39
33
  # Opens all notifications of the target.
40
34
  #
41
- # POST /:target_type/:target_id/notifcations/open_all
35
+ # POST /:target_type/:target_id/notifications/open_all
42
36
  # @overload open_all(params)
43
37
  # @param [Hash] params Request parameters
44
38
  # @option params [String] :filter (nil) Filter option to load notification index (Nothing as auto, 'opened' or 'unopened')
@@ -58,7 +52,7 @@ module ActivityNotification
58
52
 
59
53
  # Shows a notification.
60
54
  #
61
- # GET /:target_type/:target_id/notifcations/:id
55
+ # GET /:target_type/:target_id/notifications/:id
62
56
  # @overload show(params)
63
57
  # @param [Hash] params Request parameters
64
58
  # @return [Responce] HTML view as default
@@ -67,7 +61,7 @@ module ActivityNotification
67
61
 
68
62
  # Deletes a notification.
69
63
  #
70
- # DELETE /:target_type/:target_id/notifcations/:id
64
+ # DELETE /:target_type/:target_id/notifications/:id
71
65
  #
72
66
  # @overload destroy(params)
73
67
  # @param [Hash] params Request parameters
@@ -84,7 +78,7 @@ module ActivityNotification
84
78
 
85
79
  # Opens a notification.
86
80
  #
87
- # POST /:target_type/:target_id/notifcations/:id/open
81
+ # POST /:target_type/:target_id/notifications/:id/open
88
82
  # @overload open(params)
89
83
  # @param [Hash] params Request parameters
90
84
  # @option params [String] :move ('false') Whether redirects to notifiable_path after the notification is opened
@@ -104,7 +98,7 @@ module ActivityNotification
104
98
 
105
99
  # Moves to notifiable_path of the notification.
106
100
  #
107
- # GET /:target_type/:target_id/notifcations/:id/move
101
+ # GET /:target_type/:target_id/notifications/:id/move
108
102
  # @overload open(params)
109
103
  # @param [Hash] params Request parameters
110
104
  # @option params [String] :open ('false') Whether the notification will be opened
@@ -117,48 +111,19 @@ module ActivityNotification
117
111
  redirect_to @notification.notifiable_path
118
112
  end
119
113
 
120
- # Returns controller path.
121
- # This method has no action routing and is called from target_view_path method.
122
- # This method can be overriden.
123
- # @return [String] "activity_notification/notifications" as controller path
124
- def controller_path
125
- "activity_notification/notifications"
126
- end
127
-
128
114
  # Returns path of the target view templates.
129
115
  # This method has no action routing and needs to be public since it is called from view helper.
130
116
  def target_view_path
131
- target_type = @target.to_resources_name
132
- view_path = [controller_path, target_type].join('/')
133
- lookup_context.exists?(action_name, view_path) ?
134
- view_path :
135
- [controller_path, DEFAULT_VIEW_DIRECTORY].join('/')
117
+ super
136
118
  end
137
119
 
138
120
  protected
139
121
 
140
- # Sets @target instance variable from request parameters.
141
- # @api protected
142
- # @return [Object] Target instance (Returns HTTP 400 when request parameters are not enough)
143
- def set_target
144
- if (target_type = params[:target_type]).present?
145
- target_class = target_type.to_model_class
146
- @target = params[:target_id].present? ?
147
- target_class.find_by_id!(params[:target_id]) :
148
- target_class.find_by_id!(params["#{target_type.to_resource_name}_id"])
149
- else
150
- render plain: "400 Bad Request: Missing parameter", status: 400
151
- end
152
- end
153
-
154
122
  # Sets @notification instance variable from request parameters.
155
123
  # @api protected
156
124
  # @return [Object] Notification instance (Returns HTTP 403 when the target of notification is different from specified target by request parameter)
157
125
  def set_notification
158
- @notification = Notification.find_by_id!(params[:id])
159
- if @target.present? and @notification.target != @target
160
- render plain: "403 Forbidden: Wrong target", status: 403
161
- end
126
+ validate_target(@notification = Notification.includes(:target).find_by_id!(params[:id]))
162
127
  end
163
128
 
164
129
  # Sets options to load notification index from request parameters.
@@ -170,71 +135,32 @@ module ActivityNotification
170
135
  params[:reverse].to_s.to_boolean(false) : nil
171
136
  with_group_members = params[:with_group_members].present? || params[:without_grouping].present? ?
172
137
  params[:with_group_members].to_s.to_boolean(false) || params[:without_grouping].to_s.to_boolean(false) : nil
173
- @index_options = params.permit(:filter, :filtered_by_type, :filtered_by_group_type, :filtered_by_group_id, :filtered_by_key)
174
- .to_h.symbolize_keys
175
- .merge(limit: limit, reverse: reverse, with_group_members: with_group_members)
138
+ @index_options = params.permit(:filter, :filtered_by_type, :filtered_by_group_type, :filtered_by_group_id, :filtered_by_key)
139
+ .to_h.symbolize_keys
140
+ .merge(limit: limit, reverse: reverse, with_group_members: with_group_members)
176
141
  end
177
142
 
178
143
  # Loads notification index with request parameters.
179
144
  # @api protected
180
- # @param [Hash] params Request parameter options for notification index
181
- # @option params [String] :filter (nil) Filter option to load notification index (Nothing as auto, 'opened' or 'unopened')
182
- # @option params [Integer] :limit (nil) Limit to query for notifications
183
- # @option params [Boolean] :reverse (false) If notification index will be ordered as earliest first
184
- # @option params [Boolean] :without_grouping (false) If notification index will include group members
185
- # @option params [String] :filtered_by_type (nil) Notifiable type for filter
186
- # @option params [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
187
- # @option params [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
188
- # @option params [String] :filtered_by_key (nil) Key of the notification for filter
189
145
  # @return [Array] Array of notification index
190
- def load_notification_index(options = {})
191
- case options[:filter]
192
- when 'opened'
193
- @target.opened_notification_index_with_attributes(options)
194
- when 'unopened'
195
- @target.unopened_notification_index_with_attributes(options)
196
- else
197
- @target.notification_index_with_attributes(options)
198
- end
146
+ def load_index
147
+ @notifications =
148
+ case @index_options[:filter]
149
+ when :opened, 'opened'
150
+ @target.opened_notification_index_with_attributes(@index_options)
151
+ when :unopened, 'unopened'
152
+ @target.unopened_notification_index_with_attributes(@index_options)
153
+ else
154
+ @target.notification_index_with_attributes(@index_options)
155
+ end
199
156
  end
200
157
 
201
- # Sets view prefixes for target view path.
158
+ # Returns controller path.
159
+ # This method is called from target_view_path method and can be overriden.
202
160
  # @api protected
203
- def set_view_prefixes
204
- lookup_context.prefixes.prepend(target_view_path)
205
- end
206
-
207
- # Returns JavaScript view for ajax request or redirects to back as default.
208
- # @api protected
209
- # @option params [String] :filter (nil) Filter option to load notification index (Nothing as auto, 'opened' or 'unopened')
210
- # @option params [Integer] :limit (nil) Limit to query for notifications
211
- # @option params [Boolean] :reverse (false) If notification index will be ordered as earliest first
212
- # @option params [Boolean] :without_grouping (false) If notification index will include group members
213
- # @option params [String] :filtered_by_type (nil) Notifiable type for filter
214
- # @option params [String] :filtered_by_group_type (nil) Group type for filter, valid with :filtered_by_group_id
215
- # @option params [String] :filtered_by_group_id (nil) Group instance id for filter, valid with :filtered_by_group_type
216
- # @option params [String] :filtered_by_key (nil) Key of the notification for filter
217
- # @return [Responce] JavaScript view for ajax request or redirects to back as default
218
- def return_back_or_ajax
219
- set_index_options
220
- if params[:reload].to_s.to_boolean(true)
221
- @notifications = load_notification_index(@index_options)
222
- end
223
- respond_to do |format|
224
- if request.xhr?
225
- format.js
226
- # :skip-rails4:
227
- elsif Rails::VERSION::MAJOR >= 5
228
- redirect_back fallback_location: { action: :index }, **@index_options and return
229
- # :skip-rails4:
230
- # :skip-rails5:
231
- elsif request.referer
232
- redirect_to :back, **@index_options and return
233
- else
234
- redirect_to action: :index, **@index_options and return
235
- end
236
- # :skip-rails5:
237
- end
161
+ # @return [String] "activity_notification/notifications" as controller path
162
+ def controller_path
163
+ "activity_notification/notifications"
238
164
  end
239
165
 
240
166
  end
@@ -1,38 +1,6 @@
1
1
  module ActivityNotification
2
2
  # Controller to manage notifications with Devise authentication.
3
3
  class NotificationsWithDeviseController < NotificationsController
4
- prepend_before_action :authenticate_devise_resource!
5
- before_action :authenticate_target!
6
-
7
- protected
8
-
9
- # Authenticate devise resource by Devise (e.g. calling authenticate_user! method).
10
- # @api protected
11
- # @todo Needs to call authenticate method by more secure way
12
- # @return [Responce] Redirects for unsigned in target by Devise, returns HTTP 403 without neccesary target method or returns 400 when request parameters are not enough
13
- def authenticate_devise_resource!
14
- if params[:devise_type].present?
15
- authenticate_method_name = "authenticate_#{params[:devise_type].to_resource_name}!"
16
- if respond_to?(authenticate_method_name)
17
- send(authenticate_method_name)
18
- else
19
- render plain: "403 Forbidden: Unauthenticated", status: 403
20
- end
21
- else
22
- render plain: "400 Bad Request: Missing parameter", status: 400
23
- end
24
- end
25
-
26
- # Authenticate the target of requested notification with authenticated devise resource.
27
- # @api protected
28
- # @todo Needs to call authenticate method by more secure way
29
- # @return [Responce] Returns HTTP 403 for unauthorized target
30
- def authenticate_target!
31
- current_resource_method_name = "current_#{params[:devise_type].to_resource_name}"
32
- unless @target.authenticated_with_devise?(send(current_resource_method_name))
33
- render plain: "403 Forbidden: Unauthorized target", status: 403
34
- end
35
- end
36
-
4
+ include DeviceAuthenticationController
37
5
  end
38
6
  end
@@ -0,0 +1,184 @@
1
+ module ActivityNotification
2
+ # Controller to manage subscriptions.
3
+ class SubscriptionsController < ActivityNotification.config.parent_controller.constantize
4
+ # Include CommonController to select target and define common methods
5
+ include CommonController
6
+ before_action :set_subscription, except: [:index, :create]
7
+
8
+ # Shows subscription index of the target.
9
+ #
10
+ # GET /:target_type/:target_id/subscriptions
11
+ # @overload index(params)
12
+ # @param [Hash] params Request parameter options for subscription index
13
+ # @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
14
+ # @option params [String] :limit (nil) Limit to query for subscriptions
15
+ # @option params [String] :reverse ('false') If subscription index and unconfigured notification keys will be ordered as earliest first
16
+ # @option params [String] :filtered_by_key (nil) Key of the subscription for filter
17
+ # @return [Responce] HTML view as default or JSON of subscription index with json format parameter
18
+ def index
19
+ set_index_options
20
+ load_index if params[:reload].to_s.to_boolean(true)
21
+ respond_to do |format|
22
+ format.html # index.html.erb
23
+ format.json { render json: { subscriptions: @subscriptions, unconfigured_notification_keys: @notification_keys } }
24
+ end
25
+ end
26
+
27
+ # Creates a subscription.
28
+ #
29
+ # POST /:target_type/:target_id/subscriptions
30
+ #
31
+ # @overload create(params)
32
+ # @param [Hash] params Request parameters
33
+ # @option params [String] :subscription Subscription parameters
34
+ # @option params [String] :subscription[:key] Key of the subscription
35
+ # @option params [String] :subscription[:subscribing] (nil) If the target will subscribe to the notification
36
+ # @option params [String] :subscription[:subscribing_to_email] (nil) If the target will subscribe to the notification email
37
+ # @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
38
+ # @option params [String] :limit (nil) Limit to query for subscriptions
39
+ # @option params [String] :reverse ('false') If subscription index and unconfigured notification keys will be ordered as earliest first
40
+ # @option params [String] :filtered_by_key (nil) Key of the subscription for filter
41
+ # @return [Responce] JavaScript view for ajax request or redirects to back as default
42
+ def create
43
+ @target.create_subscription(subscription_params)
44
+ return_back_or_ajax
45
+ end
46
+
47
+ # Shows a subscription.
48
+ #
49
+ # GET /:target_type/:target_id/subscriptions/:id
50
+ # @overload show(params)
51
+ # @param [Hash] params Request parameters
52
+ # @return [Responce] HTML view as default
53
+ def show
54
+ end
55
+
56
+ # Deletes a subscription.
57
+ #
58
+ # DELETE /:target_type/:target_id/subscriptions/:id
59
+ #
60
+ # @overload destroy(params)
61
+ # @param [Hash] params Request parameters
62
+ # @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
63
+ # @option params [String] :limit (nil) Limit to query for subscriptions
64
+ # @option params [String] :reverse ('false') If subscription index and unconfigured notification keys will be ordered as earliest first
65
+ # @option params [String] :filtered_by_key (nil) Key of the subscription for filter
66
+ # @return [Responce] JavaScript view for ajax request or redirects to back as default
67
+ def destroy
68
+ @subscription.destroy
69
+ return_back_or_ajax
70
+ end
71
+
72
+ # Subscribes to the notification.
73
+ #
74
+ # POST /:target_type/:target_id/subscriptions/:id/subscribe
75
+ # @overload open(params)
76
+ # @param [Hash] params Request parameters
77
+ # @option params [String] :with_email_subscription ('true') If the subscriber also subscribes notification email
78
+ # @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
79
+ # @option params [String] :limit (nil) Limit to query for subscriptions
80
+ # @option params [String] :reverse ('false') If subscription index and unconfigured notification keys will be ordered as earliest first
81
+ # @option params [String] :filtered_by_key (nil) Key of the subscription for filter
82
+ # @return [Responce] JavaScript view for ajax request or redirects to back as default
83
+ def subscribe
84
+ @subscription.subscribe(with_email_subscription: params[:with_email_subscription].to_s.to_boolean(true))
85
+ return_back_or_ajax
86
+ end
87
+
88
+ # Unsubscribes to the notification.
89
+ #
90
+ # POST /:target_type/:target_id/subscriptions/:id/unsubscribe
91
+ # @overload open(params)
92
+ # @param [Hash] params Request parameters
93
+ # @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
94
+ # @option params [String] :limit (nil) Limit to query for subscriptions
95
+ # @option params [String] :reverse ('false') If subscription index and unconfigured notification keys will be ordered as earliest first
96
+ # @option params [String] :filtered_by_key (nil) Key of the subscription for filter
97
+ # @return [Responce] JavaScript view for ajax request or redirects to back as default
98
+ def unsubscribe
99
+ @subscription.unsubscribe
100
+ return_back_or_ajax
101
+ end
102
+
103
+ # Subscribes to the notification email.
104
+ #
105
+ # POST /:target_type/:target_id/subscriptions/:id/subscribe_email
106
+ # @overload open(params)
107
+ # @param [Hash] params Request parameters
108
+ # @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
109
+ # @option params [String] :limit (nil) Limit to query for subscriptions
110
+ # @option params [String] :reverse ('false') If subscription index and unconfigured notification keys will be ordered as earliest first
111
+ # @option params [String] :filtered_by_key (nil) Key of the subscription for filter
112
+ # @return [Responce] JavaScript view for ajax request or redirects to back as default
113
+ def subscribe_to_email
114
+ @subscription.subscribe_to_email
115
+ return_back_or_ajax
116
+ end
117
+
118
+ # Unsubscribes to the notification email.
119
+ #
120
+ # POST /:target_type/:target_id/subscriptions/:id/unsubscribe_email
121
+ # @overload open(params)
122
+ # @param [Hash] params Request parameters
123
+ # @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
124
+ # @option params [String] :limit (nil) Limit to query for subscriptions
125
+ # @option params [String] :reverse ('false') If subscription index and unconfigured notification keys will be ordered as earliest first
126
+ # @option params [String] :filtered_by_key (nil) Key of the subscription for filter
127
+ # @return [Responce] JavaScript view for ajax request or redirects to back as default
128
+ def unsubscribe_to_email
129
+ @subscription.unsubscribe_to_email
130
+ return_back_or_ajax
131
+ end
132
+
133
+ protected
134
+
135
+ # Sets @subscription instance variable from request parameters.
136
+ # @api protected
137
+ # @return [Object] Subscription instance (Returns HTTP 403 when the target of subscription is different from specified target by request parameter)
138
+ def set_subscription
139
+ validate_target(@subscription = Subscription.includes(:target).find_by_id!(params[:id]))
140
+ end
141
+
142
+ # Only allow a trusted parameter "white list" through.
143
+ def subscription_params
144
+ params.require(:subscription).permit(:key, :subscribing, :subscribing_to_email)
145
+ end
146
+
147
+ # Sets options to load subscription index from request parameters.
148
+ # @api protected
149
+ # @return [Hash] options to load subscription index
150
+ def set_index_options
151
+ limit = params[:limit].to_i > 0 ? params[:limit].to_i : nil
152
+ reverse = params[:reverse].present? ?
153
+ params[:reverse].to_s.to_boolean(false) : nil
154
+ @index_options = params.permit(:filter, :filtered_by_key)
155
+ .to_h.symbolize_keys.merge(limit: limit, reverse: reverse)
156
+ end
157
+
158
+ # Loads subscription index with request parameters.
159
+ # @api protected
160
+ # @return [Array] Array of subscription index
161
+ def load_index
162
+ case @index_options[:filter]
163
+ when :configured, 'configured'
164
+ @subscriptions = @target.subscription_index(@index_options.merge(with_target: true))
165
+ @notification_keys = nil
166
+ when :unconfigured, 'unconfigured'
167
+ @subscriptions = nil
168
+ @notification_keys = @target.notification_keys(@index_options.merge(filter: :unconfigured))
169
+ else
170
+ @subscriptions = @target.subscription_index(@index_options.merge(with_target: true))
171
+ @notification_keys = @target.notification_keys(@index_options.merge(filter: :unconfigured))
172
+ end
173
+ end
174
+
175
+ # Returns controller path.
176
+ # This method is called from target_view_path method and can be overriden.
177
+ # @api protected
178
+ # @return [String] "activity_notification/subscriptions" as controller path
179
+ def controller_path
180
+ "activity_notification/subscriptions"
181
+ end
182
+
183
+ end
184
+ end