タグのバージョニング

acts_as_versionedは、モデルのバージョニングを手軽に実現できるプラグイン
acts_as_taggable_on_steroidsは、タグを手軽に実現できるプラグイン
併用したとき、モデルのバージョンを戻すと同時にタグもその時点のものに戻したいと思うのが人情というもの。

方法

acts_as_taggable_on_steroidsのタグリストのキャッシュ機能を利用すれば簡単に実装できる。

試した環境

Rails 2.0.2(Rails 2.1用のacts_as_versionedはまだ試したことない)

コード

class Item < ActiveRecord::Base
  acts_as_versioned
  acts_as_taggable

  # revert_toで、cached_tag_listからtag_listを復元する
  # (cached_tag_listがないとダメだよ)
  alias :revert_to_without_taggable :revert_to
  def revert_to_with_taggable(version)
    if revert_to_without_taggable(version)
      self.tag_list = cached_tag_list
      true
    else
      false
    end
  end
  alias :revert_to :revert_to_with_taggable
end