Skip to content

Commit

Permalink
Use defaults in specs and make specs not order-dependent. Closes #222.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jun 21, 2018
1 parent c2e0bc0 commit b391da6
Show file tree
Hide file tree
Showing 24 changed files with 1,191 additions and 1,025 deletions.
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-06-19 17:36:18 -0400 using RuboCop version 0.48.1.
# on 2018-06-20 23:31:54 -0400 using RuboCop version 0.48.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -36,10 +36,10 @@ Lint/ParenthesesAsGroupedExpression:
Metrics/AbcSize:
Max: 52

# Offense count: 112
# Offense count: 117
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 807
Max: 820

# Offense count: 1
# Configuration parameters: CountComments.
Expand All @@ -50,7 +50,7 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 13

# Offense count: 478
# Offense count: 392
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Expand Down
12 changes: 8 additions & 4 deletions lib/mongoid/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ def default_settings
def trackable_class_settings(trackable_class)
trackable_settings[trackable_class.name.to_sym] || default_settings
end

def reset!
Mongoid::History.modifier_class_name = 'User'
Mongoid::History.trackable_class_options = {}
Mongoid::History.trackable_settings = {}
Mongoid::History.current_user_method ||= :current_user
end
end
end
end

Mongoid::History.modifier_class_name = 'User'
Mongoid::History.trackable_class_options = {}
Mongoid::History.trackable_settings = {}
Mongoid::History.current_user_method ||= :current_user
Mongoid::History.reset!
79 changes: 26 additions & 53 deletions spec/integration/embedded_in_polymorphic_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
require 'spec_helper'

describe Mongoid::History::Tracker do
before :all do
before :each do
class RealState
include Mongoid::Document
include Mongoid::History::Trackable

field :name, type: String
belongs_to :user
embeds_one :address, class_name: 'Contact', as: :contactable
embeds_one :embone, as: :embedable

track_history on: :all, # track title and body fields only, default is :all
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
modifier_field_optional: true,
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
track_create: true, # track document creation, default is false
track_update: true, # track document updates, default is true
track_destroy: false # track document destruction, default is false
embeds_one :embone, as: :embedable

track_history
end

class Company
Expand All @@ -30,13 +24,7 @@ class Company
embeds_one :second_address, class_name: 'Contact', as: :contactable
embeds_one :embone, as: :embedable

track_history on: :all, # track title and body fields only, default is :all
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
modifier_field_optional: true,
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
track_create: true, # track document creation, default is false
track_update: true, # track document updates, default is true
track_destroy: false # track document destruction, default is false
track_history
end

class Embone
Expand All @@ -46,14 +34,7 @@ class Embone
field :name
embedded_in :embedable, polymorphic: true

track_history on: :all, # track title and body fields only, default is :all
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
modifier_field_optional: true,
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
track_create: true, # track document creation, default is false
track_update: true, # track document updates, default is true
track_destroy: false, # track document destruction, default is false
scope: :embedable
track_history scope: :embedable
end

class Contact
Expand All @@ -65,14 +46,7 @@ class Contact
field :state
embedded_in :contactable, polymorphic: true

track_history on: :all, # track title and body fields only, default is :all
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
modifier_field_optional: true,
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
track_create: true, # track document creation, default is false
track_update: true, # track document updates, default is true
track_destroy: false, # track document destruction, default is false
scope: %i[real_state company]
track_history scope: %i[real_state company]
end

class User
Expand All @@ -82,58 +56,57 @@ class User
end
end

it 'tracks history for nested embedded documents with polymorphic relations' do
user = User.new
user.save!
after :each do
Object.send(:remove_const, :RealState)
Object.send(:remove_const, :Company)
Object.send(:remove_const, :Embone)
Object.send(:remove_const, :Contact)
Object.send(:remove_const, :User)
end

real_state = user.real_states.build(name: 'rs_name')
let!(:user) { User.create! }

it 'tracks history for nested embedded documents with polymorphic relations' do
real_state = user.real_states.build(name: 'rs_name', modifier: user)
real_state.save!
real_state.build_address(address: 'Main Street #123', city: 'Highland Park', state: 'IL').save!
real_state.build_address(address: 'Main Street #123', city: 'Highland Park', state: 'IL', modifier: user).save!
expect(real_state.history_tracks.count).to eq(2)
expect(real_state.address.history_tracks.count).to eq(1)

