magiclabs-userstamp 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/CHANGELOG +26 -0
  2. data/Gemfile +11 -0
  3. data/Gemfile.lock +55 -0
  4. data/LICENSE +20 -0
  5. data/Rakefile +38 -0
  6. data/Readme.rdoc +163 -0
  7. data/VERSION +1 -0
  8. data/init.rb +1 -0
  9. data/lib/migration_helper.rb +19 -0
  10. data/lib/stampable.rb +154 -0
  11. data/lib/stamper.rb +43 -0
  12. data/lib/userstamp.rb +52 -0
  13. data/magiclabs-userstamp.gemspec +85 -0
  14. data/rdoc/classes/Ddb/Controller.html +111 -0
  15. data/rdoc/classes/Ddb/Controller/Userstamp.html +125 -0
  16. data/rdoc/classes/Ddb/Controller/Userstamp/InstanceMethods.html +105 -0
  17. data/rdoc/classes/Ddb/Userstamp.html +121 -0
  18. data/rdoc/classes/Ddb/Userstamp/MigrationHelper.html +111 -0
  19. data/rdoc/classes/Ddb/Userstamp/MigrationHelper/InstanceMethods.html +142 -0
  20. data/rdoc/classes/Ddb/Userstamp/Stampable.html +128 -0
  21. data/rdoc/classes/Ddb/Userstamp/Stampable/ClassMethods.html +225 -0
  22. data/rdoc/classes/Ddb/Userstamp/Stamper.html +112 -0
  23. data/rdoc/classes/Ddb/Userstamp/Stamper/ClassMethods.html +142 -0
  24. data/rdoc/classes/Ddb/Userstamp/Stamper/InstanceMethods.html +207 -0
  25. data/rdoc/classes/Userstamp.html +118 -0
  26. data/rdoc/created.rid +1 -0
  27. data/rdoc/files/CHANGELOG.html +137 -0
  28. data/rdoc/files/LICENSE.html +129 -0
  29. data/rdoc/files/Readme_rdoc.html +301 -0
  30. data/rdoc/files/lib/migration_helper_rb.html +101 -0
  31. data/rdoc/files/lib/stampable_rb.html +101 -0
  32. data/rdoc/files/lib/stamper_rb.html +101 -0
  33. data/rdoc/files/lib/userstamp_rb.html +110 -0
  34. data/rdoc/fr_class_index.html +38 -0
  35. data/rdoc/fr_file_index.html +33 -0
  36. data/rdoc/fr_method_index.html +33 -0
  37. data/rdoc/index.html +24 -0
  38. data/rdoc/rdoc-style.css +208 -0
  39. data/test/compatibility_stamping_test.rb +69 -0
  40. data/test/controllers/posts_controller.rb +26 -0
  41. data/test/controllers/users_controller.rb +12 -0
  42. data/test/controllers/userstamp_controller.rb +9 -0
  43. data/test/helper.rb +61 -0
  44. data/test/models/comment.rb +5 -0
  45. data/test/models/foo.rb +3 -0
  46. data/test/models/person.rb +3 -0
  47. data/test/models/post.rb +13 -0
  48. data/test/models/user.rb +3 -0
  49. data/test/schema.rb +54 -0
  50. data/test/stamping_test.rb +138 -0
  51. data/test/userstamp_controller_test.rb +103 -0
  52. data/test/userstamp_test.rb +7 -0
  53. metadata +105 -0
