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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -5
  3. data/.rspec +2 -2
  4. data/CHANGELOG.md +80 -0
  5. data/Gemfile +3 -3
  6. data/LICENSE +21 -21
  7. data/README.md +188 -220
  8. data/Rakefile +17 -17
  9. data/activerecord-userstamp.gemspec +34 -34
  10. data/lib/active_record/userstamp.rb +30 -14
  11. data/lib/active_record/userstamp/configuration.rb +49 -0
  12. data/lib/active_record/userstamp/controller_additions.rb +38 -41
  13. data/lib/active_record/userstamp/migration_additions.rb +14 -16
  14. data/lib/active_record/userstamp/model_additions.rb +10 -3
  15. data/lib/active_record/userstamp/stampable.rb +121 -174
  16. data/lib/active_record/userstamp/stamper.rb +47 -39
  17. data/lib/active_record/userstamp/utilities.rb +18 -0
  18. data/lib/active_record/userstamp/version.rb +4 -4
  19. data/lib/activerecord/userstamp.rb +1 -1
  20. data/spec/controllers/posts_controller_spec.rb +46 -46
  21. data/spec/controllers/users_controller_spec.rb +41 -41
  22. data/spec/dummy/README.rdoc +28 -28
  23. data/spec/dummy/Rakefile +6 -6
  24. data/spec/dummy/app/assets/javascripts/application.js +13 -13
  25. data/spec/dummy/app/assets/stylesheets/application.css +15 -15
  26. data/spec/dummy/app/controllers/application_controller.rb +13 -13
  27. data/spec/dummy/app/controllers/posts_controller.rb +32 -32
  28. data/spec/dummy/app/controllers/users_controller.rb +18 -18
  29. data/spec/dummy/app/helpers/application_helper.rb +2 -2
  30. data/spec/dummy/app/models/comment.rb +5 -6
  31. data/spec/dummy/app/models/person.rb +3 -4
  32. data/spec/dummy/app/models/post.rb +14 -13
  33. data/spec/dummy/app/models/user.rb +3 -4
  34. data/spec/dummy/app/views/layouts/application.html.erb +14 -14
  35. data/spec/dummy/bin/bundle +3 -3
  36. data/spec/dummy/bin/rails +4 -4
  37. data/spec/dummy/bin/rake +4 -4
  38. data/spec/dummy/bin/setup +29 -29
  39. data/spec/dummy/config.ru +4 -4
  40. data/spec/dummy/config/application.rb +30 -30
  41. data/spec/dummy/config/boot.rb +5 -5
  42. data/spec/dummy/config/database.yml +23 -25
  43. data/spec/dummy/config/environment.rb +5 -5
  44. data/spec/dummy/config/environments/development.rb +41 -41
  45. data/spec/dummy/config/environments/production.rb +79 -79
  46. data/spec/dummy/config/environments/test.rb +37 -37
  47. data/spec/dummy/config/initializers/assets.rb +11 -11
  48. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -7
  49. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -3
  50. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -4
  51. data/spec/dummy/config/initializers/inflections.rb +16 -16
  52. data/spec/dummy/config/initializers/mime_types.rb +4 -4
  53. data/spec/dummy/config/initializers/session_store.rb +3 -3
  54. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -14
  55. data/spec/dummy/config/locales/en.yml +23 -23
  56. data/spec/dummy/config/routes.rb +56 -56
  57. data/spec/dummy/config/secrets.yml +22 -22
  58. data/spec/dummy/db/schema.rb +45 -55
  59. data/spec/dummy/public/404.html +67 -67
  60. data/spec/dummy/public/422.html +67 -67
  61. data/spec/dummy/public/500.html +66 -66
  62. data/spec/lib/configuration_spec.rb +20 -0
  63. data/spec/lib/migration_spec.rb +55 -12
  64. data/spec/lib/stamping_spec.rb +210 -170
  65. data/spec/lib/userstamp_spec.rb +7 -7
  66. data/spec/rails_helper.rb +7 -7
  67. data/spec/spec_helper.rb +97 -97
  68. data/spec/support/database_helpers.rb +20 -22
  69. metadata +6 -5
  70. data/CHANGELOG +0 -26
  71. data/spec/dummy/app/models/foo.rb +0 -3
  72. 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