real_state.reload
real_state.address.update_attribute(:address, 'Second Street')
real_state.address.update_attributes!(address: 'Second Street', modifier: user)
expect(real_state.history_tracks.count).to eq(3)
expect(real_state.address.history_tracks.count).to eq(2)
expect(real_state.history_tracks.last.action).to eq('update')

real_state.build_embone(name: 'Lorem ipsum').save!
real_state.build_embone(name: 'Lorem ipsum', modifier: user).save!
expect(real_state.history_tracks.count).to eq(4)
expect(real_state.embone.history_tracks.count).to eq(1)
expect(real_state.embone.history_tracks.last.action).to eq('create')
expect(real_state.embone.history_tracks.last.association_chain.last['name']).to eq('embone')

company = user.companies.build(name: 'co_name')
company = user.companies.build(name: 'co_name', modifier: user)
company.save!
company.build_address(address: 'Main Street #456', city: 'Evanston', state: 'IL').save!
company.build_address(address: 'Main Street #456', city: 'Evanston', state: 'IL', modifier: user).save!
expect(company.history_tracks.count).to eq(2)
expect(company.address.history_tracks.count).to eq(1)

company.reload
company.address.update_attribute(:address, 'Second Street')
company.address.update_attributes!(address: 'Second Street', modifier: user)
expect(company.history_tracks.count).to eq(3)
expect(company.address.history_tracks.count).to eq(2)
expect(company.history_tracks.last.action).to eq('update')

company.build_second_address(address: 'Main Street #789', city: 'Highland Park', state: 'IL').save!
company.build_second_address(address: 'Main Street #789', city: 'Highland Park', state: 'IL', modifier: user).save!
expect(company.history_tracks.count).to eq(4)
expect(company.second_address.history_tracks.count).to eq(1)
expect(company.second_address.history_tracks.last.action).to eq('create')
expect(company.second_address.history_tracks.last.association_chain.last['name']).to eq('second_address')

company.build_embone(name: 'Lorem ipsum').save!
company.build_embone(name: 'Lorem ipsum', modifier: user).save!
expect(company.history_tracks.count).to eq(5)
expect(company.embone.history_tracks.count).to eq(1)
expect(company.embone.history_tracks.last.action).to eq('create')
expect(company.embone.history_tracks.last.association_chain.last['name']).to eq('embone')
end

after :all do
Object.send(:remove_const, :RealState)
Object.send(:remove_const, :Company)
Object.send(:remove_const, :Embone)
Object.send(:remove_const, :Contact)
Object.send(:remove_const, :User)
end
end
89 changes: 50 additions & 39 deletions spec/integration/integration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe Mongoid::History do
before :all do
before :each do
class Post
include Mongoid::Document
include Mongoid::Timestamps
Expand Down Expand Up @@ -29,7 +29,8 @@ class Comment
field :t, as: :title
field :body
embedded_in :commentable, polymorphic: true
track_history on: %i[title body], scope: :post, track_create: true, track_destroy: true, modifier_field_optional: true # BUGBUG
# BUG: see https://github.com/mongoid/mongoid-history/issues/223, modifier_field_optional should not be necessary
track_history on: %i[title body], scope: :post, track_create: true, track_destroy: true, modifier_field_optional: true
end

class Section
Expand Down Expand Up @@ -70,19 +71,17 @@ class Tag

class Foo < Comment
end
end

class Sausage
include Mongoid::Document
include Mongoid::History::Trackable

field :flavour, localize: true
track_history on: [:flavour], track_destroy: true, modifier_field_optional: true
end

@persisted_history_options = Mongoid::History.trackable_class_options
after :each do
Object.send(:remove_const, :Post)
Object.send(:remove_const, :Comment)
Object.send(:remove_const, :Section)
Object.send(:remove_const, :User)
Object.send(:remove_const, :Tag)
Object.send(:remove_const, :Foo)
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) }
Expand All @@ -104,7 +103,7 @@ class Sausage
end

it 'should assign modifier' do
expect(comment.history_tracks.first.modifier).to eq(user)
expect(comment.history_tracks.first.modifier.id).to eq(user.id)
end

it 'should assign version' do
Expand Down Expand Up @@ -190,7 +189,7 @@ class Sausage

it 'should assign modifier' do
post.update_attributes!(title: 'Another Test')
expect(post.history_tracks.first.modifier).to eq(user)
expect(post.history_tracks.first.modifier.id).to eq(user.id)
end

it 'should assign version on history tracks' do
Expand Down Expand Up @@ -398,7 +397,7 @@ class Sausage

