-
-
Save rwilcox/1233059 to your computer and use it in GitHub Desktop.
# If the item has one comment, show it, else show the last comment and allow the user to expand | |
# (yes, a slightly contrived example) | |
def primary_comment | |
if self.comemnts.count > 1 | |
"<details class='primary_comment details'><summary><h3>" + self.comments.last.text + " (and #{self.comments.count - 1} others)" | |
"<ul>" | |
self.comments.each[0...-1] do |comment| | |
"<li>" + comment.text + "</li>" | |
end | |
else | |
"<span class='primary_url'>" + self.comments.last.text + "</span>" | |
end | |
end |
Very interesting. Thank you - I do like the idea of making the methods smaller. Still with the string concatenation, but that might be a good thing (Trying to shove ERB in the presenter might actually be a bad thing).
... and I figured out why I didn't like the string concatenation, Jim (@saturnflyer): it works great on Rails 2.x project. However, as soon as you're on a Rails 3 project you have to throw all kinds of .html_safe
calls everywhere and it gets impossible to use (too much thinking!).
I'll have to do more noodling.
... which I think I figured out how to make elegant. I'll write a blog post on same in the morning.
The cunning conclusion to this story can be found on my blog: http://rwilcox.tumblr.com/post/10546160404/presenter-pattern-rails-3-and-html-safe
You can write more methods to simplify it while opening up the possibility to reuse them elsewhere.