• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

simukappu / activity_notification / 20600234446

30 Dec 2025 03:42PM UTC coverage: 99.749%. Remained the same
20600234446

push

travis-ci

simukappu
Bump version to v2.4.1

3575 of 3584 relevant lines covered (99.75%)

1533.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/app/controllers/activity_notification/subscriptions_controller.rb
1
module ActivityNotification
15✔
2
  # Controller to manage subscriptions.
3
  class SubscriptionsController < ActivityNotification.config.parent_controller.constantize
15✔
4
    # Include CommonController to select target and define common methods
5
    include CommonController
15✔
6
    before_action :set_subscription, except: [:index, :create, :find]
15✔
7
    before_action ->{ validate_param(:key) },                  only: [:find]
255✔
8
    before_action ->{ validate_param(:optional_target_name) }, only: [:subscribe_to_optional_target, :unsubscribe_to_optional_target]
1,110✔
9

10
    # Shows subscription index of the target.
11
    #
12
    # GET /:target_type/:target_id/subscriptions
13
    # @overload index(params)
14
    #   @param [Hash] params Request parameter options for subscription index
15
    #   @option params [String] :filter          (nil)     Filter option to load subscription index by their configuration status (Nothing as all, 'configured' or 'unconfigured')
16
    #   @option params [String] :limit           (nil)     Limit to query for subscriptions
17
    #   @option params [String] :reverse         ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
18
    #   @option params [String] :filtered_by_key (nil)     Key of the subscription for filter
19
    #   @return [Response] HTML view of subscription index
20
    def index
15✔
21
      set_index_options
945✔
22
      load_index if params[:reload].to_s.to_boolean(true)
945✔
23
    end
24

25
    # Creates new subscription.
26
    #
27
    # PUT /:target_type/:target_id/subscriptions
28
    # @overload create(params)
29
    #   @param [Hash] params Request parameters
30
    #   @option params [String] :subscription                              Subscription parameters
31
    #   @option params [String] :subscription[:key]                        Key of the subscription
32
    #   @option params [String] :subscription[:subscribing]          (nil) Whether the target will subscribe to the notification
33
    #   @option params [String] :subscription[:subscribing_to_email] (nil) Whether the target will subscribe to the notification email
34
    #   @option params [String] :filter          (nil)                     Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
35
    #   @option params [String] :limit           (nil)                     Limit to query for subscriptions
36
    #   @option params [String] :reverse         ('false')                 Whether subscription index and unconfigured notification keys will be ordered as earliest first
37
    #   @option params [String] :filtered_by_key (nil)                     Key of the subscription for filter
38
    #   @return [Response] JavaScript view for ajax request or redirects to back as default
39
    def create
15✔
40
      @subscription = @target.create_subscription(subscription_params)
570✔
41
      return_back_or_ajax
510✔
42
    end
43

44
    # Finds and shows a subscription from specified key.
45
    #
46
    # GET /:target_type/:target_id/subscriptions/find
47
    # @overload index(params)
48
    #   @param [Hash] params Request parameter options for subscription index
49
    #   @option params [required, String] :key (nil) Key of the subscription
50
    #   @return [Response] HTML view as default or JSON of subscription index with json format parameter
51
    def find
15✔
52
      @subscription = @target.find_subscription(params[:key])
240✔
53
      @subscription ? redirect_to_subscription_path : render_resource_not_found("Couldn't find subscription with this target and 'key'=#{params[:key]}")
240✔
54
    end
55

56
    # Shows a subscription.
57
    #
58
    # GET /:target_type/:target_id/subscriptions/:id
59
    # @overload show(params)
60
    #   @param [Hash] params Request parameters
61
    #   @return [Response] HTML view as default
62
    def show
15✔
63
      set_index_options
150✔
64
    end
65
  
66
    # Deletes a subscription.
67
    #
68
    # DELETE /:target_type/:target_id/subscriptions/:id
69
    # @overload destroy(params)
70
    #   @param [Hash] params Request parameters
71
    #   @option params [String] :filter          (nil)     Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
72
    #   @option params [String] :limit           (nil)     Limit to query for subscriptions
73
    #   @option params [String] :reverse         ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
74
    #   @option params [String] :filtered_by_key (nil)     Key of the subscription for filter
75
    #   @return [Response] JavaScript view for ajax request or redirects to back as default
