activerecord-userstamp 2.1.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aeb470b53728ed92b0b74621e4bd002a41d56275
4
- data.tar.gz: c3c5f02aa4d41923407b92c17e99e6ceba1889a7
3
+ metadata.gz: 6113fc9e7a0eec6ad0ebe4f883c2b07d0f10d3f2
4
+ data.tar.gz: 481179a5004384a5f8173cd04efaf69f7bee1e37
5
5
  SHA512:
6
- metadata.gz: fcd728fc959bb391efb41b47259541d385b1146039992f6a9860f34dc601fadef888258e3f080246a155aa40ad19bfb6452dc84fcf20632322e6fc8b5148f461
7
- data.tar.gz: 43e02f3aea8be19d8a11148603b3cc6e875dce661ee3f55923594091140b3b928bc634773b81163c307dc1af772d77b56a86d57f361e0af38c5cd46d848e0f83
6
+ metadata.gz: 4a0260351476dde1e1893b9182bff0c19eb8118c9fa5d30db8b1be28a22dcdce1ea4a7a346e39bd0ff81a42e91a1a44b29e4c2f5d1ccacba485f12b0451f2d33
7
+ data.tar.gz: 8661e0637d5ee7231ac426243f9b1ceec0e03b42ac980d269462f243b054feaadb3717a7c3d2dc5eca5e0ae429bd436215d7c1d0f7c8237c0debf363a3711d1e
data/.gitignore CHANGED
@@ -1,5 +1,5 @@
1
- *.log
2
- *.sqlite3
3
- /.rdoc
4
- /.bundle
5
- /spec/examples.txt
1
+ *.log
2
+ *.sqlite3
3
+ /.rdoc
4
+ /spec/examples.txt
5
+ /.bundle
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --color
2
- --require spec_helper
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,80 @@
1
+ # Changelog
2
+
3
+ ## 3.0.0 (10-7-2015)
4
+ * Joel Low - Remove `compatibility_mode`. Use the `creator_attribute`,
5
+ `updater_attribute`, and `deleter_attribute` configuration options instead.
6
+ * Joel Low - The table definition migration helper should follow the gem configuration
7
+ when generating column names.
8
+ * Joel Low - When deciding whether to generate a `deleter_id` column, check the gem
9
+ configuration for the `deleter_attribute` configuration option instead of
10
+ relying on the user to specify in every migration.
11
+ * Joel Low - Remove the `deleter` option from the `stampable` model declaration.
12
+ * Joel Low - Remove the `creator_attribute`, `updater_attribute`, and `deleter_attribute`
13
+ options from the `stampable` model declaration. All models will follow
14
+ the gem configuration.
15
+ * Joel Low - Added the `default_stamper` configuration option. The controller will
16
+ automatically stamp using that model.
17
+ * Joel Low - Additional attributes passed to `stampable` would be passed to the
18
+ underlying `belongs_to` association.
19
+ * Joel Low - Declare the creator/updater/deleter setter callbacks directly in
20
+ `ActiveRecord::Base`. So now, all models will automatically be stamped
21
+ when the creator/updater/deleter attributes are present. This mirrors
22
+ `ActiveRecord::Timestamp` behaviour.
23
+ * Joel Low - Automatically declare the creator/updater/deleter associations on every
24
+ model. To add additional configuration options, simply call `stampable`.
25
+ This can be called multiple times per model; the last call takes effect.
26
+ * Joel Low - Remove support for `serialized_attributes`. It will be removed in Rails 5.
27
+
28
+ ## 2.1.1 (9-7-2015)
29
+
30
+ * Chris Hilton - Only set updater attribute if the record has changed or contains a
31
+ serialized attribute.
32
+ * Chris Hilton - Support `:with_deleted` in the `stampable` declaration.
33
+ * Chris Hilton - Only set the creator attribute if it is blank.
34
+ * Chris Branson - Fix deprecation warning in `serialized_attributes`.
35
+ * Joel Low - Trigger the updater/creator stamping before saving, so that the correct
36
+ users are stamped even if validation was not run.
37
+ * Joel Low - Allow extra parameters to be passed to the migration helpers.
38
+
39
+ ## 2.1.0 (28-3-2014)
40
+ * Thomas von Deyen - Do not automatically make every class stampable.
41
+
42
+ ## 2.0.2 (11-8-2011)
43
+ * Chris Hilton - Set the creator/updater attributes before validation, so that they can
44
+ be checked as part of validations.
45
+ * Alex - Specify that the stampable class is camelized from the given symbol,
46
+ not just capitalized, to follow ActiveRecord convention.
47
+ ## 2.0.1 (8-10-2010)
48
+ * Michael Grosser - Make stampable define the deleter association and before filter whenever
49
+ a :deleter_attribute has been passed in options, or :deleter => true is
50
+ passed, or Caboose::Acts::Paranoid is defined. This makes :deleter
51
+ functionality useable to people who aren't use acts_as_paranoid.
52
+ * Michael Grosser - do not leave record_userstamp turned on when an exception occurs inside
53
+ the `without_stamps` block
54
+
55
+ ## 2.0 (2-17-2008)
56
+ * Ben Wyrosdick - Added a migration helper that gives migration scripts a <tt>userstamps</tt>
57
+ method.
58
+ * Marshall Roch - Stamping can be temporarily turned off using the 'without_stamps' class
59
+ method.
60
+ Example:
61
+ Post.without_stamps do
62
+ post = Post.find(params[:id])
63
+ post.update_attributes(params[:post])
64
+ post.save
65
+ end
66
+
67
+ * Models that should receive updates made by 'stampers' now use the acts_as_stampable class
68
+ method. This sets up the belongs_to relationships and also injects private methods for use by
69
+ the individual callback filter methods.
70
+
71
+ * Models that are responsible for updating now use the acts_as_stamper class method. This
72
+ injects the stamper= and stamper methods that are thread safe and should be updated per
73
+ request by a controller.
74
+
75
+ * The Userstamp module is now meant to be included with one of your project's controllers (the
76
+ Application Controller is recommended). It creates a before filter called 'set_stampers' that
77
+ is responsible for setting all the current Stampers.
78
+
79
+ ## 1.0 (01-18-2006)
80
+ * Initial Release
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- Copyright (c) 2006-2008 DeLynn Berry
2
- Copyright (c) 2014-2015 Joel Low
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining
5
- a copy of this software and associated documentation files (the
6
- "Software"), to deal in the Software without restriction, including
7
- without limitation the rights to use, copy, modify, merge, publish,
8
- distribute, sublicense, and/or sell copies of the Software, and to
9
- permit persons to whom the Software is furnished to do so, subject to
10
- the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be
13
- included in all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright (c) 2006-2008 DeLynn Berry
2
+ Copyright (c) 2014-2015 Joel Low
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,220 +1,188 @@
1
- # ActiveRecord-Userstamp [![Build Status](https://travis-ci.org/lowjoel/activerecord-userstamp.svg)](https://travis-ci.org/lowjoel/activerecord-userstamp) [![Coverage Status](https://coveralls.io/repos/lowjoel/activerecord-userstamp/badge.svg?branch=master&service=github)](https://coveralls.io/github/lowjoel/activerecord-userstamp?branch=master) [![Code Climate](https://codeclimate.com/github/lowjoel/activerecord-userstamp/badges/gpa.svg)](https://codeclimate.com/github/lowjoel/activerecord-userstamp)
2
-
3
- ## Overview
4
-
5
- Userstamp extends `ActiveRecord::Base` to add automatic updating of `creator`, `updater`, and
6
- `deleter` attributes. It is based loosely on `ActiveRecord::Timestamp`.
7
-
8
- Two class methods (`model_stamper` and `stampable`) are implemented in this gem. The `model_stamper`
9
- method is used in models that are responsible for creating, updating, or deleting other objects.
10
- Typically this would be the `User` model of your application. The `stampable` method is used in
11
- models that are subject to being created, updated, or deleted by stampers.
12
-
13
- ## Forkception
14
-
15
- This is a fork of:
16
-
17
- - the [magiclabs-userstamp](https://github.com/magiclabs/userstamp) gem
18
- - which is a fork of [Michael Grosser's](https://github.com/grosser)
19
- [userstamp gem] (https://github.com/grosser/userstamp)
20
- - which is a fork of the original [userstamp plugin](https://github.com/delynn/userstamp) by
21
- [delynn](https://github.com/delynn)
22
-
23
- In addition to these, I have cherry picked ideas and changes from the following forks:
24
-
25
- - [simplificator](https://github.com/simplificator/userstamp)
26
- - [akm](https://github.com/akm/magic_userstamp)
27
- - [konvenit](https://github.com/konvenit/userstamp)
28
-
29
- Finally, this gem only supports Ruby 2.0 and above. Yes, you really should upgrade to a supported
30
- version of Ruby.
31
-
32
- ## Features
33
- ### Soft-deletes
34
- The reason for this is because the original userstamp plugin does not support databases utilising
35
- soft deletes. They are not tested explicitly within this gem, but it is expected to work with the
36
- following gems:
37
-
38
- - [acts_as_paranoid](https://github.com/ActsAsParanoid/acts_as_paranoid)
39
- - [paranoia](https://github.com/radar/paranoia)
40
-
41
- The `stampable` method has been modified to allow additional arguments to be passed to the
42
- creator/updater relations. This allows declarations like:
43
-
44
- ```ruby
45
- stampable with_deleted: true
46
- ```
47
-
48
- to result in a `belongs_to` relation which looks like:
49
-
50
- ```ruby
51
- belongs_to :creator, class_name: '::User', foreign_key: :created_by, with_deleted: true
52
- ```
53
-
54
- Do create a ticket if it is broken, with a pull-request if possible.
55
-
56
- ### Customisable column names/types
57
- While examining the userstamp gem's network on Github, it was noticed that quite a few forks were
58
- made to allow customisability in the name and type of the column with the database migration.
59
-
60
- This gem now supports customised column names.
61
-
62
- ### Saving before validation
63
- This fork includes changes to perform model stamping before validation. This allows models to
64
- enforce the presence of stamp attributes:
65
-
66
- ```ruby
67
- validates :created_by, :presence => true
68
- validates :updated_by, :presence => true
69
- ```
70
-
71
- Furthermore, the `creator` attribute is set only if the value is blank allowing for a manual
72
- override.
73
-
74
- ## Usage
75
- Assume that we are building a blog application, with User and Post objects. Add the following
76
- to the application's Gemfile:
77
-
78
- ```ruby
79
- gem 'activerecord-userstamp'
80
- ```
81
-
82
- Ensure that each model has a set of columns for creators, updaters, and deleters (if applicable.)
83
-
84
- ```ruby
85
- class CreateUsers < ActiveRecord::Migration
86
- def self.up
87
- create_table :users, :force => true do |t|
88
- ...
89
- t.userstamps # use t.userstamps(true) if you also want 'deleter_id'
90
- end
91
- end
92
-
93
- def self.down
94
- drop_table :users
95
- end
96
- end
97
-
98
- class CreatePosts < ActiveRecord::Migration
99
- def self.up
100
- create_table :posts, :force => true do |t|
101
- ...
102
- t.userstamps # use t.userstamps(true) if you also want 'deleter_id'
103
- end
104
- end
105
-
106
- def self.down
107
- drop_table :posts
108
- end
109
- end
110
- ```
111
-
112
- Declare the stamper on the User model:
113
-
114
- ```ruby
115
- class User < ActiveRecord::Base
116
- model_stamper
117
- end
118
- ```
119
-
120
- And declare that the Posts are stampable:
121
-
122
- ```ruby
123
- class Post < ActiveRecord::Base
124
- stampable
125
- end
126
- ```
127
-
128
- If your stamper is called `User`, that's it; you're done.
129
-
130
- ## Customisation
131
- More than likely you want all your associations setup on your stamped objects, and that's where the
132
- `stampable` class method comes in. So in our example we'll want to use this method in both our
133
- User and Post classes:
134
-
135
- ```ruby
136
- class User < ActiveRecord::Base
137
- model_stamper
138
- stampable
139
- end
140
-
141
- class Post < ActiveRecord::Base
142
- stampable
143
- end
144
- ```
145
-
146
- Okay, so what all have we done? The `model_stamper` class method injects two methods into the
147
- `User` class. They are `#stamper=` and `#stamper` and look like this:
148
-
149
- def stamper=(object)
150
- object_stamper = if object.is_a?(ActiveRecord::Base)
151
- object.send("#{object.class.primary_key}".to_sym)
152
- else
153
- object
154
- end
155
-
156
- Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
157
- end
158
-
159
- def stamper
160
- Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
161
- end
162
-
163
- The `stampable` method allows you to customize what columns will get stamped, and also creates the
164
- `creator`, `updater`, and `deleter` associations.
165
-
166
- The Userstamp module that we included into our ApplicationController uses the setter method to
167
- set which user is currently making the request. By default the 'set_stampers' method works perfectly
168
- with the RestfulAuthentication[http://svn.techno-weenie.net/projects/plugins/restful_authentication] plug-in:
169
-
170
- def set_stampers
171
- User.stamper = self.current_user
172
- end
173
-
174
- If you aren't using ActsAsAuthenticated, then you need to create your own version of the
175
- <tt>set_stampers</tt> method in the controller where you've included the Userstamp module.
176
-
177
- Now, let's get back to the Stampable module (since it really is the interesting one). The Stampable
178
- module sets up before_* filters that are responsible for setting those attributes at the appropriate
179
- times. It also creates the belongs_to relationships for you.
180
-
181
- If you need to customize the columns that are stamped, the <tt>stampable</tt> method can be
182
- completely customized. Here's an quick example:
183
-
184
- class Post < ActiveRecord::Base
185
- stampable :stamper_class_name => :person,
186
- :creator_attribute => :create_user,
187
- :updater_attribute => :update_user,
188
- :deleter_attribute => :delete_user,
189
- :deleter => true,
190
- :with_deleted => true
191
- end
192
-
193
- ## Upgrading
194
- ### Upgrading from delynn's 1.x/2.x with `compatibility_mode`
195
- The major difference between 1.x and 2.x is the naming of the columns. This version of the gem
196
- allows specifying the name of the column from the gem configuration.
197
-
198
- Furthermore, there is no need to include the `Userstamp` module in `ApplicationController`.
199
-
200
- However, this is where the bulk of the work is: since this is a fork of insphire's gem, where he
201
- has removed making every `ActiveRecord::Base` subclass automatically a
202
-
203
- ### Upgrading from magiclabs-userstamp
204
-
205
- There is no need to include the `Userstamp` module in `ApplicationController`.
206
-
207
- ## Tests
208
- Run
209
-
210
- $ bundle exec rspec
211
-
212
- ## Authors
213
- - [DeLynn Berry](http://delynnberry.com/): The original idea for this plugin came from the Rails
214
- Wiki article entitled
215
- [Extending ActiveRecord](http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample)
216
- - [Michael Grosser](http://pragmatig.com)
217
- - [John Dell](http://blog.spovich.com/)
218
- - [Chris Hilton](https://github.com/chrismhilton)
219
- - [Thomas von Deyen](https://github.com/tvdeyen)
220
- - [Joel Low](http://joelsplace.sg)
1
+ # ActiveRecord-Userstamp [![Build Status](https://travis-ci.org/lowjoel/activerecord-userstamp.svg)](https://travis-ci.org/lowjoel/activerecord-userstamp) [![Coverage Status](https://coveralls.io/repos/lowjoel/activerecord-userstamp/badge.svg?branch=master&service=github)](https://coveralls.io/github/lowjoel/activerecord-userstamp?branch=master) [![Code Climate](https://codeclimate.com/github/lowjoel/activerecord-userstamp/badges/gpa.svg)](https://codeclimate.com/github/lowjoel/activerecord-userstamp)
2
+
3
+ ## Overview
4
+
5
+ Userstamp extends `ActiveRecord::Base` to add automatic updating of `creator`, `updater`, and
6
+ `deleter` attributes. It is based loosely on `ActiveRecord::Timestamp`.
7
+
8
+ Two class methods (`model_stamper` and `stampable`) are implemented in this gem. The `model_stamper`
9
+ method is used in models that are responsible for creating, updating, or deleting other objects.
10
+ Typically this would be the `User` model of your application. The `stampable` method is used in
11
+ models that are subject to being created, updated, or deleted by stampers.
12
+
13
+ ## Forkception
14
+
15
+ This is a fork of:
16
+
17
+ - the [magiclabs-userstamp](https://github.com/magiclabs/userstamp) gem
18
+ - which is a fork of [Michael Grosser's](https://github.com/grosser)
19
+ [userstamp gem] (https://github.com/grosser/userstamp)
20
+ - which is a fork of the original [userstamp plugin](https://github.com/delynn/userstamp) by
21
+ [delynn](https://github.com/delynn)
22
+
23
+ In addition to these, I have cherry picked ideas and changes from the following forks:
24
+
25
+ - [simplificator](https://github.com/simplificator/userstamp)
26
+ - [akm](https://github.com/akm/magic_userstamp)
27
+ - [konvenit](https://github.com/konvenit/userstamp)
28
+
29
+ Finally, this gem only supports Ruby 2.0 and above. Yes, you really should upgrade to a supported
30
+ version of Ruby.
31
+
32
+ ## Features
33
+ ### Soft-deletes
34
+ The reason for this is because the original userstamp plugin does not support databases utilising
35
+ soft deletes. They are not tested explicitly within this gem, but it is expected to work with the
36
+ following gems:
37
+
38
+ - [acts_as_paranoid](https://github.com/ActsAsParanoid/acts_as_paranoid)
39
+ - [paranoia](https://github.com/radar/paranoia)
40
+
41
+ The `stampable` method has been modified to allow additional arguments to be passed to the
42
+ creator/updater relations. This allows declarations like:
43
+
44
+ ```ruby
45
+ stampable with_deleted: true
46
+ ```
47
+
48
+ to result in a `belongs_to` relation which looks like:
49
+
50
+ ```ruby
51
+ belongs_to :creator, class_name: '::User', foreign_key: :created_by, with_deleted: true
52
+ ```
53
+
54
+ Do create a ticket if it is broken, with a pull-request if possible.
55
+
56
+ ### Customisable column names/types
57
+ While examining the userstamp gem's network on Github, it was noticed that quite a few forks were
58
+ made to allow customisability in the name and type of the column with the database migration.
59
+
60
+ This gem now supports customised column names.
61
+
62
+ ### Saving before validation
63
+ This fork includes changes to perform model stamping before validation. This allows models to
64
+ enforce the presence of stamp attributes:
65
+
66
+ ```ruby
67
+ validates :created_by, presence: true
68
+ validates :updated_by, presence: true
69
+ ```
70
+
71
+ Furthermore, the `creator` attribute is set only if the value is blank allowing for a manual
72
+ override.
73
+
74
+ ## Usage
75
+ Assume that we are building a blog application, with User and Post objects. Add the following
76
+ to the application's Gemfile:
77
+
78
+ ```ruby
79
+ gem 'activerecord-userstamp'
80
+ ```
81
+
82
+ Define an initializer in your Rails application to configure the gem:
83
+
84
+ ```ruby
85
+ ActiveRecord::Userstamp.configure do |config|
86
+ # config.default_stamper = 'User'
87
+ # config.creator_attribute = :creator_id
88
+ # config.updater_attribute = :updater_id
89
+ config.deleter_attribute = nil
90
+ end
91
+ ```
92
+
93
+ By default, `:deleter_attribute` is set to `:deleter_id` for soft deletes. If you are not using
94
+ soft deletes, you can set the attribute no `nil`.
95
+
96
+ Ensure that each model has a set of columns for creators, updaters, and deleters (if applicable.)
97
+
98
+ ```ruby
99
+ class CreateUsers < ActiveRecord::Migration
100
+ def change
101
+ create_table :users do |t|
102
+ ...
103
+ t.userstamps
104
+ end
105
+ end
106
+ end
107
+
108
+ class CreatePosts < ActiveRecord::Migration
109
+ def change
110
+ create_table :posts do |t|
111
+ ...
112
+ t.userstamps
113
+ end
114
+ end
115
+ end
116
+ ```
117
+
118
+ Declare the stamper on the User model:
119
+
120
+ ```ruby
121
+ class User < ActiveRecord::Base
122
+ model_stamper
123
+ end
124
+ ```
125
+
126
+ If your stamper is called `User`, that's it; you're done.
127
+
128
+ ## Customisation
129
+ The association which is created on each of the `creator_id`, `updater_id`, and `deleter_id` can
130
+ be customised. Also, the stamper used by each class can also be customised. For this purpose, the
131
+ `ActiveRecord::Base.stampable` method can be used:
132
+
133
+ ```ruby
134
+ class Post < ActiveRecord::Base
135
+ stampable
136
+ end
137
+ ```
138
+
139
+ The `stampable` method allows you to customize the `creator`, `updater`, and `deleter` associations.
140
+ It also allows you to specify the name of the stamper for the class being declared. Any additional
141
+ arguments are passed to the `belongs_to` declaration.
142
+
143
+ ## Upgrading
144
+ ### Upgrading from delynn's 1.x/2.x with `compatibility_mode`
145
+ The major difference between 1.x and 2.x is the naming of the columns. This version of the gem
146
+ allows specifying the name of the column from the gem configuration:
147
+
148
+ ```ruby
149
+ ActiveRecord::Userstamp.configure do |config|
150
+ config.creator_attribute = :created_by
151
+ config.updater_attribute = :updated_by
152
+ config.deleter_attribute = :deleted_by
153
+ end
154
+ ```
155
+
156
+ ### Upgrading from delynn's 2.x without `compatibility_mode`
157
+ Within migrations, it was possible to declare `t.userstamps` within a table definition. It used
158
+ to accept one argument, which declares whether the deleter column should be created. This has
159
+ been changed to respect the gem configuration's `deleter_attribute`. If that is `nil`, then no
160
+ deleter column would be created.
161
+
162
+ There is also no need to include the `Userstamp` module in `ApplicationController`.
163
+
164
+ ### Upgrading from insphire's 2.0.1, or magiclabs-userstamp 2.0.2 or 2.1.0
165
+
166
+ That version of the gem allows every model to declare the name of the column containing the
167
+ attribute. That also means that in a large project, every model needs to declare `stampable`,
168
+ which is not very DRY.
169
+
170
+ To use this gem, normalise all database columns to use a consistent set of column names.
171
+ Configure the gem to use those names (as above) and remove all `stampable` declarations.
172
+
173
+ There is no need to include the `Userstamp` module in `ApplicationController`.
174
+
175
+ ## Tests
176
+ Run
177
+
178
+ $ bundle exec rspec
179
+
180
+ ## Authors
181
+ - [DeLynn Berry](http://delynnberry.com/): The original idea for this plugin came from the Rails
182
+ Wiki article entitled
183
+ [Extending ActiveRecord](http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample)
184
+ - [Michael Grosser](http://pragmatig.com)
185
+ - [John Dell](http://blog.spovich.com/)
186
+ - [Chris Hilton](https://github.com/chrismhilton)
187
+ - [Thomas von Deyen](https://github.com/tvdeyen)
188
+ - [Joel Low](http://joelsplace.sg)