Adding ReportTagHelper#flot_report_tag method with specs and default options - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit 1dfd44c050bf717b8d3c49acbe66f6154901bf67
(DIR) parent fac089b11bc3cd41fe1897bc71f3cf26cfa4e55e
(HTM) Author: Martin Kavalar <martin@sauspiel.de>
Date: Wed, 26 May 2010 10:36:38 +0200
Adding ReportTagHelper#flot_report_tag method with specs and default options
Diffstat:
M generators/reportable_raphael_asse… | 3 +--
M lib/saulabs/reportable/config.rb | 20 ++++++++++++++++++++
M lib/saulabs/reportable/report_tag_… | 44 +++++++++++++++++++++++++++++++
M spec/other/report_tag_helper_spec.… | 27 +++++++++++++++++++++++++++
4 files changed, 92 insertions(+), 2 deletions(-)
---
(DIR) diff --git a/generators/reportable_raphael_assets/templates/NOTES b/generators/reportable_raphael_assets/templates/NOTES
@@ -1,3 +1,2 @@
- ** Don't forget to include raphael.min.js as well as g.raphael.min.js and g.line.min.js in your layout's head section.
-
+ ** Don't forget to include Raphael as well as gRaphaels javascripts in your head section
(DIR) diff --git a/lib/saulabs/reportable/config.rb b/lib/saulabs/reportable/config.rb
@@ -25,6 +25,26 @@ module Saulabs
:hover_line_color => '2f69bf',
:hover_fill_color => '2f69bf'
}
+
+ mattr_accessor :flot_options
+
+ @@flot_options = {
+ :width => 300,
+ :height => 100,
+ :dom_id => nil,
+ :colors => ['rgba(6,122,205,1)'],
+ :grid => {
+ :show => false
+ },
+ :series => {
+ :lines => {
+ :fill => true,
+ :fillColor => 'rgba(6,122,205,.5)',
+ :lineWidth => 2
+ },
+ :shadowSize => 0
+ }
+ }
end
(DIR) diff --git a/lib/saulabs/reportable/report_tag_helper.rb b/lib/saulabs/reportable/report_tag_helper.rb
@@ -111,6 +111,50 @@ module Saulabs
});
</script>}
end
+
+ # Renders a sparkline with the given data using the jquery flot plugin.
+ #
+ # @param [Array<Array<DateTime, Float>>] data
+ # an array of report data as returned by {Saulabs::Reportable::Report#run}
+ # @param [Hash] options
+ # options for width, height, the dom id and the format
+ # @param [Hash] flot_options
+ # options that are passed directly to Raphael as JSON
+ #
+ # @option options [Fixnum] :width (300)
+ # the width of the generated graph
+ # @option options [Fixnum] :height (34)
+ # the height of the generated graph
+ # @option options [Array<Symbol>] :dom_id ("reportable_#{Time.now.to_i}")
+ # the dom id of the generated div
+ #
+ # @return [String]
+ # an div tag and the javascript code showing a sparkline for the passed +data+
+ #
+ # @example Rendering a sparkline tag for report data
+ #
+ # <%= flot_report_tag(User.registrations_report) %>
+ #
+
+ def flot_report_tag(data, options = {}, flot_options = {})
+ @__flot_report_tag_count ||= -1
+ @__flot_report_tag_count += 1
+ default_dom_id = "#{data.model_name.downcase}_#{data.report_name}#{@__flot_report_tag_count > 0 ? @__flot_report_tag_count : ''}"
+ options.reverse_merge!(Config.flot_options.slice(:width, :height, :format))
+ options.reverse_merge!(:dom_id => default_dom_id)
+ flot_options.reverse_merge!(Config.flot_options.except(:width, :height, :format))
+ %Q{<div id="#{options[:dom_id]}" style="width:#{options[:width]}px;height:#{options[:height]}px;"></div>
+ <script type="text\/javascript" charset="utf-8">
+ $(function() {
+ var set = #{data.to_json},
+ data = [];
+ for (var i = 0; i < set.length; i++) {
+ data.push([i, set[i]]);
+ }
+ $.plot($('#interactive_graph'), [data], );
+ });
+ </script>}
+ end
end
end
(DIR) diff --git a/spec/other/report_tag_helper_spec.rb b/spec/other/report_tag_helper_spec.rb
@@ -32,6 +32,33 @@ describe Saulabs::Reportable::ReportTagHelper do
end
end
+
+ describe '#flot_report_tag' do
+
+ data_set = Saulabs::Reportable::ResultSet.new([[DateTime.now, 1.0], [DateTime.now - 1.day, 3.0]], "User", "registrations")
+
+ it 'should return a string' do
+ @helper.flot_report_tag(data_set).class.should == String
+ end
+
+ it 'should contain a div tag' do
+ @helper.flot_report_tag(data_set).should =~ /^<div id=".*">.*<\/div>/
+ end
+
+ it 'should contain a script tag' do
+ @helper.flot_report_tag(data_set).should =~ /<script type="text\/javascript" charset="utf-8">.*<\/script>/m
+ end
+
+ it 'should assign a default dom id to the the div tag if none is specified' do
+ @helper.flot_report_tag(data_set).should =~ /^<div id="#{data_set.model_name.downcase}_#{data_set.report_name}".*<\/div>/
+ end
+
+ it 'should assign correct default dom ids to the the div tag if none is specified and there are more than one report tags on the page' do
+ @helper.flot_report_tag(data_set).should =~ /^<div id="#{data_set.model_name.downcase}_#{data_set.report_name}".*<\/div>/
+ @helper.flot_report_tag(data_set).should =~ /^<div id="#{data_set.model_name.downcase}_#{data_set.report_name}1".*<\/div>/
+ end
+
+ end
describe '#google_report_tag' do