[LRUG] Best practice for reports
Tom Armitage
tom at infovore.org
Mon May 21 23:53:27 PDT 2007
Just to come back to this:
> <th><%= f.name</th><td><%= Foo.count(:conditions => "blah") %></td>
I'm not sure that's so heinous, given what it looks like you're doing
could be abstracted away into a class method on Foo. I think putting
a :conditions kv pair inside the view is ugly, and puts the logic in
the wrong place, but there's nothing to stop you doing something like
this (if, say, you want a report on how many articles published this
month:)
class Article
def self.count_this_month
find (:conditions => "blah")
end
end
and then calling Article.count_this_month in the view. Yes, strictly,
it's still an AR call in a view, but you've kept the business logic
in the model where it belongs, and the view just contains a method
name. Also: it's a method call related to all "Articles", rather than
a particular one - I think class methods on a view are definitely
less smelly than business logic on individual instances of a class in
a view. I've worked on various projects where we've ended up doing
this - writing appropriate class methods to solve such a problem -
and it then, of course, means your class methods are reprposable.
It also smells less bad than your original, but avoids having to go
down fiddly pattern-based routes. Thoughts?
---
Tom Armitage
tom at infovore.org
07813 060578
More information about the Chat
mailing list