Sha256: 07482ff7c537bdd0c3a220f34897fb338ea7eeedc89fe7cf2efe95c4afece088

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

require_relative "inertia_rails"

module InertiaRails
  module Controller
    extend ActiveSupport::Concern

    included do
      before_action do
        # :inertia_errors are deleted from the session by the middleware
        InertiaRails.share(errors: session[:inertia_errors]) if session[:inertia_errors].present?
      end
    end

    module ClassMethods
      def inertia_share(**args, &block)
        before_action do
          InertiaRails.share(**args) if args
          InertiaRails.share_block(block) if block
        end
      end
    end

    def redirect_to(options = {}, response_options = {})
      capture_inertia_errors(response_options)
      super(options, response_options)
    end

    def redirect_back(fallback_location:, allow_other_host: true, **options)
      capture_inertia_errors(options)
      super(
        fallback_location: fallback_location,
        allow_other_host: allow_other_host,
        **options,
      )
    end

    private

    def inertia_location(url)
      headers['X-Inertia-Location'] = url
      head :conflict
    end

    def capture_inertia_errors(options)
      if (inertia_errors = options.dig(:inertia, :errors))
        session[:inertia_errors] = inertia_errors
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
inertia_rails-1.11.1 lib/inertia_rails/controller.rb
inertia_rails-1.11.0 lib/inertia_rails/controller.rb
inertia_rails-1.10.0 lib/inertia_rails/controller.rb
inertia_rails-1.9.2 lib/inertia_rails/controller.rb
inertia_rails-1.9.1 lib/inertia_rails/controller.rb