spec/integration/integration_spec.rb in mongoid-history-0.4.7 vs spec/integration/integration_spec.rb in mongoid-history-0.5.0

- old
+ new

@@ -73,24 +73,24 @@ @persisted_history_options = Mongoid::History.trackable_class_options end before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options } - let(:user) { User.create(name: 'Aaron', email: 'aaron@randomemail.com', aliases: ['bob'], country: 'Canada', city: 'Toronto', address: '21 Jump Street') } - let(:another_user) { User.create(name: 'Another Guy', email: 'anotherguy@randomemail.com') } - let(:post) { Post.create(title: 'Test', body: 'Post', modifier: user, views: 100) } - let(:comment) { post.comments.create(title: 'test', body: 'comment', modifier: user) } - let(:tag) { Tag.create(title: 'test') } + let(:user) { User.create!(name: 'Aaron', email: 'aaron@randomemail.com', aliases: ['bob'], country: 'Canada', city: 'Toronto', address: '21 Jump Street') } + let(:another_user) { User.create!(name: 'Another Guy', email: 'anotherguy@randomemail.com') } + let(:post) { Post.create!(title: 'Test', body: 'Post', modifier: user, views: 100) } + let(:comment) { post.comments.create!(title: 'test', body: 'comment', modifier: user) } + let(:tag) { Tag.create!(title: 'test') } describe 'track' do describe 'on creation' do it 'should have one history track in comment' do expect(comment.history_tracks.count).to eq(1) end it 'should assign title and body on modified' do - expect(comment.history_tracks.first.modified).to eq('t' => 'test', 'body' => 'comment') + expect(comment.history_tracks.first.modified).to eq('t' => 'test', 'body' => 'comment') end it 'should not assign title and body on original' do expect(comment.history_tracks.first.original).to eq({}) end @@ -210,11 +210,11 @@ user.history_tracks.first.undo! nil expect(user.reload.name).to eq(name) end it 'should undo non-existing field changes' do - post = Post.create(modifier: user, views: 100) + post = Post.create!(modifier: user, views: 100) expect(post.reload.title).to be_nil post.update_attributes(title: 'Aaron2') expect(post.reload.title).to eq('Aaron2') post.history_tracks.first.undo! nil expect(post.reload.title).to be_nil @@ -500,14 +500,13 @@ expect(post.comments.count).to eq(2) end end describe 'embedded with cascading callbacks' do + let(:tag_foo) { post.tags.create!(title: 'foo', updated_by: user) } + let(:tag_bar) { post.tags.create!(title: 'bar') } - let(:tag_foo) { post.tags.create(title: 'foo', updated_by: user) } - let(:tag_bar) { post.tags.create(title: 'bar') } - # it "should have cascaded the creation callbacks and set timestamps" do # tag_foo; tag_bar # initialize # tag_foo.created_at.should_not be_nil # tag_foo.updated_at.should_not be_nil # end @@ -625,11 +624,11 @@ comment.update_attributes!(title: 'Test3') # version == 3 comment.update_attributes!(title: 'Test4') # version == 4 end describe 'undo' do - { 'undo' => [nil], 'undo!' => [nil, :reload] }.each do |test_method, methods| + { 'undo' => [nil], 'undo!' => [nil, :reload] }.each do |test_method, methods| methods.each do |method| context "#{method || 'instance'}" do it 'recognizes :from, :to options' do comment.send test_method, user, from: 4, to: 2 comment.send(method) if method @@ -652,11 +651,11 @@ comment.send test_method, user, last: 2 comment.send(method) if method expect(comment.title).to eq('Test2') end - if Mongoid::History.mongoid3? + if Mongoid::Compatibility::Version.mongoid3? context 'protected attributes' do before :each do Comment.attr_accessible(nil) end @@ -711,11 +710,11 @@ comment.redo! user, last: 1 comment.send(method) if method expect(comment.title).to eq('Test5') end - if Mongoid::History.mongoid3? + if Mongoid::Compatibility::Version.mongoid3? context 'protected attributes' do before :each do Comment.attr_accessible(nil) end @@ -736,11 +735,10 @@ end end end end end - end end describe 'localized fields' do before :each do @@ -752,11 +750,11 @@ track_history on: [:flavour], track_destroy: true end end it 'should correctly undo and redo' do if Sausage.respond_to?(:localized_fields) - sausage = Sausage.create(flavour_translations: { 'en' => 'Apple', 'nl' => 'Appel' }) + sausage = Sausage.create!(flavour_translations: { 'en' => 'Apple', 'nl' => 'Appel' }) sausage.update_attributes(flavour: 'Guinness') track = sausage.history_tracks.last track.undo! user @@ -775,11 +773,11 @@ describe 'embedded with a polymorphic trackable' do let(:foo) { Foo.new(title: 'a title', body: 'a body') } before :each do post.comments << foo - post.save + post.save! end it 'should assign interface name in association chain' do foo.update_attribute(:body, 'a changed body') expected_root = { 'name' => 'Post', 'id' => post.id } expected_node = { 'name' => 'coms', 'id' => foo.id } @@ -826,11 +824,10 @@ default_scope -> { where(title: nil) } end end describe 'post' do - it 'should correctly undo and redo' do post.update_attributes(title: 'a new title') track = post.history_tracks.last track.undo! user expect(post.reload.title).to eq('Test') @@ -897,9 +894,32 @@ track = tag.history_tracks.last track.undo! user track.redo! user expect(tag.reload.title).to eq('testing') end + end + end + + describe 'overriden changes_method with additional fields' do + before :each do + class OverriddenChangesMethod + include Mongoid::Document + include Mongoid::History::Trackable + + track_history on: [:foo], changes_method: :my_changes + + def my_changes + { foo: %w(bar baz) } + end + end + end + + it 'should add foo to the changes history' do + o = OverriddenChangesMethod.create + o.save! + track = o.history_tracks.last + expect(track.modified).to eq('foo' => 'baz') + expect(track.original).to eq('foo' => 'bar') end end end end