lean_stamper 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
@@ -0,0 +1,39 @@
1
+ == 0.0.2
2
+
3
+ Including modules does all the work, no need to call stamper/stampable explicitly
4
+
5
+ include Ddb::Userstamp::Stampable
6
+ include Ddb::Userstamp::Stamper
7
+
8
+ == 0.0.1
9
+
10
+ Only creator and updater are set and other changes lost in history
11
+
12
+ == Older changes
13
+
14
+ 2.0 (2-17-2008)
15
+ * [Ben Wyrosdick] - Added a migration helper that gives migration scripts a <tt>userstamps</tt>
16
+ method.
17
+ * [Marshall Roch] - Stamping can be temporarily turned off using the 'without_stamps' class
18
+ method.
19
+ Example:
20
+ Post.without_stamps do
21
+ post = Post.find(params[:id])
22
+ post.update_attributes(params[:post])
23
+ post.save
24
+ end
25
+
26
+ * Models that should receive updates made by 'stampers' now use the acts_as_stampable class
27
+ method. This sets up the belongs_to relationships and also injects private methods for use by
28
+ the individual callback filter methods.
29
+
30
+ * Models that are responsible for updating now use the acts_as_stamper class method. This
31
+ injects the stamper= and stamper methods that are thread safe and should be updated per
32
+ request by a controller.
33
+
34
+ * The Userstamp module is now meant to be included with one of your project's controllers (the
35
+ Application Controller is recommended). It creates a before filter called 'set_stampers' that
36
+ is responsible for setting all the current Stampers.
37
+
38
+ 1.0 (01-18-2006)
39
+ * Initial Release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lean_userstamp.gemspec
4
+ gemspec
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,183 @@
1
+ == Fork info
2
+
3
+ Please read the Changelog file for changes.
4
+
5
+ = Userstamp Plugin (v 2.0)
6
+
7
+ == Overview
8
+
9
+ The Userstamp Plugin extends ActiveRecord::Base[http://api.rubyonrails.com/classes/ActiveRecord/Base.html] to add automatic updating of 'creator',
10
+ 'updater', and 'deleter' attributes. It is based loosely on the ActiveRecord::Timestamp[http://api.rubyonrails.com/classes/ActiveRecord/Timestamp.html] module.
11
+
12
+ Two class methods (<tt>model_stamper</tt> and <tt>stampable</tt>) are implemented in this plugin.
13
+ The <tt>model_stamper</tt> method is used in models that are responsible for creating, updating, or
14
+ deleting other objects. The <tt>stampable</tt> method is used in models that are subject to being
15
+ created, updated, or deleted by 'stampers'.
16
+
17
+
18
+ == Installation
19
+ Installation of the plugin can be done using the built in Rails plugin script. Issue the following
20
+ command from the root of your application:
21
+
22
+ script/plugin install git://github.com/delynn/userstamp.git
23
+
24
+ Once installed you will need to restart your application for the plugin to be loaded into the Rails
25
+ environment.
26
+
27
+ You might also be interested in using Piston[http://piston.rubyforge.org/index.html] to manage the
28
+ importing and future updating of this plugin.
29
+
30
+ == Usage
31
+ In this new version of the Userstamp plug-in, the assumption is that you have two different
32
+ categories of objects; those that mani˝pulate, and those that are manipulated. For those objects
33
+ that are being manipulated there's the Stampable module and for the manipulators there's the
34
+ Stamper module. There's also the actual Userstamp module for your controllers that assists in
35
+ setting up your environment on a per request basis.
36
+
37
+ To better understand how all this works, I think an example is in order. For this example we will
38
+ assume that a weblog application is comprised of User and Post objects. The first thing we need to
39
+ do is create the migrations for these objects, and the plug-in gives you a <tt>userstamps</tt>
40
+ method for very easily doing this:
41
+
42
+ class CreateUsers < ActiveRecord::Migration
43
+ def self.up
44
+ create_table :users, :force => true do |t|
45
+ t.timestamps
46
+ t.userstamps
47
+ t.name
48
+ end
49
+ end
50
+
51
+ def self.down
52
+ drop_table :users
53
+ end
54
+ end
55
+
56
+ class CreatePosts < ActiveRecord::Migration
57
+ def self.up
58
+ create_table :posts, :force => true do |t|
59
+ t.timestamps
60
+ t.userstamps
61
+ t.title
62
+ end
63
+ end
64
+
65
+ def self.down
66
+ drop_table :posts
67
+ end
68
+ end
69
+
70
+ Second, since Users are going to manipulate other objects in our project, we'll use the
71
+ <tt>model_stamper</tt> method in our User class:
72
+
73
+ class User < ActiveRecord::Base
74
+ model_stamper
75
+ end
76
+
77
+ Finally, we need to setup a controller to set the current user of the application. It's
78
+ recommended that you do this in your ApplicationController:
79
+
80
+ class ApplicationController < ActionController::Base
81
+ include Userstamp
82
+ end
83
+
84
+ If all you are interested in is making sure all tables that have the proper columns are stamped
85
+ by the currently logged in user you can stop right here. More than likely you want all your
86
+ associations setup on your stamped objects, and that's where the <tt>stampable</tt> class method
87
+ comes in. So in our example we'll want to use this method in both our User and Post classes:
88
+
89
+ class User < ActiveRecord::Base
90
+ model_stamper
91
+ stampable
92
+ end
93
+
94
+ class Post < ActiveRecord::Base
95
+ stampable
96
+ end
97
+
98
+ Okay, so what all have we done? The <tt>model_stamper</tt> class method injects two methods into the
99
+ User class. They are #stamper= and #stamper and look like this:
100
+
101
+ def stamper=(object)
102
+ object_stamper = if object.is_a?(ActiveRecord::Base)
103
+ object.send("#{object.class.primary_key}".to_sym)
104
+ else
105
+ object
106
+ end
107
+
108
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
109
+ end
110
+
111
+ def stamper
112
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
113
+ end
114
+
115
+ The big change with this new version is that we are now using Thread.current to save the current
116
+ stamper so as to avoid conflict with concurrent requests.
117
+
118
+ The <tt>stampable</tt> method allows you to customize what columns will get stamped, and also
119
+ creates the +creator+, +updater+, and +deleter+ associations.
120
+
121
+ The Userstamp module that we included into our ApplicationController uses the setter method to
122
+ set which user is currently making the request. By default the 'set_stampers' method works perfectly
123
+ with the RestfulAuthentication[http://svn.techno-weenie.net/projects/plugins/restful_authentication] plug-in:
124
+
125
+ def set_stampers
126
+ User.stamper = self.current_user
127
+ end
128
+
129
+ If you aren't using ActsAsAuthenticated, then you need to create your own version of the
130
+ <tt>set_stampers</tt> method in the controller where you've included the Userstamp module.
131
+
132
+ Now, let's get back to the Stampable module (since it really is the interesting one). The Stampable
133
+ module sets up before_* filters that are responsible for setting those attributes at the appropriate
134
+ times. It also creates the belongs_to relationships for you.
135
+
136
+ If you need to customize the columns that are stamped, the <tt>stampable</tt> method can be
137
+ completely customized. Here's an quick example:
138
+
139
+ class Post < ActiveRecord::Base
140
+ acts_as_stampable :stamper_class_name => :person,
141
+ :creator_attribute => :create_user,
142
+ :updater_attribute => :update_user,
143
+ :deleter_attribute => :delete_user
144
+ end
145
+
146
+ If you are upgrading your application from the old version of Userstamp, there is a compatibility
147
+ mode to have the plug-in use the old "_by" columns by default. To enable this mode, add the
148
+ following line to the RAILS_ROOT/config/environment.rb file:
149
+
150
+ Ddb::Userstamp.compatibility_mode = true
151
+
152
+ If you are having a difficult time getting the Userstamp plug-in to work, I recommend you checkout
153
+ the sample application that I created. You can find this application on GitHub[http://github.com/delynn/userstamp_sample]
154
+
155
+ == Uninstall
156
+ Uninstalling the plugin can be done using the built in Rails plugin script. Issue the following
157
+ command from the root of your application:
158
+
159
+ script/plugin remove userstamp
160
+
161
+
162
+ == Documentation
163
+ RDoc has been run on the plugin directory and is available in the doc directory.
164
+
165
+
166
+ == Running Unit Tests
167
+ There are extensive unit tests in the "test" directory of the plugin. These test can be run
168
+ individually by executing the following command from the userstamp directory:
169
+
170
+ ruby test/compatibility_stamping_test.rb
171
+ ruby test/stamping_test.rb
172
+ ruby test/userstamp_controller_test.rb
173
+
174
+
175
+ == Bugs & Feedback
176
+ Bug reports and feedback are welcome via my delynn+userstamp@gmail.com email address. I also
177
+ encouraged everyone to clone the git repository and make modifications--I'll be more than happy
178
+ to merge any changes from other people's branches that would be beneficial to the whole project.
179
+
180
+
181
+ == Credits and Special Thanks
182
+ The original idea for this plugin came from the Rails Wiki article entitled
183
+ {Extending ActiveRecord}[http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample].
@@ -0,0 +1,22 @@
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', 'CHANGELOG', 'LICENSE')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'userstamp'
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["DeLynn Berry"]
5
+ gem.email = ["delynn@gmail.com"]
6
+ gem.description = %q{A stripped down version of original userstamp plugin}
7
+ gem.summary = %q{A stripped down version of original userstamp plugin. References the stamper object itself, not the id. Only creator and updater.}
8
+ gem.homepage = ""
9
+
10
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.name = "lean_stamper"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = "0.0.2"
16
+ end
@@ -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, :null => false, :references => 'users')
11
+ column(Ddb::Userstamp.compatibility_mode ? :updated_by : :updater_id, :integer, :null => false, :references => 'users')
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,50 @@
1
+ module Ddb
2
+ module Userstamp
3
+ module Stampable
4
+ def self.included(base)
5
+ super
6
+
7
+ base.extend(ClassMethods)
8
+ base.class_eval do
9
+ include InstanceMethods
10
+ self.stampable
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def stampable
16
+ class_eval do
17
+ belongs_to :creator, :class_name => 'User',
18
+ :foreign_key => :creator_id
19
+
20
+ belongs_to :updater, :class_name => 'User',
21
+ :foreign_key => :updater_id
22
+
23
+ before_save :set_updater_attribute
24
+ before_create :set_creator_attribute
25
+ end
26
+ end
27
+ end
28
+
29
+ module InstanceMethods
30
+
31
+ private
32
+ def has_stamper?
33
+ !User.stamper.nil?
34
+ end
35
+
36
+ def set_creator_attribute
37
+ if respond_to?(:creator=) && has_stamper?
38
+ self.creator = User.stamper
39
+ end
40
+ end
41
+
42
+ def set_updater_attribute
43
+ if respond_to?(:updater=) && has_stamper?
44
+ self.updater = User.stamper
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,27 @@
1
+ module Ddb #:nodoc:
2
+ module Userstamp
3
+ module Stamper
4
+ def self.included(base) # :nodoc:
5
+ base.extend(Ddb::Userstamp::Stamper::ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ # Used to set the stamper for a particular request. See the Userstamp module for more
10
+ # details on how to use this method.
11
+ def stamper=(object)
12
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object
13
+ end
14
+
15
+ # Retrieves the existing stamper for the current request.
16
+ def stamper
17
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
18
+ end
19
+
20
+ # Sets the stamper back to +nil+ to prepare for the next request.
21
+ def reset_stamper
22
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = nil
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+ require 'stamper'
2
+ require 'stampable'
3
+ require 'migration_helper'
4
+
5
+ module Ddb
6
+ module Controller
7
+ # The Userstamp module, when included into a controller, adds a before filter
8
+ # (named <tt>set_stamper</tt>) and an after filter (name <tt>reset_stamper</tt>).
9
+ # These methods assume a couple of things, but can be re-implemented in your
10
+ # controller to better suite your application.
11
+ #
12
+ # See the documentation for <tt>set_stamper</tt> and <tt>reset_stamper</tt> for
13
+ # specific implementation details.
14
+ module Userstamp
15
+ def self.included(base) # :nodoc:
16
+ base.send :include, InstanceMethods
17
+ base.before_filter :set_stamper
18
+ base.after_filter :reset_stamper
19
+ end
20
+
21
+ module InstanceMethods
22
+ private
23
+ # The <tt>set_stamper</tt> method as implemented here assumes a couple
24
+ # of things. First, that you are using a +User+ model as the stamper
25
+ # and second that your controller has a <tt>current_user</tt> method
26
+ # that contains the currently logged in stamper. If either of these
27
+ # are not the case in your application you will want to manually add
28
+ # your own implementation of this method to the private section of
29
+ # the controller where you are including the Userstamp module.
30
+ def set_stamper
31
+ User.stamper = self.current_user
32
+ end
33
+
34
+ # The <tt>reset_stamper</tt> method as implemented here assumes that a
35
+ # +User+ model is being used as the stamper. If this is not the case then
36
+ # you will need to manually add your own implementation of this method to
37
+ # the private section of the controller where you are including the
38
+ # Userstamp module.
39
+ def reset_stamper
40
+ User.reset_stamper
41
+ end
42
+ #end private
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ ActionController::Base.send(:include, Ddb::Controller) if defined?(ActionController)
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lean_stamper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - DeLynn Berry
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A stripped down version of original userstamp plugin
15
+ email:
16
+ - delynn@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - init.rb
28
+ - lean_stamper.gemspec
29
+ - lib/lean_stamper.rb
30
+ - lib/migration_helper.rb
31
+ - lib/stampable.rb
32
+ - lib/stamper.rb
33
+ - lib/userstamp.rb
34
+ homepage: ''
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.11
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: A stripped down version of original userstamp plugin. References the stamper
58
+ object itself, not the id. Only creator and updater.
59
+ test_files: []