Sha256: aee4967795bbb1b95acd23092e79dd3147eedcfdb2b16b8ca7dbf05b452eda86

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true
module IRuby
  module Chartkick
    class BaseChart
      include ::Chartkick::Helper

      ArgumentModifier = ->(args, callee) do
        modified = args.inject({}) do |sum, item|
          item.each do |k, v|
            sum[k] = v
          end
          sum
        end
        modified[:id] = callee.randomized_dom_id
        modified
      end

      def initialize(data)
        @data = data
      end

      def js_prefix
        javascripts.map do |js|
          <<-JS
            <script type="text/javascript">
              #{js}
            </script>
          JS
        end.join("\n")
      end

      def javascripts
        [
          adapter_javascript,
          chartkick_javascript
        ]
      end

      def adapter_javascript
        adapter_js_path = File.join(File.dirname(__FILE__), "..", "..", "..", "vendor", "assets", "javascripts", "google_charts_loader.js")
        File.read(adapter_js_path)
      end

      def chartkick_javascript
        chartkick_js_path = File.join(File.dirname(__FILE__), "..", "..", "..", "vendor", "assets", "javascripts", "chartkick.js")
        File.read(chartkick_js_path)
      end

      def randomized_dom_id
        "#{self.class.to_s.downcase.gsub("::", "-")}-#{rand(100000)}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iruby-chartkick-0.2.0 lib/iruby/chartkick/base_chart.rb