76
    def destroy
15✔
77
      @subscription.destroy
360✔
78
      return_back_or_ajax
360✔
79
    end
80

81
    # Updates a subscription to subscribe to notifications.
82
    #
83
    # PUT /:target_type/:target_id/subscriptions/:id/subscribe
84
    # @overload open(params)
85
    #   @param [Hash] params Request parameters
86
    #   @option params [String] :with_email_subscription ('true')  Whether the subscriber also subscribes notification email
87
    #   @option params [String] :with_optional_targets   ('true')  Whether the subscriber also subscribes optional targets
88
    #   @option params [String] :filter                  (nil)     Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
89
    #   @option params [String] :limit                   (nil)     Limit to query for subscriptions
90
    #   @option params [String] :reverse                 ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
91
    #   @option params [String] :filtered_by_key         (nil)     Key of the subscription for filter
92
    #   @return [Response] JavaScript view for ajax request or redirects to back as default
93
    def subscribe
15✔
94
      @subscription.subscribe(with_email_subscription: params[:with_email_subscription].to_s.to_boolean(ActivityNotification.config.subscribe_to_email_as_default),
375✔
95
                              with_optional_targets:   params[:with_optional_targets].to_s.to_boolean(ActivityNotification.config.subscribe_to_optional_targets_as_default))
96
      return_back_or_ajax
375✔
97
    end
98

99
    # Updates a subscription to unsubscribe to the notifications.
100
    #
101
    # PUT /:target_type/:target_id/subscriptions/:id/unsubscribe
102
    # @overload open(params)
103
    #   @param [Hash] params Request parameters
104
    #   @option params [String] :filter          (nil)     Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
105
    #   @option params [String] :limit           (nil)     Limit to query for subscriptions
106
    #   @option params [String] :reverse         ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
107
    #   @option params [String] :filtered_by_key (nil)     Key of the subscription for filter
108
    #   @return [Response] JavaScript view for ajax request or redirects to back as default
109
    def unsubscribe
15✔
110
      @subscription.unsubscribe
375✔
111
      return_back_or_ajax
375✔
112
    end
113

114
    # Updates a subscription to subscribe to the notification email.
115
    #
116
    # PUT /:target_type/:target_id/subscriptions/:id/subscribe_email
117
    # @overload open(params)
118
    #   @param [Hash] params Request parameters
119
    #   @option params [String] :filter          (nil)     Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
120
    #   @option params [String] :limit           (nil)     Limit to query for subscriptions
121
    #   @option params [String] :reverse         ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
122
    #   @option params [String] :filtered_by_key (nil)     Key of the subscription for filter
123
    #   @return [Response] JavaScript view for ajax request or redirects to back as default
124
    def subscribe_to_email
15✔
125
      @subscription.subscribe_to_email
510✔
126
      return_back_or_ajax
510✔
127
    end
128

129
    # Updates a subscription to unsubscribe to the notification email.
130
    #
131
    # PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_email
132
    # @overload open(params)
133
    #   @param [Hash] params Request parameters
134
    #   @option params [String] :filter          (nil)     Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
135
    #   @option params [String] :limit           (nil)     Limit to query for subscriptions
136
    #   @option params [String] :reverse         ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
137
    #   @option params [String] :filtered_by_key (nil)     Key of the subscription for filter
138
    #   @return [Response] JavaScript view for ajax request or redirects to back as default
139
    def unsubscribe_to_email
15✔
140
      @subscription.unsubscribe_to_email
375✔
141
      return_back_or_ajax
375✔
142
    end
143

144
    # Updates a subscription to subscribe to the specified optional target.
145
    #
146
    # PUT /:target_type/:target_id/subscriptions/:id/subscribe_to_optional_target
147
    # @overload open(params)
148
    #   @param [Hash] params Request parameters
149
    #   @option params [required, String] :optional_target_name (nil)     Class name of the optional target implementation (e.g. 'amazon_sns', 'slack')
150
    #   @option params [String]           :filter               (nil)     Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
151
    #   @option params [String]           :limit                (nil)     Limit to query for subscriptions
152
    #   @option params [String]           :reverse              ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
153
    #   @option params [String]           :filtered_by_key      (nil)     Key of the subscription for filter
154
    #   @return [Response] JavaScript view for ajax request or redirects to back as default
