Riff Rails Plugin

Riff is a handy diff tool for ActiveRecord models. The Riff plugin adds just three methods to the ActiveRecord class, diff?, diff, and diff_against_attributes. You install Riff like any other plugin using the script/plugin install command:

script/plugin install http://tfletcher.com/svn/rails-plugins/riff/

Once installed all you have to do to use riff is to create model records.

firstPost = Post.new(:title => 'Rails Plugin')
secondPost = Post.new(:title => 'Plugin Rails')

To check if two objects are different from each other you can use the diff? method.

firstPost.diff?(secondPost) # true

To check what the diferences are use the diff method. The differences are returned as a hash of attributes with the two distinct values for the attribute as an array. Let’s see if the code can explain this a bit further:

diff_hash = one.diff(two)
diff_hash.each { |key, value|
  # value is an array
  value.each { |diff_value|
    logger << "\n + #{diff_value}"
  }
}

The key for the diff_hash is a symbol for the attribute that differs.

You can also diff a model instance against what is saved in the database by using the diff? and diff methods without a parameter. Here is an example of that.

post = Post.find(params[:id])
post.title = 'Rails Rocks'
if post.diff?
  # post is diff dan that in da db
end

Technorati Tags: , , , , , ,

Related posts:

  1. Simply Helpful Rails Plugin
  2. Acts As Voteable Rails Plugin
  3. Top 11 Rails Plugins
  4. Graphs Rails Plugin
  5. Updating A Rails Plugin
  6. Rails PDF Plugin


One Response to “Riff Rails Plugin”

Leave a Reply