mongoid-history 0.4.7 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,13 +8,13 @@ module Mongoid
8
8
  include Mongoid::Timestamps
9
9
  attr_writer :trackable
10
10
 
11
- field :association_chain, type: Array, default: []
11
+ field :association_chain, type: Array, default: []
12
12
  field :modified, type: Hash
13
13
  field :original, type: Hash
14
14
  field :version, type: Integer
15
15
  field :action, type: String
16
16
  field :scope, type: String
17
- belongs_to :modifier, class_name: Mongoid::History.modifier_class_name
17
+ belongs_to :modifier, class_name: Mongoid::History.modifier_class_name
18
18
 
19
19
  index(scope: 1)
20
20
  index(association_chain: 1)
@@ -27,7 +27,7 @@ module Mongoid
27
27
  re_create
28
28
  elsif action.to_sym == :create
29
29
  re_destroy
30
- elsif Mongoid::History.mongoid3?
30
+ elsif Mongoid::Compatibility::Version.mongoid3?
31
31
  trackable.update_attributes!(undo_attr(modifier), without_protection: true)
32
32
  else
33
33
  trackable.update_attributes!(undo_attr(modifier))
@@ -39,7 +39,7 @@ module Mongoid
39
39
  re_destroy
40
40
  elsif action.to_sym == :create
41
41
  re_create
42
- elsif Mongoid::History.mongoid3?
42
+ elsif Mongoid::Compatibility::Version.mongoid3?
43
43
  trackable.update_attributes!(redo_attr(modifier), without_protection: true)
44
44
  else
45
45
  trackable.update_attributes!(redo_attr(modifier))
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module History
3
- VERSION = '0.4.7'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
@@ -20,12 +20,13 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_runtime_dependency 'easy_diff'
22
22
  s.add_runtime_dependency 'mongoid', '>= 3.0'
23
+ s.add_runtime_dependency 'mongoid-compatibility'
23
24
  s.add_runtime_dependency 'activesupport'
24
25
 
25
26
  s.add_development_dependency 'rake'
26
27
  s.add_development_dependency 'rspec', '~> 3.1'
27
28
  s.add_development_dependency 'bundler'
28
- s.add_development_dependency 'rubocop', '0.27.1'
29
+ s.add_development_dependency 'rubocop', '0.34.1'
29
30
  s.add_development_dependency 'yard'
30
31
  s.add_development_dependency 'gem-release'
31
32
  s.add_development_dependency 'coveralls'
@@ -11,10 +11,10 @@ describe Mongoid::History::Tracker do
11
11
  embeds_one :address, class_name: 'Contact', as: :contactable
12
12
  embeds_one :embone, as: :embedable
13
13
 
14
- track_history on: :all, # track title and body fields only, default is :all
14
+ track_history on: :all, # track title and body fields only, default is :all
15
15
  modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
16
16
  version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
17
- track_create: true, # track document creation, default is false
17
+ track_create: true, # track document creation, default is false
18
18
  track_update: true, # track document updates, default is true
19
19
  track_destroy: false # track document destruction, default is false
20
20
  end
@@ -27,14 +27,14 @@ describe Mongoid::History::Tracker do
27
27
  belongs_to :user
28
28
  embeds_one :address, class_name: 'Contact', as: :contactable
29
29
  embeds_one :second_address, class_name: 'Contact', as: :contactable
30
- embeds_one :embone, as: :embedable
30
+ embeds_one :embone, as: :embedable
31
31
 
32
- track_history on: :all, # track title and body fields only, default is :all
32
+ track_history on: :all, # track title and body fields only, default is :all
33
33
  modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
34
34
  version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
35
- track_create: true, # track document creation, default is false
36
- track_update: true, # track document updates, default is true
37
- track_destroy: false # track document destruction, default is false
35
+ track_create: true, # track document creation, default is false
36
+ track_update: true, # track document updates, default is true
37
+ track_destroy: false # track document destruction, default is false
38
38
  end
39
39
 
40
40
  class Embone
@@ -44,12 +44,12 @@ describe Mongoid::History::Tracker do
44
44
  field :name
45
45
  embedded_in :embedable, polymorphic: true
46
46
 
47
- track_history on: :all, # track title and body fields only, default is :all
47
+ track_history on: :all, # track title and body fields only, default is :all
48
48
  modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
49
49
  version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
