Module: GoogleDataSource::DataSource::Helper

Defined in:
lib/google_data_source/helper.rb

Instance Method Summary collapse

Instance Method Details

#google_data_source_includes(ssl = false) ⇒ Object

Includes the JavaScript files neccessary for data source visualization The helper should be called the header of the layout



6
7
8
9
# File 'lib/google_data_source/helper.rb', line 6

def google_data_source_includes(ssl = false)
  html = "<script src='http#{"s" if ssl}://www.google.com/jsapi' type='text/javascript'></script>"
  html.html_safe
end

Returns a export link for the given format



53
54
55
56
# File 'lib/google_data_source/helper.rb', line 53

def google_datasource_export_link(format)
  label = t("google_data_source.export_links.#{format}")
  link_to(label, '#', :class => "export_as_#{format}")
end

#google_datatable(url = nil, options = {}) ⇒ Object

Shows a Google data table



59
60
61
# File 'lib/google_data_source/helper.rb', line 59

def google_datatable(url = nil, options = {})
  google_visualization('Table', url, options)
end

#google_timeline(url = nil, options = {}) ⇒ Object

Shows a Google annotated timeline



64
65
66
# File 'lib/google_data_source/helper.rb', line 64

def google_timeline(url = nil, options = {})
  google_visualization('TimeLine', url, options)
end

#google_visualization(type, url = nil, options = {}) ⇒ Object

Shows a Google visualization. Available types include:

  • Table

  • TimeLine

url defines the URL to the data source and defaults to +url_for(:format => ‘datasource’). The options are generally passed to the visualization JS objects after camlizing the keys Options that are not passed include:

  • :container_id : The Dom id of the container element



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/google_data_source/helper.rb', line 19

def google_visualization(type, url = nil, options = {})
  # extract options that are not meant for the javascript part
  container_id = options.delete(:container_id) || "google_#{type.underscore}"

  # camelize option keys
  js_options = options.to_a.inject({}) { |memo, opt| memo[opt.first.to_s.camelize(:lower)] = opt.last; memo }
  
  url ||= url_for(:format => 'datasource')
  html = (:div, :id => container_id) { }
  html << javascript_tag("DataSource.Visualization.create('#{type.camelize}', '#{url}', '#{container_id}', #{js_options.to_json});")

  html << reporting_controls(container_id, options)
  html
end

#reporting_controls(container_id, options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/google_data_source/helper.rb', line 34

def reporting_controls(container_id, options)
  html = tag(:div, {:id => "#{container_id}_controls", :class => "data_source_controls"}, true)

  # Add Export links
  unless options[:exportable_as].nil? || options[:exportable_as].empty?
    html << tag(:div, {:id => "#{container_id}_export_as"}, true)
    html << t('google_data_source.export_links.export_as')
    html << ' '
    options[:exportable_as].each do |format|
      html << google_datasource_export_link(format)
    end
    html << ActiveSupport::SafeBuffer.new("</div>")
  end

  html << ActiveSupport::SafeBuffer.new("</div>") # ugly, any ideas?
  html
end