Sha256: a14df5ad3bdca5bf9bddd84d82c07568f0ffe76fbdbe707563f0c462a8e5ac5f

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

class Gon
  module ViewHelpers
    def include_gon(options = {})
      if variables_for_request_present?
        Gon::Base.render_data(options)
      elsif Gon.global.all_variables.present? || options[:init].present?
        Gon.clear
        Gon::Base.render_data(options)
      else
        ''
      end
    end

    def include_gon_amd(options={})
      Gon::Base.render_data(options.merge({amd: true}))
    end

    private

    def variables_for_request_present?
      current_gon && current_gon.gon
    end

    def current_gon
      RequestStore.store[:gon]
    end
  end

  module ControllerHelpers
    def gon
      if wrong_gon_request?
        gon_request = Request.new(request.env)
        gon_request.id = gon_request_uuid
        RequestStore.store[:gon] = gon_request
      end
      Gon
    end

    private

    def wrong_gon_request?
      current_gon.blank? || current_gon.id != gon_request_uuid
    end

    def current_gon
      RequestStore.store[:gon]
    end

    def gon_request_uuid
      request.uuid
    end
  end
end

ActiveSupport.on_load :action_view do
  ActionView::Base.send :include, Gon::ViewHelpers
end
ActiveSupport.on_load :action_controller do
  ActionController::Base.send :include, Gon::ControllerHelpers
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gon-6.4.0 lib/gon/helpers.rb
gon-6.3.2 lib/gon/helpers.rb
gon-6.3.1 lib/gon/helpers.rb