50
- track_create: true, # track document creation, default is false
51
- track_update: true, # track document updates, default is true
52
- track_destroy: false, # track document destruction, default is false
50
+ track_create: true, # track document creation, default is false
51
+ track_update: true, # track document updates, default is true
52
+ track_destroy: false, # track document destruction, default is false
53
53
  scope: :embedable
54
54
  end
55
55
 
@@ -62,12 +62,12 @@ describe Mongoid::History::Tracker do
62
62
  field :state
63
63
  embedded_in :contactable, polymorphic: true
64
64
 
65
- track_history on: :all, # track title and body fields only, default is :all
65
+ track_history on: :all, # track title and body fields only, default is :all
66
66
  modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
67
67
  version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
68
- track_create: true, # track document creation, default is false
69
- track_update: true, # track document updates, default is true
70
- track_destroy: false, # track document destruction, default is false
68
+ track_create: true, # track document creation, default is false
69
+ track_update: true, # track document updates, default is true
70
+ track_destroy: false, # track document destruction, default is false
71
71
  scope: [:real_state, :company]
72
72
  end
73
73
 
@@ -97,8 +97,8 @@ describe Mongoid::History::Tracker do
97
97
  real_state.build_embone(name: 'Lorem ipsum').save!
98
98
  expect(real_state.history_tracks.count).to eq(4)
99
99
  expect(real_state.embone.history_tracks.count).to eq(1)
100
- expect(real_state.history_tracks.last.action).to eq('create')
101
- expect(real_state.history_tracks.last.association_chain.last['name']).to eq('embone')
100
+ expect(real_state.embone.history_tracks.last.action).to eq('create')
101
+ expect(real_state.embone.history_tracks.last.association_chain.last['name']).to eq('embone')
102
102
 
103
103
  company = user.companies.build(name: 'co_name')
104
104
  company.save!
@@ -115,13 +115,13 @@ describe Mongoid::History::Tracker do
115
115
  company.build_second_address(address: 'Main Street #789', city: 'Highland Park', state: 'IL').save!
116
116
  expect(company.history_tracks.count).to eq(4)
117
117
  expect(company.second_address.history_tracks.count).to eq(1)
118
- expect(company.history_tracks.last.action).to eq('create')
119
- expect(company.history_tracks.last.association_chain.last['name']).to eq('second_address')
118
+ expect(company.second_address.history_tracks.last.action).to eq('create')
119
+ expect(company.second_address.history_tracks.last.association_chain.last['name']).to eq('second_address')
120
120
 
121
121
  company.build_embone(name: 'Lorem ipsum').save!
122
122
  expect(company.history_tracks.count).to eq(5)
123
123
  expect(company.embone.history_tracks.count).to eq(1)
124
- expect(company.history_tracks.last.action).to eq('create')
125
- expect(company.history_tracks.last.association_chain.last['name']).to eq('embone')
124
+ expect(company.embone.history_tracks.last.action).to eq('create')
125
+ expect(company.embone.history_tracks.last.association_chain.last['name']).to eq('embone')
126
126
  end
127
127
  end
@@ -75,11 +75,11 @@ describe Mongoid::History do
75
75
  end
76
76
 
77
77
  before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
78
- let(:user) { User.create(name: 'Aaron', email: 'aaron@randomemail.com', aliases: ['bob'], country: 'Canada', city: 'Toronto', address: '21 Jump Street') }
79
- let(:another_user) { User.create(name: 'Another Guy', email: 'anotherguy@randomemail.com') }
80
- let(:post) { Post.create(title: 'Test', body: 'Post', modifier: user, views: 100) }
81
- let(:comment) { post.comments.create(title: 'test', body: 'comment', modifier: user) }
82
- let(:tag) { Tag.create(title: 'test') }
78
+ let(:user) { User.create!(name: 'Aaron', email: 'aaron@randomemail.com', aliases: ['bob'], country: 'Canada', city: 'Toronto', address: '21 Jump Street') }
79
+ let(:another_user) { User.create!(name: 'Another Guy', email: 'anotherguy@randomemail.com') }
80
+ let(:post) { Post.create!(title: 'Test', body: 'Post', modifier: user, views: 100) }
81
+ let(:comment) { post.comments.create!(title: 'test', body: 'comment', modifier: user) }
82
+ let(:tag) { Tag.create!(title: 'test') }
83
83
 
84
84
  describe 'track' do
85
85
  describe 'on creation' do
@@ -88,7 +88,7 @@ describe Mongoid::History do
88
88
  end
89
89
 
90
90
  it 'should assign title and body on modified' do
