public
Description: create reports via sql or ruby code for your rails app in minutes
Homepage: http://upstream-berlin.com/blog/open-source/#dead_simple_reports
Clone URL: git://github.com/langalex/dead_simple_reports.git
now showing column names in first row in sql reports
Alexander Lang (author)
Mon Jul 07 10:26:07 -0700 2008
commit  e8861b75c47b2f0bb7792bbece62ebbc1e5ed2ea
tree    5e999ac9f9df971e0775724f21f22053e22a9eb3
parent  40201b24606610377f61e45c90830fa7b98e5e7e
...
3
4
5
6
7
8
9
 
 
10
11
12
13
 
 
 
 
 
 
 
 
 
 
14
...
3
4
5
 
6
7
 
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -3,12 +3,22 @@ class Report < ActiveRecord::Base
0
   validates_presence_of :code, :name, :kind
0
   validates_uniqueness_of :name
0
   
0
-  
0
   def run
0
     if kind == 'sql'
0
-      ActiveRecord::Base.connection.select_all(code).map(&:values)
0
+      rows = ActiveRecord::Base.connection.select_all(code)
0
+      header(rows) + rows.map(&:values)
0
     else
0
       eval(code)
0
     end
0
   end
0
+  
0
+  private
0
+  
0
+  def header(rows)
0
+    if rows.first
0
+      [rows.first.map{|k,v| k}]
0
+    else
0
+      []
0
+    end
0
+  end
0
 end
...
18
19
20
21
22
 
 
 
 
 
 
 
 
23
24
...
18
19
20
 
 
21
22
23
24
25
26
27
28
29
30
0
@@ -18,7 +18,13 @@ describe Report do
0
   
0
   it "should run the sql and return an array of arrays" do
0
     @report.kind = 'sql'
0
-    @report.code = 'SELECT 1, 2 UNION SELECT 2, 3'
0
-    @report.run.should == [['1', '2'], ['2', '3']]
0
+    @report.code = 'SELECT 1 AS col_1, 2 AS col_2 UNION SELECT 2 AS col_1, 3 AS col_3'
0
+    @report.run.should == [['col_1', 'col_2'], ['1', '2'], ['2', '3']]
0
+  end
0
+  
0
+  it "should return an empty array if no results" do
0
+    @report.kind = 'sql'
0
+    @report.code = 'SELECT 1 WHERE 0'
0
+    @report.run.should == []
0
   end
0
 end

Comments