Sha256: 614ef062099c44d616081bbddf099f3ba25f8d20b53b4fb7145bff8ec6536eec

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

class Gon
  module Helpers

    def self.included base
      base.send(:include, InstanceMethods)
    end

    module InstanceMethods

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

      private

      def variables_for_request_present?
        current_gon.gon.present?
      end

      def current_gon
        Thread.current['gon']
      end

    end
  end

  module GonHelpers

    def self.included base
      base.send(:include, InstanceMethods)
    end

    module InstanceMethods

      def gon
        if wrong_gon_request?
          gon_request = Request.new(env)
          gon_request.id = request.uuid
          Thread.current['gon'] = gon_request
        end
        Gon
      end

      private

      def wrong_gon_request?
        current_gon.blank? || current_gon.id != request.uuid
      end

      def current_gon
        Thread.current['gon']
      end

    end
  end
end

ActionView::Base.send :include, Gon::Helpers
ActionController::Base.send :include, Gon::GonHelpers

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gon-5.0.0 lib/gon/helpers.rb