91
- expect(comment.history_tracks.first.modified).to eq('t' => 'test', 'body' => 'comment')
91
+ expect(comment.history_tracks.first.modified).to eq('t' => 'test', 'body' => 'comment')
92
92
  end
93
93
 
94
94
  it 'should not assign title and body on original' do
@@ -212,7 +212,7 @@ describe Mongoid::History do
212
212
  end
213
213
 
214
214
  it 'should undo non-existing field changes' do
215
- post = Post.create(modifier: user, views: 100)
215
+ post = Post.create!(modifier: user, views: 100)
216
216
  expect(post.reload.title).to be_nil
217
217
  post.update_attributes(title: 'Aaron2')
218
218
  expect(post.reload.title).to eq('Aaron2')
@@ -502,9 +502,8 @@ describe Mongoid::History do
502
502
  end
503
503
 
504
504
  describe 'embedded with cascading callbacks' do
505
-
506
- let(:tag_foo) { post.tags.create(title: 'foo', updated_by: user) }
507
- let(:tag_bar) { post.tags.create(title: 'bar') }
505
+ let(:tag_foo) { post.tags.create!(title: 'foo', updated_by: user) }
506
+ let(:tag_bar) { post.tags.create!(title: 'bar') }
508
507
 
509
508
  # it "should have cascaded the creation callbacks and set timestamps" do
510
509
  # tag_foo; tag_bar # initialize
@@ -627,7 +626,7 @@ describe Mongoid::History do
627
626
  end
628
627
 
629
628
  describe 'undo' do
630
- { 'undo' => [nil], 'undo!' => [nil, :reload] }.each do |test_method, methods|
629
+ { 'undo' => [nil], 'undo!' => [nil, :reload] }.each do |test_method, methods|
631
630
  methods.each do |method|
632
631
  context "#{method || 'instance'}" do
633
632
  it 'recognizes :from, :to options' do
@@ -654,7 +653,7 @@ describe Mongoid::History do
654
653
  expect(comment.title).to eq('Test2')
655
654
  end
656
655
 
657
- if Mongoid::History.mongoid3?
656
+ if Mongoid::Compatibility::Version.mongoid3?
658
657
  context 'protected attributes' do
659
658
  before :each do
660
659
  Comment.attr_accessible(nil)
@@ -713,7 +712,7 @@ describe Mongoid::History do
713
712
  expect(comment.title).to eq('Test5')
714
713
  end
715
714
 
716
- if Mongoid::History.mongoid3?
715
+ if Mongoid::Compatibility::Version.mongoid3?
717
716
  context 'protected attributes' do
718
717
  before :each do
719
718
  Comment.attr_accessible(nil)
@@ -738,7 +737,6 @@ describe Mongoid::History do
738
737
  end
739
738
  end
740
739
  end
741
-
742
740
  end
743
741
  end
744
742
 
@@ -754,7 +752,7 @@ describe Mongoid::History do
754
752
  end
755
753
  it 'should correctly undo and redo' do
756
754
  if Sausage.respond_to?(:localized_fields)
757
- sausage = Sausage.create(flavour_translations: { 'en' => 'Apple', 'nl' => 'Appel' })
755
+ sausage = Sausage.create!(flavour_translations: { 'en' => 'Apple', 'nl' => 'Appel' })
758
756
  sausage.update_attributes(flavour: 'Guinness')
759
757
 
760
758
  track = sausage.history_tracks.last
@@ -777,7 +775,7 @@ describe Mongoid::History do
777
775
  let(:foo) { Foo.new(title: 'a title', body: 'a body') }
778
776
  before :each do
779
777
  post.comments << foo
780
- post.save
778
+ post.save!
781
779
  end
782
780
  it 'should assign interface name in association chain' do
783
781
  foo.update_attribute(:body, 'a changed body')
@@ -828,7 +826,6 @@ describe Mongoid::History do
828
826
  end
829
827
 
830
828
  describe 'post' do
831
-
832
829
  it 'should correctly undo and redo' do
833
830
  post.update_attributes(title: 'a new title')
834
831
  track = post.history_tracks.last
@@ -901,5 +898,28 @@ describe Mongoid::History do
901
898
  end
902
899
  end
903
900
  end
901
+
902
+ describe 'overriden changes_method with additional fields' do
903
+ before :each do
904
+ class OverriddenChangesMethod
905
+ include Mongoid::Document
906
+ include Mongoid::History::Trackable
907
+
908
+ track_history on: [:foo], changes_method: :my_changes
909
+
910
+ def my_changes
911
+ { foo: %w(bar baz) }
912
+ end
913
+ end
914
+ end
915
+
916
+ it 'should add foo to the changes history' do
917
+ o = OverriddenChangesMethod.create
918
+ o.save!
919
+ track = o.history_tracks.last
920
+ expect(track.modified).to eq('foo' => 'baz')
921
+ expect(track.original).to eq('foo' => 'bar')
922
+ end
923
+ end
904
924
  end