it 'should assign modifier' do
post.update_attributes!(title: 'Another Test', modifier: another_user)
expect(post.history_tracks.last.modifier).to eq(another_user)
expect(post.history_tracks.last.modifier.id).to eq(another_user.id)
end
end

Expand Down Expand Up @@ -437,7 +436,7 @@ class Sausage

it 'should assign modifier' do
post.update_attributes!(title: 'Another Test', modifier: another_user)
expect(post.history_tracks.last.modifier).to eq(another_user)
expect(post.history_tracks.last.modifier.id).to eq(another_user.id)
end
end

Expand Down Expand Up @@ -489,7 +488,7 @@ class Sausage

it 'should assign modifier' do
section.update_attributes!(title: 'Business', modifier: another_user)
expect(post.history_tracks.last.modifier).to eq(another_user)
expect(post.history_tracks.last.modifier.id).to eq(another_user.id)
end
end

Expand Down Expand Up @@ -532,12 +531,6 @@ class Sausage
let(:tag_foo) { post.tags.create!(title: 'foo', updated_by: user) }
let(:tag_bar) { post.tags.create!(title: 'bar', updated_by: user) }

# 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

it 'should allow an update through the parent model' do
update_hash = { 'post' => { 'tags_attributes' => { '1234' => { 'id' => tag_bar.id, 'title' => 'baz' } } } }
post.update_attributes!(update_hash['post'])
Expand Down Expand Up @@ -591,7 +584,7 @@ class Sausage
post.update_attributes!(title: 'Test2')
post.history_tracks.where(version: 2).last.undo!(user)
post.reload
expect(post.history_tracks.where(version: 2).last.modifier).to eq(user)
expect(post.history_tracks.where(version: 2).last.modifier.id).to eq(user.id)
end

it 'should stay the same after undo and redo' do
Expand Down Expand Up @@ -632,7 +625,7 @@ class Sausage
comment.update_attributes!(title: 'Test2')
comment.history_tracks.where(version: 2).first.undo!(user)
comment.reload
expect(comment.history_tracks.where(version: 3).first.modifier).to eq(user)
expect(comment.history_tracks.where(version: 3).first.modifier.id).to eq(user.id)
end

it 'should stay the same after undo and redo' do
Expand Down Expand Up @@ -806,7 +799,7 @@ class Sausage
end

describe 'when default scope is present' do
before do
before :each do
class Post
default_scope -> { where(title: nil) }
end
Expand Down Expand Up @@ -909,6 +902,10 @@ def my_changes
end
end

after :each do
Object.send(:remove_const, :OverriddenChangesMethod)
end

it 'should add foo to the changes history' do
o = OverriddenChangesMethod.create(modifier: user)
o.save!
Expand All @@ -919,24 +916,38 @@ def my_changes
end

describe 'localized fields' do
before :each do
class Sausage
include Mongoid::Document
include Mongoid::History::Trackable

field :flavour, localize: true
track_history on: [:flavour], track_destroy: true, modifier_field_optional: true
end
end

after :each do
Object.send(:remove_const, :Sausage)
end

it 'should correctly undo and redo' do
if Sausage.respond_to?(:localized_fields)
sausage = Sausage.create!(flavour_translations: { 'en' => 'Apple', 'nl' => 'Appel' }, modifier: user)
sausage.update_attributes!(flavour: 'Guinness')
pending unless Sausage.respond_to?(:localized_fields)

track = sausage.history_tracks.last
sausage = Sausage.create!(flavour_translations: { 'en' => 'Apple', 'nl' => 'Appel' }, modifier: user)
sausage.update_attributes!(flavour: 'Guinness')

track.undo! user
expect(sausage.reload.flavour).to eq('Apple')
track = sausage.history_tracks.last

track.redo! user
expect(sausage.reload.flavour).to eq('Guinness')
track.undo! user
expect(sausage.reload.flavour).to eq('Apple')

sausage.destroy
expect(sausage.history_tracks.last.action).to eq('destroy')
sausage.history_tracks.last.undo! user
expect(sausage.reload.flavour).to eq('Guinness')
end
track.redo! user
expect(sausage.reload.flavour).to eq('Guinness')

sausage.destroy
expect(sausage.history_tracks.last.action).to eq('destroy')
sausage.history_tracks.last.undo! user
expect(sausage.reload.flavour).to eq('Guinness')
end
end
end
Expand Down
Loading

0 comments on commit b391da6

Please sign in to comment.