audited 4.7.0 → 4.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
+ SingleCov.covered! uncovered: 13 # not testing proxy_respond_to? hack / 2 methods / deprecation of `version`
4
+
3
5
  describe Audited::Auditor do
4
6
 
5
7
  describe "configuration" do
@@ -20,6 +22,24 @@ describe Audited::Auditor do
20
22
  context "should be configurable which conditions are audited" do
21
23
  subject { ConditionalCompany.new.send(:auditing_enabled) }
22
24
 
25
+ context "when condition method is private" do
26
+ subject { ConditionalPrivateCompany.new.send(:auditing_enabled) }
27
+
28
+ before do
29
+ class ConditionalPrivateCompany < ::ActiveRecord::Base
30
+ self.table_name = 'companies'
31
+
32
+ audited if: :foo?
33
+
34
+ private def foo?
35
+ true
36
+ end
37
+ end
38
+ end
39
+
40
+ it { is_expected.to be_truthy }
41
+ end
42
+
23
43
  context "when passing a method name" do
24
44
  before do
25
45
  class ConditionalCompany < ::ActiveRecord::Base
@@ -192,7 +212,7 @@ describe Audited::Auditor do
192
212
  expect(user.audits.last.audited_changes.keys).to eq(%w{non_column_attr})
193
213
  end
194
214
 
195
- if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL' && Rails.version >= "4.2.0.0" # Postgres json and jsonb support was added in Rails 4.2
215
+ if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
196
216
  describe "'json' and 'jsonb' audited_changes column type" do
197
217
  let(:migrations_path) { SPEC_ROOT.join("support/active_record/postgres") }
198
218
 
@@ -246,7 +266,7 @@ describe Audited::Auditor do
246
266
  end
247
267
 
248
268
  describe "on create" do
249
- let( :user ) { create_user audit_comment: "Create" }
269
+ let( :user ) { create_user status: :reliable, audit_comment: "Create" }
250
270
 
251
271
  it "should change the audit count" do
