Sha256: 5bcbb5b7516a423627bb3ab2363f5d1c1533fa3bfb3c9db1b6c4ce8517f1a1f0

Contents?: true

Size: 819 Bytes

Versions: 4

Compression:

Stored size: 819 Bytes

Contents

module Thoth
  module Rails
    module ControllerContext

      def self.included(base)
        base.around_action(:set_thoth_request_context)
      end

      def set_thoth_request_context
        Thoth.context = Thoth.context.merge(thoth_request_context)
        yield
        Thoth.clear_context!
      end

      def thoth_request_context
        #>= Rails 4.2
        if ::Rails::VERSION::MAJOR >= 5 || (::Rails::VERSION::MAJOR == 4 && ::Rails::VERSION::MINOR >= 2)
          context = params.to_unsafe_h
        else
          context = params.to_h
        end
        context[:current_user] = current_user.try(:id) if defined?(current_user)
        context
      end
    end
  end
end

if defined?(::ActionController)
  ::ActiveSupport.on_load(:action_controller) { include Thoth::Rails::ControllerContext }
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
thoth-3.0.0 lib/thoth/rails/controller_context.rb
thoth-2.0.0 lib/thoth/rails/controller_context.rb
thoth-1.0.2 lib/thoth/rails/controller_context.rb
thoth-1.0.1 lib/thoth/rails/controller_context.rb