activerecord-userstamp 2.1.1 → 3.0.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.
- checksums.yaml +4 -4
- data/.gitignore +5 -5
- data/.rspec +2 -2
- data/CHANGELOG.md +80 -0
- data/Gemfile +3 -3
- data/LICENSE +21 -21
- data/README.md +188 -220
- data/Rakefile +17 -17
- data/activerecord-userstamp.gemspec +34 -34
- data/lib/active_record/userstamp.rb +30 -14
- data/lib/active_record/userstamp/configuration.rb +49 -0
- data/lib/active_record/userstamp/controller_additions.rb +38 -41
- data/lib/active_record/userstamp/migration_additions.rb +14 -16
- data/lib/active_record/userstamp/model_additions.rb +10 -3
- data/lib/active_record/userstamp/stampable.rb +121 -174
- data/lib/active_record/userstamp/stamper.rb +47 -39
- data/lib/active_record/userstamp/utilities.rb +18 -0
- data/lib/active_record/userstamp/version.rb +4 -4
- data/lib/activerecord/userstamp.rb +1 -1
- data/spec/controllers/posts_controller_spec.rb +46 -46
- data/spec/controllers/users_controller_spec.rb +41 -41
- data/spec/dummy/README.rdoc +28 -28
- data/spec/dummy/Rakefile +6 -6
- data/spec/dummy/app/assets/javascripts/application.js +13 -13
- data/spec/dummy/app/assets/stylesheets/application.css +15 -15
- data/spec/dummy/app/controllers/application_controller.rb +13 -13
- data/spec/dummy/app/controllers/posts_controller.rb +32 -32
- data/spec/dummy/app/controllers/users_controller.rb +18 -18
- data/spec/dummy/app/helpers/application_helper.rb +2 -2
- data/spec/dummy/app/models/comment.rb +5 -6
- data/spec/dummy/app/models/person.rb +3 -4
- data/spec/dummy/app/models/post.rb +14 -13
- data/spec/dummy/app/models/user.rb +3 -4
- data/spec/dummy/app/views/layouts/application.html.erb +14 -14
- data/spec/dummy/bin/bundle +3 -3
- data/spec/dummy/bin/rails +4 -4
- data/spec/dummy/bin/rake +4 -4
- data/spec/dummy/bin/setup +29 -29
- data/spec/dummy/config.ru +4 -4
- data/spec/dummy/config/application.rb +30 -30
- data/spec/dummy/config/boot.rb +5 -5
- data/spec/dummy/config/database.yml +23 -25
- data/spec/dummy/config/environment.rb +5 -5
- data/spec/dummy/config/environments/development.rb +41 -41
- data/spec/dummy/config/environments/production.rb +79 -79
- data/spec/dummy/config/environments/test.rb +37 -37
- data/spec/dummy/config/initializers/assets.rb +11 -11
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -7
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -3
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -4
- data/spec/dummy/config/initializers/inflections.rb +16 -16
- data/spec/dummy/config/initializers/mime_types.rb +4 -4
- data/spec/dummy/config/initializers/session_store.rb +3 -3
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -14
- data/spec/dummy/config/locales/en.yml +23 -23
- data/spec/dummy/config/routes.rb +56 -56
- data/spec/dummy/config/secrets.yml +22 -22
- data/spec/dummy/db/schema.rb +45 -55
- data/spec/dummy/public/404.html +67 -67
- data/spec/dummy/public/422.html +67 -67
- data/spec/dummy/public/500.html +66 -66
- data/spec/lib/configuration_spec.rb +20 -0
- data/spec/lib/migration_spec.rb +55 -12
- data/spec/lib/stamping_spec.rb +210 -170
- data/spec/lib/userstamp_spec.rb +7 -7
- data/spec/rails_helper.rb +7 -7
- data/spec/spec_helper.rb +97 -97
- data/spec/support/database_helpers.rb +20 -22
- metadata +6 -5
- data/CHANGELOG +0 -26
- data/spec/dummy/app/models/foo.rb +0 -3
- data/spec/lib/compatibility_stamping_spec.rb +0 -69
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'Configuration' do
|
4
|
+
describe '.configure' do
|
5
|
+
it 'yields a block with the config argument' do
|
6
|
+
block = nil
|
7
|
+
ActiveRecord::Userstamp.configure do |config|
|
8
|
+
block = config
|
9
|
+
end
|
10
|
+
expect(block).to be(ActiveRecord::Userstamp::Configuration)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '.config' do
|
15
|
+
it 'matches default_stamper and default_stamper_class' do
|
16
|
+
config = ActiveRecord::Userstamp.config
|
17
|
+
expect(config.default_stamper_class.name).to eq(config.default_stamper)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/lib/migration_spec.rb
CHANGED
@@ -1,26 +1,69 @@
|
|
1
1
|
require 'rails_helper'
|
2
2
|
|
3
3
|
RSpec.describe 'Migration helpers', type: :model do
|
4
|
-
|
5
|
-
|
4
|
+
after do
|
5
|
+
ActiveRecord::Userstamp.configure do |config|
|
6
|
+
config.creator_attribute = :creator_id
|
7
|
+
config.updater_attribute = :updater_id
|
8
|
+
config.deleter_attribute = :deleter_id
|
9
|
+
end
|
6
10
|
end
|
7
|
-
subject { self.class::Random.new }
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
context 'when default attribute names are used' do
|
13
|
+
class self::DefaultRandom < ActiveRecord::Base
|
14
|
+
stampable
|
15
|
+
end
|
16
|
+
subject { self.class::DefaultRandom.new }
|
17
|
+
|
18
|
+
temporary_table(:default_randoms) do |t|
|
19
|
+
t.userstamps(null: false)
|
20
|
+
end
|
21
|
+
|
22
|
+
with_temporary_table(:default_randoms) do
|
23
|
+
it 'has a creator_id association' do
|
24
|
+
expect(subject.has_attribute?(:creator_id)).to be true
|
25
|
+
expect(subject.class.columns.find {|c| c.name == 'creator_id' }.null).to be false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'has an updater_id association' do
|
29
|
+
expect(subject.has_attribute?(:updater_id)).to be true
|
30
|
+
expect(subject.class.columns.find {|c| c.name == 'updater_id' }.null).to be false
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'has a deleter_id association' do
|
34
|
+
expect(subject.has_attribute?(:deleter_id)).to be true
|
35
|
+
expect(subject.class.columns.find {|c| c.name == 'deleter_id' }.null).to be false
|
36
|
+
end
|
37
|
+
end
|
11
38
|
end
|
12
39
|
|
13
|
-
|
14
|
-
|
15
|
-
|
40
|
+
context 'when overridden attribute names are used' do
|
41
|
+
class self::OverriddenRandom < ActiveRecord::Base
|
42
|
+
stampable
|
16
43
|
end
|
44
|
+
subject { self.class::OverriddenRandom.new }
|
17
45
|
|
18
|
-
|
19
|
-
|
46
|
+
temporary_table(:overridden_randoms) do |t|
|
47
|
+
ActiveRecord::Userstamp.configure do |config|
|
48
|
+
config.creator_attribute = :created_by
|
49
|
+
config.updater_attribute = :updated_by
|
50
|
+
config.deleter_attribute = :deleted_by
|
51
|
+
end
|
52
|
+
t.userstamps
|
20
53
|
end
|
21
54
|
|
22
|
-
|
23
|
-
|
55
|
+
with_temporary_table(:overridden_randoms) do
|
56
|
+
it 'has a created_by attribute' do
|
57
|
+
expect(subject.has_attribute?(:created_by)).to be true
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'has an updated_by attribute' do
|
61
|
+
expect(subject.has_attribute?(:updated_by)).to be true
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'has a deleted_by attribute' do
|
65
|
+
expect(subject.has_attribute?(:deleted_by)).to be true
|
66
|
+
end
|
24
67
|
end
|
25
68
|
end
|
26
69
|
end
|
data/spec/lib/stamping_spec.rb
CHANGED
@@ -1,170 +1,210 @@
|
|
1
|
-
require 'rails_helper'
|
2
|
-
|
3
|
-
RSpec.describe 'Stamping', type: :model do
|
4
|
-
before(:each) do
|
5
|
-
reset_to_defaults
|
6
|
-
User.stamper = @zeus
|
7
|
-
Person.stamper = @delynn
|
8
|
-
end
|
9
|
-
|
10
|
-
context 'when creating a Person' do
|
11
|
-
context 'when the stamper is an object' do
|
12
|
-
it 'sets the correct creator and updater' do
|
13
|
-
expect(User.stamper).to eq(@zeus.id)
|
14
|
-
|
15
|
-
person = Person.create(:
|
16
|
-
expect(person.creator_id).to eq(@zeus.id)
|
17
|
-
expect(person.updater_id).to eq(@zeus.id)
|
18
|
-
expect(person.creator).to eq(@zeus)
|
19
|
-
expect(person.updater).to eq(@zeus)
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
expect(
|
69
|
-
|
70
|
-
|
71
|
-
@
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
expect(
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
expect(
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
expect(
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
expect(@delynn.
|
149
|
-
expect(@delynn.
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'Stamping', type: :model do
|
4
|
+
before(:each) do
|
5
|
+
reset_to_defaults
|
6
|
+
User.stamper = @zeus
|
7
|
+
Person.stamper = @delynn
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'when creating a Person' do
|
11
|
+
context 'when the stamper is an object' do
|
12
|
+
it 'sets the correct creator and updater' do
|
13
|
+
expect(User.stamper).to eq(@zeus.id)
|
14
|
+
|
15
|
+
person = Person.create(name: 'David')
|
16
|
+
expect(person.creator_id).to eq(@zeus.id)
|
17
|
+
expect(person.updater_id).to eq(@zeus.id)
|
18
|
+
expect(person.creator).to eq(@zeus)
|
19
|
+
expect(person.updater).to eq(@zeus)
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when a creator is specified' do
|
23
|
+
it 'does not reset the creator' do
|
24
|
+
expect(User.stamper).to eq(@zeus.id)
|
25
|
+
|
26
|
+
person = Person.create(name: 'David', creator: @hera)
|
27
|
+
expect(person.creator_id).to eq(@hera.id)
|
28
|
+
expect(person.updater_id).to eq(@zeus.id)
|
29
|
+
expect(person.creator).to eq(@hera)
|
30
|
+
expect(person.updater).to eq(@zeus)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when saving without validations' do
|
35
|
+
it 'sets the correct creator and updater' do
|
36
|
+
expect(User.stamper).to eq(@zeus.id)
|
37
|
+
|
38
|
+
person = Person.new(name: 'David')
|
39
|
+
person.save(validate: false)
|
40
|
+
expect(person.creator_id).to eq(@zeus.id)
|
41
|
+
expect(person.updater_id).to eq(@zeus.id)
|
42
|
+
expect(person.creator).to eq(@zeus)
|
43
|
+
expect(person.updater).to eq(@zeus)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when temporarily disabling stampng' do
|
48
|
+
it 'does not set the creator and updater' do
|
49
|
+
expect(User.stamper).to eq(@zeus.id)
|
50
|
+
|
51
|
+
Person.without_stamps do
|
52
|
+
person = Person.create(name: 'David')
|
53
|
+
expect(person.creator_id).to be_nil
|
54
|
+
expect(person.updater_id).to be_nil
|
55
|
+
expect(person.creator).to be_nil
|
56
|
+
expect(person.updater).to be_nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'when the stamper is an ID' do
|
63
|
+
it 'sets the correct creator and updater' do
|
64
|
+
User.stamper = @hera.id
|
65
|
+
expect(User.stamper).to eq(@hera.id)
|
66
|
+
|
67
|
+
person = Person.create(name: 'Daniel')
|
68
|
+
expect(person.creator_id).to eq(@hera.id)
|
69
|
+
expect(person.updater_id).to eq(@hera.id)
|
70
|
+
expect(person.creator).to eq(@hera)
|
71
|
+
expect(person.updater).to eq(@hera)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when creating a Post' do
|
77
|
+
context 'when the stamper is an object' do
|
78
|
+
it 'sets the correct creator and updater' do
|
79
|
+
expect(Person.stamper).to eq(@delynn.id)
|
80
|
+
|
81
|
+
post = Post.create(title: 'Test Post - 1')
|
82
|
+
expect(post.creator_id).to eq(@delynn.id)
|
83
|
+
expect(post.updater_id).to eq(@delynn.id)
|
84
|
+
expect(post.creator).to eq(@delynn)
|
85
|
+
expect(post.updater).to eq(@delynn)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'when the stamper is an ID' do
|
90
|
+
it 'sets the correct creator and updater' do
|
91
|
+
Person.stamper = @nicole.id
|
92
|
+
expect(Person.stamper).to eq(@nicole.id)
|
93
|
+
|
94
|
+
post = Post.create(title: 'Test Post - 2')
|
95
|
+
expect(post.creator_id).to eq(@nicole.id)
|
96
|
+
expect(post.updater_id).to eq(@nicole.id)
|
97
|
+
expect(post.creator).to eq(@nicole)
|
98
|
+
expect(post.updater).to eq(@nicole)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context 'when updating a Person' do
|
104
|
+
context 'when the stamper is an object' do
|
105
|
+
it 'sets the correct updater' do
|
106
|
+
User.stamper = @hera
|
107
|
+
expect(User.stamper).to eq(@hera.id)
|
108
|
+
|
109
|
+
@delynn.name << " Berry"
|
110
|
+
@delynn.save
|
111
|
+
@delynn.reload
|
112
|
+
expect(@delynn.creator).to eq(@zeus)
|
113
|
+
expect(@delynn.updater).to eq(@hera)
|
114
|
+
expect(@delynn.creator_id).to eq(@zeus.id)
|
115
|
+
expect(@delynn.updater_id).to eq(@hera.id)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'when temporarily disabling stamping' do
|
120
|
+
it 'does not set the updater' do
|
121
|
+
User.stamper = @zeus
|
122
|
+
expect(User.stamper).to eq(@zeus.id)
|
123
|
+
|
124
|
+
original_updater = @delynn.updater
|
125
|
+
Person.without_stamps do
|
126
|
+
@delynn.name << " Berry"
|
127
|
+
@delynn.save
|
128
|
+
@delynn.reload
|
129
|
+
expect(@delynn.creator).to eq(@zeus)
|
130
|
+
expect(@delynn.updater).to eq(original_updater)
|
131
|
+
expect(@delynn.creator_id).to eq(@zeus.id)
|
132
|
+
expect(@delynn.updater_id).to eq(original_updater.id)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context 'when the stamper is an ID' do
|
139
|
+
it 'sets the correct updater' do
|
140
|
+
User.stamper = @hera.id
|
141
|
+
expect(User.stamper).to eq(@hera.id)
|
142
|
+
|
143
|
+
@delynn.name << " Berry"
|
144
|
+
@delynn.save
|
145
|
+
@delynn.reload
|
146
|
+
expect(@delynn.creator_id).to eq(@zeus.id)
|
147
|
+
expect(@delynn.updater_id).to eq(@hera.id)
|
148
|
+
expect(@delynn.creator).to eq(@zeus)
|
149
|
+
expect(@delynn.updater).to eq(@hera)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
context 'when updating a Post' do
|
154
|
+
context 'when the stamper is an ID' do
|
155
|
+
it 'sets the correct updater' do
|
156
|
+
Person.stamper = @nicole.id
|
157
|
+
expect(Person.stamper).to eq(@nicole.id)
|
158
|
+
|
159
|
+
@first_post.title << " - Updated"
|
160
|
+
@first_post.save
|
161
|
+
@first_post.reload
|
162
|
+
expect(@first_post.creator_id).to eq(@delynn.id)
|
163
|
+
expect(@first_post.updater_id).to eq(@nicole.id)
|
164
|
+
expect(@first_post.creator).to eq(@delynn)
|
165
|
+
expect(@first_post.updater).to eq(@nicole)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
context 'when the stamper is an object' do
|
170
|
+
it 'sets the correct updater' do
|
171
|
+
Person.stamper = @nicole
|
172
|
+
expect(Person.stamper).to eq(@nicole.id)
|
173
|
+
|
174
|
+
@first_post.title << " - Updated"
|
175
|
+
@first_post.save
|
176
|
+
@first_post.reload
|
177
|
+
expect(@first_post.creator_id).to eq(@delynn.id)
|
178
|
+
expect(@first_post.updater_id).to eq(@nicole.id)
|
179
|
+
expect(@first_post.creator).to eq(@delynn)
|
180
|
+
expect(@first_post.updater).to eq(@nicole)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context 'when destroying a Post' do
|
186
|
+
context 'when the stamper is an ID' do
|
187
|
+
it 'sets the deleter' do
|
188
|
+
expect(@first_post.deleted_at).to be_nil
|
189
|
+
|
190
|
+
Person.stamper = @nicole.id
|
191
|
+
expect(Person.stamper).to eq(@nicole.id)
|
192
|
+
|
193
|
+
@first_post.destroy
|
194
|
+
@first_post.save
|
195
|
+
@first_post.reload
|
196
|
+
|
197
|
+
expect(@first_post.deleted_at).to be_present
|
198
|
+
expect(@first_post.deleter_id).to eq(@nicole.id)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context 'when a deleter attribute is specified' do
|
204
|
+
it 'creates a deleter relation' do
|
205
|
+
expect(@first_post.respond_to?('creator')).to eq(true)
|
206
|
+
expect(@first_post.respond_to?('updater')).to eq(true)
|
207
|
+
expect(@first_post.respond_to?('deleter')).to eq(true)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|