905
925
  end
@@ -13,7 +13,7 @@ describe Mongoid::History::Tracker do
13
13
  track_history on: [:name, :user, :external_user_ids], # track title and body fields only, default is :all
14
14
  modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
15
15
  modifier_field_inverse_of: nil, # no inverse modifier relationship
16
- version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
16
+ version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
17
17
  track_create: false, # track document creation, default is false
18
18
  track_update: true, # track document updates, default is true
19
19
  track_destroy: false # track document destruction, default is false
@@ -10,7 +10,7 @@ describe Mongoid::History::Tracker do
10
10
  belongs_to :user, inverse_of: :models
11
11
  embeds_many :embones
12
12
 
13
- track_history on: :all, # track title and body fields only, default is :all
13
+ track_history on: :all, # track title and body fields only, default is :all
14
14
  modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
15
15
  version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
16
16
  track_create: false, # track document creation, default is false
@@ -26,12 +26,12 @@ describe Mongoid::History::Tracker do
26
26
  embeds_many :embtwos, store_as: :ems
27
27
  embedded_in :model
28
28
 
29
- track_history on: :all, # track title and body fields only, default is :all
29
+ track_history on: :all, # track title and body fields only, default is :all
30
30
  modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
31
31
  version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
32
32
  track_create: false, # track document creation, default is false
33
33
  track_update: true, # track document updates, default is true
34
- track_destroy: false, # track document destruction, default is false
34
+ track_destroy: false, # track document destruction, default is false
35
35
  scope: :model
36
36
  end
37
37
 
@@ -42,12 +42,12 @@ describe Mongoid::History::Tracker do
42
42
  field :name
43
43
  embedded_in :embone
44
44
 
45
- track_history on: :all, # track title and body fields only, default is :all
45
+ track_history on: :all, # track title and body fields only, default is :all
46
46
  modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
47
47
  version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
48
48
  track_create: false, # track document creation, default is false
49
49
  track_update: true, # track document updates, default is true
50
- track_destroy: false, # track document destruction, default is false
50
+ track_destroy: false, # track document destruction, default is false
51
51
  scope: :model
52
52
  end
53
53
 
@@ -8,7 +8,7 @@ describe Mongoid::History::Tracker do
8
8
 
9
9
  field :name, type: String
10
10
  belongs_to :user
11
- embeds_one :embedded_one, as: :embedable
11
+ embeds_one :embedded_one, as: :embedable
12
12
 
13
13
  track_history on: :all,
14
14
  modifier_field: :modifier,
@@ -24,7 +24,7 @@ describe Mongoid::History::Tracker do
24
24
 
25
25
  field :name, type: String
26
26
  belongs_to :user
27
- embeds_one :embedded_one, as: :embedable
27
+ embeds_one :embedded_one, as: :embedable
28
28
 
29
29
  track_history on: :all,
30
30
  modifier_field: :modifier,
@@ -93,8 +93,8 @@ describe Mongoid::History::Tracker do
93
93
  modelone.build_embedded_one(name: 'Lorem ipsum').save!
94
94
  expect(modelone.history_tracks.count).to eq(4)
95
95
  expect(modelone.embedded_one.history_tracks.count).to eq(1)
96
- expect(modelone.history_tracks.last.action).to eq('create')
97
- expect(modelone.history_tracks.last.association_chain.last['name']).to eq('embedded_one')
96
+ expect(modelone.embedded_one.history_tracks.last.action).to eq('create')
97
+ expect(modelone.embedded_one.history_tracks.last.association_chain.last['name']).to eq('embedded_one')
98
98
 
99
99
  embedded_one1 = modelone.embedded_one.embedded_twos.create(name: 'modelone_embedded_one_1')
100
100
  expect(modelone.history_tracks.count).to eq(5)
@@ -116,8 +116,8 @@ describe Mongoid::History::Tracker do
116
116
  modeltwo.build_embedded_one(name: 'Lorem ipsum').save!
117
117
  expect(modeltwo.history_tracks.count).to eq(4)
118
118
  expect(modeltwo.embedded_one.history_tracks.count).to eq(1)
