class ChartCandy::Helpers::ChartCandyTagHelper

Public Class Methods

new(rails_helpers, id, from, to, step) click to toggle source
# File lib/chart-candy/helpers.rb, line 23
def initialize(rails_helpers, id, from, to, step)
  @rails_helpers = rails_helpers
  @id = id
  @from = from
  @to = to
  @step = step || 'month'
end

Public Instance Methods

counter(options={}) click to toggle source
# File lib/chart-candy/helpers.rb, line 31
def counter(options={})
  chart 'counter', options
end
donut(options={}) click to toggle source
# File lib/chart-candy/helpers.rb, line 48
def donut(options={})
  options.reverse_merge! tools: {}
  options[:tools].reverse_merge! export_xls: true, step: false, template: true

  chart 'donut', options
end
excel_chart_button(options={}) click to toggle source
# File lib/chart-candy/helpers.rb, line 35
def excel_chart_button(options={})
  build_url 'line', options if not @url

  tool_export_xls options[:label]
end
line(options={}) click to toggle source
# File lib/chart-candy/helpers.rb, line 41
def line(options={})
  options.reverse_merge! tools: {}
  options[:tools].reverse_merge! export_xls: true, step: true, template: true

  chart 'line', options
end

Private Instance Methods

build_url(nature, options={}) click to toggle source
# File lib/chart-candy/helpers.rb, line 57
def build_url(nature, options={})
  params = { from: @from, format: 'json', id: @id, nature: nature, nonce: SecureRandom.hex(20), step: @step, timestamp: Time.now.utc.iso8601, to: @to, version: 'v1' }

  options.each { |k,v| params[k] = v if not ['class', 'tools'].include? k.to_s }

  params[:token] = build_url_token(params)

  @url = @rails_helpers.candy_chart_url params
end
build_url_token(params) click to toggle source
# File lib/chart-candy/helpers.rb, line 67
def build_url_token(params)
  compacted_params = ChartCandy::Authentication.compact_params(params)

  url = @rails_helpers.candy_charts_url + compacted_params

  return ChartCandy::Authentication.tokenize(url)
end
chart(nature, options={}) click to toggle source
# File lib/chart-candy/helpers.rb, line 75
def chart(nature, options={})
  options.reverse_merge! class: ""
  options[:class] += " wrapper-chart chart-#{nature}"

  build_url nature, options

  content = ''
  content += title_tag
  content += chart_tools(nature, options[:tools]) if options[:tools]
  content += content_tag(:div, content_tag(:div, '', class: 'chart') + content_tag(:div, '', class: 'table'), class: 'templates')

  wrapper_options = { id: @id, class: options[:class], 'data-chart-candy' => nature, 'data-url' => @url}
  wrapper_options['data-update-delay'] = options[:update_every].to_i if options[:update_every]

  return content_tag(:div, content.html_safe, wrapper_options)
end
chart_select_tag(name, choices, selection) click to toggle source
# File lib/chart-candy/helpers.rb, line 92
def chart_select_tag(name, choices, selection)
  if form_candy?
    candy.select(name, choices, selection)
  else
    select_tag(name, options_for_select(choices, selection), id: nil)
  end
end
chart_switch_tag(name, choices, selection) click to toggle source
# File lib/chart-candy/helpers.rb, line 100
def chart_switch_tag(name, choices, selection)
  if form_candy?
    candy.switch(name, choices, selection)
  else
    select_tag(name, options_for_select(choices, selection), id: nil)
  end
end
chart_tools(nature, options={}) click to toggle source
# File lib/chart-candy/helpers.rb, line 108
def chart_tools(nature, options={})
  content = form_tag(@url) do
    tools = ''
    tools += tool_export_xls if options[:export_xls]
    tools += tool_step if options[:step]
    tools += tool_template if options[:template]

    tools.html_safe
  end

  return content_tag(:div, content.html_safe, class: 'tools')
end
form_candy?() click to toggle source
# File lib/chart-candy/helpers.rb, line 121
def form_candy?
  begin
    (candy ? true : false)
  rescue
    false
  end
end
method_missing(*args, &block) click to toggle source
# File lib/chart-candy/helpers.rb, line 157
def method_missing(*args, &block)
  if [:candy, :content_tag, :form_tag, :link_to, :options_for_select, :select_tag].include?(args.first)
    return @rails_helpers.send(*args, &block)
  else
    raise NoMethodError.new("undefined local variable or method '#{args.first}' for #{self.class}")
  end
end
t(path) click to toggle source
# File lib/chart-candy/helpers.rb, line 129
def t(path)
  ChartCandy.translate(path)
end
title_tag() click to toggle source
# File lib/chart-candy/helpers.rb, line 133
def title_tag
  content_tag(:h2, t("#{@id.underscore}.title").html_safe, class: 'title-chart')
end
tool_export_xls(label=nil) click to toggle source
# File lib/chart-candy/helpers.rb, line 137
def tool_export_xls(label=nil)
  label = t('base.xls_export') if not label

  content = link_to(content_tag(:span, label, class: 'text'), @url.gsub('.json', '.xls'), class: 'button', title: t('base.xls_export'))

  return content_tag(:div, content.html_safe, class: 'tool holder-export-xls')
end
tool_step() click to toggle source
# File lib/chart-candy/helpers.rb, line 145
def tool_step
  choices = ['day', 'week', 'month'].map{ |c| [t("base.steps.#{c}"), c] }

  return content_tag(:div, chart_select_tag('step', choices, @step), class: 'tool holder-step')
end
tool_template() click to toggle source
# File lib/chart-candy/helpers.rb, line 151
def tool_template
  choices = ['chart', 'table'].map { |c| [t("base.template.#{c}"), c] }

  return content_tag(:div, chart_switch_tag('template', choices, 'chart'), class: 'tool holder-template')
end