activerecord-userstamp 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +16 -0
  5. data/CHANGELOG +26 -0
  6. data/Gemfile +3 -0
  7. data/LICENSE +21 -0
  8. data/README.md +220 -0
  9. data/Rakefile +17 -0
  10. data/activerecord-userstamp.gemspec +34 -0
  11. data/lib/active_record/userstamp.rb +14 -0
  12. data/lib/active_record/userstamp/controller_additions.rb +41 -0
  13. data/lib/active_record/userstamp/migration_additions.rb +16 -0
  14. data/lib/active_record/userstamp/model_additions.rb +3 -0
  15. data/lib/active_record/userstamp/stampable.rb +174 -0
  16. data/lib/active_record/userstamp/stamper.rb +39 -0
  17. data/lib/active_record/userstamp/version.rb +4 -0
  18. data/lib/activerecord/userstamp.rb +1 -0
  19. data/spec/controllers/posts_controller_spec.rb +46 -0
  20. data/spec/controllers/users_controller_spec.rb +41 -0
  21. data/spec/coverage_helper.rb +65 -0
  22. data/spec/dummy/README.rdoc +28 -0
  23. data/spec/dummy/Rakefile +6 -0
  24. data/spec/dummy/app/assets/images/.keep +0 -0
  25. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  26. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  27. data/spec/dummy/app/controllers/application_controller.rb +13 -0
  28. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  29. data/spec/dummy/app/controllers/posts_controller.rb +32 -0
  30. data/spec/dummy/app/controllers/users_controller.rb +18 -0
  31. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  32. data/spec/dummy/app/mailers/.keep +0 -0
  33. data/spec/dummy/app/models/comment.rb +6 -0
  34. data/spec/dummy/app/models/concerns/.keep +0 -0
  35. data/spec/dummy/app/models/foo.rb +3 -0
  36. data/spec/dummy/app/models/person.rb +4 -0
  37. data/spec/dummy/app/models/post.rb +13 -0
  38. data/spec/dummy/app/models/user.rb +4 -0
  39. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  40. data/spec/dummy/bin/bundle +3 -0
  41. data/spec/dummy/bin/rails +4 -0
  42. data/spec/dummy/bin/rake +4 -0
  43. data/spec/dummy/bin/setup +29 -0
  44. data/spec/dummy/config.ru +4 -0
  45. data/spec/dummy/config/application.rb +30 -0
  46. data/spec/dummy/config/boot.rb +5 -0
  47. data/spec/dummy/config/database.yml +25 -0
  48. data/spec/dummy/config/environment.rb +5 -0
  49. data/spec/dummy/config/environments/development.rb +41 -0
  50. data/spec/dummy/config/environments/production.rb +79 -0
  51. data/spec/dummy/config/environments/test.rb +37 -0
  52. data/spec/dummy/config/initializers/assets.rb +11 -0
  53. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  55. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  56. data/spec/dummy/config/initializers/inflections.rb +16 -0
  57. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  58. data/spec/dummy/config/initializers/session_store.rb +3 -0
  59. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  60. data/spec/dummy/config/locales/en.yml +23 -0
  61. data/spec/dummy/config/routes.rb +56 -0
  62. data/spec/dummy/config/secrets.yml +22 -0
  63. data/spec/dummy/db/schema.rb +55 -0
  64. data/spec/dummy/lib/assets/.keep +0 -0
  65. data/spec/dummy/log/.keep +0 -0
  66. data/spec/dummy/public/404.html +67 -0
  67. data/spec/dummy/public/422.html +67 -0
  68. data/spec/dummy/public/500.html +66 -0
  69. data/spec/dummy/public/favicon.ico +0 -0
  70. data/spec/lib/compatibility_stamping_spec.rb +69 -0
  71. data/spec/lib/migration_spec.rb +26 -0
  72. data/spec/lib/stamping_spec.rb +170 -0
  73. data/spec/lib/userstamp_spec.rb +7 -0
  74. data/spec/rails_helper.rb +7 -0
  75. data/spec/spec_helper.rb +97 -0
  76. data/spec/support/database_helpers.rb +22 -0
  77. data/spec/support/with_temporary_table.rb +50 -0
  78. metadata +276 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aeb470b53728ed92b0b74621e4bd002a41d56275
