[LRUG] [Bulk] Best practice for reports
Daniel Lucraft
dan at fluentradical.com
Mon May 21 10:12:51 PDT 2007
Hi Paul,
It may help you to know that what you are calling a 'report' is called a
'Presenter' in the rails world, which should help you with googling.
Kudos for independently inventing a design pattern. :)
If you google for "ruby rails presenter" the top two articles by Jay
Fields do exactly what you do, and he seems to think it's a good idea.
Other people have talked about Presenters in the context of rails best
practice.
Personally I would only go this far if (1) I was extremely serious about
doing things right and testing properly or (2) the complexity/size of
the code made it look like a good idea.
Other people with more experience may feel different.
best,
Dan
Paul Doerwald wrote:
> Hi everyone,
>
> I'm asking this question in the interests of separating business logic
> from presentation. I have a standard reporting problem — I have a bunch
> of data in a database, and I want to dig through it, sort it, sum it,
> frob it, average it, and display it.
>
> The easiest way to do it is in the view:
>
> <th><%= f.name</th><td><%= Foo.count(:conditions => "blah") %></td>
>
> but then I've committed the heinous sin of putting the business
> (reporting!) logic into the view. I could avoid that by stuffing it into
> the controller, essentially mimicking the table structure, but in a hash
> of some sort:
>
> report = Hash.new
> report["Baz"] = { :name => f.name, :sum => Foo.count(:conditions =>
> "blah") }
>
> But I'm not convinced that I'm winning anything here.... plus the
> "Rails" way of doing things is to move the logic out of the controller
> into the model... but then what if I'm pulling data from half a dozen
> different tables? Presumably then I need to create a new
> (non-ActiveRecord) model, perhaps:
>
> class Report
> class self.main_report
> # create the hash here
> end
> end
>
> But that is just indirection another step away, so what I really should
> do is:
>
> class Report
> end
>
> class MainReport < Report
> attr_accessor :title, ...all the other fields I might want to use...
>
> def frob
> # prepare the report
> end
> end
>
> and then in the controller, instantiate the MainReport, and do the
> looping in the view... but now I've created a whole lot of work for
> myself to simply do what I could do with much less effort in the first
> instance.
>
> I suspect the last option is probably the best — at least of the ones
> I've presented — but I'm more interested in what others have read about
> or done which they feel is the "best practice" for reporting.
>
> Along this thread, has anyone used http://rubyreports.org/ ? Is it
> appropriate for me? I haven't looked closely yet, but am about to.
>
> Thanks,
> Paul.
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> chat mailing list
> chat at lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
More information about the Chat
mailing list