Module: Cutoff::Rails::Controller

Defined in:
lib/cutoff/rails/controller.rb

Overview

Rails controller integration

Instance Method Summary collapse

Instance Method Details

#cutoff(seconds, options = {}) ⇒ Object

Set a cutoff for the controller

Can be called multiple times with different options to configure cutoffs for various conditions. If multiple conditions match a given controller, the last applied cutoff "wins".

Examples:

class ApplicationController
  # Apply a global maximum
  cutoff 30
end

class UsersController < ApplicationController
  # Override the base time limit
  cutoff 5.0
  cutoff 3.0, only: :show
  cutoff 7, if: :signed_in
end

Parameters:

  • seconds (Float, Integer)

    The allowed seconds for a controller action

  • options (Hash) (defaults to: {})

    Options to pass to around_action. For example, pass :only, :except, :if, to limit the scope of the cutoff.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cutoff/rails/controller.rb', line 33

def cutoff(seconds, options = {})
  prepend_around_action(options) do |_controller, action|
    next action.call if @cutoff_wrapped

    begin
      @cutoff_wrapped = true
      Cutoff.wrap(seconds, &action)
    ensure
      @cutoff_wrapped = false
    end
  end
end