4
+ data.tar.gz: c3c5f02aa4d41923407b92c17e99e6ceba1889a7
5
+ SHA512:
6
+ metadata.gz: fcd728fc959bb391efb41b47259541d385b1146039992f6a9860f34dc601fadef888258e3f080246a155aa40ad19bfb6452dc84fcf20632322e6fc8b5148f461
7
+ data.tar.gz: 43e02f3aea8be19d8a11148603b3cc6e875dce661ee3f55923594091140b3b928bc634773b81163c307dc1af772d77b56a86d57f361e0af38c5cd46d848e0f83
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.log
2
+ *.sqlite3
3
+ /.rdoc
4
+ /.bundle
5
+ /spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.6
6
+ - 2.2.2
7
+ - ruby-head
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: ruby-head
11
+
12
+ bundler_args: "--jobs=3 --retry=3"
13
+ cache: bundler
14
+
15
+ before_install:
16
+ - gem update bundler
data/CHANGELOG ADDED
@@ -0,0 +1,26 @@
1
+ 2.0 (2-17-2008)
2
+ * [Ben Wyrosdick] - Added a migration helper that gives migration scripts a <tt>userstamps</tt>
3
+ method.
4
+ * [Marshall Roch] - Stamping can be temporarily turned off using the 'without_stamps' class
5
+ method.
6
+ Example:
7
+ Post.without_stamps do
8
+ post = Post.find(params[:id])
9
+ post.update_attributes(params[:post])
10
+ post.save
11
+ end
12
+
13
+ * Models that should receive updates made by 'stampers' now use the acts_as_stampable class
14
+ method. This sets up the belongs_to relationships and also injects private methods for use by
15
+ the individual callback filter methods.
16
+
17
+ * Models that are responsible for updating now use the acts_as_stamper class method. This
18
+ injects the stamper= and stamper methods that are thread safe and should be updated per
19
+ request by a controller.
20
+
21
+ * The Userstamp module is now meant to be included with one of your project's controllers (the
22
+ Application Controller is recommended). It creates a before filter called 'set_stampers' that
23
+ is responsible for setting all the current Stampers.
24
+
25
+ 1.0 (01-18-2006)
26
+ * Initial Release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +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.
data/README.md ADDED
@@ -0,0 +1,220 @@
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)
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/setup'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+
9
+ require 'rdoc/task'
10
+ desc 'Generate documentation for the userstamp plugin.'
11
+ Rake::RDocTask.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'Userstamp'
14
+ rdoc.options << '--line-numbers' << '--inline-source'
15
+ rdoc.rdoc_files.include('Readme.rdoc', 'CHANGELOG', 'LICENSE')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/userstamp/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'activerecord-userstamp'
8
+ s.version = ActiveRecord::Userstamp::VERSION
9
+ s.authors = ['Joel Low']
10
+ s.email = ['joel@joelsplace.sg']
11
+
12
+ s.summary = 'Adds magic creator and updater attributes to your ActiveRecord models.'
13
+ s.description = 'This gem extends ActiveRecord::Base to add automatic updating of created_by and updated_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created_(at/on) and updated_(at/on) attributes.'
14
+ s.homepage = 'https://github.com/lowjoel/activerecord-userstamp'
15
+ s.license = 'MIT'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ['lib']
21
+
22
+ s.add_dependency 'activesupport', '~> 4.2'
23
+ s.add_dependency 'activerecord', '~> 4.2'
24
+
25
+ s.add_development_dependency 'actionview', '~> 4.2'
26
+ s.add_development_dependency 'tzinfo-data'
27
+ s.add_development_dependency 'rake'
28
+ s.add_development_dependency 'rdoc'
29
+ s.add_development_dependency 'rspec-rails', '~> 3.3'
30
+ s.add_development_dependency 'simplecov'
31
+ s.add_development_dependency 'coveralls'
32
+ s.add_development_dependency 'codeclimate-test-reporter'
33
+ s.add_development_dependency 'sqlite3'
34
+ end
@@ -0,0 +1,14 @@
1
+ module ActiveRecord::Userstamp
2
+ extend ActiveSupport::Autoload
3
+
4
+ autoload :Stampable
5
+ autoload :Stamper
6
+
7
+ eager_autoload do
8
+ autoload :ControllerAdditions
9
+ autoload :MigrationAdditions
10
+ autoload :ModelAdditions
11
+ end
12
+
13
+ eager_load!
14
+ end
@@ -0,0 +1,41 @@
1
+ # The Userstamp module, when included into a controller, adds a before filter
2
+ # (named <tt>set_stamper</tt>) and an after filter (name <tt>reset_stamper</tt>).
3
+ # These methods assume a couple of things, but can be re-implemented in your
4
+ # controller to better suite your application.
5
+ #
6
+ # See the documentation for <tt>set_stamper</tt> and <tt>reset_stamper</tt> for
7
+ # specific implementation details.
8
+ module ActiveRecord::Userstamp::ControllerAdditions
9
+ def self.included(base) # :nodoc:
10
+ base.send :include, InstanceMethods
11
+ base.before_filter :set_stamper
12
+ base.after_filter :reset_stamper
13
+ end
14
+
15
+ module InstanceMethods
16
+ private
17
+ # The <tt>set_stamper</tt> method as implemented here assumes a couple
18
+ # of things. First, that you are using a +User+ model as the stamper
19
+ # and second that your controller has a <tt>current_user</tt> method
20
+ # that contains the currently logged in stamper. If either of these
21
+ # are not the case in your application you will want to manually add
22
+ # your own implementation of this method to the private section of
23
+ # the controller where you are including the Userstamp module.
24
+ def set_stamper
25
+ User.stamper = self.current_user
26
+ end
27
+
28
+ # The <tt>reset_stamper</tt> method as implemented here assumes that a
29
+ # +User+ model is being used as the stamper. If this is not the case then
30
+ # you will need to manually add your own implementation of this method to
31
+ # the private section of the controller where you are including the
32
+ # Userstamp module.
33
+ def reset_stamper
34
+ User.reset_stamper
35
+ end
36
+ end
37
+ end
38
+
39
+ if defined?(ActionController)
40
+ ActionController::Base.send(:include, ActiveRecord::Userstamp::ControllerAdditions)
41
+ end
@@ -0,0 +1,16 @@
1
+ module ActiveRecord::Userstamp::MigrationHelper
2
+ def self.included(base) # :nodoc:
3
+ base.send(:include, InstanceMethods)
4
+ end
5
+
6
+ module InstanceMethods
7
+ def userstamps(include_deleted_by = false, *args)
8
+ column(ActiveRecord::Userstamp.compatibility_mode ? :created_by : :creator_id, :integer, *args)
9
+ column(ActiveRecord::Userstamp.compatibility_mode ? :updated_by : :updater_id, :integer, *args)
10
+ column(ActiveRecord::Userstamp.compatibility_mode ? :deleted_by : :deleter_id, :integer, *args) if include_deleted_by
11
+ end
12
+ end
13
+ end
14
+
15
+ ActiveRecord::ConnectionAdapters::TableDefinition.send(:include,
16
+ ActiveRecord::Userstamp::MigrationHelper)
@@ -0,0 +1,3 @@
1
+ module ActiveRecord::Userstamp::ModelAdditions; end
2
+ ActiveRecord::Base.send(:include, ActiveRecord::Userstamp::Stampable)
3
+ ActiveRecord::Base.send(:include, ActiveRecord::Userstamp::Stamper)