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. Acts As Voteable Rails Plugin
  2. Simply Helpful Rails Plugin
  3. Acts As Blog Plugin
  4. Graphs Rails Plugin
  5. Rails Plugin Tutorial

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

One Trackback

  1. [...] Riff Active Record diff plugin di Pull Monkey huh dasar monyet!, keterangan soal riff plugin di wikinya rails ah ternyata lagi lagi ke wiki rails juga ya, trus sama ini postingan di blognya si juixe [...]

Post a Comment

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

*
*