119
- expect(modeltwo.history_tracks.last.action).to eq('create')
120
- expect(modeltwo.history_tracks.last.association_chain.last['name']).to eq('embedded_one')
119
+ expect(modeltwo.embedded_one.history_tracks.last.action).to eq('create')
120
+ expect(modeltwo.embedded_one.history_tracks.last.association_chain.last['name']).to eq('embedded_one')
121
121
 
122
122
  embedded_one2 = modeltwo.embedded_one.embedded_twos.create(name: 'modeltwo_embedded_one_1')
123
123
  expect(modeltwo.history_tracks.count).to eq(5)
@@ -12,10 +12,8 @@ require 'mongoid/history'
12
12
 
13
13
  RSpec.configure do |config|
14
14
  config.raise_errors_for_deprecations!
15
- config.expect_with :rspec do |expectations|
16
- expectations.syntax = [:should, :expect]
17
- end
18
- config.mock_with :rspec do |mocks|
19
- mocks.syntax = [:should, :expect]
15
+ config.before :all do
16
+ Mongoid.logger.level = Logger::INFO
17
+ Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5?
20
18
  end
21
19
  end
@@ -6,6 +6,12 @@ class MyModel
6
6
  field :foo
7
7
  end
8
8
 
9
+ class MyDynamicModel
10
+ include Mongoid::Document
11
+ include Mongoid::History::Trackable
12
+ include Mongoid::Attributes::Dynamic unless Mongoid::Compatibility::Version.mongoid3?
13
+ end
14
+
9
15
  class HistoryTracker
10
16
  include Mongoid::History::Tracker
11
17
  end
@@ -95,6 +101,50 @@ describe Mongoid::History::Trackable do
95
101
  it 'should allow field aliases' do
96
102
  expect(MyModel.tracked_field?(:id, :destroy)).to be true
97
103
  end
104
+
105
+ context 'when model is dynamic' do
106
+ it 'should allow dynamic fields tracking' do
107
+ MyDynamicModel.track_history
108
+ expect(MyDynamicModel.tracked_field?(:dynamic_field, :destroy)).to be true
109
+ end
110
+ end
111
+
112
+ unless Mongoid::Compatibility::Version.mongoid3?
113
+ context 'when model is not dynamic' do
114
+ it 'should not allow dynamic fields tracking' do
115
+ MyModel.track_history
116
+ expect(MyModel.tracked_field?(:dynamic_field, :destroy)).to be false
117
+ end
118
+ end
119
+ end
120
+
121
+ it 'allows a non-database field to be specified' do
122
+ class MyNonDatabaseModel
123
+ include Mongoid::Document
124
+ include Mongoid::History::Trackable
125
+ track_history on: ['baz']
126
+ end
127
+
128
+ expect(MyNonDatabaseModel.tracked_field?(:baz)).to be true
129
+ end
130
+ end
131
+
132
+ context '#dynamic_field?' do
133
+ context 'when model is dynamic' do
134
+ it 'should return true' do
135
+ MyDynamicModel.track_history
136
+ expect(MyDynamicModel.dynamic_field?(:dynamic_field)).to be true
137
+ end
138
+ end
139
+
140
+ unless Mongoid::Compatibility::Version.mongoid3?
141
+ context 'when model is not dynamic' do
142
+ it 'should return false' do
143
+ MyModel.track_history
144
+ expect(MyModel.dynamic_field?(:dynamic_field)).to be false
145
+ end
146
+ end
147
+ end
98
148
  end
99
149
 
100
150
  context 'sub-model' do
@@ -113,9 +163,7 @@ describe Mongoid::History::Trackable do
113
163
  end
114
164
 
115
165
  describe '#track_history?' do
116
-
117
166
  context 'when tracking is globally enabled' do
118
-
119
167
  it 'should be enabled on the current thread' do
120
168
  expect(Mongoid::History.enabled?).to eq(true)
121
169
  expect(MyModel.new.track_history?).to eq(true)
@@ -154,7 +202,6 @@ describe Mongoid::History::Trackable do
154
202
  end
155
203
 
156
204
  context 'when tracking is globally disabled' do
157
-
158
205
  around(:each) do |example|
159
206
  Mongoid::History.disable do
160
207
  example.run
@@ -213,7 +260,6 @@ describe Mongoid::History::Trackable do
213
260
  end
214
261
 
215
262
  describe ':changes_method' do
216
-
217
263
  it 'should default to :changes' do
218
264
  m = MyModel.create
219
265
  expect(m).to receive(:changes).exactly(3).times.and_call_original