Sha256: d33885fb00a9dfbfc6c40bc91cdbd8e74ca3b6481a43ddc5862af03ebc3d3f6a

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Pjax
  extend ActiveSupport::Concern

  included do
    layout proc { |c| pjax_request? ? pjax_layout : 'application' }
    helper_method :pjax_request?

    before_filter :strip_pjax_param, :if => :pjax_request?
    before_filter :set_pjax_url,     :if => :pjax_request?
  end

  class Error < StandardError; end
  class Unsupported < Error; end

  rescue_from Pjax::Unsupported, :with => :pjax_unsupported

  protected
    def pjax_request?
      env['HTTP_X_PJAX'].present?
    end

    def pjax_layout
      false
    end

    def pjax_container
      return unless pjax_request?
      request.headers['X-PJAX-Container']
    end

    def pjax_unsupported
      head :not_acceptable
    end

    # Call in a before_filter or in an action to disable pjax on an action.
    #
    # Examples
    #
    #     before_filter :prevent_pjax!
    #
    #     def login
    #       prevent_pjax!
    #       # ...
    #     end
    #
    def prevent_pjax!
      raise PjaxUnsupported if pjax_request?
    end

    def strip_pjax_param
      params.delete(:_pjax)
      request.env['QUERY_STRING'] = request.env['QUERY_STRING'].sub(/_pjax=[^&]+&?/, '')

      request.env.delete('rack.request.query_string')
      request.env.delete('rack.request.query_hash')
      request.env.delete('action_dispatch.request.query_parameters')

      request.instance_variable_set('@original_fullpath', nil)
      request.instance_variable_set('@fullpath', nil)
    end

    def set_pjax_url
      response.headers['X-PJAX-URL'] = request.url
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pjax_rails-0.3.0 lib/pjax.rb