| 35 | | </p> |
|---|
| 36 | | </div> |
|---|
| 37 | | <div id="secondaryContent"> |
|---|
| | 35 | </p> |
|---|
| | 36 | |
|---|
| | 37 | <a name="csv"><h3>Basic Grouping of CSV data</h3></a> |
|---|
| | 38 | |
|---|
| | 39 | <p> |
|---|
| | 40 | Say you want to load in a CSV from file and group it by people's names, |
|---|
| | 41 | and get a PDF report back. This is easy in Ruport. |
|---|
| | 42 | </p> |
|---|
| | 43 | |
|---|
| | 44 | <p>Below is some sample data: (foo.csv)</p> |
|---|
| | 45 | |
|---|
| | 46 | <pre> |
|---|
| | 47 | name,login time,machine |
|---|
| | 48 | Gregory,10:00,bittle |
|---|
| | 49 | Joe,11:45,soda |
|---|
| | 50 | Jim,9:00,kitten |
|---|
| | 51 | Joe,12:15,soda |
|---|
| | 52 | Gregory,5:00,kitten |
|---|
| | 53 | Joe,12:45,bittle |
|---|
| | 54 | </pre> |
|---|
| | 55 | |
|---|
| | 56 | <p>To do the report we mentioned before, we'd just do something like this:</p> |
|---|
| | 57 | |
|---|
| | 58 | <pre> |
|---|
| | 59 | t = Table("foo.csv") |
|---|
| | 60 | grouping = Grouping(t,:by => "name") |
|---|
| | 61 | puts grouping.to_pdf |
|---|
| | 62 | </pre> |
|---|
| | 63 | |
|---|
| | 64 | <p> |
|---|
| | 65 | Directing the output of that script to a file results in nice output like this: |
|---|
| | 66 | </p> |
|---|
| | 67 | <div align="center"> |
|---|
| | 68 | <a href="samples/csv_simple.pdf"><img src="samples/csv_simple.png"></a> |
|---|
| | 69 | </div> |
|---|
| | 70 | |
|---|
| | 71 | <p> |
|---|
| | 72 | There are actually several styles for output, and also, it's trivial to do HTML, |
|---|
| | 73 | Text, or CSV formatted output as well. |
|---|
| | 74 | </p> |
|---|
| | 75 | |
|---|
| | 76 | <a name="aar"><h3>A trivial dump of an ActiveRecord model to CSV</h3></a> |
|---|
| | 77 | |
|---|
| | 78 | <p> |
|---|
| | 79 | If you're looking to just grab a CSV dump of some model in your Rails app, |
|---|
| | 80 | Ruport might be the easiest way to do it. Just add <tt>require "ruport"</tt> |
|---|
| | 81 | in your <tt>environment.rb</tt> file, and then the following code to your |
|---|
| | 82 | model: |
|---|
| | 83 | </p> |
|---|
| | 84 | |
|---|
| | 85 | <pre> |
|---|
| | 86 | class MyModel < ActiveRecord::Base |
|---|
| | 87 | acts_as_reportable |
|---|
| | 88 | end |
|---|
| | 89 | </pre> |
|---|
| | 90 | |
|---|
| | 91 | <p> |
|---|
| | 92 | If you want a full dump, just do this: |
|---|
| | 93 | </p> |
|---|
| | 94 | |
|---|
| | 95 | <pre> |
|---|
| | 96 | puts MyModel.report_table.to_csv |
|---|
| | 97 | </pre> |
|---|
| | 98 | |
|---|
| | 99 | <p> |
|---|
| | 100 | If you want to reduce via an AR find, you can pass along any of those options |
|---|
| | 101 | as well. |
|---|
| | 102 | </p> |
|---|
| | 103 | |
|---|
| | 104 | <pre> |
|---|
| | 105 | puts MyModel.report_table(:all, :conditions => ["name = ?", "gregory"]).to_csv |
|---|
| | 106 | </pre> |
|---|
| | 107 | |
|---|
| | 108 | <p> |
|---|
| | 109 | Ruport can do a whole lot more tricks with ActiveRecord / Rails, including |
|---|
| | 110 | handle all types of associations and model methods. For details, see |
|---|
| | 111 | the <a href="http://stonecode.svnrepository.com/ruport/trac.cgi/wiki/ActsAsReportable"> |
|---|
| | 112 | AAR wiki page.</a> |
|---|
| | 113 | </p> |
|---|
| | 114 | |
|---|
| | 115 | <a name="svg"><h3>SVG Graphs with ruport-util</h3></a> |
|---|
| | 116 | |
|---|
| | 117 | <p> |
|---|
| | 118 | If you're using the ruport-util package, it's very easy to generate beautiful |
|---|
| | 119 | SVG graphs. The functionality is still a bit basic, but for the most common |
|---|
| | 120 | needs, the output is quite nice (Thanks to Scruffy!) |
|---|
| | 121 | </p> |
|---|
| | 122 | |
|---|
| | 123 | <p> |
|---|
| | 124 | Here is a trivial graphing example: |
|---|
| | 125 | </p> |
|---|
| | 126 | <pre> |
|---|
| | 127 | require "rubygems" |
|---|
| | 128 | require "ruport" |
|---|
| | 129 | |
|---|
| | 130 | require "ruport/util" |
|---|
| | 131 | |
|---|
| | 132 | class GraphReport < Ruport::Report |
|---|
| | 133 | |
|---|
| | 134 | def generate |
|---|
| | 135 | graph = Ruport::Graph(:column_names => %w[a b c d e]) |
|---|
| | 136 | graph.add_line [1,2,3,4,5], :name => "foo" |
|---|
| | 137 | graph.add_line [11,22,70,2,19], :name => "bar" |
|---|
| | 138 | graph.to_svg |
|---|
| | 139 | end |
|---|
| | 140 | |
|---|
| | 141 | end |
|---|
| | 142 | |
|---|
| | 143 | GraphReport.run { |r| r.write("foo.svg") } |
|---|
| | 144 | </pre> |
|---|
| | 145 | |
|---|
| | 146 | <p>The output ends up looking something like this:</p> |
|---|
| | 147 | <div align="right"> |
|---|
| | 148 | <a href="samples/foo.svg"><img src="samples/graph.png"/></a> |
|---|
| | 149 | </div> |
|---|
| | 150 | |
|---|
| | 151 | <p> |
|---|
| | 152 | Of course, if you need to display this easily in a browser, you might want |
|---|
| | 153 | to convert it to another format such as PNG or JPG. We're looking for patches |
|---|
| | 154 | to make this easier, but haven't found solutions based on RMagick to be |
|---|
| | 155 | easy enough to support. |
|---|
| | 156 | </p> |
|---|
| | 157 | |
|---|
| | 158 | <h3>Enough Flashy Examples for Now</h3> |
|---|
| | 159 | |
|---|
| | 160 | <p> |
|---|
| | 161 | Hopefully we've at least sold you on the idea of using <tt>ruport</tt> |
|---|
| | 162 | for your project. Still, we'd like to take this chance to remind you that |
|---|
| | 163 | the real gains you'll see from Ruport are not at this high of a level, but |
|---|
| | 164 | rather deeper in the trenches. We're aiming to provide a super light weight |
|---|
| | 165 | foundation to build reporting apps on top of, because we feel that's what |
|---|
| | 166 | Rubyists will best benefit from. Hopefully, you'll find that to be true for |
|---|
| | 167 | your work. |
|---|
| | 168 | </p> |
|---|
| | 169 | |
|---|
| | 170 | <p> |
|---|
| | 171 | If you're convinced and ready to give Ruport a spin, have a look at some of |
|---|
| | 172 | the available <a href="resources.html">resources</a>, and then come say hello |
|---|
| | 173 | on the <a href="http://lists.rubyreports.org">Ruport mailing list</a>. |
|---|
| | 174 | |
|---|
| | 175 | Happy Hacking! |
|---|
| | 176 | </p> |
|---|
| | 177 | </div> |
|---|
| | 178 | <div id="secondaryContent"> |
|---|
| | 179 | <p><a href="#csv">Basic Grouping of CSV data</a></p> |
|---|
| | 180 | <p><a href="#aar">A trivial dump of an ActiveRecord model to CSV</a></p> |
|---|
| | 181 | <p><a href="#svg">SVG Graphs with ruport-util</a></p> |
|---|