Sha256: 9bee5961e0321ea34b8ba66e56d4a98b651dbf4587c01857ce2add4ce454ee16

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module 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?
        Gon::Request.env &&
        Gon::Request.id == request.object_id &&
        Gon.all_variables.present?
      end

    end
  end

  module GonHelpers

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

    module InstanceMethods

      def gon
        if wrong_gon_request?
          Gon::Request.id = request.object_id
          Gon::Request.env = request.env
        end
        Gon
      end

      private

      def wrong_gon_request?
        Gon::Request.env.blank? ||
        Gon::Request.id != request.object_id
      end

    end

  end
end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gon-3.0.5 lib/gon/helpers.rb
gon-3.0.4 lib/gon/helpers.rb
gon-3.0.3 lib/gon/helpers.rb