originator 3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +31 -0
  3. data/Gemfile +8 -0
  4. data/LICENSE +20 -0
  5. data/Rakefile +30 -0
  6. data/Readme.rdoc +162 -0
  7. data/VERSION +1 -0
  8. data/lib/migration_helper.rb +19 -0
  9. data/lib/stampable.rb +152 -0
  10. data/lib/stamper.rb +43 -0
  11. data/lib/userstamp.rb +52 -0
  12. data/originator.gemspec +84 -0
  13. data/rdoc/classes/Ddb/Controller.html +111 -0
  14. data/rdoc/classes/Ddb/Controller/Userstamp.html +125 -0
  15. data/rdoc/classes/Ddb/Controller/Userstamp/InstanceMethods.html +105 -0
  16. data/rdoc/classes/Ddb/Userstamp.html +121 -0
  17. data/rdoc/classes/Ddb/Userstamp/MigrationHelper.html +111 -0
  18. data/rdoc/classes/Ddb/Userstamp/MigrationHelper/InstanceMethods.html +142 -0
  19. data/rdoc/classes/Ddb/Userstamp/Stampable.html +128 -0
  20. data/rdoc/classes/Ddb/Userstamp/Stampable/ClassMethods.html +225 -0
  21. data/rdoc/classes/Ddb/Userstamp/Stamper.html +112 -0
  22. data/rdoc/classes/Ddb/Userstamp/Stamper/ClassMethods.html +142 -0
  23. data/rdoc/classes/Ddb/Userstamp/Stamper/InstanceMethods.html +207 -0
  24. data/rdoc/classes/Userstamp.html +118 -0
  25. data/rdoc/created.rid +1 -0
  26. data/rdoc/files/CHANGELOG.html +137 -0
  27. data/rdoc/files/LICENSE.html +129 -0
  28. data/rdoc/files/Readme_rdoc.html +301 -0
  29. data/rdoc/files/lib/migration_helper_rb.html +101 -0
  30. data/rdoc/files/lib/stampable_rb.html +101 -0
  31. data/rdoc/files/lib/stamper_rb.html +101 -0
  32. data/rdoc/files/lib/userstamp_rb.html +110 -0
  33. data/rdoc/fr_class_index.html +38 -0
  34. data/rdoc/fr_file_index.html +33 -0
  35. data/rdoc/fr_method_index.html +33 -0
  36. data/rdoc/index.html +24 -0
  37. data/rdoc/rdoc-style.css +208 -0
  38. data/test/compatibility_stamping_test.rb +69 -0
  39. data/test/controllers/posts_controller.rb +26 -0
  40. data/test/controllers/users_controller.rb +12 -0
  41. data/test/controllers/userstamp_controller.rb +9 -0
  42. data/test/helper.rb +61 -0
  43. data/test/models/comment.rb +5 -0
  44. data/test/models/foo.rb +3 -0
  45. data/test/models/person.rb +3 -0
  46. data/test/models/post.rb +13 -0
  47. data/test/models/user.rb +3 -0
  48. data/test/schema.rb +54 -0
  49. data/test/stamping_test.rb +138 -0
  50. data/test/userstamp_controller_test.rb +103 -0
  51. data/test/userstamp_test.rb +7 -0
  52. metadata +138 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3538e1c300e8493901bf8065ce785c5d10e2d8f9