@@ -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,11 @@
1
+ source :rubygems
2
+
3
+ group :dev do
4
+ gem 'rake'
5
+ gem 'redgreen'
6
+ gem 'jeweler'
7
+ gem 'activerecord', :require => 'active_record'
8
+ gem 'activesupport', :require => 'active_support'
9
+ gem 'actionpack', :require => 'action_pack'
10
+ gem 'sqlite3-ruby', :require => 'sqlite3'
11
+ end
@@ -0,0 +1,55 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionpack (3.0.3)
6
+ activemodel (= 3.0.3)
7
+ activesupport (= 3.0.3)
8
+ builder (~> 2.1.2)
9
+ erubis (~> 2.6.6)
10
+ i18n (~> 0.4)
11
+ rack (~> 1.2.1)
12
+ rack-mount (~> 0.6.13)
13
+ rack-test (~> 0.5.6)
14
+ tzinfo (~> 0.3.23)
15
+ activemodel (3.0.3)
16
+ activesupport (= 3.0.3)
17
+ builder (~> 2.1.2)
18
+ i18n (~> 0.4)
19
+ activerecord (3.0.3)
20
+ activemodel (= 3.0.3)
21
+ activesupport (= 3.0.3)
22
+ arel (~> 2.0.2)
23
+ tzinfo (~> 0.3.23)
24
+ activesupport (3.0.3)
25
+ arel (2.0.6)
26
+ builder (2.1.2)
27
+ erubis (2.6.6)
28
+ abstract (>= 1.0.0)
29
+ git (1.2.5)
30
+ i18n (0.5.0)
31
+ jeweler (1.5.2)
32
+ bundler (~> 1.0.0)
33
+ git (>= 1.2.5)
34
+ rake
35
+ rack (1.2.1)
36
+ rack-mount (0.6.13)
37
+ rack (>= 1.0.0)
38
+ rack-test (0.5.7)
39
+ rack (>= 1.0)
40
+ rake (0.8.7)
41
+ redgreen (1.2.2)
42
+ sqlite3-ruby (1.3.2)
43
+ tzinfo (0.3.23)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ actionpack
50
+ activerecord
51
+ activesupport
52
+ jeweler
53
+ rake
54
+ redgreen
55
+ sqlite3-ruby
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.
@@ -0,0 +1,38 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the userstamp plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the userstamp plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Userstamp'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('Readme.rdoc', 'CHANGELOG', 'LICENSE')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ begin
25
+ require 'jeweler'
26
+ project_name = 'magiclabs-userstamp'
27
+ Jeweler::Tasks.new do |gem|
28
+ gem.name = project_name
29
+ gem.summary = "This Rails plugin 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."
30
+ gem.email = "delynn@gmail.com"
31
+ gem.homepage = "https://github.com/magiclabs/userstamp"
32
+ gem.authors = ["DeLynn Berry"]
33
+ end
34
+
35
+ Jeweler::GemcutterTasks.new
36
+ rescue LoadError
37
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
38
+ end
@@ -0,0 +1,163 @@
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 'userstamp', :git => "git://github.com/insphire/userstamp.git"
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
+ If all you are interested in is making sure all tables that have the proper columns are stamped
84
+ by the currently logged in user you can stop right here. More than likely you want all your
85
+ associations setup on your stamped objects, and that's where the <tt>stampable</tt> class method
86
+ comes in. So in our example we'll want to use this method in both our User and Post classes:
87
+
88
+ class User < ActiveRecord::Base
89
+ model_stamper
90
+ stampable
91
+ end
92
+
93
+ class Post < ActiveRecord::Base
94
+ stampable
95
+ end
96
+
97
+ Okay, so what all have we done? The <tt>model_stamper</tt> class method injects two methods into the
98
+ User class. They are #stamper= and #stamper and look like this:
99
+
100
+ def stamper=(object)
101
+ object_stamper = if object.is_a?(ActiveRecord::Base)
102
+ object.send("#{object.class.primary_key}".to_sym)
103
+ else
104
+ object
105
+ end
106
+
107
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
108
+ end
109
+
110
+ def stamper
111
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
112
+ end
113
+
114
+ The <tt>stampable</tt> method allows you to customize what columns will get stamped, and also
115
+ creates the +creator+, +updater+, and +deleter+ associations.
116
+
117
+ The Userstamp module that we included into our ApplicationController uses the setter method to
118
+ set which user is currently making the request. By default the 'set_stampers' method works perfectly
119
+ with the RestfulAuthentication[http://svn.techno-weenie.net/projects/plugins/restful_authentication] plug-in:
120
+
121
+ def set_stampers
122
+ User.stamper = self.current_user
123
+ end
124
+
125
+ If you aren't using ActsAsAuthenticated, then you need to create your own version of the
126
+ <tt>set_stampers</tt> method in the controller where you've included the Userstamp module.
127
+
128
+ Now, let's get back to the Stampable module (since it really is the interesting one). The Stampable
129
+ module sets up before_* filters that are responsible for setting those attributes at the appropriate
130
+ times. It also creates the belongs_to relationships for you.
131
+
132
+ If you need to customize the columns that are stamped, the <tt>stampable</tt> method can be
133
+ completely customized. Here's an quick example:
134
+
135
+ class Post < ActiveRecord::Base
136
+ stampable :stamper_class_name => :person,
137
+ :creator_attribute => :create_user,
138
+ :updater_attribute => :update_user,
139
+ :deleter_attribute => :delete_user,
140
+ :deleter => true
141
+ end
142
+
143
+ == Upgrade from 1.x
144
+ # config/environment.rb
145
+ Ddb::Userstamp.compatibility_mode = true
146
+
147
+ {Example userstamp application}[http://github.com/delynn/userstamp_sample]
148
+
149
+ == Running Unit Tests
150
+
151
+ All: rake
152
+ One: ruby test/compatibility_stamping_test.rb
153
+
154
+ == Author
155
+ {DeLynn Berry}[http://delynnberry.com/]
156
+
157
+ The original idea for this plugin came from the Rails Wiki article entitled
158
+ {Extending ActiveRecord}[http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample].
159
+
160
+ == Contributors / maintenance / enhancement
161
+ - {Michael Grosser}[http://pragmatig.com]
162
+ - {John Dell}[http://blog.spovich.com/]
163
+ - Chris Hilton
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.0.2
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'userstamp'
@@ -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)
@@ -0,0 +1,154 @@
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
+
50
+ self.stampable
51
+ end
52
+ end
53
+
54
+ module ClassMethods
55
+ # This method is automatically called on for all classes that inherit from
56
+ # ActiveRecord, but if you need to customize how the plug-in functions, this is the
57
+ # method to use. Here's an example:
58
+ #
59
+ # class Post < ActiveRecord::Base
60
+ # stampable :stamper_class_name => :person,
61
+ # :creator_attribute => :create_user,
62
+ # :updater_attribute => :update_user,
63
+ # :deleter_attribute => :delete_user
64
+ # :deleter => true
65
+ # end
66
+ #
67
+ # The method will automatically setup all the associations, and create <tt>before_save</tt>
68
+ # and <tt>before_create</tt> filters for doing the stamping.
69
+ # By default, the deleter association and before filter are not defined unless you are using
70
+ # acts_as_paranoid or you set the :deleter_attribute or set the :deleter option to true.
71
+ def stampable(options = {})
72
+ compatability = Ddb::Userstamp.compatibility_mode
73
+ defaults = {
74
+ :stamper_class_name => :user,
75
+ :creator_attribute => (compatability ? :created_by : :creator_id),
76
+ :updater_attribute => (compatability ? :updated_by : :updater_id),
77
+ :deleter_attribute => (compatability ? :deleted_by : :deleter_id),
78
+ :deleter => !!(options.has_key?(:deleter_attribute) or defined?(Caboose::Acts::Paranoid))
79
+ }.merge(options)
80
+
81
+ self.stamper_class_name = defaults[:stamper_class_name].to_sym
82
+ self.creator_attribute = defaults[:creator_attribute].to_sym
83
+ self.updater_attribute = defaults[:updater_attribute].to_sym
84
+ self.deleter_attribute = defaults[:deleter_attribute].to_sym
85
+
86
+ class_eval do
87
+ klass = "::#{stamper_class_name.to_s.singularize.camelize}"
88
+ belongs_to :creator, :class_name => klass, :foreign_key => creator_attribute
89
+ belongs_to :updater, :class_name => klass, :foreign_key => updater_attribute
90
+
91
+ before_validation :set_updater_attribute
92
+ before_validation :set_creator_attribute, :on => :create
93
+
94
+ if defaults[:deleter]
95
+ belongs_to :deleter, :class_name => klass, :foreign_key => deleter_attribute
96
+ before_destroy :set_deleter_attribute
97
+ end
98
+ end
99
+ end
100
+
101
+ # Temporarily allows you to turn stamping off. For example:
102
+ #
103
+ # Post.without_stamps do
104
+ # post = Post.find(params[:id])
105
+ # post.update_attributes(params[:post])
106
+ # post.save
107
+ # end
108
+ def without_stamps
109
+ original_value = self.record_userstamp
110
+ self.record_userstamp = false
111
+ yield
112
+ ensure
113
+ self.record_userstamp = original_value
114
+ end
115
+
116
+ def stamper_class #:nodoc:
117
+ stamper_class_name.to_s.camelize.constantize rescue nil
118
+ end
119
+ end
120
+
121
+ module InstanceMethods #:nodoc:
122
+ private
123
+ def has_stamper?
124
+ !self.class.stamper_class.nil? && !self.class.stamper_class.stamper.nil? rescue false
125
+ end
126
+
127
+ def set_creator_attribute
128
+ return unless self.record_userstamp
129
+ if respond_to?(self.creator_attribute.to_sym) && has_stamper?
130
+ self.send("#{self.creator_attribute}=".to_sym, self.class.stamper_class.stamper)
131
+ end
132
+ end
133
+
134
+ def set_updater_attribute
135
+ return unless self.record_userstamp
136
+ if respond_to?(self.updater_attribute.to_sym) && has_stamper?
137
+ self.send("#{self.updater_attribute}=".to_sym, self.class.stamper_class.stamper)
138
+ end
139
+ end
140
+
141
+ def set_deleter_attribute
142
+ return unless self.record_userstamp
143
+ if respond_to?(self.deleter_attribute.to_sym) && has_stamper?
144
+ self.send("#{self.deleter_attribute}=".to_sym, self.class.stamper_class.stamper)
145
+ save
146
+ end
147
+ end
148
+ #end private
149
+ end
150
+ end
151
+ end
152
+ end
153
+
154
+ ActiveRecord::Base.send(:include, Ddb::Userstamp::Stampable) if defined?(ActiveRecord)