Permalink
Browse files

Upgraded to mongoid 2.2.0.

  • Loading branch information...
1 parent 1b1fa5d commit 9cac7a3dda8146e558bf50dfe326f13ad3f76025 @dblock dblock committed Aug 30, 2011
Showing with 55 additions and 52 deletions.
  1. +3 −2 Gemfile
  2. +1 −0 README.rdoc
  3. +10 −9 lib/mongoid/history/trackable.rb
  4. +40 −41 spec/integration/integration_spec.rb
  5. +1 −0 spec/trackable_spec.rb
View
@@ -1,7 +1,8 @@
source "http://rubygems.org"
-gem 'easy_diff'
-gem 'mongoid', '~> 2.0.0.rc.7'
+gem "easy_diff"
+gem "bson_ext", ">= 1.3.1"
+gem "mongoid", "~> 2.2"
group :development do
gem "rspec", "~> 2.3.0"
View
@@ -42,6 +42,7 @@ Here is a quick example on how to use this plugin. For more details, please look
:modifier_field => :modifier, # Adds "referened_in :modifier" to track who made the change. Default is :modifier
:version_field => :version, # Adds "field :version, :type => Integer" to track current version. Default is :version
:track_create => false, # Do you want to track document creation? Default is false
+ :track_update => true, # Do you want to track document updates? Default is true
:track_destroy => false, # Do you want to track document destruction? Default is false
end
@@ -12,6 +12,7 @@ def track_history(options={})
:version_field => :version,
:scope => model_name,
:track_create => false,
+ :track_update => true,
:track_destroy => false,
}
@@ -41,9 +42,9 @@ def track_history(options={})
delegate :history_trackable_options, :to => 'self.class'
delegate :track_history?, :to => 'self.class'
- before_update :track_update
+ before_update :track_update if options[:track_update]
before_create :track_create if options[:track_create]
- after_destroy :track_destroy if options[:track_destroy]
+ before_destroy :track_destroy if options[:track_destroy]
Mongoid::History.trackable_classes ||= []
Mongoid::History.trackable_classes << self
@@ -72,7 +73,7 @@ def track_history_flag
module InstanceMethods
def history_tracks
- @history_tracks ||= Mongoid::History.tracker_class.where(:scope => history_trackable_options[:scope], :association_chain => triverse_association_chain)
+ @history_tracks ||= Mongoid::History.tracker_class.where(:scope => history_trackable_options[:scope], :association_chain => traverse_association_chain)
end
# undo :from => 1, :to => 5
@@ -81,7 +82,7 @@ def history_tracks
def undo!(modifier, options_or_version=nil)
versions = get_versions_criteria(options_or_version).to_a
versions.sort!{|v1, v2| v2.version <=> v1.version}
-
+
versions.each do |v|
undo_attr = v.undo_attr(modifier)
self.attributes = v.undo_attr(modifier)
@@ -115,7 +116,7 @@ def get_versions_criteria(options_or_version)
end
else
options_or_version = options_or_version.to_a if options_or_version.is_a?(Range)
- version = options_or_version || self.attributes[history_trackable_options[:version_field]]
+ version = options_or_version || self.attributes[history_trackable_options[:version_field].to_s]
version = [ version ].flatten
versions = history_tracks.where(:version.in => version)
end
@@ -126,8 +127,8 @@ def should_track_update?
track_history? && !modified_attributes_for_update.blank?
end
- def triverse_association_chain(node=self)
- list = node._parent ? triverse_association_chain(node._parent) : []
+ def traverse_association_chain(node=self)
+ list = node._parent ? traverse_association_chain(node._parent) : []
list << { 'name' => node.class.name, 'id' => node.id }
list
end
@@ -165,9 +166,9 @@ def modified_attributes_for_destroy
def history_tracker_attributes(method)
return @history_tracker_attributes if @history_tracker_attributes
-
+
@history_tracker_attributes = {
- :association_chain => triverse_association_chain,
+ :association_chain => traverse_association_chain,
:scope => history_trackable_options[:scope],
:modifier => send(history_trackable_options[:modifier_field])
}
@@ -105,62 +105,62 @@ class User
describe "on update non-embedded" do
it "should create a history track if changed attributes match tracked attributes" do
lambda {
- @post.update_attributes(:title => "Another Test")
+ @post.update_attributes!(:title => "Another Test")
}.should change(HistoryTracker, :count).by(1)
end
it "should not create a history track if changed attributes do not match tracked attributes" do
lambda {
- @post.update_attributes(:rating => "untracked")
+ @post.update_attributes!(:rating => "untracked")
}.should change(HistoryTracker, :count).by(0)
end
it "should assign modified fields" do
- @post.update_attributes(:title => "Another Test")
+ @post.update_attributes!(:title => "Another Test")
@post.history_tracks.first.modified.should == {
"title" => "Another Test"
}
end
it "should assign method field" do
- @post.update_attributes(:title => "Another Test")
+ @post.update_attributes!(:title => "Another Test")
@post.history_tracks.first.action.should == "update"
end
it "should assign original fields" do
- @post.update_attributes(:title => "Another Test")
+ @post.update_attributes!(:title => "Another Test")
@post.history_tracks.first.original.should == {
"title" => "Test"
}
end
it "should assign modifier" do
- @post.update_attributes(:title => "Another Test")
+ @post.update_attributes!(:title => "Another Test")
@post.history_tracks.first.modifier.should == @user
end
it "should assign version on history tracks" do
- @post.update_attributes(:title => "Another Test")
+ @post.update_attributes!(:title => "Another Test")
@post.history_tracks.first.version.should == 1
end
it "should assign version on post" do
- @post.update_attributes(:title => "Another Test")
+ @post.update_attributes!(:title => "Another Test")
@post.version.should == 1
end
it "should assign scope" do
- @post.update_attributes(:title => "Another Test")
+ @post.update_attributes!(:title => "Another Test")
@post.history_tracks.first.scope == "Post"
end
it "should assign association_chain" do
- @post.update_attributes(:title => "Another Test")
+ @post.update_attributes!(:title => "Another Test")
@post.history_tracks.first.association_chain = [{:id => @post.id, :name => "Post"}]
end
it "should exclude defined options" do
- @user.update_attributes(:name => "Aaron2", :email => "aaronsnewemail@randomemail.com")
+ @user.update_attributes!(:name => "Aaron2", :email => "aaronsnewemail@randomemail.com")
@user.history_tracks.first.modified.should == {
"name" => "Aaron2"
}
@@ -169,81 +169,81 @@ class User
describe "on update non-embedded twice" do
it "should assign version on post" do
- @post.update_attributes(:title => "Test2")
- @post.update_attributes(:title => "Test3")
+ @post.update_attributes!(:title => "Test2")
+ @post.update_attributes!(:title => "Test3")
@post.version.should == 2
end
it "should create a history track if changed attributes match tracked attributes" do
lambda {
- @post.update_attributes(:title => "Test2")
- @post.update_attributes(:title => "Test3")
+ @post.update_attributes!(:title => "Test2")
+ @post.update_attributes!(:title => "Test3")
}.should change(HistoryTracker, :count).by(2)
end
it "should create a history track of version 2" do
- @post.update_attributes(:title => "Test2")
- @post.update_attributes(:title => "Test3")
+ @post.update_attributes!(:title => "Test2")
+ @post.update_attributes!(:title => "Test3")
@post.history_tracks.where(:version => 2).first.should_not be_nil
end
it "should assign modified fields" do
- @post.update_attributes(:title => "Test2")
- @post.update_attributes(:title => "Test3")
+ @post.update_attributes!(:title => "Test2")
+ @post.update_attributes!(:title => "Test3")
@post.history_tracks.where(:version => 2).first.modified.should == {
"title" => "Test3"
}
end
it "should assign original fields" do
- @post.update_attributes(:title => "Test2")
- @post.update_attributes(:title => "Test3")
+ @post.update_attributes!(:title => "Test2")
+ @post.update_attributes!(:title => "Test3")
@post.history_tracks.where(:version => 2).first.original.should == {
"title" => "Test2"
}
end
it "should assign modifier" do
- @post.update_attributes(:title => "Another Test", :modifier => @another_user)
+ @post.update_attributes!(:title => "Another Test", :modifier => @another_user)
@post.history_tracks.first.modifier.should == @another_user
end
end
describe "on update embedded" do
it "should assign version on comment" do
- @comment.update_attributes(:title => "Test2")
+ @comment.update_attributes!(:title => "Test2")
@comment.version.should == 2 # first track generated on creation
end
it "should create a history track of version 2" do
- @comment.update_attributes(:title => "Test2")
+ @comment.update_attributes!(:title => "Test2")
@comment.history_tracks.where(:version => 2).first.should_not be_nil
end
it "should assign modified fields" do
- @comment.update_attributes(:title => "Test2")
+ @comment.update_attributes!(:title => "Test2")
@comment.history_tracks.where(:version => 2).first.modified.should == {
"title" => "Test2"
}
end
it "should assign original fields" do
- @comment.update_attributes(:title => "Test2")
+ @comment.update_attributes!(:title => "Test2")
@comment.history_tracks.where(:version => 2).first.original.should == {
"title" => "test"
}
end
it "should assign modifier" do
- @post.update_attributes(:title => "Another Test", :modifier => @another_user)
+ @post.update_attributes!(:title => "Another Test", :modifier => @another_user)
@post.history_tracks.first.modifier.should == @another_user
end
end
describe "non-embedded" do
it "should undo changes" do
- @post.update_attributes(:title => "Test2")
+ @post.update_attributes!(:title => "Test2")
@post.history_tracks.where(:version => 1).first.undo!(@user)
@post.reload
@post.title.should == "Test"
@@ -256,21 +256,21 @@ class User
end
it "should create a new history track after undo" do
- @post.update_attributes(:title => "Test2")
+ @post.update_attributes!(:title => "Test2")
@post.history_tracks.where(:version => 1).first.undo!(@user)
@post.reload
@post.history_tracks.count.should == 2
end
it "should assign @user as the modifier of the newly created history track" do
- @post.update_attributes(:title => "Test2")
+ @post.update_attributes!(:title => "Test2")
@post.history_tracks.where(:version => 1).first.undo!(@user)
@post.reload
@post.history_tracks.where(:version => 2).first.modifier.should == @user
end
it "should stay the same after undo and redo" do
- @post.update_attributes(:title => "Test2")
+ @post.update_attributes!(:title => "Test2")
@track = @post.history_tracks.where(:version => 1).first
@track.undo!(@user)
@track.redo!(@user)
@@ -291,7 +291,7 @@ class User
describe "embedded" do
it "should undo changes" do
- @comment.update_attributes(:title => "Test2")
+ @comment.update_attributes!(:title => "Test2")
@comment.history_tracks.where(:version => 2).first.undo!(@user)
# reloading an embedded document === KAMIKAZE
# at least for the current release of mongoid...
@@ -301,23 +301,23 @@ class User
end
it "should create a new history track after undo" do
- @comment.update_attributes(:title => "Test2")
+ @comment.update_attributes!(:title => "Test2")
@comment.history_tracks.where(:version => 2).first.undo!(@user)
@post.reload
@comment = @post.comments.first
@comment.history_tracks.count.should == 3
end
it "should assign @user as the modifier of the newly created history track" do
- @comment.update_attributes(:title => "Test2")
+ @comment.update_attributes!(:title => "Test2")
@comment.history_tracks.where(:version => 2).first.undo!(@user)
@post.reload
@comment = @post.comments.first
@comment.history_tracks.where(:version => 3).first.modifier.should == @user
end
it "should stay the same after undo and redo" do
- @comment.update_attributes(:title => "Test2")
+ @comment.update_attributes!(:title => "Test2")
@track = @comment.history_tracks.where(:version => 2).first
@track.undo!(@user)
@track.redo!(@user)
@@ -330,9 +330,9 @@ class User
describe "trackables" do
before :each do
- @comment.update_attributes(:title => "Test2") # version == 2
- @comment.update_attributes(:title => "Test3") # version == 3
- @comment.update_attributes(:title => "Test4") # version == 4
+ @comment.update_attributes!(:title => "Test2") # version == 2
+ @comment.update_attributes!(:title => "Test3") # version == 3
+ @comment.update_attributes!(:title => "Test4") # version == 4
end
describe "undo" do
@@ -342,8 +342,7 @@ class User
end
it "should recognize parameter as version number" do
- @comment.undo! @user, 3
-
+ @comment.undo! @user, 3
@comment.title.should == "Test2"
end
@@ -360,7 +359,7 @@ class User
describe "redo" do
before :each do
- @comment.update_attributes(:title => "Test5")
+ @comment.update_attributes!(:title => "Test5")
end
it "should recognize :from, :to options" do
View
@@ -44,6 +44,7 @@ class MyModel
:scope => :my_model,
:except => ["created_at", "updated_at", "version", "modifier_id", "_id", "id"],
:track_create => false,
+ :track_update => true,
:track_destroy => false,
}
end

0 comments on commit 9cac7a3

Please sign in to comment.