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

simukappu / activity_notification / 17085190434

20 Aug 2025 12:33AM UTC coverage: 99.749%. Remained the same
17085190434

push

travis-ci

simukappu
Update docs to remove online demo on Heroku

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
/lib/activity_notification/controllers/common_controller.rb
1
module ActivityNotification
15✔
2
  # Module included in controllers to select target
3
  module CommonController
15✔
4
    extend ActiveSupport::Concern
15✔
5

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

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

17
    DEFAULT_VIEW_DIRECTORY = "default"
15✔
18
  
19
    protected
15✔
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
15✔
25
        if (target_type = params[:target_type]).present?
9,180✔
26
          target_class = target_type.to_model_class
9,090✔
27
          @target = params[:target_id].present? ?
9,090✔
28
            target_class.find_by!(id: params[:target_id]) :
126✔
29
            target_class.find_by!(id: params["#{target_type.to_resource_name[/([^\/]+)$/]}_id"])
3,510✔
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")
90✔
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)
15✔
40
        if @target.present? && belonging_model.target != @target
4,605✔
41
          render status: 403, json: error_response(code: 403, message: "Forbidden because of invalid parameter", type: "Wrong target is specified")
120✔
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
15✔
50
        raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
15✔
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
15✔
58
        raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
15✔
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
15✔
66
        raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
15✔
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
15✔
73
        target_type = @target.to_resources_name
8,970✔
74
        view_path = [controller_path, target_type].join('/')
8,970✔
75
        lookup_context.exists?(action_name, view_path) ?
8,970✔
76
          view_path :
77
          [controller_path, DEFAULT_VIEW_DIRECTORY].join('/')
8,970✔
78
      end
79

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

86
      # Returns error response as Hash
87
      # @api protected
88
      # @return [Hash] Error message
89
      def error_response(error_info = {})
15✔
90
        { gem: "activity_notification", error: error_info }
1,020✔
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)
15✔
97
        message_type = error.respond_to?(:message) ? error.message : error
240✔
98
        render status: 404, json: error_response(code: 404, message: "Resource not found", type: message_type)
240✔
99
      end
100

101
      # Render Invalid Parameter error with 400 status
102
      # @api protected
103
      # @return [void]
104
      def render_invalid_parameter(message)
15✔
105
        render status: 400, json: error_response(code: 400, message: "Invalid parameter", type: message)
240✔
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)
15✔
113
        render_invalid_parameter("Parameter is missing: #{param_name}") if params[param_name].blank?
1,545✔
114
      end
115

116
      # Render Invalid Parameter error with 400 status
117
      # @api protected
118
      # @return [void]
119
      def render_unprocessable_entity(message)
15✔
120
        render status: 422, json: error_response(code: 422, message: "Unprocessable entity", type: message)
150✔
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
15✔
127
        set_index_options
4,320✔
128
        respond_to do |format|
4,320✔
129
          if request.xhr?
4,320✔
130
            load_index if params[:reload].to_s.to_boolean(true)
1,440✔
131
            format.js
1,440✔
132
          else
133
            redirect_back(fallback_location: { action: :index }, **@index_options) and return
2,880✔
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