252
272
  expect {
@@ -270,6 +290,10 @@ describe Audited::Auditor do
270
290
  expect(user.audits.first.audited_changes).to eq(user.audited_attributes)
271
291
  end
272
292
 
293
+ it "should store enum value" do
294
+ expect(user.audits.first.audited_changes["status"]).to eq(1)
295
+ end
296
+
273
297
  it "should store comment" do
274
298
  expect(user.audits.first.comment).to eq('Create')
275
299
  end
@@ -288,7 +312,7 @@ describe Audited::Auditor do
288
312
 
289
313
  describe "on update" do
290
314
  before do
291
- @user = create_user( name: 'Brandon', audit_comment: 'Update' )
315
+ @user = create_user( name: 'Brandon', status: :active, audit_comment: 'Update' )
292
316
  end
293
317
 
294
318
  it "should save an audit" do
@@ -301,17 +325,22 @@ describe Audited::Auditor do
301
325
  end
302
326
 
303
327
  it "should set the action to 'update'" do
304
- @user.update_attributes name: 'Changed'
328
+ @user.update! name: 'Changed'
305
329
  expect(@user.audits.last.action).to eq('update')
306
330
  expect(Audited::Audit.updates.order(:id).last).to eq(@user.audits.last)
307
331
  expect(@user.audits.updates.last).to eq(@user.audits.last)
308
332
  end
309
333
 
310
334
  it "should store the changed attributes" do
311
- @user.update_attributes name: 'Changed'
335
+ @user.update! name: 'Changed'
312
336
  expect(@user.audits.last.audited_changes).to eq({ 'name' => ['Brandon', 'Changed'] })
313
337
  end
314
338
 
339
+ it "should store changed enum values" do
340
+ @user.update! status: 1
341
+ expect(@user.audits.last.audited_changes["status"]).to eq([0, 1])
342
+ end
343
+
315
344
  it "should store audit comment" do
316
345
  expect(@user.audits.last.comment).to eq('Update')
317
346
  end
@@ -319,12 +348,12 @@ describe Audited::Auditor do
319
348
  it "should not save an audit if only specified on create/destroy" do
320
349
  on_create_destroy = Models::ActiveRecord::OnCreateDestroy.create( name: 'Bart' )
321
350
  expect {
322
- on_create_destroy.update_attributes name: 'Changed'
351
+ on_create_destroy.update! name: 'Changed'
323
352
  }.to_not change( Audited::Audit, :count )
324
353
  end
325
354
 
326
355
  it "should not save an audit if the value doesn't change after type casting" do
327
- @user.update_attributes! logins: 0, activated: true
356
+ @user.update! logins: 0, activated: true
328
357
  expect { @user.update_attribute :logins, '0' }.to_not change( Audited::Audit, :count )
329
358
  expect { @user.update_attribute :activated, 1 }.to_not change( Audited::Audit, :count )
330
359
  expect { @user.update_attribute :activated, '1' }.to_not change( Audited::Audit, :count )
@@ -348,7 +377,7 @@ describe Audited::Auditor do
348
377
 
349
378
  describe "on destroy" do
350
379
  before do
351
- @user = create_user
380
+ @user = create_user(status: :active)
352
381
  end
353
382
 
354
383
  it "should save an audit" do
@@ -373,6 +402,11 @@ describe Audited::Auditor do
373
402
  expect(@user.audits.last.audited_changes).to eq(@user.audited_attributes)
374
403
  end
375
404
 
405
+ it "should store enum value" do
406
+ @user.destroy
407
+ expect(@user.audits.last.audited_changes["status"]).to eq(0)
408
+ end
409
+
376
410
  it "should be able to reconstruct a destroyed record without history" do
377
411
  @user.audits.delete_all
378
412
  @user.destroy
@@ -474,13 +508,13 @@ describe Audited::Auditor do
474
508
  it "should delete old extra audits after introducing limit" do
475
509
  stub_global_max_audits(nil) do
476
510
  user = Models::ActiveRecord::User.create!(name: 'Brandon', username: 'brandon')
477
- user.update_attributes(name: 'Foobar')
478
- user.update_attributes(name: 'Awesome', username: 'keepers')
479
- user.update_attributes(activated: true)
511
+ user.update!(name: 'Foobar')
512
+ user.update!(name: 'Awesome', username: 'keepers')
513
+ user.update!(activated: true)
480
514
 
481
515
  Audited.max_audits = 3
482
516
  Models::ActiveRecord::User.send(:normalize_audited_options)
483
- user.update_attributes(favourite_device: 'Android Phone')
517
+ user.update!(favourite_device: 'Android Phone')
484
518
  audits = user.audits
485
519
 
486
520
  expect(audits.count).to eq(3)
@@ -531,8 +565,8 @@ describe Audited::Auditor do
531
565
 
532
566
  it "should set the attributes for each revision" do
533
567
  u = Models::ActiveRecord::User.create(name: 'Brandon', username: 'brandon')
534
- u.update_attributes name: 'Foobar'
535
- u.update_attributes name: 'Awesome', username: 'keepers'
568
+ u.update! name: 'Foobar'
569
+ u.update! name: 'Awesome', username: 'keepers'
536
570
 
537
571
  expect(u.revisions.size).to eql(3)
538
572
 
@@ -548,8 +582,8 @@ describe Audited::Auditor do
548
582
 
549
583
  it "access to only recent revisions" do
550
584
  u = Models::ActiveRecord::User.create(name: 'Brandon', username: 'brandon')
551
- u.update_attributes name: 'Foobar'
552
- u.update_attributes name: 'Awesome', username: 'keepers'
585
+ u.update! name: 'Foobar'
586
+ u.update! name: 'Awesome', username: 'keepers'
553
587
 
554
588
  expect(u.revisions(2).size).to eq(2)
555
589
 
@@ -566,7 +600,7 @@ describe Audited::Auditor do
566
600
  end
567
601
 
568
602
  it "should ignore attributes that have been deleted" do
569
- user.audits.last.update_attributes audited_changes: {old_attribute: 'old value'}
603
+ user.audits.last.update! audited_changes: {old_attribute: 'old value'}
570
604
  expect { user.revisions }.to_not raise_error
571
605
  end
572
606
  end
@@ -581,21 +615,21 @@ describe Audited::Auditor do
581
615
  it "should find the given revision" do
582
616
  revision = user.revision(3)
583
617
  expect(revision).to be_a_kind_of( Models::ActiveRecord::User )
584
- expect(revision.version).to eq(3)
618
+ expect(revision.audit_version).to eq(3)
585
619
  expect(revision.name).to eq('Foobar 3')
586
620
  end
587
621
 
588
622
  it "should find the previous revision with :previous" do
589
623
  revision = user.revision(:previous)
590
- expect(revision.version).to eq(4)
624
+ expect(revision.audit_version).to eq(4)
591
625
  #expect(revision).to eq(user.revision(4))
592
626
  expect(revision.attributes).to eq(user.revision(4).attributes)
593
627
  end
594
628
 
595
629
  it "should be able to get the previous revision repeatedly" do
596
630
  previous = user.revision(:previous)
597
- expect(previous.version).to eq(4)
598
- expect(previous.revision(:previous).version).to eq(3)
631
+ expect(previous.audit_version).to eq(4)
632
+ expect(previous.revision(:previous).audit_version).to eq(3)
599
633
  end
600
634
 
601
635
  it "should be able to set protected attributes" do
@@ -615,8 +649,8 @@ describe Audited::Auditor do
615
649
 
616
650
  it "should set the attributes for each revision" do
617
651
  u = Models::ActiveRecord::User.create(name: 'Brandon', username: 'brandon')
618
- u.update_attributes name: 'Foobar'
619
- u.update_attributes name: 'Awesome', username: 'keepers'
652
+ u.update! name: 'Foobar'
653
+ u.update! name: 'Awesome', username: 'keepers'
620
654
 
621
655
  expect(u.revision(3).name).to eq('Awesome')
622
656
  expect(u.revision(3).username).to eq('keepers')
@@ -628,6 +662,16 @@ describe Audited::Auditor do
628
662
  expect(u.revision(1).username).to eq('brandon')
629
663
  end
630
664
 
665
+ it "should correctly restore revision with enum" do
666
+ u = Models::ActiveRecord::User.create(status: :active)
667
+ u.update_attribute(:status, :reliable)
668
+ u.update_attribute(:status, :banned)
669
+
670
+ expect(u.revision(3)).to be_banned
671
+ expect(u.revision(2)).to be_reliable
672
+ expect(u.revision(1)).to be_active
673
+ end
674
+
631
675
  it "should be able to get time for first revision" do
632
676
  suspended_at = Time.zone.now
633
677
  u = Models::ActiveRecord::User.create(suspended_at: suspended_at)
@@ -668,8 +712,8 @@ describe Audited::Auditor do
668
712
  audit = user.audits.first
669
713
  audit.created_at = 1.hour.ago
670
714
  audit.save!
671
- user.update_attributes name: 'updated'
672
- expect(user.revision_at( 2.minutes.ago ).version).to eq(1)
715
+ user.update! name: 'updated'
716
+ expect(user.revision_at( 2.minutes.ago ).audit_version).to eq(1)
673
717
  end
674
718
 
675
719
  it "should be nil if given a time before audits" do
@@ -677,6 +721,33 @@ describe Audited::Auditor do
677
721
  end
678
722
  end
679
723
 
724
+ describe "own_and_associated_audits" do
725
+ it "should return audits for self and associated audits" do
726
+ owner = Models::ActiveRecord::Owner.create!
727
+ company = owner.companies.create!
728
+ company.update!(name: "Collective Idea")
729
+
730
+ other_owner = Models::ActiveRecord::Owner.create!
731
+ other_owner.companies.create!
732
+
733
+ expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
734
+ end
735
+
736
+ it "should order audits by creation time" do
737
+ owner = Models::ActiveRecord::Owner.create!
738
+ first_audit = owner.audits.first
739
+ first_audit.update_column(:created_at, 1.year.ago)
740
+
741
+ company = owner.companies.create!
742
+ second_audit = company.audits.first
743
+ second_audit.update_column(:created_at, 1.month.ago)
744
+
745
+ company.update!(name: "Collective Idea")
746
+ third_audit = company.audits.last
747
+ expect(owner.own_and_associated_audits.to_a).to eq([third_audit, second_audit, first_audit])
748
+ end
749
+ end
750
+
680
751
  describe "without auditing" do
681
752
  it "should not save an audit when calling #save_without_auditing" do
682
753
  expect {
@@ -697,31 +768,103 @@ describe Audited::Auditor do
697
768
  end
698
769
 
699
770
  it "should be thread safe using a #without_auditing block" do
700
- begin
701
- t1 = Thread.new do
702
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
703
- Models::ActiveRecord::User.without_auditing do
704
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
705
- Models::ActiveRecord::User.create!( name: 'Bart' )
706
- sleep 1
707
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
708
- end
709
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
771
+ skip if Models::ActiveRecord::User.connection.class.name.include?("SQLite")
772
+
773
+ t1 = Thread.new do
774
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
775
+ Models::ActiveRecord::User.without_auditing do
776
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
777
+ Models::ActiveRecord::User.create!( name: 'Bart' )
778
+ sleep 1
779
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
710
780
  end
781
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
782
+ end
783
+
784
+ t2 = Thread.new do
785
+ sleep 0.5
786
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
787
+ Models::ActiveRecord::User.create!( name: 'Lisa' )
788
+ end
789
+ t1.join
790
+ t2.join
711
791
 
712
- t2 = Thread.new do
713
- sleep 0.5
792
+ expect(Models::ActiveRecord::User.find_by_name('Bart').audits.count).to eq(0)
793
+ expect(Models::ActiveRecord::User.find_by_name('Lisa').audits.count).to eq(1)
794
+ end
795
+
796
+ it "should not save an audit when auditing is globally disabled" do
797
+ expect(Audited.auditing_enabled).to eq(true)
798
+ Audited.auditing_enabled = false
799
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
800
+
801
+ user = create_user
802
+ expect(user.audits.count).to eq(0)
803
+
804
+ Audited.auditing_enabled = true
805
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
806
+
807
+ user.update!(name: 'Test')
808
+ expect(user.audits.count).to eq(1)
809
+ Models::ActiveRecord::User.enable_auditing
810
+ end
811
+ end
812
+
813
+ describe "with auditing" do
814
+ it "should save an audit when calling #save_with_auditing" do
815
+ expect {
816
+ u = Models::ActiveRecord::User.new(name: 'Brandon')
817
+ Models::ActiveRecord::User.auditing_enabled = false
818
+ expect(u.save_with_auditing).to eq(true)
819
+ Models::ActiveRecord::User.auditing_enabled = true
820
+ }.to change( Audited::Audit, :count ).by(1)
821
+ end
822
+
823
+ it "should save an audit inside of the #with_auditing block" do
824
+ expect {
825
+ Models::ActiveRecord::User.auditing_enabled = false
826
+ Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!( name: 'Brandon' ) }
827
+ Models::ActiveRecord::User.auditing_enabled = true
828
+ }.to change( Audited::Audit, :count ).by(1)
829
+ end
830
+
831
+ it "should reset auditing status even it raises an exception" do
832
+ Models::ActiveRecord::User.disable_auditing
833
+ Models::ActiveRecord::User.with_auditing { raise } rescue nil
834
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
835
+ Models::ActiveRecord::User.enable_auditing
836
+ end
837
+
838
+ it "should be thread safe using a #with_auditing block" do
839
+ skip if Models::ActiveRecord::User.connection.class.name.include?("SQLite")
840
+
841
+ t1 = Thread.new do
842
+ Models::ActiveRecord::User.disable_auditing
843
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
844
+ Models::ActiveRecord::User.with_auditing do
845
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
846
+
847
+ Models::ActiveRecord::User.create!( name: 'Shaggy' )
848
+ sleep 1
714
849
  expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
715
- Models::ActiveRecord::User.create!( name: 'Lisa' )
716
850
  end
717
- t1.join
718
- t2.join
851
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
852
+ Models::ActiveRecord::User.enable_auditing
853
+ end
719
854
 
720
- expect(Models::ActiveRecord::User.find_by_name('Bart').audits.count).to eq(0)
721
- expect(Models::ActiveRecord::User.find_by_name('Lisa').audits.count).to eq(1)
722
- rescue ActiveRecord::StatementInvalid
723
- STDERR.puts "Thread safety tests cannot be run with SQLite"
855
+ t2 = Thread.new do
856
+ sleep 0.5
857
+ Models::ActiveRecord::User.disable_auditing
858
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
859
+ Models::ActiveRecord::User.create!( name: 'Scooby' )
860
+ Models::ActiveRecord::User.enable_auditing
724
861
  end
862
+ t1.join
863
+ t2.join
864
+
865
+ Models::ActiveRecord::User.enable_auditing
866
+ expect(Models::ActiveRecord::User.find_by_name('Shaggy').audits.count).to eq(1)
867
+ expect(Models::ActiveRecord::User.find_by_name('Scooby').audits.count).to eq(0)
725
868
  end
726
869
  end
727
870
 
@@ -758,21 +901,21 @@ describe Audited::Auditor do
758
901
  let( :on_destroy_user ) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
759
902
 
760
903
  it "should not validate when audit_comment is not supplied" do
761
- expect(user.update_attributes(name: 'Test')).to eq(false)
904
+ expect(user.update(name: 'Test')).to eq(false)
762
905
  end
763
906
 
764
907
  it "should validate when audit_comment is not supplied, and updating is not being audited" do
765
- expect(on_create_user.update_attributes(name: 'Test')).to eq(true)
766
- expect(on_destroy_user.update_attributes(name: 'Test')).to eq(true)
908
+ expect(on_create_user.update(name: 'Test')).to eq(true)
909
+ expect(on_destroy_user.update(name: 'Test')).to eq(true)
767
910
  end
768
911
 
769
912
  it "should validate when audit_comment is supplied" do
770
- expect(user.update_attributes(name: 'Test', audit_comment: 'Update')).to eq(true)
913
+ expect(user.update(name: 'Test', audit_comment: 'Update')).to eq(true)
771
914
  end
772
915
 
773
916
  it "should validate when audit_comment is not supplied, and auditing is disabled" do
774
917
  Models::ActiveRecord::CommentRequiredUser.disable_auditing
775
- expect(user.update_attributes(name: 'Test')).to eq(true)
918
+ expect(user.update(name: 'Test')).to eq(true)
776
919
  Models::ActiveRecord::CommentRequiredUser.enable_auditing
777
920
  end
778
921
  end
@@ -805,6 +948,16 @@ describe Audited::Auditor do
805
948
 
806
949
  end
807
950
 
951
+ describe "no update with comment only" do
952
+ let( :user ) { Models::ActiveRecord::NoUpdateWithCommentOnlyUser.create }
953
+
954
+ it "does not create an audit when only an audit_comment is present" do
955
+ user.audit_comment = "Comment"
956
+ expect { user.save! }.to_not change( Audited::Audit, :count )
957
+ end
958
+
959
+ end
960
+
808
961
  describe "attr_protected and attr_accessible" do
809
962
 
810
963
  it "should not raise error when attr_accessible is set and protected is false" do
@@ -826,7 +979,7 @@ describe Audited::Auditor do
826
979
  it "should record user objects" do
827
980
  Models::ActiveRecord::Company.audit_as( user ) do
828
981
  company = Models::ActiveRecord::Company.create name: 'The auditors'
829
- company.update_attributes name: 'The Auditors'
982
+ company.update! name: 'The Auditors'
830
983
 
831
984
  company.audits.each do |audit|
832
985
  expect(audit.user).to eq(user)
@@ -837,7 +990,7 @@ describe Audited::Auditor do
837
990
  it "should record usernames" do
838
991
  Models::ActiveRecord::Company.audit_as( user.name ) do
839
992
  company = Models::ActiveRecord::Company.create name: 'The auditors'
840
- company.update_attributes name: 'The Auditors'
993
+ company.update! name: 'The Auditors'
841
994
 
842
995
  company.audits.each do |audit|
843
996
  expect(audit.user).to eq(user.name)
@@ -847,7 +1000,7 @@ describe Audited::Auditor do
847
1000
  end
848
1001
 
849
1002
  describe "after_audit" do
850
- let( :user ) { user = Models::ActiveRecord::UserWithAfterAudit.new }
1003
+ let( :user ) { Models::ActiveRecord::UserWithAfterAudit.new }
851
1004
 
852
1005
  it "should invoke after_audit callback on create" do
853
1006
  expect(user.bogus_attr).to be_nil
@@ -857,7 +1010,7 @@ describe Audited::Auditor do
857
1010
  end
858
1011
 
859
1012
  describe "around_audit" do
860
- let( :user ) { user = Models::ActiveRecord::UserWithAfterAudit.new }
1013
+ let( :user ) { Models::ActiveRecord::UserWithAfterAudit.new }
861
1014
 
862
1015
  it "should invoke around_audit callback on create" do
863
1016
  expect(user.around_attr).to be_nil
@@ -872,7 +1025,7 @@ describe Audited::Auditor do
872
1025
  expect(company.type).to eq("Models::ActiveRecord::Company::STICompany")
873
1026
  expect {
874
1027
  Models::ActiveRecord::Company.auditing_enabled = false
875
- company.update_attributes name: 'STI auditors'
1028
+ company.update! name: 'STI auditors'
876
1029
  Models::ActiveRecord::Company.auditing_enabled = true
877
1030
  }.to_not change( Audited::Audit, :count )
878
1031
  end
@@ -1,5 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
+ SingleCov.covered! uncovered: 2 # 2 conditional on_load conditions
4
+
3
5
  class AuditsController < ActionController::Base
4
6
  before_action :populate_user
5
7
 
@@ -11,7 +13,7 @@ class AuditsController < ActionController::Base
11
13
  end
12
14
 
13
15
  def update
14
- current_user.update_attributes(password: 'foo')
16
+ current_user.update!(password: 'foo')
15
17
  head :ok
16
18
  end
17
19
 
@@ -27,14 +29,13 @@ describe AuditsController do
27
29
  include RSpec::Rails::ControllerExampleGroup
28
30
  render_views
29
31
 
30
- before(:each) do
32
+ before do
31
33
  Audited.current_user_method = :current_user
32
34
  end
33
35
 
34
36
  let(:user) { create_user }
35
37
 
36
38
  describe "POST audit" do
37
-
38
39
  it "should audit user" do
39
40
  controller.send(:current_user=, user)
40
41
  expect {
@@ -44,6 +45,15 @@ describe AuditsController do
44
45
  expect(controller.company.audits.last.user).to eq(user)
45
46
  end
46
47
 
48
+ it "does not audit when method is not found" do
49
+ controller.send(:current_user=, user)
50
+ Audited.current_user_method = :nope
51
+ expect {
52
+ post :create
53
+ }.to change( Audited::Audit, :count )
54
+ expect(controller.company.audits.last.user).to eq(nil)
55
+ end
56
+
47
57
  it "should support custom users for sweepers" do
48
58
  controller.send(:custom_user=, user)
49
59
  Audited.current_user_method = :custom_user
@@ -84,7 +94,6 @@ describe AuditsController do
84
94
 
85
95
  expect(controller.company.audits.last.user).to eq(user)
86
96
  end
87
-
88
97
  end
89
98
 
90
99
  describe "PUT update" do
@@ -98,7 +107,6 @@ describe AuditsController do
98
107
  end
99
108
  end
100
109
 
101
-
102
110
  describe Audited::Sweeper do
103
111
 
104
112
  it "should be thread-safe" do
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -6,3 +6,8 @@ module RailsApp
6
6
  config.i18n.enforce_available_locales = true
7
7
  end
8
8
  end
9
+
10
+ require 'active_record/connection_adapters/sqlite3_adapter'
11
+ if ActiveRecord::ConnectionAdapters::SQLite3Adapter.respond_to?(:represent_boolean_as_integer)
12
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true
13
+ end
@@ -19,6 +19,7 @@ mysql: &MYSQL
19
19
  username: root
20
20
  password:
21
21
  database: audited_test
22
+ charset: utf8
22
23
 
23
24
  test:
24
25
  <<: *<%= ENV['DB'] || 'SQLITE3MEM' %>
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  ENV['RAILS_ENV'] = 'test'
2
+ require 'bundler/setup'
3
+ require 'single_cov'
4
+ SingleCov.setup :rspec
2
5
 
3
- require 'bundler'
4
6
  if Bundler.definition.dependencies.map(&:name).include?('protected_attributes')
5
7
  require 'protected_attributes'
6
8
  end
@@ -7,6 +7,7 @@ module Models
7
7
  audited except: :password
8
8
  attribute :non_column_attr if Rails.version >= '5.1'
9
9
  attr_protected :logins if respond_to?(:attr_protected)
10
+ enum status: { active: 0, reliable: 1, banned: 2 }
10
11
 
11
12
  def name=(val)
12
13
  write_attribute(:name, CGI.escapeHTML(val))
@@ -44,6 +45,11 @@ module Models
44
45
  audited comment_required: true, on: :destroy
45
46
  end
46
47
 
48
+ class NoUpdateWithCommentOnlyUser < ::ActiveRecord::Base
49
+ self.table_name = :users
50
+ audited update_with_comment_only: false
51
+ end
52
+
47
53
  class AccessibleAfterDeclarationUser < ::ActiveRecord::Base
48
54
  self.table_name = :users
49
55
  audited
@@ -91,6 +97,7 @@ module Models
91
97
 
92
98
  class Owner < ::ActiveRecord::Base
93
99
  self.table_name = 'users'
100
+ audited
94
101
  has_associated_audits
95
102
  has_many :companies, class_name: "OwnedCompany", dependent: :destroy
96
103
  end
@@ -15,10 +15,10 @@ begin
15
15
  db_config[:configure_connection] = false
16
16
  end
17
17
  adapter = ActiveRecord::Base.send("#{db_type}_connection", db_config)
18
- adapter.recreate_database db_name
18
+ adapter.recreate_database db_name, db_config.slice('charset').symbolize_keys
19
19
  adapter.disconnect!
20
20
  end
21
- rescue Exception => e
21
+ rescue => e
22
22
  Kernel.warn e
23
23
  end
24
24
 
@@ -35,6 +35,7 @@ ActiveRecord::Schema.define do
35
35
  t.column :username, :string
36
36
  t.column :password, :string
37
37
  t.column :activated, :boolean
38
+ t.column :status, :integer, default: 0
38
39
  t.column :suspended_at, :datetime
39
40
  t.column :logins, :integer, default: 0
40
41
  t.column :created_at, :datetime
data/test/db/version_6.rb CHANGED
@@ -14,4 +14,6 @@ ActiveRecord::Schema.define do
14
14
  t.column :associated_id, :integer
15
15
  t.column :associated_type, :string
16
16
  end
17
+
18
+ add_index :audits, [:auditable_type, :auditable_id], name: 'auditable_index'
17
19
  end
data/test/test_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  ENV['RAILS_ENV'] = 'test'
2
2
 
3
- $:.unshift File.dirname(__FILE__)
3
+ $LOAD_PATH.unshift File.dirname(__FILE__)
4
4
 
5
5
  require File.expand_path('../../spec/rails_app/config/environment', __FILE__)
6
6
  require 'rails/test_help'
@@ -8,7 +8,6 @@ require 'rails/test_help'
8
8
  require 'audited'
9
9
 
10
10
  class ActiveSupport::TestCase
11
-
12
11
  setup do
13
12
  ActiveRecord::Migration.verbose = false
14
13
  end
@@ -79,6 +79,16 @@ class UpgradeGeneratorTest < Rails::Generators::TestCase
79
79
  end
80
80
  end
81
81
 
82
+ test "should add 'version' to auditable_index" do
83
+ load_schema 6
84
+
85
+ run_generator %w(upgrade)
86
+
87
+ assert_migration "db/migrate/add_version_to_auditable_index.rb" do |content|
88
+ assert_match(/add_index :audits, \[:auditable_type, :auditable_id, :version\]/, content)
89
+ end
90
+ end
91
+
82
92
  test "generate migration with correct AR migration parent" do
83
93
  load_schema 1
84
94