4
+ data.tar.gz: f947dd914d4e6f737b96c1b6a92208b6692d3564
5
+ SHA512:
6
+ metadata.gz: 00ae6b73e6ad3a1c0e9e6662e26ee820a74e6c3b962aafa9555c0a1eb5c7b58fca8c88c2a527204be25abd1a39f2e16f51031eaafc93782818701959031e127d
7
+ data.tar.gz: be4df2f560d977609e744903eb87e6ddf343943af176cdabcdb14f7f21dbe6ced80bf5aaeb66b616a32d7135e4a88bc8a1d003532f8a0d7a455602e9b228f832
data/CHANGELOG ADDED
@@ -0,0 +1,31 @@
1
+ 3.0 (2016-09-13)
2
+
3
+ * Removed Rails < 4 support [Thomas von Deyen]
4
+ * Adds Rails 5 support [Thomas von Deyen]
5
+
6
+ 2.0 (2-17-2008)
7
+ * [Ben Wyrosdick] - Added a migration helper that gives migration scripts a <tt>userstamps</tt>
8
+ method.
9
+ * [Marshall Roch] - Stamping can be temporarily turned off using the 'without_stamps' class
10
+ method.
11
+ Example:
12
+ Post.without_stamps do
13
+ post = Post.find(params[:id])
14
+ post.update_attributes(params[:post])
15
+ post.save
16
+ end
17
+
18
+ * Models that should receive updates made by 'stampers' now use the acts_as_stampable class
19
+ method. This sets up the belongs_to relationships and also injects private methods for use by
20
+ the individual callback filter methods.
21
+
22
+ * Models that are responsible for updating now use the acts_as_stamper class method. This
23
+ injects the stamper= and stamper methods that are thread safe and should be updated per
24
+ request by a controller.
25
+
26
+ * The Userstamp module is now meant to be included with one of your project's controllers (the
27
+ Application Controller is recommended). It creates a before filter called 'set_stampers' that
28
+ is responsible for setting all the current Stampers.
29
+
30
+ 1.0 (01-18-2006)
31
+ * Initial Release
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'redgreen'
7
+ gem 'activesupport'
8
+ gem 'sqlite3'
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006-2008 DeLynn Berry
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'rake/testtask'
4
+
5
+ begin
6
+ require 'rdoc/task'
7
+ rescue LoadError
8
+ require 'rdoc/rdoc'
9
+ require 'rake/rdoctask'
10
+ RDoc::Task = Rake::RDocTask
11
+ end
12
+
13
+ desc 'Default: run unit tests.'
14
+ task :default => :test
15
+
16
+ desc 'Test the userstamp plugin.'
17
+ Rake::TestTask.new(:test) do |t|
18
+ t.libs << 'lib'
19
+ t.pattern = 'test/**/*_test.rb'
20
+ t.verbose = true
21
+ end
22
+
23
+ desc 'Generate documentation for the userstamp plugin.'
24
+ RDoc::Task.new(:rdoc) do |rdoc|
25
+ rdoc.rdoc_dir = 'rdoc'
26
+ rdoc.title = 'Userstamp'
27
+ rdoc.options << '--line-numbers' << '--inline-source'
28
+ rdoc.rdoc_files.include('Readme.rdoc', 'CHANGELOG', 'LICENSE')
29
+ rdoc.rdoc_files.include('lib/**/*.rb')
30
+ end
data/Readme.rdoc ADDED
@@ -0,0 +1,162 @@
1
+ == Userstamp
2
+
3
+
4
+ == Fork Details
5
+
6
+ This is a fork of the grosser fork [https://github.com/grosser/userstamp] of the original userstamp plugin created by delynn [https://github.com/delynn/userstamp].
7
+
8
+ This fork has been created to combine the grosser changes that enable use of the plugin within applications that perform soft deletes but are not using the acts_as_paranoid plugin/gem (for example this fork can now be used in conjunction with the rails3_acts_as_paranoid plugin/gem [https://github.com/goncalossilva/rails3_acts_as_paranoid]).
9
+
10
+ This fork also includes changes to perform the model stamping before validation is performed so that the model can enforce the presence of stamp attribute values eg.
11
+
12
+ validates :created_by, presence: true
13
+ validates :updated_by, presence: true
14
+
15
+
16
+ == Using the Fork
17
+
18
+ To use this fork add the following to your application gemfile:
19
+
20
+ gem 'originator', github: 'AlchemyCMS/originator'
21
+
22
+
23
+ == Overview
24
+
25
+ Userstamp extends ActiveRecord::Base[http://api.rubyonrails.com/classes/ActiveRecord/Base.html] to add automatic updating of 'creator',
26
+ 'updater', and 'deleter' attributes. It is based loosely on the ActiveRecord::Timestamp[http://api.rubyonrails.com/classes/ActiveRecord/Timestamp.html] module.
27
+
28
+ Two class methods (<tt>model_stamper</tt> and <tt>stampable</tt>) are implemented in this plugin.
29
+ The <tt>model_stamper</tt> method is used in models that are responsible for creating, updating, or
30
+ deleting other objects. The <tt>stampable</tt> method is used in models that are subject to being
31
+ created, updated, or deleted by 'stampers'.
32
+
33
+
34
+ == Usage
35
+ The assumption is that you have two different
36
+ categories of objects; those that manipulate, and those that are manipulated. For those objects
37
+ that are being manipulated there's the Stampable module and for the manipulators there's the
38
+ Stamper module. There's also the actual Userstamp module for your controllers that assists in
39
+ setting up your environment on a per request basis.
40
+
41
+ === Example
42
+ Assume a weblog application has User and Post objects.
43
+ # 1: Create the migrations for these objects
44
+
45
+ class CreateUsers < ActiveRecord::Migration
46
+ def self.up
47
+ create_table :users, :force => true do |t|
48
+ ...
49
+ t.userstamps # use t.userstamps(true) if you also want 'deleter_id'
50
+ end
51
+ end
52
+
53
+ def self.down
54
+ drop_table :users
55
+ end
56
+ end
57
+
58
+ class CreatePosts < ActiveRecord::Migration
59
+ def self.up
60
+ create_table :posts, :force => true do |t|
61
+ ...
62
+ t.userstamps # use t.userstamps(true) if you also want 'deleter_id'
63
+ end
64
+ end
65
+
66
+ def self.down
67
+ drop_table :posts
68
+ end
69
+ end
70
+
71
+ # 2: Users are going to manipulate Post's, use the <tt>model_stamper</tt>:
72
+
73
+ class User < ActiveRecord::Base
74
+ model_stamper
75
+ end
76
+
77
+ # 3: Set the current user in the ApplicationController:
78
+
79
+ class ApplicationController < ActionController::Base
80
+ include Userstamp
81
+ end
82
+
83
+ More than likely you want all your associations setup on your stamped objects,
84
+ and that's where the <tt>stampable</tt> class method comes in.
85
+ So in our example we'll want to use this method in both our User and Post classes:
86
+
87
+ class User < ActiveRecord::Base
88
+ model_stamper
89
+ stampable
90
+ end
91
+
92
+ class Post < ActiveRecord::Base
93
+ stampable
94
+ end
95
+
96
+ Okay, so what all have we done? The <tt>model_stamper</tt> class method injects two methods into the
97
+ User class. They are #stamper= and #stamper and look like this:
98
+
99
+ def stamper=(object)
100
+ object_stamper = if object.is_a?(ActiveRecord::Base)
101
+ object.send("#{object.class.primary_key}".to_sym)
102
+ else
103
+ object
104
+ end
105
+
106
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
107
+ end
108
+
109
+ def stamper
110
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
111
+ end
112
+
113
+ The <tt>stampable</tt> method allows you to customize what columns will get stamped, and also
114
+ creates the +creator+, +updater+, and +deleter+ associations.
115
+
116
+ The Userstamp module that we included into our ApplicationController uses the setter method to
117
+ set which user is currently making the request. By default the 'set_stampers' method works perfectly
118
+ with the RestfulAuthentication[http://svn.techno-weenie.net/projects/plugins/restful_authentication] plug-in:
119
+
120
+ def set_stampers
121
+ User.stamper = self.current_user
122
+ end
123
+
124
+ If you aren't using ActsAsAuthenticated, then you need to create your own version of the
125
+ <tt>set_stampers</tt> method in the controller where you've included the Userstamp module.
126
+
127
+ Now, let's get back to the Stampable module (since it really is the interesting one). The Stampable
128
+ module sets up before_* filters that are responsible for setting those attributes at the appropriate
129
+ times. It also creates the belongs_to relationships for you.
130
+
131
+ If you need to customize the columns that are stamped, the <tt>stampable</tt> method can be
132
+ completely customized. Here's an quick example:
133
+
134
+ class Post < ActiveRecord::Base
135
+ stampable :stamper_class_name => :person,
136
+ :creator_attribute => :create_user,
137
+ :updater_attribute => :update_user,
138
+ :deleter_attribute => :delete_user,
139
+ :deleter => true
140
+ end
141
+
142
+ == Upgrade from 1.x
143
+ # config/environment.rb
144
+ Ddb::Userstamp.compatibility_mode = true
145
+
146
+ {Example userstamp application}[http://github.com/delynn/userstamp_sample]
147
+
148
+ == Running Unit Tests
149
+
150
+ All: rake
151
+ One: ruby test/compatibility_stamping_test.rb
152
+
153
+ == Author
154
+ {DeLynn Berry}[http://delynnberry.com/]
155
+
156
+ The original idea for this plugin came from the Rails Wiki article entitled
157
+ {Extending ActiveRecord}[http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample].
158
+
159
+ == Contributors / maintenance / enhancement
160
+ - {Michael Grosser}[http://pragmatig.com]
161
+ - {John Dell}[http://blog.spovich.com/]
162
+ - Chris Hilton
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 3.0
@@ -0,0 +1,19 @@
1
+ module Ddb
2
+ module Userstamp
3
+ module MigrationHelper
4
+ def self.included(base) # :nodoc:
5
+ base.send(:include, InstanceMethods)
6
+ end
7
+
8
+ module InstanceMethods
9
+ def userstamps(include_deleted_by = false)
10
+ column(Ddb::Userstamp.compatibility_mode ? :created_by : :creator_id, :integer)
11
+ column(Ddb::Userstamp.compatibility_mode ? :updated_by : :updater_id, :integer)
12
+ column(Ddb::Userstamp.compatibility_mode ? :deleted_by : :deleter_id, :integer) if include_deleted_by
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, Ddb::Userstamp::MigrationHelper)
data/lib/stampable.rb ADDED
@@ -0,0 +1,152 @@
1
+ module Ddb #:nodoc:
2
+ module Userstamp
3
+ # Determines what default columns to use for recording the current stamper.
4
+ # By default this is set to false, so the plug-in will use columns named
5
+ # <tt>creator_id</tt>, <tt>updater_id</tt>, and <tt>deleter_id</tt>.
6
+ #
7
+ # To turn compatibility mode on, place the following line in your environment.rb
8
+ # file:
9
+ #
10
+ # Ddb::Userstamp.compatibility_mode = true
11
+ #
12
+ # This will cause the plug-in to use columns named <tt>created_by</tt>,
13
+ # <tt>updated_by</tt>, and <tt>deleted_by</tt>.
14
+ mattr_accessor :compatibility_mode
15
+ @@compatibility_mode = false
16
+
17
+ # Extends the stamping functionality of ActiveRecord by automatically recording the model
18
+ # responsible for creating, updating, and deleting the current object. See the Stamper
19
+ # and Userstamp modules for further documentation on how the entire process works.
20
+ module Stampable
21
+ def self.included(base) #:nodoc:
22
+ super
23
+
24
+ base.extend(ClassMethods)
25
+ base.class_eval do
26
+ include InstanceMethods
27
+
28
+ # Should ActiveRecord record userstamps? Defaults to true.
29
+ class_attribute :record_userstamp
30
+ self.record_userstamp = true
31
+
32
+ # Which class is responsible for stamping? Defaults to :user.
33
+ class_attribute :stamper_class_name
34
+
35
+ # What column should be used for the creator stamp?
36
+ # Defaults to :creator_id when compatibility mode is off
37
+ # Defaults to :created_by when compatibility mode is on
38
+ class_attribute :creator_attribute
39
+
40
+ # What column should be used for the updater stamp?
41
+ # Defaults to :updater_id when compatibility mode is off
42
+ # Defaults to :updated_by when compatibility mode is on
43
+ class_attribute :updater_attribute
44
+
45
+ # What column should be used for the deleter stamp?
46
+ # Defaults to :deleter_id when compatibility mode is off
47
+ # Defaults to :deleted_by when compatibility mode is on
48
+ class_attribute :deleter_attribute
49
+ end
50
+ end
51
+
52
+ module ClassMethods
53
+ # This method is automatically called on for all classes that inherit from
54
+ # ActiveRecord, but if you need to customize how the plug-in functions, this is the
55
+ # method to use. Here's an example:
56
+ #
57
+ # class Post < ActiveRecord::Base
58
+ # stampable :stamper_class_name => :person,
59
+ # :creator_attribute => :create_user,
60
+ # :updater_attribute => :update_user,
61
+ # :deleter_attribute => :delete_user
62
+ # :deleter => true
63
+ # end
64
+ #
65
+ # The method will automatically setup all the associations, and create <tt>before_save</tt>
66
+ # and <tt>before_create</tt> filters for doing the stamping.
67
+ # By default, the deleter association and before filter are not defined unless you are using
68
+ # acts_as_paranoid or you set the :deleter_attribute or set the :deleter option to true.
69
+ def stampable(options = {})
70
+ compatability = Ddb::Userstamp.compatibility_mode
71
+ defaults = {
72
+ :stamper_class_name => :user,
73
+ :creator_attribute => (compatability ? :created_by : :creator_id),
74
+ :updater_attribute => (compatability ? :updated_by : :updater_id),
75
+ :deleter_attribute => (compatability ? :deleted_by : :deleter_id),
76
+ :deleter => !!(options.has_key?(:deleter_attribute) or defined?(Caboose::Acts::Paranoid))
77
+ }.merge(options)
78
+
79
+ self.stamper_class_name = defaults[:stamper_class_name].to_sym
80
+ self.creator_attribute = defaults[:creator_attribute].to_sym
81
+ self.updater_attribute = defaults[:updater_attribute].to_sym
82
+ self.deleter_attribute = defaults[:deleter_attribute].to_sym
83
+
84
+ class_eval do
85
+ klass = "::#{stamper_class_name.to_s.singularize.camelize}"
86
+ belongs_to :creator, :class_name => klass, :foreign_key => creator_attribute
87
+ belongs_to :updater, :class_name => klass, :foreign_key => updater_attribute
88
+
89
+ before_validation :set_updater_attribute
90
+ before_validation :set_creator_attribute, :on => :create
91
+
92
+ if defaults[:deleter]
93
+ belongs_to :deleter, :class_name => klass, :foreign_key => deleter_attribute
94
+ before_destroy :set_deleter_attribute
95
+ end
96
+ end
97
+ end
98
+
99
+ # Temporarily allows you to turn stamping off. For example:
100
+ #
101
+ # Post.without_stamps do
102
+ # post = Post.find(params[:id])
103
+ # post.update_attributes(params[:post])
104
+ # post.save
105
+ # end
106
+ def without_stamps
107
+ original_value = self.record_userstamp
108
+ self.record_userstamp = false
109
+ yield
110
+ ensure
111
+ self.record_userstamp = original_value
112
+ end
113
+
114
+ def stamper_class #:nodoc:
115
+ stamper_class_name.to_s.camelize.constantize rescue nil
116
+ end
117
+ end
118
+
119
+ module InstanceMethods #:nodoc:
120
+ private
121
+ def has_stamper?
122
+ !self.class.stamper_class.nil? && !self.class.stamper_class.stamper.nil? rescue false
123
+ end
124
+
125
+ def set_creator_attribute
126
+ return unless self.record_userstamp
127
+ if respond_to?(self.creator_attribute.to_sym) && has_stamper?
128
+ self.send("#{self.creator_attribute}=".to_sym, self.class.stamper_class.stamper)
129
+ end
130
+ end
131
+
132
+ def set_updater_attribute
133
+ return unless self.record_userstamp
134
+ if respond_to?(self.updater_attribute.to_sym) && has_stamper?
135
+ self.send("#{self.updater_attribute}=".to_sym, self.class.stamper_class.stamper)
136
+ end
137
+ end
138
+
139
+ def set_deleter_attribute
140
+ return unless self.record_userstamp
141
+ if respond_to?(self.deleter_attribute.to_sym) && has_stamper?
142
+ self.send("#{self.deleter_attribute}=".to_sym, self.class.stamper_class.stamper)
143
+ save
144
+ end
145
+ end
146
+ #end private
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ ActiveRecord::Base.send(:include, Ddb::Userstamp::Stampable) if defined?(ActiveRecord)
data/lib/stamper.rb ADDED
@@ -0,0 +1,43 @@
1
+ module Ddb #:nodoc:
2
+ module Userstamp
3
+ module Stamper
4
+ def self.included(base) # :nodoc:
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def model_stamper
10
+ # don't allow multiple calls
11
+ return if self.included_modules.include?(Ddb::Userstamp::Stamper::InstanceMethods)
12
+ send(:extend, Ddb::Userstamp::Stamper::InstanceMethods)
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+ # Used to set the stamper for a particular request. See the Userstamp module for more
18
+ # details on how to use this method.
19
+ def stamper=(object)
20
+ object_stamper = if object.is_a?(ActiveRecord::Base)
21
+ object.send("#{object.class.primary_key}".to_sym)
22
+ else
23
+ object
24
+ end
25
+
26
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
27
+ end
28
+
29
+ # Retrieves the existing stamper for the current request.
30
+ def stamper
31
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
32
+ end
33
+
34
+ # Sets the stamper back to +nil+ to prepare for the next request.
35
+ def reset_stamper
36
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = nil
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ ActiveRecord::Base.send(:include, Ddb::Userstamp::Stamper) if defined?(ActiveRecord)