155
    def subscribe_to_optional_target
15✔
156
      @subscription.subscribe_to_optional_target(params[:optional_target_name])
510✔
157
      return_back_or_ajax
510✔
158
    end
159

160
    # Updates a subscription to unsubscribe to the specified optional target.
161
    #
162
    # PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_to_optional_target
163
    # @overload open(params)
164
    #   @param [Hash] params Request parameters
165
    #   @option params [required, String] :optional_target_name (nil)     Class name of the optional target implementation (e.g. 'amazon_sns', 'slack')
166
    #   @option params [String]           :filter               (nil)     Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
167
    #   @option params [String]           :limit                (nil)     Limit to query for subscriptions
168
    #   @option params [String]           :reverse              ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
169
    #   @option params [String]           :filtered_by_key      (nil)     Key of the subscription for filter
170
    #   @return [Response] JavaScript view for ajax request or redirects to back as default
171
    def unsubscribe_to_optional_target
15✔
172
      @subscription.unsubscribe_to_optional_target(params[:optional_target_name])
375✔
173
      return_back_or_ajax
375✔
174
    end
175

176
    protected
15✔
177

178
      # Sets @subscription instance variable from request parameters.
179
      # @api protected
180
      # @return [Object] Subscription instance (Returns HTTP 403 when the target of subscription is different from specified target by request parameter)
181
      def set_subscription
15✔
182
        validate_target(@subscription = Subscription.with_target.find(params[:id]))
3,240✔
183
      end
184

185
      # Only allow a trusted parameter "white list" through.
186
      def subscription_params
15✔
187
        if params[:subscription].present?
570✔
188
          optional_target_keys = (params[:subscription][:optional_targets] || {}).keys.select { |key| key.to_s.start_with?("subscribing_to_") }
960✔
189
          optional_target_keys.each do |optional_target_key|
570✔
190
            boolean_value = params[:subscription][:optional_targets][optional_target_key].respond_to?(:to_boolean) ? params[:subscription][:optional_targets][optional_target_key].to_boolean : !!params[:subscription][:optional_targets][optional_target_key]
330✔
191
            params[:subscription][:optional_targets][optional_target_key] = boolean_value
330✔
192
          end
193
        end
194
        params.require(:subscription).permit(:key, :subscribing, :subscribing_to_email, optional_targets: optional_target_keys)
570✔
195
      end
196

197
      # Sets options to load subscription index from request parameters.
198
      # @api protected
199
      # @return [Hash] options to load subscription index
200
      def set_index_options
15✔
201
        limit          = params[:limit].to_i > 0 ? params[:limit].to_i : nil
3,765✔
202
        reverse        = params[:reverse].present? ?
3,765✔
203
                           params[:reverse].to_s.to_boolean(false) : nil
204
        @index_options = params.permit(:filter, :filtered_by_key, :routing_scope, :devise_default_routes)
3,765✔
205
                               .to_h.symbolize_keys.merge(limit: limit, reverse: reverse)
206
      end
207

208
      # Loads subscription index with request parameters.
209
      # @api protected
210
      # @return [Array] Array of subscription index
211
      def load_index
15✔
212
        case @index_options[:filter]
1,785✔
213
        when :configured, 'configured'
214
          @subscriptions = @target.subscription_index(@index_options.merge(with_target: true))
90✔
215
          @notification_keys = nil
90✔
216
        when :unconfigured, 'unconfigured'
217
          @subscriptions = nil
90✔
218
          @notification_keys = @target.notification_keys(@index_options.merge(filter: :unconfigured))
90✔
219
        else
220
          @subscriptions = @target.subscription_index(@index_options.merge(with_target: true))
1,605✔
221
          @notification_keys = @target.notification_keys(@index_options.merge(filter: :unconfigured))
1,605✔
222
        end
223
      end
224

225
      # Redirect to subscription path
226
      # @api protected
227
      def redirect_to_subscription_path
15✔
228
        redirect_to action: :show, id: @subscription
90✔
229
      end
230

231
      # Returns controller path.
232
      # This method is called from target_view_path method and can be overridden.
233
      # @api protected
234
      # @return [String] "activity_notification/subscriptions" as controller path
235
      def controller_path
15✔
236
        "activity_notification/subscriptions"
10,470✔
237
      end
238

239
  end
240
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc