Sha256: c1ca443684ba0afb007720dc5251873c5af19c8f49237ea5c70783011aee2a2c

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'active_support/core_ext'
require 'chartkick'

module Chartkick::Remote
  module Helper
    include Chartkick::Helper

    %w{line_chart pie_chart column_chart bar_chart area_chart geo_chart}.each do |type|
      define_method :"#{type}_with_remote" do |data_source = nil, options = {}, &block|
        chartkick_remote_chart type, data_source, options, &block
      end

      alias_method_chain :"#{type}", :remote
    end

    private

    def chartkick_remote_chart(type, data_source, options, &block)
      if block_given? && data_source.is_a?(Hash)
        options = data_source
      end

      options = options.dup
      options.reverse_merge!(controller.chartkick_options) if controller.respond_to?(:chartkick_options)

      standalone = options.delete(:standalone)
      remote = options.delete(:remote)
      skip = false

      if remote
        @remote_chart_id = (@remote_chart_id || 0) + 1
        chart_id = controller.params[:_chartkick_remote_chart_id]
        skip = params[:_chartkick_remote_standalone] && chart_id.to_s != @remote_chart_id.to_s
        controller.chartkick_remote_blocks ||= {}
        controller.chartkick_remote_blocks[@remote_chart_id] = block
        data_source = url_for(params.
                                except(:_chartkick_remote_standalone).
                                merge(_chartkick_remote_chart_id: @remote_chart_id, format: :json))
      elsif block_given?
        data_source = block.call
      end

      if skip
        result = '<div>Skipped</div>'.html_safe
      else
        result = send(:"#{type}_without_remote", data_source, options)
      end

      if remote && standalone
        standalone_link = link_to 'Standalone',
                                  url_for(params.merge(_chartkick_remote_chart_id: @remote_chart_id,
                                                       _chartkick_remote_standalone: 1))

        result += standalone_link.html_safe
      end

      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chartkick-remote-1.1.6 lib/chartkick/remote/helper.rb