spec/integration/integration_spec.rb in mongoid-history-0.2.3 vs spec/integration/integration_spec.rb in mongoid-history-0.2.4
- old
+ new
@@ -57,13 +57,15 @@
class Tag
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::History::Trackable
+
+ belongs_to :updated_by, :class_name => "User"
field :title
- track_history :on => [:title], :scope => :post, :track_create => true, :track_destroy => true
+ track_history :on => [:title], :scope => :post, :track_create => true, :track_destroy => true, :modifier_field => :updated_by
end
end
before :each do
@user = User.create(:name => "Aaron", :email => "aaron@randomemail.com")
@@ -186,13 +188,12 @@
@post.history_tracks.last.association_chain.should == [{'id' => @post.id, 'name' => "Post"}]
end
it "should exclude defined options" do
@user.update_attributes(:name => "Aaron2", :email => "aaronsnewemail@randomemail.com")
- @user.history_tracks.first.modified.should == {
- "name" => "Aaron2"
- }
+ @user.history_tracks.first.modified.keys.should include "name"
+ @user.history_tracks.first.modified.keys.should_not include "email"
end
end
describe "on update non-embedded twice" do
it "should assign version on post" do
@@ -358,13 +359,20 @@
end
end
describe "embedded with cascading callbacks" do
before(:each) do
- @tag_foo = @post.tags.create(:title => "foo", :modifier => @user)
- @tag_bar = @post.tags.create(:title => "bar", :modifier => @user)
+ Mongoid.instantiate_observers
+ Thread.current[:mongoid_history_sweeper_controller] = self
+ self.stub!(:current_user).and_return @user
+ @tag_foo = @post.tags.create(:title => "foo", :updated_by => @user)
+ @tag_bar = @post.tags.create(:title => "bar")
end
+
+ after(:each) do
+ Thread.current[:mongoid_history_sweeper_controller] = nil
+ end
it "should have cascaded the creation callbacks and set timestamps" do
@tag_foo.created_at.should_not be_nil
@tag_foo.updated_at.should_not be_nil
end
@@ -379,9 +387,24 @@
@post.tags.count.should == 2
update_hash = { "post" => { "tags_attributes" => { "1234" => { "id" => @tag_bar.id, "title" => "baz", "_destroy" => "true"} } } }
@post.update_attributes(update_hash["post"])
@post.tags.count.should == 1
@post.history_tracks.last.action.should == "destroy"
+ end
+
+ it "should write relationship name for association_chain hiearchy instead of class name when using _destroy macro" do
+ update_hash = {"tags_attributes" => { "1234" => { "id" => @tag_foo.id, "_destroy" => "1"} } }
+ @post.update_attributes(update_hash)
+
+ # historically this would have evaluated to 'Tags' and an error would be thrown
+ # on any call that walked up the association_chain, e.g. 'trackable'
+ @tag_foo.history_tracks.last.association_chain.last["name"].should == "tags"
+ lambda{ @tag_foo.history_tracks.last.trackable }.should_not raise_error
+ end
+
+ it "should save modifier" do
+ @tag_foo.history_tracks.last.modifier.should eq @user
+ @tag_bar.history_tracks.last.modifier.should eq @user
end
end
describe "non-embedded" do
it "should undo changes" do