Skip to content

Instantly share code, notes, and snippets.

@mihar
Created August 5, 2009 14:57
Show Gist options
  • Save mihar/162741 to your computer and use it in GitHub Desktop.
Save mihar/162741 to your computer and use it in GitHub Desktop.
### 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
### 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