module Scorpion::Rails::Controller

Adds a scorpion nest to support injection into rails controllers.

Constants

ENV_KEY

Public Class Methods

included( base ) click to toggle source

@overload scorpion

@return [Scorpion] the current scorpion

@overload scorpion( scope )

Stings the given `scope` with the current scorpion.
@param [ActiveRecord::Relation,#with_scorpion] an ActiveRecord relation,
  scope or model class.
@return [ActiveRecord::Relation] scorpion scoped relation.
Calls superclass method
# File lib/scorpion/rails/controller.rb, line 27
def self.included( base )
  # Setup dependency injection
  base.send :include, Scorpion::Object
  base.send :include, Scorpion::Rails::Nest

  base.around_action :with_scorpion

  base.class_eval do
    if respond_to?( :helper_method )
      helper_method :scorpion
    end

    # Defined here to override the #scorpion method provided by Scorpion::Object.
    def scorpion( scope = nil )
      if scope
        super
      else
        ensure_scorpion( request.env[ENV_KEY] )
      end
    end
  end

  super
end

Public Instance Methods

scorpion( scope = nil ) click to toggle source

Defined here to override the scorpion method provided by Scorpion::Object.

Calls superclass method
# File lib/scorpion/rails/controller.rb, line 40
def scorpion( scope = nil )
  if scope
    super
  else
    ensure_scorpion( request.env[ENV_KEY] )
  end
end

Private Instance Methods

assign_scorpion( scorpion ) click to toggle source
# File lib/scorpion/rails/controller.rb, line 72
def assign_scorpion( scorpion )
  request.env[ENV_KEY] = scorpion
end
fetch( *args, &block ) click to toggle source

Fetch an object from the controller's {#scorpion}. @see Scorpion#fetch

# File lib/scorpion/rails/controller.rb, line 13
def fetch( *args, &block )
  scorpion.fetch *args, &block
end
free_scorpion() click to toggle source
# File lib/scorpion/rails/controller.rb, line 76
def free_scorpion
  return unless conceived_scorpion?

  scorpion.try( :destroy )
  request.env.delete ENV_KEY
end
prepare_scorpion( scorpion ) click to toggle source

Fetch a scorpion and feed the controller it's dependencies

# File lib/scorpion/rails/controller.rb, line 55
def prepare_scorpion( scorpion )
  scorpion.prepare do |hunter|
    # Allow dependencies to access the controller
    hunter.hunt_for AbstractController::Base, return: self

    # Allow dependencies to access the current request/response
    hunter.hunt_for ActionDispatch::Request do |hunt|
      hunt.fetch( AbstractController::Base ).request
    end
    hunter.hunt_for ActionDispatch::Response do |hunt|
      hunt.fetch( AbstractController::Base ).response
    end

    hunter.hunt_for Scorpion::Rack::Env, return: request.env
  end
end