has_versions is a simple plugin to version ActiveRecord objects. The versioned data is saved using Marshal. You can specify what attributes are going to be versioned and if versioning is automatically or not.
-
Install the plugin with
script/plugin install git://github.com/fnando/has_versions.git
-
Generate a migration with
script/generate migration create_versions
and add the following code:class CreateVersions < ActiveRecord::Migration def self.up create_table :versions do |t| t.references :versionable, :polymorphic => true t.references :user t.binary :data t.datetime :created_at t.integer :version, :default => 0, :null => false end
add_index :versions, :versionable_type add_index :versions, :versionable_id add_index :versions, :user_id add_index :versions, :version
end
def self.down drop_table :versions end end
-
Add the column
version
to all models you want to be versioned; first create the migration:script/generate migration add_version_support_to_posts
Then add the following code:
class AddVersionSupportToPosts < ActiveRecord::Migration
def self.up
change_table :posts do |t|
t.integer :version, :default => 0, :null => false
end
end
def self.down
remove_column :posts, :version
end
end
- Run the migrations with
rake db:migrate
-
Add the association to your User model
class User < ActiveRecord::Base has_many :versions, :dependent => :nullify end
-
Add the method call
has_comments
to your model.class Post < ActiveRecord::Base has_versions end
post = Post.first user = User.first
post.create_version
post.create_version!
post.version
post.versions.current
post.versions.get(3)
post.versions.revert_to(1)
post.versions.revert_to!(100)
post.save_without_version # => same as post.save post.save_without_version(false) # => same as post.save(false) post.save_without_version! # => post.save!
post.version_author = user
post.version_author
post.versioned?
post.save_without_version?
post.versioned_attributes
class Post < ActiveRecord::Base has_versions
def create_version? false end end
has_versions :attributes => :all has_versions :attributes => :content has_versions :attributes => %w(content excerpt) has_versions :auto => false has_versions :except => :formatted_content
What about displaying a diff from two different versions? Well, you can use
the helper method diff
.
<%= diff @newer.data['content'], @newer.data['content'] %>
Use this CSS to display some formatted text:
.diff ins {
background: #baffa6;
text-decoration: none;
}
.diff del {
background: #f88;
text-decoration: none;
}
NOTE: You should have an User model. Otherwise, this won't work!
Create view helpers to display formatted version diffs
- Nando Vieira (http://simplesideias.com.br)
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.