module Strongbolt::BoltedController::ClassMethods

Public Instance Methods

actions_mapping() click to toggle source

Returns the actions mapping of this controller

# File lib/strongbolt/bolted_controller.rb, line 120
def actions_mapping
  # Defaults to a duplicate of the standard mapping
  @actions_mapping ||= ACTIONS_MAPPING.dup
end
model_for_authorization() click to toggle source

Returns the model used for authorization, using controller name if not defined

# File lib/strongbolt/bolted_controller.rb, line 35
def model_for_authorization
  if @model_for_authorization.present?
    @model_for_authorization
  else
    # We cannot just do controller_name.classify as it doesn't keep the modules
    # We'll also check demoduling one module after the other for case when
    # the controller and/or the model have different modules
    full_name = name.sub('Controller', '').classify
    # Split by ::
    splits = full_name.split('::')
    # While we still have modules to test
    while splits.size >= 1
      begin
        return constantize_model splits.join('::')
      rescue Strongbolt::ModelNotFound
      ensure
        # Removes first element
        splits.shift
      end
    end
    raise Strongbolt::ModelNotFound, "Model for controller #{controller_name} wasn't found"
  end
end
model_for_authorization=(model) click to toggle source

Allows defining a specific model for this controller, if it cannot be infer from the controller name

# File lib/strongbolt/bolted_controller.rb, line 21
def model_for_authorization=(model)
  @model_for_authorization = case model
                             when Class then model
                             when String then constantize_model(model)
                             when nil then nil
                             else
                               raise ArgumentError, 'Model for authorization must be a Class or the name of the Class'
                             end
end
render(*args) click to toggle source

It needs to be created afterward, No idea why

# File lib/strongbolt/bolted_controller.rb, line 100
def render(*args)
  if render_without_authorization?
    Strongbolt.without_authorization { _render(*args) }
  else
    _render(*args)
  end
end
render_with_authorization() click to toggle source

Render with authorization

# File lib/strongbolt/bolted_controller.rb, line 113
def render_with_authorization
  self.actions_without_authorization = nil
end
render_without_authorization(*actions) click to toggle source

Render without authorization, for better performance

# File lib/strongbolt/bolted_controller.rb, line 92
def render_without_authorization(*actions)
  self.actions_without_authorization = [*actions]

  class_eval do
    #
    # It needs to be created afterward,
    # No idea why
    #
    def render(*args)
      if render_without_authorization?
        Strongbolt.without_authorization { _render(*args) }
      else
        _render(*args)
      end
    end
  end
end
skip_all_authorization(opts = {}) click to toggle source

Skip all authorization checking for the controller, or a subset of actions

# File lib/strongbolt/bolted_controller.rb, line 73
def skip_all_authorization(opts = {})
  skip_controller_authorization opts
  around_action :disable_authorization, opts
end
skip_controller_authorization(opts = {}) click to toggle source

Skips controller authorization check for this controller No argument given will skip for all actions, and can be passed only: [] or except: []

# File lib/strongbolt/bolted_controller.rb, line 64
def skip_controller_authorization(opts = {})
  opts = { raise: false }.merge(opts) if Rails::VERSION::MAJOR >= 5
  skip_before_action :check_authorization, opts
end

Private Instance Methods

constantize_model(name) click to toggle source

Try to constantize a class

# File lib/strongbolt/bolted_controller.rb, line 130
def constantize_model(name)
  name.constantize
rescue NameError
  raise Strongbolt::ModelNotFound, "Model for controller #{controller_name} wasn't found"
end