auditing 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -3
- data/README.rdoc +5 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/auditing.gemspec +5 -5
- data/lib/auditing/base.rb +4 -1
- data/spec/auditing/audit_relationship_spec.rb +109 -16
- data/spec/auditing/auditor_spec.rb +17 -17
- data/spec/auditing/base_spec.rb +44 -38
- data/spec/schema.rb +12 -6
- data/spec/spec_helper.rb +0 -1
- metadata +5 -6
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -24,8 +24,6 @@ GEM
|
|
24
24
|
rspec-expectations (2.0.0.beta.20)
|
25
25
|
diff-lcs (>= 1.1.2)
|
26
26
|
rspec-mocks (2.0.0.beta.20)
|
27
|
-
rspec-rails (2.0.0.beta.20)
|
28
|
-
rspec (= 2.0.0.beta.20)
|
29
27
|
sqlite3-ruby (1.3.1)
|
30
28
|
tzinfo (0.3.23)
|
31
29
|
|
@@ -34,5 +32,5 @@ PLATFORMS
|
|
34
32
|
|
35
33
|
DEPENDENCIES
|
36
34
|
activerecord (~> 3.0.0.rc2)
|
37
|
-
rspec
|
35
|
+
rspec (~> 2.0)
|
38
36
|
sqlite3-ruby (~> 1.3)
|
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= auditing
|
1
|
+
= auditing
|
2
2
|
|
3
3
|
Auditing is a simple way to track and rollback changes to a record.
|
4
4
|
|
@@ -23,6 +23,7 @@ You will have to supply a model named Audit in your rails application with the f
|
|
23
23
|
end
|
24
24
|
|
25
25
|
If you want to track the user, uncomment the t.integer :user_id above.
|
26
|
+
|
26
27
|
TODO: more to follow about tracking the user
|
27
28
|
|
28
29
|
class Audit < ActiveRecord::Base
|
@@ -70,8 +71,10 @@ to enable a similar type of auditing on the other model(s)
|
|
70
71
|
|
71
72
|
As above, you can supply a :fields hash of which attributes will be logged.
|
72
73
|
If your relationship is polymorphic, you can supply an array of classes
|
73
|
-
that you
|
74
|
+
that you only want to log.
|
74
75
|
|
76
|
+
audit_relationship_enabled :only => [Company, Person]
|
77
|
+
|
75
78
|
== Note on Patches/Pull Requests
|
76
79
|
|
77
80
|
* Fork the project.
|
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ begin
|
|
10
10
|
gem.email = "brad.cantin@gmail.com"
|
11
11
|
gem.homepage = "http://github.com/bcantin/auditing"
|
12
12
|
gem.authors = ["Brad Cantin"]
|
13
|
-
gem.add_development_dependency "rspec", ">=
|
13
|
+
gem.add_development_dependency "rspec", ">= 2.0"
|
14
14
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
15
|
end
|
16
16
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.1.0
|
data/auditing.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{auditing}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brad Cantin"]
|
12
|
-
s.date = %q{2010-10-
|
12
|
+
s.date = %q{2010-10-16}
|
13
13
|
s.description = %q{acts_as_versioned is good. This allows an attribute level rollback instead}
|
14
14
|
s.email = %q{brad.cantin@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -56,12 +56,12 @@ Gem::Specification.new do |s|
|
|
56
56
|
s.specification_version = 3
|
57
57
|
|
58
58
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
59
|
-
s.add_development_dependency(%q<rspec>, [">=
|
59
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0"])
|
60
60
|
else
|
61
|
-
s.add_dependency(%q<rspec>, [">=
|
61
|
+
s.add_dependency(%q<rspec>, [">= 2.0"])
|
62
62
|
end
|
63
63
|
else
|
64
|
-
s.add_dependency(%q<rspec>, [">=
|
64
|
+
s.add_dependency(%q<rspec>, [">= 2.0"])
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
data/lib/auditing/base.rb
CHANGED
@@ -60,8 +60,11 @@ module Auditing
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def log_association_destroy(item)
|
63
|
+
mark_as_undoable = audits.where({:association_id => item.id, :association_type => item.class.to_s})
|
64
|
+
mark_as_undoable.each do |i|
|
65
|
+
i.update_attribute('undoable', false)
|
66
|
+
end
|
63
67
|
add_audit(:action => 'removed', :association => item, :undoable => false)
|
64
|
-
# TODO: update all of this items previous audits to be undoable
|
65
68
|
end
|
66
69
|
|
67
70
|
private
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "AuditingRelationship" do
|
4
|
-
|
4
|
+
|
5
5
|
describe "adding audit_relationship_enabled on a model" do
|
6
6
|
before do
|
7
7
|
class Company < ActiveRecord::Base
|
@@ -13,52 +13,145 @@ describe "AuditingRelationship" do
|
|
13
13
|
audit_relationship_enabled
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
it "should respond to audit_relationship_enabled when we add audit_relationship_enabled" do
|
18
18
|
PhoneNumber.should respond_to(:audit_relationship_enabled)
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
it "should build an array of audit_enabled_models" do
|
22
22
|
PhoneNumber.audit_enabled_models.should == [:phoneable]
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
it "should build an array of field_names" do
|
26
26
|
PhoneNumber.field_names.should =~ ['number', 'extension']
|
27
27
|
end
|
28
|
-
|
29
|
-
describe "creating a new
|
28
|
+
|
29
|
+
describe "creating a new has_many object" do
|
30
30
|
before do
|
31
31
|
@company = Company.create(:name => 'Apple')
|
32
32
|
end
|
33
|
-
|
34
33
|
it "should add an audit to the parent instance object (company) when we create a phone number" do
|
35
34
|
lambda {
|
36
35
|
@company.phone_numbers << PhoneNumber.new(:number => '1-800-orange')
|
37
|
-
|
36
|
+
}.should change{@company.audits.count}
|
38
37
|
end
|
39
|
-
|
38
|
+
|
40
39
|
it "should add an audit for each field" do
|
41
40
|
lambda {
|
42
41
|
@company.phone_numbers << PhoneNumber.new(:number => '1-800-orange', :extension => '1')
|
43
|
-
|
42
|
+
}.should change{@company.audits.count}.by(2)
|
44
43
|
end
|
45
|
-
|
46
|
-
describe "updating an existing
|
44
|
+
|
45
|
+
describe "updating an existing has_many object" do
|
47
46
|
before do
|
48
47
|
@ph = PhoneNumber.new(:number => '1-800-orange', :extension => '1')
|
49
48
|
@company.phone_numbers << @ph
|
50
49
|
end
|
51
|
-
|
52
50
|
it "should add an audit to the parent instance when we update a phone number" do
|
53
|
-
lambda {@ph.update_attributes(:number => '1-800-apple')
|
51
|
+
lambda {@ph.update_attributes(:number => '1-800-apple')
|
52
|
+
}.should change{@company.audits.count}
|
54
53
|
end
|
55
|
-
|
54
|
+
|
56
55
|
describe "deleting a child_object" do
|
57
56
|
it "should add an audit to the parent instance when we delete a phone number" do
|
58
57
|
lambda {PhoneNumber.destroy(@ph)}.should change{@company.audits.count}
|
59
58
|
end
|
59
|
+
|
60
|
+
it "should mark previous audits involving this has_many object so they can not be reversed" do
|
61
|
+
ph_id = @ph.id
|
62
|
+
ph_assoc = @ph.class.to_s
|
63
|
+
|
64
|
+
PhoneNumber.destroy(@ph)
|
65
|
+
ph_audits = @company.audits.where(
|
66
|
+
{:association_id => ph_id, :association_type => ph_assoc}
|
67
|
+
)
|
68
|
+
collection = ph_audits.collect(&:undoable)
|
69
|
+
collection.uniq.should == [false]
|
70
|
+
end
|
60
71
|
end
|
61
72
|
end
|
62
73
|
end
|
63
74
|
end
|
64
|
-
|
75
|
+
|
76
|
+
describe "has_many :through" do
|
77
|
+
before do
|
78
|
+
class Company < ActiveRecord::Base
|
79
|
+
has_many :employments
|
80
|
+
has_many :people, :through => :employments
|
81
|
+
audit_enabled
|
82
|
+
end
|
83
|
+
class Person < ActiveRecord::Base
|
84
|
+
has_many :employments
|
85
|
+
has_many :companies, :through => :employments
|
86
|
+
audit_enabled
|
87
|
+
end
|
88
|
+
class Employment < ActiveRecord::Base
|
89
|
+
belongs_to :person
|
90
|
+
belongs_to :company
|
91
|
+
audit_relationship_enabled :fields => :start_date
|
92
|
+
end
|
93
|
+
@company = Company.create(:name => 'Apple')
|
94
|
+
@person = Person.create(:first_name => 'Steve')
|
95
|
+
end
|
96
|
+
|
97
|
+
it "adds an audit to the company" do
|
98
|
+
lambda {Employment.create(:person => @person,
|
99
|
+
:company => @company,
|
100
|
+
:start_date => 'yesterdayish')
|
101
|
+
}.should change{@company.audits.count}.by(1)
|
102
|
+
end
|
103
|
+
it "adds an audit to the person" do
|
104
|
+
lambda {Employment.create(:person => @person,
|
105
|
+
:company => @company,
|
106
|
+
:start_date => 'yesterdayish')
|
107
|
+
}.should change{@person.audits.count}.by(1)
|
108
|
+
end
|
109
|
+
it "increases the total audit count" do
|
110
|
+
lambda {Employment.create(:person => @person,
|
111
|
+
:company => @company,
|
112
|
+
:start_date => 'yesterdayish')
|
113
|
+
}.should change{Audit.count}.by(2)
|
114
|
+
end
|
115
|
+
|
116
|
+
describe "updating" do
|
117
|
+
before do
|
118
|
+
@emp = Employment.create(:person => @person,
|
119
|
+
:company => @company,
|
120
|
+
:start_date => 'yesterdayish')
|
121
|
+
end
|
122
|
+
it "increases the audit log for the company" do
|
123
|
+
lambda {@emp.update_attribute(:start_date, 'today')
|
124
|
+
}.should change{@company.audits.count}.by(1)
|
125
|
+
end
|
126
|
+
it "increases the audit log for the person" do
|
127
|
+
lambda {@emp.update_attribute(:start_date, 'today')
|
128
|
+
}.should change{@person.audits.count}.by(1)
|
129
|
+
end
|
130
|
+
it "increases the total audit count" do
|
131
|
+
lambda {@emp.update_attribute(:start_date, 'today')
|
132
|
+
}.should change{Audit.count}.by(2)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "deleting" do
|
137
|
+
before do
|
138
|
+
@emp = Employment.create(:person => @person,
|
139
|
+
:company => @company,
|
140
|
+
:start_date => 'yesterdayish')
|
141
|
+
end
|
142
|
+
it "increases the audit log for the company" do
|
143
|
+
lambda {Employment.destroy(@emp)
|
144
|
+
}.should change{@company.audits.count}.by(1)
|
145
|
+
end
|
146
|
+
it "increases the audit log for the person" do
|
147
|
+
lambda {Employment.destroy(@emp)
|
148
|
+
}.should change{@person.audits.count}.by(1)
|
149
|
+
end
|
150
|
+
it "increases the total audit count" do
|
151
|
+
lambda {Employment.destroy(@emp)
|
152
|
+
}.should change{Audit.count}.by(2)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
@@ -4,7 +4,7 @@ describe "Auditor" do
|
|
4
4
|
it 'adds the Auditing::Auditor module to the Audit class' do
|
5
5
|
Audit.new.should respond_to(:show_action)
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
describe "#rollback" do
|
9
9
|
before do
|
10
10
|
class School < ActiveRecord::Base
|
@@ -14,24 +14,24 @@ describe "Auditor" do
|
|
14
14
|
@school.update_attributes(:name => 'PS99')
|
15
15
|
@audit = @school.audits.first
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
it "the latest audit should be the audit we want to rollback" do
|
19
19
|
@audit.action.should == 'updated'
|
20
20
|
@audit.new_value.should == 'PS99'
|
21
21
|
@audit.old_value.should == 'PS118'
|
22
22
|
@audit.association.should == nil
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
it "performs the rollback" do
|
26
26
|
@audit.rollback
|
27
27
|
@school.reload
|
28
28
|
@school.name.should == 'PS118'
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
it "creates an audit when a rollback is performed" do
|
32
32
|
lambda { @audit.rollback }.should change { Audit.count }.by(1)
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
it "the latest audit after a rollback should contain the changed values" do
|
36
36
|
@audit.rollback
|
37
37
|
@school.reload
|
@@ -39,7 +39,7 @@ describe "Auditor" do
|
|
39
39
|
@school.audits.first.new_value.should == 'PS118'
|
40
40
|
end
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
describe "#rollback belongs_to attribute" do
|
44
44
|
before do
|
45
45
|
class Car < ActiveRecord::Base
|
@@ -51,12 +51,12 @@ describe "Auditor" do
|
|
51
51
|
end
|
52
52
|
@automaker = AutoMaker.create(:name => 'maker of fast cars')
|
53
53
|
@new_automaker = AutoMaker.create(:name => 'maker of safe cars')
|
54
|
-
|
54
|
+
|
55
55
|
@car = Car.create(:name => 'fast car', :auto_maker => @automaker)
|
56
56
|
@car.update_attributes(:auto_maker => @new_automaker)
|
57
57
|
@audit = @car.audits.first
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
it "the latest audit should be the audit we want to rollback" do
|
61
61
|
@audit.action.should == 'updated'
|
62
62
|
@audit.new_value.should == @new_automaker.id
|
@@ -64,17 +64,17 @@ describe "Auditor" do
|
|
64
64
|
@audit.association.should == nil
|
65
65
|
@car.auto_maker.should == @new_automaker
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
it "performs the rollback" do
|
69
69
|
@audit.rollback
|
70
70
|
@car.reload
|
71
71
|
@car.auto_maker.should == @automaker
|
72
72
|
end
|
73
|
-
|
73
|
+
|
74
74
|
it "creates an audit when a rollback is performed" do
|
75
75
|
lambda { @audit.rollback }.should change { Audit.count }.by(1)
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
it "the latest audit after a rollback should contain the changed values" do
|
79
79
|
@audit.rollback
|
80
80
|
@car.reload
|
@@ -82,7 +82,7 @@ describe "Auditor" do
|
|
82
82
|
@car.audits.first.new_value.should == @automaker.id
|
83
83
|
end
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
describe "rollback has_many attributes" do
|
87
87
|
before do
|
88
88
|
class Company < ActiveRecord::Base
|
@@ -99,24 +99,24 @@ describe "Auditor" do
|
|
99
99
|
@ph.update_attributes(:number => '1-800-call-apple')
|
100
100
|
@audit = @company.audits.first
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
it "the latest audit should be the audit we want to rollback" do
|
104
104
|
@audit.action.should == 'updated'
|
105
105
|
@audit.new_value.should == '1-800-call-apple'
|
106
106
|
@audit.old_value.should == '1-800-orange'
|
107
107
|
@audit.association.should == @ph
|
108
108
|
end
|
109
|
-
|
109
|
+
|
110
110
|
it "performs the rollback" do
|
111
111
|
@audit.rollback
|
112
112
|
@company.reload
|
113
113
|
@company.phone_numbers.first.number.should == '1-800-orange'
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
it "creates an audit when a rollback is performed" do
|
117
117
|
lambda { @audit.rollback }.should change { Audit.count }.by(1)
|
118
118
|
end
|
119
|
-
|
119
|
+
|
120
120
|
it "the latest audit after a rollback should contain the changed values" do
|
121
121
|
@audit.rollback
|
122
122
|
@company.reload
|
@@ -124,4 +124,4 @@ describe "Auditor" do
|
|
124
124
|
@company.audits.first.new_value.should == '1-800-orange'
|
125
125
|
end
|
126
126
|
end
|
127
|
-
end
|
127
|
+
end
|
data/spec/auditing/base_spec.rb
CHANGED
@@ -1,24 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "Auditing" do
|
4
|
-
|
4
|
+
|
5
5
|
describe "auditing default values" do
|
6
6
|
before do
|
7
7
|
class School < ActiveRecord::Base
|
8
8
|
audit_enabled
|
9
9
|
end
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
it "responds to audit_enabled when auditing is added to an AR object" do
|
13
13
|
School.should respond_to(:audit_enabled)
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
it "responds to @auditing_fields" do
|
17
17
|
School.should respond_to(:auditing_fields)
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
it "has default values when no fields are passed in" do
|
21
|
-
School.stub!(:column_names).and_return(
|
21
|
+
School.stub!(:column_names).and_return(
|
22
|
+
["id", "name", "established_on", "created_at", "updated_at"])
|
22
23
|
School.gather_fields_for_auditing.should == ["name", "established_on"]
|
23
24
|
end
|
24
25
|
end # auditing default values
|
@@ -30,21 +31,21 @@ describe "Auditing" do
|
|
30
31
|
end
|
31
32
|
School.auditing_fields.should == ['name']
|
32
33
|
end
|
33
|
-
|
34
|
+
|
34
35
|
it "accepts a single value as a string" do
|
35
36
|
class School < ActiveRecord::Base
|
36
37
|
audit_enabled :fields => 'name'
|
37
38
|
end
|
38
39
|
School.auditing_fields.should == ['name']
|
39
40
|
end
|
40
|
-
|
41
|
+
|
41
42
|
it "accepts an array of symbols" do
|
42
43
|
class School < ActiveRecord::Base
|
43
44
|
audit_enabled :fields => [:name, :established_on]
|
44
45
|
end
|
45
46
|
School.auditing_fields.should == ['name', 'established_on']
|
46
47
|
end
|
47
|
-
|
48
|
+
|
48
49
|
it "accepts an array of strings" do
|
49
50
|
class School < ActiveRecord::Base
|
50
51
|
audit_enabled :fields => ['name', 'established_on']
|
@@ -52,7 +53,7 @@ describe "Auditing" do
|
|
52
53
|
School.auditing_fields.should == ['name', 'established_on']
|
53
54
|
end
|
54
55
|
end # auditing :fields => [:foo,:bar]
|
55
|
-
|
56
|
+
|
56
57
|
describe "creating a new instance" do
|
57
58
|
before do
|
58
59
|
class School < ActiveRecord::Base
|
@@ -63,23 +64,23 @@ describe "Auditing" do
|
|
63
64
|
it "creates an audit" do
|
64
65
|
lambda { School.create(:name => 'PS118') }.should change { Audit.count }.by(1)
|
65
66
|
end
|
66
|
-
|
67
|
+
|
67
68
|
it "the first audit has an action of 'created'" do
|
68
69
|
school = School.create(:name => 'PS118')
|
69
70
|
school.audits.first.action.should == 'created'
|
70
71
|
end
|
71
|
-
|
72
|
+
|
72
73
|
it "the first audit should not be reversable" do
|
73
74
|
school = School.create(:name => 'PS118')
|
74
75
|
school.audits.first.reversable?.should == false
|
75
76
|
end
|
76
|
-
|
77
|
+
|
77
78
|
it "the audit.auditable should be the object that created the audit" do
|
78
79
|
school = School.create(:name => 'PS118')
|
79
80
|
school.audits.first.auditable.should == school
|
80
81
|
end
|
81
82
|
end # creating a new instance
|
82
|
-
|
83
|
+
|
83
84
|
describe "updating an existing record" do
|
84
85
|
before do
|
85
86
|
class School < ActiveRecord::Base
|
@@ -87,47 +88,50 @@ describe "Auditing" do
|
|
87
88
|
end
|
88
89
|
@school = School.create(:name => 'PS118')
|
89
90
|
end
|
90
|
-
|
91
|
+
|
91
92
|
it "creates an audit" do
|
92
|
-
lambda {
|
93
|
+
lambda {@school.update_attributes(:name => 'PS99')
|
94
|
+
}.should change { Audit.count }.by(1)
|
93
95
|
end
|
94
|
-
|
96
|
+
|
95
97
|
it "the first audit has an action of 'updated" do
|
96
98
|
@school.update_attributes(:name => 'PS99')
|
97
99
|
@school.audits.first.action.should == 'updated'
|
98
100
|
end
|
99
|
-
|
101
|
+
|
100
102
|
it "the first audit should be reversable" do
|
101
103
|
@school.update_attributes(:name => 'PS99')
|
102
104
|
@school.audits.first.reversable?.should == true
|
103
105
|
end
|
104
|
-
|
106
|
+
|
105
107
|
it "the first audit stored the new value" do
|
106
108
|
@school.update_attributes(:name => 'PS99')
|
107
109
|
@school.audits.first.new_value.should == 'PS99'
|
108
110
|
end
|
109
|
-
|
111
|
+
|
110
112
|
it "the first audit stored the old value" do
|
111
113
|
@school.update_attributes(:name => 'PS99')
|
112
114
|
@school.audits.first.old_value.should == 'PS118'
|
113
115
|
end
|
114
|
-
|
116
|
+
|
115
117
|
it "the audit.auditable should be the object that created the audit" do
|
116
118
|
@school.update_attributes(:name => 'PS99')
|
117
119
|
@school.audits.first.auditable.should == @school
|
118
120
|
end
|
119
|
-
|
121
|
+
|
120
122
|
describe "does not create an audit when" do
|
121
123
|
it "a value did not change" do
|
122
|
-
lambda {
|
124
|
+
lambda {@school.update_attributes(:name => 'PS118')
|
125
|
+
}.should_not change { Audit.count }
|
123
126
|
end
|
124
|
-
|
127
|
+
|
125
128
|
it "a value is not part of auditing_fields" do
|
126
|
-
lambda {
|
129
|
+
lambda {@school.update_attributes(:established_on => Time.now)
|
130
|
+
}.should_not change { Audit.count }
|
127
131
|
end
|
128
132
|
end
|
129
133
|
end # updating an existing record
|
130
|
-
|
134
|
+
|
131
135
|
describe "belongs_to relationships" do
|
132
136
|
before do
|
133
137
|
class Car < ActiveRecord::Base
|
@@ -137,24 +141,25 @@ describe "Auditing" do
|
|
137
141
|
class AutoMaker < ActiveRecord::Base
|
138
142
|
has_many :cars
|
139
143
|
end
|
140
|
-
|
144
|
+
|
141
145
|
@car = Car.create(:name => 'fast car')
|
142
146
|
@automaker = AutoMaker.create(:name => 'maker of fast cars')
|
143
147
|
end
|
144
|
-
|
148
|
+
|
145
149
|
it "creates an audit when we add a belongs_to relationship" do
|
146
|
-
lambda {
|
150
|
+
lambda {@car.update_attributes(:auto_maker => @automaker)
|
151
|
+
}.should change { Audit.count }.by(1)
|
147
152
|
end
|
148
|
-
|
153
|
+
|
149
154
|
describe "our latest audit" do
|
150
155
|
before do
|
151
156
|
@car.update_attributes(:auto_maker => @automaker)
|
152
157
|
end
|
153
|
-
|
158
|
+
|
154
159
|
it "the action should be 'updated" do
|
155
160
|
@car.audits.first.action.should == 'updated'
|
156
161
|
end
|
157
|
-
|
162
|
+
|
158
163
|
it "new_value should be an id" do
|
159
164
|
@car.audits.first.new_value.should == @automaker.id
|
160
165
|
end
|
@@ -163,17 +168,18 @@ describe "Auditing" do
|
|
163
168
|
@car.audits.first.reversable?.should == true
|
164
169
|
end
|
165
170
|
end
|
166
|
-
|
171
|
+
|
167
172
|
describe "updating a belongs_to" do
|
168
173
|
before do
|
169
174
|
@new_automaker = AutoMaker.create(:name => 'maker of safe car')
|
170
175
|
@car.update_attributes(:auto_maker => @automaker)
|
171
176
|
end
|
172
|
-
|
177
|
+
|
173
178
|
it "creates an audit when we change a belongs_to relationship" do
|
174
|
-
lambda {
|
179
|
+
lambda {@car.update_attributes(:auto_maker => @new_automaker)
|
180
|
+
}.should change { Audit.count }.by(1)
|
175
181
|
end
|
176
|
-
|
182
|
+
|
177
183
|
describe "our latest audit" do
|
178
184
|
before do
|
179
185
|
@car.update_attributes(:auto_maker => @new_automaker)
|
@@ -186,16 +192,16 @@ describe "Auditing" do
|
|
186
192
|
it "new_value should be an id" do
|
187
193
|
@car.audits.first.new_value.should == @new_automaker.id
|
188
194
|
end
|
189
|
-
|
195
|
+
|
190
196
|
it "old_value should be an id" do
|
191
197
|
@car.audits.first.old_value.should == @automaker.id
|
192
198
|
end
|
193
|
-
|
199
|
+
|
194
200
|
it "should be reversable" do
|
195
201
|
@car.audits.first.reversable?.should == true
|
196
202
|
end
|
197
203
|
end
|
198
204
|
end
|
199
205
|
end # belongs_to relationships
|
200
|
-
|
206
|
+
|
201
207
|
end
|
data/spec/schema.rb
CHANGED
@@ -36,19 +36,25 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
36
36
|
t.timestamps
|
37
37
|
end
|
38
38
|
|
39
|
+
create_table :phone_numbers do |t|
|
40
|
+
t.string :number
|
41
|
+
t.string :extension
|
42
|
+
t.string :phoneable_type
|
43
|
+
t.integer :phoneable_id
|
44
|
+
t.timestamps
|
45
|
+
end
|
46
|
+
|
39
47
|
create_table :people do |t|
|
40
48
|
t.string :first_name
|
41
49
|
t.string :last_name
|
42
50
|
t.timestamps
|
43
51
|
end
|
44
52
|
|
45
|
-
create_table :
|
46
|
-
t.
|
47
|
-
t.
|
48
|
-
t.string
|
49
|
-
t.integer :phoneable_id
|
53
|
+
create_table :employments do |t|
|
54
|
+
t.integer :person_id
|
55
|
+
t.integer :company_id
|
56
|
+
t.string :start_date
|
50
57
|
t.timestamps
|
51
58
|
end
|
52
|
-
|
53
59
|
|
54
60
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 1.0.0
|
9
|
+
version: 1.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Brad Cantin
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-16 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,10 +26,9 @@ dependencies:
|
|
26
26
|
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
28
|
segments:
|
29
|
-
- 1
|
30
29
|
- 2
|
31
|
-
-
|
32
|
-
version:
|
30
|
+
- 0
|
31
|
+
version: "2.0"
|
33
32
|
type: :development
|
34
33
|
version_requirements: *id001
|
35
34
|
description: acts_as_versioned is good. This allows an attribute level rollback instead
|