activerecord-userstamp 3.0.2 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -5
- data/.rspec +2 -2
- data/CHANGELOG.md +103 -99
- data/Gemfile +3 -3
- data/LICENSE +21 -21
- data/README.md +189 -189
- data/Rakefile +17 -17
- data/activerecord-userstamp.gemspec +34 -34
- data/lib/active_record/userstamp.rb +30 -30
- data/lib/active_record/userstamp/configuration.rb +49 -49
- data/lib/active_record/userstamp/controller_additions.rb +50 -50
- data/lib/active_record/userstamp/migration_additions.rb +14 -14
- data/lib/active_record/userstamp/model_additions.rb +10 -10
- data/lib/active_record/userstamp/stampable.rb +123 -123
- data/lib/active_record/userstamp/stamper.rb +54 -54
- data/lib/active_record/userstamp/utilities.rb +55 -54
- data/lib/active_record/userstamp/version.rb +4 -4
- data/lib/activerecord/userstamp.rb +1 -1
- data/spec/controllers/posts_controller_spec.rb +44 -44
- data/spec/controllers/users_controller_spec.rb +50 -50
- 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 +36 -36
- data/spec/dummy/app/controllers/users_controller.rb +22 -22
- data/spec/dummy/app/helpers/application_helper.rb +2 -2
- data/spec/dummy/app/models/comment.rb +5 -5
- data/spec/dummy/app/models/person.rb +3 -3
- data/spec/dummy/app/models/post.rb +14 -14
- data/spec/dummy/app/models/tag.rb +3 -0
- data/spec/dummy/app/models/user.rb +3 -3
- 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 -23
- 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 +54 -45
- 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 -20
- data/spec/lib/migration_spec.rb +71 -71
- data/spec/lib/stamper_spec.rb +66 -66
- data/spec/lib/stamping_spec.rb +250 -238
- 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 +22 -22
- data/spec/support/with_temporary_table.rb +51 -51
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 352c22aa4ca4d1dd7c9cc5ff0a7c3e9e2ccf9ba1
|
4
|
+
data.tar.gz: bf2df186ca39cb6ff78370ac3eca3e9e13624b64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb311b3579621119fa951df7a229e67bd3593bccf3a025e0e47ca1d6fd34b906e2dad3bf74474a7beb66929940a2ad463d928776c11f9256816c40b42a5d8d7e
|
7
|
+
data.tar.gz: 4e7114880578dd234f11b25ed44c8773b44480037672b8895a8586bbfdb78c1dc269c6175c0296c3abe07bd080c57775434da4f89b41d14ad68ab38cd529479f
|
data/.gitignore
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
*.log
|
2
|
-
*.sqlite3
|
3
|
-
/.rdoc
|
4
|
-
|
5
|
-
|
1
|
+
*.log
|
2
|
+
*.sqlite3
|
3
|
+
/.rdoc
|
4
|
+
/.bundle
|
5
|
+
/spec/examples.txt
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--color
|
2
|
-
--require spec_helper
|
1
|
+
--color
|
2
|
+
--require spec_helper
|
data/CHANGELOG.md
CHANGED
@@ -1,99 +1,103 @@
|
|
1
|
-
# Changelog
|
2
|
-
## 3.0.
|
3
|
-
* Joel Low -
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
* Joel Low -
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
* Joel Low -
|
32
|
-
|
33
|
-
the
|
34
|
-
* Joel Low -
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
* Joel Low -
|
39
|
-
|
40
|
-
|
41
|
-
`
|
42
|
-
* Joel Low -
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
*
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
* Chris
|
54
|
-
|
55
|
-
|
56
|
-
*
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
* Michael Grosser -
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
*
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
* Models that
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
*
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
1
|
+
# Changelog
|
2
|
+
## 3.0.3 (14-7-2015)
|
3
|
+
* Joel Low - Allow using ActiveRecord-Userstamp with generated tables (e.g.
|
4
|
+
`has_and_belongs_to_many` join tables.)
|
5
|
+
|
6
|
+
## 3.0.2 (12-7-2015)
|
7
|
+
* Joel Low - Depending on what was set to a stamper (ID or record object), the
|
8
|
+
association ID or association setter is used to assign the
|
9
|
+
creator/updater/deleter attributes. This only applies if the attributes
|
10
|
+
end with `_id`; otherwise the attribute would be used verbatim (e.g. the
|
11
|
+
compatibility mode `created_by`).
|
12
|
+
* Joel Low - Provide a `with_stamper` method to specify the stamper for a given
|
13
|
+
stamper class during the execution of the block.
|
14
|
+
* Joel Low - Ensure that the `set_stamper` and `reset_stamper` calls from the
|
15
|
+
controller are always paired so that the stamper state is always properly
|
16
|
+
restored. `set_stamper` and `reset_stamper` is now deprecated and will be
|
17
|
+
removed in ActiveRecord::Userstamp 3.1 and replaced with a single
|
18
|
+
`with_stamper` `around_action` callback.
|
19
|
+
|
20
|
+
## 3.0.1 (11-7-2015)
|
21
|
+
* Joel Low - Only declare the creator/updater/deleter associations when the table has
|
22
|
+
the attribute columns. If the columns cannot be determined (e.g. if the
|
23
|
+
table has not been defined, such as during tests), then the model would
|
24
|
+
need to explicitly call `stampable`.
|
25
|
+
|
26
|
+
## 3.0.0 (10-7-2015)
|
27
|
+
* Joel Low - Remove `compatibility_mode`. Use the `creator_attribute`,
|
28
|
+
`updater_attribute`, and `deleter_attribute` configuration options instead.
|
29
|
+
* Joel Low - The table definition migration helper should follow the gem configuration
|
30
|
+
when generating column names.
|
31
|
+
* Joel Low - When deciding whether to generate a `deleter_id` column, check the gem
|
32
|
+
configuration for the `deleter_attribute` configuration option instead of
|
33
|
+
relying on the user to specify in every migration.
|
34
|
+
* Joel Low - Remove the `deleter` option from the `stampable` model declaration.
|
35
|
+
* Joel Low - Remove the `creator_attribute`, `updater_attribute`, and `deleter_attribute`
|
36
|
+
options from the `stampable` model declaration. All models will follow
|
37
|
+
the gem configuration.
|
38
|
+
* Joel Low - Added the `default_stamper` configuration option. The controller will
|
39
|
+
automatically stamp using that model.
|
40
|
+
* Joel Low - Additional attributes passed to `stampable` would be passed to the
|
41
|
+
underlying `belongs_to` association.
|
42
|
+
* Joel Low - Declare the creator/updater/deleter setter callbacks directly in
|
43
|
+
`ActiveRecord::Base`. So now, all models will automatically be stamped
|
44
|
+
when the creator/updater/deleter attributes are present. This mirrors
|
45
|
+
`ActiveRecord::Timestamp` behaviour.
|
46
|
+
* Joel Low - Automatically declare the creator/updater/deleter associations on every
|
47
|
+
model. To add additional configuration options, simply call `stampable`.
|
48
|
+
This can be called multiple times per model; the last call takes effect.
|
49
|
+
* Joel Low - Remove support for `serialized_attributes`. It will be removed in Rails 5.
|
50
|
+
|
51
|
+
## 2.1.1 (9-7-2015)
|
52
|
+
|
53
|
+
* Chris Hilton - Only set updater attribute if the record has changed or contains a
|
54
|
+
serialized attribute.
|
55
|
+
* Chris Hilton - Support `:with_deleted` in the `stampable` declaration.
|
56
|
+
* Chris Hilton - Only set the creator attribute if it is blank.
|
57
|
+
* Chris Branson - Fix deprecation warning in `serialized_attributes`.
|
58
|
+
* Joel Low - Trigger the updater/creator stamping before saving, so that the correct
|
59
|
+
users are stamped even if validation was not run.
|
60
|
+
* Joel Low - Allow extra parameters to be passed to the migration helpers.
|
61
|
+
|
62
|
+
## 2.1.0 (28-3-2014)
|
63
|
+
* Thomas von Deyen - Do not automatically make every class stampable.
|
64
|
+
|
65
|
+
## 2.0.2 (11-8-2011)
|
66
|
+
* Chris Hilton - Set the creator/updater attributes before validation, so that they can
|
67
|
+
be checked as part of validations.
|
68
|
+
* Alex - Specify that the stampable class is camelized from the given symbol,
|
69
|
+
not just capitalized, to follow ActiveRecord convention.
|
70
|
+
## 2.0.1 (8-10-2010)
|
71
|
+
* Michael Grosser - Make stampable define the deleter association and before filter whenever
|
72
|
+
a :deleter_attribute has been passed in options, or :deleter => true is
|
73
|
+
passed, or Caboose::Acts::Paranoid is defined. This makes :deleter
|
74
|
+
functionality useable to people who aren't use acts_as_paranoid.
|
75
|
+
* Michael Grosser - do not leave record_userstamp turned on when an exception occurs inside
|
76
|
+
the `without_stamps` block
|
77
|
+
|
78
|
+
## 2.0 (2-17-2008)
|
79
|
+
* Ben Wyrosdick - Added a migration helper that gives migration scripts a <tt>userstamps</tt>
|
80
|
+
method.
|
81
|
+
* Marshall Roch - Stamping can be temporarily turned off using the 'without_stamps' class
|
82
|
+
method.
|
83
|
+
Example:
|
84
|
+
Post.without_stamps do
|
85
|
+
post = Post.find(params[:id])
|
86
|
+
post.update_attributes(params[:post])
|
87
|
+
post.save
|
88
|
+
end
|
89
|
+
|
90
|
+
* Models that should receive updates made by 'stampers' now use the acts_as_stampable class
|
91
|
+
method. This sets up the belongs_to relationships and also injects private methods for use by
|
92
|
+
the individual callback filter methods.
|
93
|
+
|
94
|
+
* Models that are responsible for updating now use the acts_as_stamper class method. This
|
95
|
+
injects the stamper= and stamper methods that are thread safe and should be updated per
|
96
|
+
request by a controller.
|
97
|
+
|
98
|
+
* The Userstamp module is now meant to be included with one of your project's controllers (the
|
99
|
+
Application Controller is recommended). It creates a before filter called 'set_stampers' that
|
100
|
+
is responsible for setting all the current Stampers.
|
101
|
+
|
102
|
+
## 1.0 (01-18-2006)
|
103
|
+
* 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,189 +1,189 @@
|
|
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. This gem is tested only on Rails 4.2; but it should work with Rails 4+.
|
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. See the [usage](#usage) section on the
|
61
|
-
configuration options supported.
|
62
|
-
|
63
|
-
### Saving before validation
|
64
|
-
This fork includes changes to perform model stamping before validation. This allows models to
|
65
|
-
enforce the presence of stamp attributes:
|
66
|
-
|
67
|
-
```ruby
|
68
|
-
validates :created_by, presence: true
|
69
|
-
validates :updated_by, presence: true
|
70
|
-
```
|
71
|
-
|
72
|
-
Furthermore, the `creator` attribute is set only if the value is blank allowing for a manual
|
73
|
-
override.
|
74
|
-
|
75
|
-
## Usage
|
76
|
-
Assume that we are building a blog application, with User and Post objects. Add the following
|
77
|
-
to the application's Gemfile:
|
78
|
-
|
79
|
-
```ruby
|
80
|
-
gem 'activerecord-userstamp'
|
81
|
-
```
|
82
|
-
|
83
|
-
Define an initializer in your Rails application to configure the gem:
|
84
|
-
|
85
|
-
```ruby
|
86
|
-
ActiveRecord::Userstamp.configure do |config|
|
87
|
-
# config.default_stamper = 'User'
|
88
|
-
# config.creator_attribute = :creator_id
|
89
|
-
# config.updater_attribute = :updater_id
|
90
|
-
config.deleter_attribute = nil
|
91
|
-
end
|
92
|
-
```
|
93
|
-
|
94
|
-
By default, `:deleter_attribute` is set to `:deleter_id` for soft deletes. If you are not using
|
95
|
-
soft deletes, you can set the attribute no `nil`.
|
96
|
-
|
97
|
-
Ensure that each model has a set of columns for creators, updaters, and deleters (if applicable.)
|
98
|
-
|
99
|
-
```ruby
|
100
|
-
class CreateUsers < ActiveRecord::Migration
|
101
|
-
def change
|
102
|
-
create_table :users do |t|
|
103
|
-
...
|
104
|
-
t.userstamps
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
class CreatePosts < ActiveRecord::Migration
|
110
|
-
def change
|
111
|
-
create_table :posts do |t|
|
112
|
-
...
|
113
|
-
t.userstamps
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
```
|
118
|
-
|
119
|
-
Declare the stamper on the User model:
|
120
|
-
|
121
|
-
```ruby
|
122
|
-
class User < ActiveRecord::Base
|
123
|
-
model_stamper
|
124
|
-
end
|
125
|
-
```
|
126
|
-
|
127
|
-
If your stamper is called `User`, that's it; you're done.
|
128
|
-
|
129
|
-
## Customisation
|
130
|
-
The association which is created on each of the `creator_id`, `updater_id`, and `deleter_id` can
|
131
|
-
be customised. Also, the stamper used by each class can also be customised. For this purpose, the
|
132
|
-
`ActiveRecord::Base.stampable` method can be used:
|
133
|
-
|
134
|
-
```ruby
|
135
|
-
class Post < ActiveRecord::Base
|
136
|
-
stampable
|
137
|
-
end
|
138
|
-
```
|
139
|
-
|
140
|
-
The `stampable` method allows you to customize the `creator`, `updater`, and `deleter` associations.
|
141
|
-
It also allows you to specify the name of the stamper for the class being declared. Any additional
|
142
|
-
arguments are passed to the `belongs_to` declaration.
|
143
|
-
|
144
|
-
## Upgrading
|
145
|
-
### Upgrading from delynn's 1.x/2.x with `compatibility_mode`
|
146
|
-
The major difference between 1.x and 2.x is the naming of the columns. This version of the gem
|
147
|
-
allows specifying the name of the column from the gem configuration:
|
148
|
-
|
149
|
-
```ruby
|
150
|
-
ActiveRecord::Userstamp.configure do |config|
|
151
|
-
config.creator_attribute = :created_by
|
152
|
-
config.updater_attribute = :updated_by
|
153
|
-
config.deleter_attribute = :deleted_by
|
154
|
-
end
|
155
|
-
```
|
156
|
-
|
157
|
-
### Upgrading from delynn's 2.x without `compatibility_mode`
|
158
|
-
Within migrations, it was possible to declare `t.userstamps` within a table definition. It used
|
159
|
-
to accept one argument, which declares whether the deleter column should be created. This has
|
160
|
-
been changed to respect the gem configuration's `deleter_attribute`. If that is `nil`, then no
|
161
|
-
deleter column would be created.
|
162
|
-
|
163
|
-
There is also no need to include the `Userstamp` module in `ApplicationController`.
|
164
|
-
|
165
|
-
### Upgrading from insphire's 2.0.1, or magiclabs-userstamp 2.0.2 or 2.1.0
|
166
|
-
|
167
|
-
That version of the gem allows every model to declare the name of the column containing the
|
168
|
-
attribute. That also means that in a large project, every model needs to declare `stampable`,
|
169
|
-
which is not very DRY.
|
170
|
-
|
171
|
-
To use this gem, normalise all database columns to use a consistent set of column names.
|
172
|
-
Configure the gem to use those names (as above) and remove all `stampable` declarations.
|
173
|
-
|
174
|
-
There is no need to include the `Userstamp` module in `ApplicationController`.
|
175
|
-
|
176
|
-
## Tests
|
177
|
-
Run
|
178
|
-
|
179
|
-
$ bundle exec rspec
|
180
|
-
|
181
|
-
## Authors
|
182
|
-
- [DeLynn Berry](http://delynnberry.com/): The original idea for this plugin came from the Rails
|
183
|
-
Wiki article entitled
|
184
|
-
[Extending ActiveRecord](http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample)
|
185
|
-
- [Michael Grosser](http://pragmatig.com)
|
186
|
-
- [John Dell](http://blog.spovich.com/)
|
187
|
-
- [Chris Hilton](https://github.com/chrismhilton)
|
188
|
-
- [Thomas von Deyen](https://github.com/tvdeyen)
|
189
|
-
- [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. This gem is tested only on Rails 4.2; but it should work with Rails 4+.
|
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. See the [usage](#usage) section on the
|
61
|
+
configuration options supported.
|
62
|
+
|
63
|
+
### Saving before validation
|
64
|
+
This fork includes changes to perform model stamping before validation. This allows models to
|
65
|
+
enforce the presence of stamp attributes:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
validates :created_by, presence: true
|
69
|
+
validates :updated_by, presence: true
|
70
|
+
```
|
71
|
+
|
72
|
+
Furthermore, the `creator` attribute is set only if the value is blank allowing for a manual
|
73
|
+
override.
|
74
|
+
|
75
|
+
## Usage
|
76
|
+
Assume that we are building a blog application, with User and Post objects. Add the following
|
77
|
+
to the application's Gemfile:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
gem 'activerecord-userstamp'
|
81
|
+
```
|
82
|
+
|
83
|
+
Define an initializer in your Rails application to configure the gem:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
ActiveRecord::Userstamp.configure do |config|
|
87
|
+
# config.default_stamper = 'User'
|
88
|
+
# config.creator_attribute = :creator_id
|
89
|
+
# config.updater_attribute = :updater_id
|
90
|
+
config.deleter_attribute = nil
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
By default, `:deleter_attribute` is set to `:deleter_id` for soft deletes. If you are not using
|
95
|
+
soft deletes, you can set the attribute no `nil`.
|
96
|
+
|
97
|
+
Ensure that each model has a set of columns for creators, updaters, and deleters (if applicable.)
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class CreateUsers < ActiveRecord::Migration
|
101
|
+
def change
|
102
|
+
create_table :users do |t|
|
103
|
+
...
|
104
|
+
t.userstamps
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
class CreatePosts < ActiveRecord::Migration
|
110
|
+
def change
|
111
|
+
create_table :posts do |t|
|
112
|
+
...
|
113
|
+
t.userstamps
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
Declare the stamper on the User model:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
class User < ActiveRecord::Base
|
123
|
+
model_stamper
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
127
|
+
If your stamper is called `User`, that's it; you're done.
|
128
|
+
|
129
|
+
## Customisation
|
130
|
+
The association which is created on each of the `creator_id`, `updater_id`, and `deleter_id` can
|
131
|
+
be customised. Also, the stamper used by each class can also be customised. For this purpose, the
|
132
|
+
`ActiveRecord::Base.stampable` method can be used:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
class Post < ActiveRecord::Base
|
136
|
+
stampable
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
140
|
+
The `stampable` method allows you to customize the `creator`, `updater`, and `deleter` associations.
|
141
|
+
It also allows you to specify the name of the stamper for the class being declared. Any additional
|
142
|
+
arguments are passed to the `belongs_to` declaration.
|
143
|
+
|
144
|
+
## Upgrading
|
145
|
+
### Upgrading from delynn's 1.x/2.x with `compatibility_mode`
|
146
|
+
The major difference between 1.x and 2.x is the naming of the columns. This version of the gem
|
147
|
+
allows specifying the name of the column from the gem configuration:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
ActiveRecord::Userstamp.configure do |config|
|
151
|
+
config.creator_attribute = :created_by
|
152
|
+
config.updater_attribute = :updated_by
|
153
|
+
config.deleter_attribute = :deleted_by
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
157
|
+
### Upgrading from delynn's 2.x without `compatibility_mode`
|
158
|
+
Within migrations, it was possible to declare `t.userstamps` within a table definition. It used
|
159
|
+
to accept one argument, which declares whether the deleter column should be created. This has
|
160
|
+
been changed to respect the gem configuration's `deleter_attribute`. If that is `nil`, then no
|
161
|
+
deleter column would be created.
|
162
|
+
|
163
|
+
There is also no need to include the `Userstamp` module in `ApplicationController`.
|
164
|
+
|
165
|
+
### Upgrading from insphire's 2.0.1, or magiclabs-userstamp 2.0.2 or 2.1.0
|
166
|
+
|
167
|
+
That version of the gem allows every model to declare the name of the column containing the
|
168
|
+
attribute. That also means that in a large project, every model needs to declare `stampable`,
|
169
|
+
which is not very DRY.
|
170
|
+
|
171
|
+
To use this gem, normalise all database columns to use a consistent set of column names.
|
172
|
+
Configure the gem to use those names (as above) and remove all `stampable` declarations.
|
173
|
+
|
174
|
+
There is no need to include the `Userstamp` module in `ApplicationController`.
|
175
|
+
|
176
|
+
## Tests
|
177
|
+
Run
|
178
|
+
|
179
|
+
$ bundle exec rspec
|
180
|
+
|
181
|
+
## Authors
|
182
|
+
- [DeLynn Berry](http://delynnberry.com/): The original idea for this plugin came from the Rails
|
183
|
+
Wiki article entitled
|
184
|
+
[Extending ActiveRecord](http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample)
|
185
|
+
- [Michael Grosser](http://pragmatig.com)
|
186
|
+
- [John Dell](http://blog.spovich.com/)
|
187
|
+
- [Chris Hilton](https://github.com/chrismhilton)
|
188
|
+
- [Thomas von Deyen](https://github.com/tvdeyen)
|
189
|
+
- [Joel Low](http://joelsplace.sg)
|