File: base_controller.rb

package info (click to toggle)
ruby-ahoy-matey 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 380 kB
  • sloc: ruby: 1,303; makefile: 3
file content (35 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Ahoy
  class BaseController < ApplicationController
    filters = _process_action_callbacks.map(&:filter) - Ahoy.preserve_callbacks
    skip_before_action(*filters, raise: false)
    skip_after_action(*filters, raise: false)
    skip_around_action(*filters, raise: false)

    before_action :verify_request_size
    before_action :renew_cookies

    if respond_to?(:protect_from_forgery)
      protect_from_forgery with: :null_session, if: -> { Ahoy.protect_from_forgery }
    end

    protected

    def ahoy
      @ahoy ||= Ahoy::Tracker.new(controller: self, api: true)
    end

    # set proper ttl if cookie generated from JavaScript
    # approach is not perfect, as user must reload the page
    # for new cookie settings to take effect
    def renew_cookies
      set_ahoy_cookies if params[:js] && !Ahoy.api_only
    end

    def verify_request_size
      if request.content_length > Ahoy.max_content_length
        logger.info "[ahoy] Payload too large"
        render plain: "Payload too large\n", status: 413
      end
    end
  end
end