Sha256: 9e8cda91a6c3dc432e3de0e3a13a3c335985528e9803e6b07d76f1a422c122b3

Contents?: true

Size: 1.95 KB

Versions: 10

Compression:

Stored size: 1.95 KB

Contents

module Rodauth
  module Rails
    module Feature
      module Callbacks
        extend ActiveSupport::Concern

        private

        def _around_rodauth
          rails_controller_around { super }
        end

        # Runs controller callbacks and rescue handlers around Rodauth actions.
        def rails_controller_around
          result = nil

          rails_controller_rescue do
            rails_controller_callbacks do
              result = catch(:halt) { yield }
            end
          end

          result = handle_rails_controller_response(result)

          throw :halt, result if result
        end

        # Runs any #(before|around|after)_action controller callbacks.
        def rails_controller_callbacks(&block)
          rails_controller_instance.run_callbacks(:process_action, &block)
        end

        # Runs any registered #rescue_from controller handlers.
        def rails_controller_rescue
          yield
        rescue Exception => exception
          rails_controller_instance.rescue_with_handler(exception) || raise

          unless rails_controller_instance.performed?
            raise Rodauth::Rails::Error, "rescue_from handler didn't write any response"
          end
        end

        # Handles controller rendering a response or setting response headers.
        def handle_rails_controller_response(result)
          if rails_controller_instance.performed?
            rails_controller_response
          elsif result
            result[1].merge!(rails_controller_instance.response.headers)
            result
          end
        end

        # Returns Roda response from controller response if set.
        def rails_controller_response
          controller_response = rails_controller_instance.response

          response.status = controller_response.status
          response.headers.merge! controller_response.headers
          response.write controller_response.body

          response.finish
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rodauth-rails-1.13.0 lib/rodauth/rails/feature/callbacks.rb
rodauth-rails-1.12.0 lib/rodauth/rails/feature/callbacks.rb
rodauth-rails-1.11.0 lib/rodauth/rails/feature/callbacks.rb
rodauth-rails-1.10.0 lib/rodauth/rails/feature/callbacks.rb
rodauth-rails-1.9.0 lib/rodauth/rails/feature/callbacks.rb
rodauth-rails-1.8.0 lib/rodauth/rails/feature/callbacks.rb
rodauth-rails-1.7.1 lib/rodauth/rails/feature/callbacks.rb
rodauth-rails-1.7.0 lib/rodauth/rails/feature/callbacks.rb
rodauth-rails-1.6.4 lib/rodauth/rails/feature/callbacks.rb
rodauth-rails-1.6.3 lib/rodauth/rails/feature/callbacks.rb