@@ -1,26 +1,69 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  RSpec.describe 'Migration helpers', type: :model do
4
- class self::Random < ActiveRecord::Base
5
- stampable
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
- temporary_table(:randoms) do |t|
10
- t.userstamps
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
- with_temporary_table(:randoms) do
14
- it 'has a creator_id association' do
15
- expect(subject.has_attribute?(:creator_id)).to be true
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
- it 'has an updater_id association' do
19
- expect(subject.has_attribute?(:updater_id)).to be true
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
- it 'has a deleter_id association' do
23
- expect(subject.has_attribute?(:deleter_id)).to be false
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
@@ -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(: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
- end
22
-
23
- context 'when the stamper is an ID' do
24
- it 'sets the correct creator and updater' do
25
- User.stamper = @hera.id
26
- expect(User.stamper).to eq(@hera.id)
27
-
28
- person = Person.create(:name => "Daniel")
29
- expect(person.creator_id).to eq(@hera.id)
30
- expect(person.updater_id).to eq(@hera.id)
31
- expect(person.creator).to eq(@hera)
32
- expect(person.updater).to eq(@hera)
33
- end
34
- end
35
- end
36
-
37
- context 'when creating a Post' do
38
- context 'when the stamper is an object' do
39
- it 'sets the correct creator and updater' do
40
- expect(Person.stamper).to eq(@delynn.id)
41
-
42
- post = Post.create(:title => "Test Post - 1")
43
- expect(post.creator_id).to eq(@delynn.id)
44
- expect(post.updater_id).to eq(@delynn.id)
45
- expect(post.creator).to eq(@delynn)
46
- expect(post.updater).to eq(@delynn)
47
- end
48
- end
49
-
50
- context 'when the stamper is an ID' do
51
- it 'sets the correct creator and updater' do
52
- Person.stamper = @nicole.id
53
- expect(Person.stamper).to eq(@nicole.id)
54
-
55
- post = Post.create(:title => "Test Post - 2")
56
- expect(post.creator_id).to eq(@nicole.id)
57
- expect(post.updater_id).to eq(@nicole.id)
58
- expect(post.creator).to eq(@nicole)
59
- expect(post.updater).to eq(@nicole)
60
- end
61
- end
62
- end
63
-
64
- context 'when updating a Person' do
65
- context 'when the stamper is an object' do
66
- it 'sets the correct updater' do
67
- User.stamper = @hera
68
- expect(User.stamper).to eq(@hera.id)
69
-
70
- @delynn.name << " Berry"
71
- @delynn.save
72
- @delynn.reload
73
- expect(@delynn.creator).to eq(@zeus)
74
- expect(@delynn.updater).to eq(@hera)
75
- expect(@delynn.creator_id).to eq(@zeus.id)
76
- expect(@delynn.updater_id).to eq(@hera.id)
77
- end
78
- end
79
- end
80
-
81
- context 'when the stamper is an ID' do
82
- it 'sets the correct updater' do
83
- User.stamper = @hera.id
84
- expect(User.stamper).to eq(@hera.id)
85
-
86
- @delynn.name << " Berry"
87
- @delynn.save
88
- @delynn.reload
89
- expect(@delynn.creator_id).to eq(@zeus.id)
90
- expect(@delynn.updater_id).to eq(@hera.id)
91
- expect(@delynn.creator).to eq(@zeus)
92
- expect(@delynn.updater).to eq(@hera)
93
- end
94
- end
95
-
96
- context 'when updating a Post' do
97
- context 'when the stamper is an ID' do
98
- it 'sets the correct updater' do
99
- Person.stamper = @nicole.id
100
- expect(Person.stamper).to eq(@nicole.id)
101
-
102
- @first_post.title << " - Updated"
103
- @first_post.save
104
- @first_post.reload
105
- expect(@first_post.creator_id).to eq(@delynn.id)
106
- expect(@first_post.updater_id).to eq(@nicole.id)
107
- expect(@first_post.creator).to eq(@delynn)
108
- expect(@first_post.updater).to eq(@nicole)
109
- end
110
- end
111
-
112
- context 'when the stamper is an object' do
113
- it 'sets the correct updater' do
114
- Person.stamper = @nicole
115
- expect(Person.stamper).to eq(@nicole.id)
116
-
117
- @first_post.title << " - Updated"
118
- @first_post.save
119
- @first_post.reload
120
- expect(@first_post.creator_id).to eq(@delynn.id)
121
- expect(@first_post.updater_id).to eq(@nicole.id)
122
- expect(@first_post.creator).to eq(@delynn)
123
- expect(@first_post.updater).to eq(@nicole)
124
- end
125
- end
126
- end
127
-
128
- context 'when destroying a Post' do
129
- context 'when the stamper is an ID' do
130
- it 'sets the deleter' do
131
- expect(@first_post.deleted_at).to be_nil
132
-
133
- Person.stamper = @nicole.id
134
- expect(Person.stamper).to eq(@nicole.id)
135
-
136
- @first_post.destroy
137
- @first_post.save
138
- @first_post.reload
139
-
140
- expect(@first_post.deleted_at).to be_present
141
- expect(@first_post.deleter_id).to eq(@nicole.id)
142
- end
143
- end
144
- end
145
-
146
- context 'when no deleter column is present' do
147
- it 'does not create a deleter relation' do
148
- expect(@delynn.respond_to?('creator')).to eq(true)
149
- expect(@delynn.respond_to?('updater')).to eq(true)
150
- expect(@delynn.respond_to?('deleter')).to eq(false)
151
- end
152
- end
153
-
154
- context 'when a deleter column is present' do
155
- it 'creates a deleter relation' do
156
- expect(@first_post.respond_to?('creator')).to eq(true)
157
- expect(@first_post.respond_to?('updater')).to eq(true)
158
- expect(@first_post.respond_to?('deleter')).to eq(true)
159
- end
160
- end
161
-
162
- context 'when the deleter attribute is explicitly set' do
163
- it 'creates a deleter relation' do
164
- @foo = Foo.create
165
- expect(@foo.respond_to?('creator')).to eq(true)
166
- expect(@foo.respond_to?('updater')).to eq(true)
167
- expect(@foo.respond_to?('deleter')).to eq(true)
168
- end
169
- end
170
- end
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