Optimizing the RedCloth Helper

In my previous post I posted several small Rails tips, one of which was a cleaner RedCloth helper. Unfortunately, this helper requires that the textile is parsed each time the page is loaded, and that can get nasty. So it our item is a text field called details, just add a details_html field to your model… then create a private conversion method that you call using a before_filter.

Something like:

before_filter :convert_details

def convert_details
  return if self.details.nil?
  self.details_html = RedCloth.new(self.details).to_html
end 

Then just display details_html in your view instead. This way the textile only gets converted when you save and make changes to it.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s