Created
August 5, 2009 14:57
-
-
Save mihar/162741 to your computer and use it in GitHub Desktop.
This file contains 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
### Inside the helper file that uses it. | |
class Spitter | |
include ActionView::Helpers::TagHelper | |
include ActionView::Helpers::TextHelper | |
include ActionView::Helpers::UrlHelper | |
def initialize(klass) | |
@klass = klass | |
end | |
def method_missing(field, tag = :p) | |
content = @klass.send(field) | |
# Don't escape HTML for DB stored textilized strings. Just reset the tag to DIV. | |
unless tag == :textilized | |
content = h(content) | |
else | |
tag = :div | |
end | |
unless !content or content.blank? or content.nil? | |
# Special cases like email and web links. | |
case field | |
when :email | |
link_to content, "mailto:#{content}", :class => field | |
when :url, :www | |
link_to content, content, :class => field | |
else | |
# Textilize content marked and reset the tag to DIV. | |
if tag == :textilize | |
content = textilize(content) | |
tag = :div | |
end | |
# Output any content. | |
content_tag tag.to_sym, content, :class => field | |
end | |
else | |
"" | |
end | |
end | |
end | |
### Goes into your helper module. | |
def spit(klass, field = '', tag = :p) | |
s = Spitter.new(klass) | |
if block_given? | |
yield s | |
else | |
s.send field, tag | |
end | |
end |
This file contains 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
### View in HAML. | |
- spit @organization do |o| | |
= o.title | |
= o.hours | |
= o.full_address | |
= o.email | |
= o.url | |
= o.description_summary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment