Acts As Blog Plugin

The Ruby on Rails plugin acts_as_blog does not make your ActiveRecord model into a blog… The acts_as_blog plugin helps your model by converting simple text to HTML formatted text. You can use acts_as_blog in blogs, as the name alludes to, or in a wiki-like system. Acts As Blog currently supports text-to-HTML formats Textfile via the RedCloth gem, Markdown via the BlueCloth gem, and SmartyPants via the RubyPants gem. You need to install any one, or all, of these gems before you use this plugin.

gem install RedCloth
gem install BlueCloth
gem install RubyPants

To install the plugin just run this script:

script/plugin install http://svn.recentrambles.com/plugins/acts_as_blog

In your model you need to call the acts_as_blog method just like any other ActiveRecord plugin. For this plugin you will also need to do some additional processing before the model is persisted into the database.

class Post < ActiveRecord::Base
  acts_as_blog

  before_save :txt_to_html

  def txt_to_html
    self.post = Post.convert_to_html(self.raw_post, 'textile')
  end
end

You can switch ‘markdown’ or ‘smartypants’ as the text-to-HTML format in the convert_to_html method by replacing ‘textile’. Also notice that in the txt_to_html method a field raw_post is converted to HTML and copied to the field post. If you use code similar to this you will need a raw_post and a post column in your database posts table. The data in the post column of the posts database will have the HTML formatted text while the raw_post column will keep the original unformatted data. Using the acts_as_blog plugin in this fashion you will stash the model data and the presentation data in the database.

You can also use BlueCloth, RedCloth, and/or RubyPants without using this plugin. Personally, I would store the raw text in database and only convert it to HTML in the view without using the plugin.

<%= BlueCloth.new("### HELLO\nTECHKNOW\n").to_html %>

I recommend you use a helper function that will do the conversion so that you can switch between the different text-to-HTML formats more easily.

Technorati Tags: , , , , , , , , ,

Related posts:

  1. Acts As Versioned Plugin
  2. Acts As Taggable Plugin
  3. Acts As Voteable Rails Plugin
  4. Acts As Commentable Plugin
  5. Acts As Bookmarkable Plugin
  6. Acts As Rateable Plugin


2 Responses to “Acts As Blog Plugin”

  • Sonny Says:

    > Personally, I would store the raw text in database
    > and only convert it to HTML in the view without
    > using the plugin.

    I was thinking the same thought… under what circumstances have you found the plugin useful?

  • Brian Says:

    We are storing the raw_text. but are now considering storing the html. The reason. Whey convert the raw text and unlimited number of times to html on the fly and put the extra load on the server, when you can convert it one time going into the database and then pull it all you want.. We haven’t switched yet, but we are considering.

Leave a Reply