Created
September 21, 2011 19:33
-
-
Save rwilcox/1233059 to your computer and use it in GitHub Desktop.
presenter_pattern_gone_wrong?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
Author
Author
... 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.
Author
... which I think I figured out how to make elegant. I'll write a blog post on same in the morning.
Author
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).