make sure meaningful default dom ids are used - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit d1eb67d6de166aa56e020e4510150c1e91a93fed
(DIR) parent b8ee111f12ef95eec333b448a14179d9aa7eef2f
(HTM) Author: Marco Otte-Witte <marco.otte-witte@simplabs.com>
Date: Thu, 13 May 2010 13:43:43 +0200
make sure meaningful default dom ids are used
Diffstat:
M lib/saulabs/reportable/report_tag_… | 16 +++++++++-------
M spec/other/report_tag_helper_spec.… | 21 +++++++++++++++------
2 files changed, 24 insertions(+), 13 deletions(-)
---
(DIR) diff --git a/lib/saulabs/reportable/report_tag_helper.rb b/lib/saulabs/reportable/report_tag_helper.rb
@@ -58,15 +58,14 @@ module Saulabs
end
- # Renders a sparkline with the given data using grafico.
+ # Renders a sparkline with the given data using Raphael.
#
# @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] grafico_options
- # options for grafico which to_json get called on,
- # see the grafico documentation for more information.
+ # @param [Hash] raphael_options
+ # options that are passed directly to Raphael as JSON
#
# @option options [Fixnum] :width (300)
# the width of the generated graph
@@ -80,11 +79,14 @@ module Saulabs
#
# @example Rendering a sparkline tag for report data
#
- # <%= raphael_report_tag(User.registrations_report, {:width => 200, :height => 100, :format => "div(100).to_i"}, {:vertical_label_unit => "registrations"}) %>
+ # <%= raphael_report_tag(User.registrations_report, { :width => 200, :height => 100, :format => 'div(100).to_i' }, { :vertical_label_unit => 'registrations' }) %>
#
def raphael_report_tag(data, options = {}, raphael_options = {})
+ @__raphael_report_tag_count ||= -1
+ @__raphael_report_tag_count += 1
+ default_dom_id = "#{data.model_name.downcase}_#{data.report_name}#{@__raphael_report_tag_count > 0 ? @__raphael_report_tag_count : ''}"
options.reverse_merge!(Config.raphael_options.slice(:width, :height, :format))
- options.reverse_merge!(:dom_id => "#{data.model_name.downcase}_#{data.report_name}")
+ options.reverse_merge!(:dom_id => default_dom_id)
raphael_options.reverse_merge!(Config.raphael_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">
@@ -92,7 +94,7 @@ module Saulabs
graph.g.linechart(
-10, 4, #{options[:width]}, #{options[:height]},
#{(0..data.size).to_a.to_json},
- #{data.map{|d| eval options[:format], d[1].send(:binding) }.to_json},
+ #{data.map { |d| eval options[:format], d[1].send(:binding) }.to_json},
#{raphael_options.to_json}
).hover(function() {
this.disc = graph.g.disc(this.x, this.y, 3).attr({fill: "#2F69BF", stroke: '#2F69BF' }).insertBefore(this);
(DIR) diff --git a/spec/other/report_tag_helper_spec.rb b/spec/other/report_tag_helper_spec.rb
@@ -5,23 +5,32 @@ describe Saulabs::Reportable::ReportTagHelper do
before do
@helper = TestHelper.new
end
-
+
describe '#raphael_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.raphael_report_tag(data_set).class.should == String
end
-
+
it 'should contain a div tag' do
@helper.raphael_report_tag(data_set).should =~ /^<div id=".*">.*<\/div>/
end
-
+
it 'should contain a script tag' do
@helper.raphael_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.raphael_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.raphael_report_tag(data_set).should =~ /^<div id="#{data_set.model_name.downcase}_#{data_set.report_name}".*<\/div>/
+ @helper.raphael_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