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

simukappu / activity_notification / 20661354035

02 Jan 2026 03:51PM UTC coverage: 99.759%. Remained the same
20661354035

push

travis-ci

simukappu
[WIP] Allow use with Rails 8.1

3727 of 3736 relevant lines covered (99.76%)

1706.32 hits per line

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

100.0
/lib/activity_notification/controllers/common_controller.rb
1
module ActivityNotification
17✔
2
  # Module included in controllers to select target
3
  module CommonController
17✔
4
    extend ActiveSupport::Concern
17✔
5

6
    included do
17✔
7
      # Include StoreController to allow ActivityNotification access to controller instance
8
      include StoreController
51✔
9
      # Include PolymorphicHelpers to resolve string extentions
10
      include PolymorphicHelpers
51✔
11

12
      prepend_before_action :set_target
51✔
13
      before_action :set_view_prefixes
51✔
14
      rescue_from ActivityNotification::RecordInvalidError, with: ->(e){ render_unprocessable_entity(e.message) }
221✔
15
    end
16

17
    DEFAULT_VIEW_DIRECTORY = "default"
17✔
18
  
19
    protected
17✔
20

21
      # Sets @target instance variable from request parameters.
22
      # @api protected
23
      # @return [Object] Target instance (Returns HTTP 400 when request parameters are invalid)
24
      def set_target
17✔
25
        if (target_type = params[:target_type]).present?
10,404✔
26
          target_class = target_type.to_model_class
10,302✔
27
          @target = params[:target_id].present? ?
10,302✔
28
            target_class.find_by!(id: params[:target_id]) :
168✔
29
            target_class.find_by!(id: params["#{target_type.to_resource_name[/([^\/]+)$/]}_id"])
4,680✔
30
        else
31
          render status: 400, json: error_response(code: 400, message: "Invalid parameter", type: "Parameter is missing or the value is empty: target_type")
102✔
32
        end
33
      end
34

35
      # Validate target with belonging model (e.g. Notification and Subscription)
36
      # @api protected
37
      # @param [Object] belonging_model belonging model (e.g. Notification and Subscription)
38
      # @return Nil or render HTTP 403 status
39
      def validate_target(belonging_model)
17✔
40
        if @target.present? && belonging_model.target != @target
5,219✔
41
          render status: 403, json: error_response(code: 403, message: "Forbidden because of invalid parameter", type: "Wrong target is specified")
136✔
42
        end
43
      end
44

45
      # Sets options to load resource index from request parameters.
46
      # This method is to be overridden.
47
      # @api protected
48
      # @return [Hash] options to load resource index
49
      def set_index_options
17✔
50
        raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
17✔
51
      end
52

53
      # Loads resource index with request parameters.
54
      # This method is to be overridden.
55
      # @api protected
56
      # @return [Array] Array of resource index
57
      def load_index
17✔
58
        raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
17✔
59
      end
60

61
      # Returns controller path.
62
      # This method is called from target_view_path method and can be overridden.
63
      # @api protected
64
      # @return [String] "activity_notification" as controller path
65
      def controller_path
17✔
66
        raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
17✔
67
      end
68

69
      # Returns path of the target view templates.
70
      # Do not make this method public unless Rendarable module calls controller's target_view_path method to render resources.
71
      # @api protected
72
      def target_view_path
17✔
73
        target_type = @target.to_resources_name
10,166✔
74
        view_path = [controller_path, target_type].join('/')
10,166✔
75
        lookup_context.exists?(action_name, view_path) ?
10,166✔
76
          view_path :
77
          [controller_path, DEFAULT_VIEW_DIRECTORY].join('/')
10,166✔
78
      end
79

80
      # Sets view prefixes for target view path.
81
      # @api protected
82
      def set_view_prefixes
17✔
83
        lookup_context.prefixes.prepend(target_view_path)
10,166✔
84
      end
85

86
      # Returns error response as Hash
87
      # @api protected
88
      # @return [Hash] Error message
89
      def error_response(error_info = {})
17✔
90
        { gem: "activity_notification", error: error_info }
1,156✔
91
      end
92

93
      # Render Resource Not Found error with 404 status
94
      # @api protected
95
      # @return [void]
96
      def render_resource_not_found(error = nil)
17✔
97
        message_type = error.respond_to?(:message) ? error.message : error
272✔
98
        render status: 404, json: error_response(code: 404, message: "Resource not found", type: message_type)
272✔
99
      end
100

101
      # Render Invalid Parameter error with 400 status
102
      # @api protected
103
      # @return [void]
104
      def render_invalid_parameter(message)
17✔
105
        render status: 400, json: error_response(code: 400, message: "Invalid parameter", type: message)
272✔
106
      end
107

108
      # Validate param and return HTTP 400 unless it presents.
109
      # @api protected
110
      # @param [String, Symbol] param_name Parameter name to validate
111
      # @return [void]
112
      def validate_param(param_name)
17✔
113
        render_invalid_parameter("Parameter is missing: #{param_name}") if params[param_name].blank?
1,751✔
114
      end
115

116
      # Render Invalid Parameter error with 400 status
117
      # @api protected
118
      # @return [void]
119
      def render_unprocessable_entity(message)
17✔
120
        render status: 422, json: error_response(code: 422, message: "Unprocessable entity", type: message)
170✔
121
      end
122

123
      # Returns JavaScript view for ajax request or redirects to back as default.
124
      # @api protected
125
      # @return [Response] JavaScript view for ajax request or redirects to back as default
126
      def return_back_or_ajax
17✔
127
        set_index_options
4,896✔
128
        respond_to do |format|
4,896✔
129
          if request.xhr?
4,896✔
130
            load_index if params[:reload].to_s.to_boolean(true)
1,632✔
131
            format.js
1,632✔
132
          else
133
            redirect_back(fallback_location: { action: :index }, **@index_options) and return
3,264✔
134
          end
135
        end
136
      end
137
  end
138
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