Grouping with aar_views

0 views
Skip to first unread message

AndyV

unread,
May 2, 2007, 10:51:48 PM5/2/07
to Ruby Reports
I'm experimenting with acts_as_reportable (aar_views branch) and
wondering if it is possible to use Groupings. The table that I want
to report (Activity) uses STI. I'd like to generate the report,
grouping on the inheritance column.

Currently I have...

-----------------
class MyControler < ActionController::Base
...

def example
activities = Activity.report_table(:all)
@activities = Grouping.new(activities, :by=>'activity_type')
end
...
end

-----------------

example.report

draw_table(activities)

-----------------

This throws an exception since the Grouping does not have the
necessary column_names collection.

Thanks,
AndyV

Michael Milner

unread,
May 2, 2007, 11:09:39 PM5/2/07
to ruby-r...@googlegroups.com

Hi Andy,

There are two basic approaches you could take here.  If you don't need to do any special formatting for the grouping, you can just use the built-in formatter.  Then you don't need the report template at all. So, something like the following at the end of your example method:

send_data @activities.to_pdf, :type => "application/pdf"

We have a few different formatting options for grouping, so you can try each one and see what you like @activities.to_pdf(:style => :inline) is the default, but you can use :justified, :separated, or :offset in place of :inline.

However, if you do need special formatting beyond what grouping offers out of the box, then in the template you have above, just do:

@activities.each do |group|
  draw_table(group)
end

Hopefully, this works for you, but let me know if you have any problems with it or any other questions.

Mike

AndyV

unread,
May 3, 2007, 9:57:55 AM5/3/07
to Ruby Reports
Thanks, Mike. I prefer the template approach for a few reasons. The
primary one is that we'll have considerably more complex reports to
generate in the future and I want to settle on one methodology that
can produce both the complex and the simple in a way that's intuitive
to the other developers on the team. The aar_view approach seems to
fit the bill with it's fit in the MVC-style approach.

I tried both recommendations above. I got the first one (send_data)
to work without any problem. The only thing that I did not like was
that the pdf came down as a file download rather than displayed as a
PDF in the browser.

The second approach did not work. I'd tried something similar earlier
in the evening but tried again this morning and still got the 'missing
column_names' exception. I've hacked around it and come up with this
(very ugly) code to make it work. I'm sure there is a better way and
open to any suggestions.

example.report
-------------------------------------------------------------------------------------------------------------------------
| options.paper_size = "letter"
|
| add_text "Alphabetical listing of
Activities", :justification=>:center, :font_size=>14
|
| pad(4) do
| add_text "by Andy Vanasse", :justification=>:center, :font_size=>10
| end
|
| @activities.each do |group|
| group.each do |row|
| if row.is_a?(String)
| pad(10) { add_text row.pluralize, :font_size=>12 }
| else
| draw_table(row, :width=>400, :font_size=>10)
| end
| end
|
| end
-------------------------------------------------------------------------------------------------------------------------

For reference, the example action looks like this:
def example
activities = Activity.report_table(:all, :order=>'activity_type,
name', :except=>:id)
@activities = Grouping(activities, :by=>'activity_type')
logger.debug @activities.to_s
end


Best regards,
AndyV

On May 2, 11:09 pm, "Michael Milner" <mikem...@gmail.com> wrote:

Gregory Brown

unread,
May 3, 2007, 10:09:54 AM5/3/07
to ruby-r...@googlegroups.com
On 5/3/07, AndyV <ar...@cornell.edu> wrote:
>
> Thanks, Mike. I prefer the template approach for a few reasons. The
> primary one is that we'll have considerably more complex reports to
> generate in the future and I want to settle on one methodology that
> can produce both the complex and the simple in a way that's intuitive
> to the other developers on the team. The aar_view approach seems to
> fit the bill with it's fit in the MVC-style approach.
>
> I tried both recommendations above. I got the first one (send_data)
> to work without any problem. The only thing that I did not like was
> that the pdf came down as a file download rather than displayed as a
> PDF in the browser.
>
> The second approach did not work. I'd tried something similar earlier
> in the evening but tried again this morning and still got the 'missing
> column_names' exception. I've hacked around it and come up with this
> (very ugly) code to make it work. I'm sure there is a better way and
> open to any suggestions.

it looks like Mike may have given you the wrong signature for Grouping#each

>> a = Table(%w[a b]) << %w[apple banana]
>> a << %w[cat dog]
>> a << %w[apple cat]
>> grouping = Grouping(a,:by => "a")

You tried this:
>> grouping.each { |g| p g.class }
Array
Array

What you probably want is this:
>> grouping.each { |name,group| p group.class }
Ruport::Data::Group
Ruport::Data::Group

It yields name,group, not just group.
I'll make the change in your code below.

> example.report
> -------------------------------------------------------------------------------------------------------------------------
> | options.paper_size = "letter"
> |
> | add_text "Alphabetical listing of
> Activities", :justification=>:center, :font_size=>14
> |
> | pad(4) do
> | add_text "by Andy Vanasse", :justification=>:center, :font_size=>10
> | end
> |

+ | @activities.each do |name,group|
+ # add :title => name if you want the group's name added to
the top of the table
> | draw_table(group, :width=>400, :font_size=>10)
> | end
> |
> | end
> -------------------------------------------------------------------------------------------------------------------------

Let me know if that does the trick.

Gregory Brown

unread,
May 3, 2007, 10:10:50 AM5/3/07
to ruby-r...@googlegroups.com
On 5/3/07, Gregory Brown <gregory...@gmail.com> wrote:

> it looks like Mike may have given you the wrong signature for Grouping#each

For what it's worth, I may have IMed him that bad signature, so you
can blame me :)

AndyV

unread,
May 3, 2007, 10:31:27 AM5/3/07
to Ruby Reports
Brilliant. That was it. Just looking at my code I knew something was
wrong. Thanks!

On May 3, 10:09 am, "Gregory Brown" <gregory.t.br...@gmail.com> wrote:

Gregory Brown

unread,
May 3, 2007, 10:38:50 AM5/3/07
to ruby-r...@googlegroups.com
On 5/3/07, AndyV <ar...@cornell.edu> wrote:
>
> Brilliant. That was it. Just looking at my code I knew something was
> wrong. Thanks!

The API docs are also silent on this, but I'll fix that today.

Warm Regards,
-gregory

AndyV

unread,
May 3, 2007, 10:43:21 AM5/3/07
to Ruby Reports
Amazing, self-deprecating honesty to boot. No blame, just lots of
thanks for great work.

On May 3, 10:10 am, "Gregory Brown" <gregory.t.br...@gmail.com> wrote:

Gregory Brown

unread,
May 3, 2007, 10:53:02 AM5/3/07
to ruby-r...@googlegroups.com
On 5/3/07, Gregory Brown <gregory...@gmail.com> wrote:
> On 5/3/07, AndyV <ar...@cornell.edu> wrote:
> >
> > Brilliant. That was it. Just looking at my code I knew something was
> > wrong. Thanks!
>
> The API docs are also silent on this, but I'll fix that today.

http://stonecode.svnrepository.com/ruport/trac.cgi/changeset/885

Minimal, but at least the yield args will show up properly in the docs now.

Timo Springmann

unread,
May 3, 2007, 11:15:52 AM5/3/07
to ruby-r...@googlegroups.com
Hi,

> The only thing that I did not like was
> that the pdf came down as a file download rather than displayed as a
> PDF in the browser.

try something like this:

send_data @activities.to_pdf, :type => "application/pdf", :disposition
=> 'inline'

Regards,
Timo

Reply all
Reply to author
Forward
0 new messages