Generate Report

7 views
Skip to first unread message

Rodney Jan Mandap

unread,
Oct 1, 2015, 11:07:44 AM10/1/15
to Ruby on Rails: Talk
Hello guys, I'm 100% newbie in RoR. I have a question, how can I generate report in RoR like crystal report?

Walther Diechmann

unread,
Oct 1, 2015, 12:34:32 PM10/1/15
to Ruby

that’s a nice broad question - so I’ll give you a broad answer (the very MVP with lots of room for optimization)

:)

(and I’ve probably left one or two BIG mistakes - and my apologies for that)

cheers
Walther


—— here goes:

if you by report mean a list of, say accounts, each listing eg. transactions like this

sales inland
q1 - transactions
1/1/15 burning red 1,500.00
1/3/15 smoothers 252.75
q2 - transactions
4/21/15 broke back 975.00

I’d make myself a report action in the accounts controller (like /accounts/report) or use the :index action and supply an argument when you call it (like /accounts?report=true) or construct a special ‘decorator’-kind of controller (like ReportsController) and call it like /reports?accounts=true

Your controller (let’s stick with the ReportsController for now)

def accounts
@collection = Account.all.includes :transactions
end


Add a route in routes.rb

ressources :reports do
collection do
get :accounts
end
end

In your views (ie app/views/reports) I would build the report in account.html.haml

%table
%thead
%tr
%th t(‘report.account.quarter')
%th= t(‘report.account.transaction’)
%tbody
= @collection.each do |account|
= render partial: ‘account’, locals: { account: account }

Each account gets built in app/views/accounts/_accoount.html.haml

%tr
%td= account.title
%td

- quarter = 1
- account.transactions.sort(:transaction_date) do |transaction|
- q = find_quarter(transaction)
- if q > quarter
- quarter = q
%tr
%td.quarter= “q%s - transactions” % say_quarter transaction
%td
%table.transactions
%tr
%td.transaction-date= transaction.transaction_date
%td.transaction-text= transaction.description
- if transaction.amount < 0
%td
%td.transaction-amount= transaction.amount
- else
%td{ colspan: ‘2’}.transaction-amount= transaction.amount

Then you could order it with

= link_to t(‘report.on-accounts’), “#!”, data: { url: "<on of the url’s listed above>”}, class: “report”

And in the bottom of your index.html.haml (or what ever your page with the above link is) you’d put

:coffeescript

$ ->
$(document.body).on ‘click’, ‘.report’, (e) ->
elem = $(e.currentTarget)
window.open elm.data(‘url’)


> Den 01/10/2015 kl. 17.03 skrev Rodney Jan Mandap <rodneyja...@gmail.com>:
>
> Hello guys, I'm 100% newbie in RoR. I have a question, how can I generate report in RoR like crystal report?
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9ff5a893-0b2f-4ce9-9c4e-98f7fd8fc02a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> Denne besked er blevet skannet af
> ALCO Stopspam, og menes at være fri for vira og spam.
> Klik her for at rapportere denne besked som spam.

Anthony McDonald

unread,
Oct 1, 2015, 12:41:32 PM10/1/15
to Ruby on Rails: Talk
Hey Rodney,

I've been using RoR for a couple months (still quite green). You should look for a gem to do what you're looking for, start looking at https://rubygems.org/

Onward.

Andrey Nering

unread,
Oct 1, 2015, 1:56:00 PM10/1/15
to Ruby on Rails: Talk
A common solution is generating the report in HTML and converting it to PDF with these gems:


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Andrey Nering
Skype: andrey.nering

James Jelinek

unread,
Oct 1, 2015, 2:19:45 PM10/1/15
to rubyonra...@googlegroups.com
+1 on this!  I’ve used Wicked and it worked out really well.  I’ve also used prawn to generate PDF reports as well but I sort of lean towards WickedPDF.

Reply all
Reply to author
Forward
0 new messages