Updating A Rails Plugin

I received an email asking me how to best enhance the comment model from the Acts as Commentable plugin. It seems like a user of the plugin wants to add some rails validation to the comment model class. I have to admit that I would have just edited the plugin code in place, in fact this is what I have done in the past. I didn’t want to brush off the question because it seems like a legitimate concern: how to add validation and methods to models provided by Ruby on Rails plugins? Well, the answer is easy because in Ruby a class’ implementation can be reopened at any time. In this case we have to open the Comment class and evaluate new code:

Comment.class_eval do
def my_new_method
# do something
end
# do something else
end

For this to work we need to place the above code in the application_helper.rb file.

Technorati Tags: , , , , ,

Related posts:

  1. Rails Plugin Tutorial
  2. Acts As Commentable Plugin
  3. Comments on Acts As Commentable
  4. Acts As Voteable Rails Plugin
  5. Top 11 Rails Plugins

This entry was posted in Ruby, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*