userstamps 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +64 -0
- data/Rakefile +29 -0
- data/VERSION +1 -0
- data/lib/active_record/associations/user_stamps.rb +50 -0
- data/lib/userstamps.rb +3 -0
- data/spec/.gitignore +1 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/user_stamps_spec.rb +101 -0
- data/userstamps.gemspec +51 -0
- metadata +78 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/*
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Reid MacDonald <reid@laruby.com>
|
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/README.rdoc
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= UserStamps
|
2
|
+
|
3
|
+
Adds a simple +belongs_to+ associations to a user model based on a magic columns
|
4
|
+
+created_by+ and +updated_by+ as it's foreign key.
|
5
|
+
|
6
|
+
== Example
|
7
|
+
|
8
|
+
class User < ActiveRecord::Base
|
9
|
+
end
|
10
|
+
|
11
|
+
class Item < ActiveRecord::Base
|
12
|
+
stamped_by :user
|
13
|
+
end
|
14
|
+
|
15
|
+
i = Item.find(1)
|
16
|
+
i.created_by #=> 3
|
17
|
+
i.creator #=> #<User id:3>
|
18
|
+
i.updated_by #=> 5
|
19
|
+
i.updator #=> #<User id:5>
|
20
|
+
|
21
|
+
== Configuration
|
22
|
+
|
23
|
+
You can specify configure how your +creator+ and +updator+ can be specified but
|
24
|
+
you have to pass options to +created_by+ and +updated_by+ separately:
|
25
|
+
|
26
|
+
class Item < ActiveRecord::Base
|
27
|
+
created_by :user
|
28
|
+
updated_by :admin
|
29
|
+
end
|
30
|
+
|
31
|
+
i = Item.find(1)
|
32
|
+
i.created_by #=> 3
|
33
|
+
i.creator #=> #<User id:3>
|
34
|
+
i.updated_by #=> 1
|
35
|
+
i.updator #=> #<Admin id:1>
|
36
|
+
|
37
|
+
== Options
|
38
|
+
|
39
|
+
By default, the creator is required, you can turn this off with a :required option:
|
40
|
+
|
41
|
+
created_by :user, :required => false
|
42
|
+
|
43
|
+
By default, the updator is _not_ required because on create you will need to specify
|
44
|
+
an updator, but you can turn this on with a :required option:
|
45
|
+
|
46
|
+
updated_by :admin, :required => true
|
47
|
+
|
48
|
+
You could pass a :foriegn_key option to set a specific +created_by+ or +updated_by+
|
49
|
+
foriegn key but you might as well just setup the association yourself at that point.
|
50
|
+
|
51
|
+
== Using only Created By
|
52
|
+
|
53
|
+
If you only care about creators you can just specify a +created_by+ instead of
|
54
|
+
+stamped_by+ as an option.
|
55
|
+
|
56
|
+
class Item < ActiveRecord::Base
|
57
|
+
created_by :user
|
58
|
+
end
|
59
|
+
|
60
|
+
i = Item.find(1)
|
61
|
+
i.created_by #=> 3
|
62
|
+
i.creator #=> #<User id:3>
|
63
|
+
|
64
|
+
Copyright (c) 2010 Reid MacDonald <reid@laruby.com>, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "userstamps"
|
8
|
+
gem.summary = %Q{Extending ActiveRecord with stamping for created_by and updated_by fields}
|
9
|
+
gem.description = %Q{Gem that extends ActiveRecord with stamping for created_by and updated_by fields}
|
10
|
+
gem.email = "stephan.kaag@holder.nl"
|
11
|
+
gem.homepage = "http://github.com/stephankaag/userstamps"
|
12
|
+
gem.authors = ["Stephan Kaag"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
|
22
|
+
desc 'Default: run specs.'
|
23
|
+
task :default => :spec
|
24
|
+
|
25
|
+
desc 'Run the specs'
|
26
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
27
|
+
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
|
28
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActiveRecord # :nodoc:
|
2
|
+
module Associations # :nodoc:
|
3
|
+
module UserStamps # :nodoc:
|
4
|
+
##
|
5
|
+
# Extends ActiveRecord::Base with ClassMethods
|
6
|
+
def self.included base
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
##
|
11
|
+
# Methods added to ActiveRecord::Base
|
12
|
+
module ClassMethods
|
13
|
+
##
|
14
|
+
# Creates associations for +created_by+ and +updated_by+ using the respective configuration of each.
|
15
|
+
def stamped_by user_model
|
16
|
+
created_by user_model
|
17
|
+
updated_by user_model
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# Creates an association on the +created_by+ column to the supplied +user_model+ argument.
|
22
|
+
# You can override the +created_by+ column name with :foriegn_key but at that point you might
|
23
|
+
# as well set up the relationship yourself.
|
24
|
+
#
|
25
|
+
# Required by default.
|
26
|
+
def created_by user_model, options = {}
|
27
|
+
user_association :creator, user_model, options.reverse_merge(:foreign_key => 'created_by', :required => true)
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
# Creates an association on the +updated_by+ column to the supplied +user_model+ argument.
|
32
|
+
# You can override the +updated_by+ column name with :foriegn_key but at that point you might
|
33
|
+
# as well set up the relationship yourself.
|
34
|
+
#
|
35
|
+
# Not required by default.
|
36
|
+
def updated_by user_model, options = {}
|
37
|
+
user_association :updator, user_model, options.reverse_merge(:foreign_key => 'updated_by', :required => false)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def user_association association, user_model, options
|
42
|
+
configuration = {:class_name => user_model.to_s.classify}.merge!(options)
|
43
|
+
|
44
|
+
belongs_to association, :foreign_key => configuration[:foreign_key], :class_name => configuration[:class_name]
|
45
|
+
validates_presence_of association if configuration[:required]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/userstamps.rb
ADDED
data/spec/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.log
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
begin
|
2
|
+
require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
|
3
|
+
rescue LoadError => e
|
4
|
+
puts e
|
5
|
+
puts "You need to install rspec in your base app"
|
6
|
+
exit
|
7
|
+
end
|
8
|
+
|
9
|
+
plugin_spec_dir = File.dirname(__FILE__)
|
10
|
+
ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class MockActiveRecord < ActiveRecord::Base
|
4
|
+
def self.column_names
|
5
|
+
%w[id created_by updated_by]
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.columns
|
9
|
+
column_names.map do |name|
|
10
|
+
ActiveRecord::ConnectionAdapters::Column.new(name.to_s, nil)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe ActiveRecord::Base do
|
16
|
+
it "includes ActiveRecord::Assocations::UserStamps::ClassMethods" do
|
17
|
+
ActiveRecord::Base.methods.should be_member('stamped_by')
|
18
|
+
ActiveRecord::Base.methods.should be_member('created_by')
|
19
|
+
ActiveRecord::Base.methods.should be_member('updated_by')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe :created_by do
|
24
|
+
describe :belongs_to do
|
25
|
+
it "creates the belongs_to :creator association with default options" do
|
26
|
+
MockActiveRecord.should_receive(:belongs_to).with(:creator, :class_name => 'MockCreator', :foreign_key => 'created_by')
|
27
|
+
MockActiveRecord.created_by :mock_creator
|
28
|
+
end
|
29
|
+
|
30
|
+
it "creates the belongs_to association with alternate foreign_key" do
|
31
|
+
MockActiveRecord.should_receive(:belongs_to).with(:creator, :class_name => 'MockCreator', :foreign_key => 'creator_id')
|
32
|
+
MockActiveRecord.created_by :mock_creator, :foreign_key => 'creator_id'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "creates the belongs_to association with alternate class_name (but why?)" do
|
36
|
+
MockActiveRecord.should_receive(:belongs_to).with(:creator, :class_name => 'AnotherClassName', :foreign_key => 'created_by')
|
37
|
+
MockActiveRecord.created_by :mock_creator, :class_name => 'AnotherClassName'
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'ignores all other options' do
|
41
|
+
MockActiveRecord.should_receive(:belongs_to).with(:creator, :class_name => 'MockCreator', :foreign_key => 'created_by')
|
42
|
+
MockActiveRecord.created_by :mock_creator, :foo => 'Bar'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe :validates_presence_of do
|
47
|
+
it 'is added by default' do
|
48
|
+
MockActiveRecord.should_receive(:validates_presence_of).with(:creator)
|
49
|
+
MockActiveRecord.created_by :mock_creator
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'turns off validation for creator' do
|
53
|
+
MockActiveRecord.should_not_receive(:validates_presence_of)
|
54
|
+
MockActiveRecord.created_by :mock_creator, :required => false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe :updated_by do
|
60
|
+
describe :belongs_to do
|
61
|
+
it "creates the belongs_to :updator association with default options" do
|
62
|
+
MockActiveRecord.should_receive(:belongs_to).with(:updator, :class_name => 'MockUpdator', :foreign_key => 'updated_by')
|
63
|
+
MockActiveRecord.updated_by :mock_updator
|
64
|
+
end
|
65
|
+
|
66
|
+
it "creates the belongs_to association with alternate foreign_key" do
|
67
|
+
MockActiveRecord.should_receive(:belongs_to).with(:updator, :class_name => 'MockUpdator', :foreign_key => 'updator_id')
|
68
|
+
MockActiveRecord.updated_by :mock_updator, :foreign_key => 'updator_id'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "creates the belongs_to association with alternate class_name (but why?)" do
|
72
|
+
MockActiveRecord.should_receive(:belongs_to).with(:updator, :class_name => 'AnotherClassName', :foreign_key => 'updated_by')
|
73
|
+
MockActiveRecord.updated_by :mock_updator, :class_name => 'AnotherClassName'
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'ignores all other options' do
|
77
|
+
MockActiveRecord.should_receive(:belongs_to).with(:updator, :class_name => 'MockUpdator', :foreign_key => 'updated_by')
|
78
|
+
MockActiveRecord.updated_by :mock_updator, :foo => 'Bar'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe :validates_presence_of do
|
83
|
+
it 'is not added by default' do
|
84
|
+
MockActiveRecord.should_not_receive(:validates_presence_of).with(:updator)
|
85
|
+
MockActiveRecord.updated_by :mock_updator
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'turns on validation for updator' do
|
89
|
+
MockActiveRecord.should_receive(:validates_presence_of)
|
90
|
+
MockActiveRecord.updated_by :mock_updator, :required => true
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe :stamped_by do
|
96
|
+
it 'uses the same model on created_by and updated_by' do
|
97
|
+
MockActiveRecord.should_receive(:created_by).with(:user)
|
98
|
+
MockActiveRecord.should_receive(:updated_by).with(:user)
|
99
|
+
MockActiveRecord.stamped_by :user
|
100
|
+
end
|
101
|
+
end
|
data/userstamps.gemspec
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{userstamps}
|
8
|
+
s.version = "0.0.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Stephan Kaag"]
|
12
|
+
s.date = %q{2010-06-04}
|
13
|
+
s.description = %q{Gem that extends ActiveRecord with stamping for created_by and updated_by fields}
|
14
|
+
s.email = %q{stephan.kaag@holder.nl}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README.rdoc",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"lib/active_record/associations/user_stamps.rb",
|
25
|
+
"lib/userstamps.rb",
|
26
|
+
"spec/.gitignore",
|
27
|
+
"spec/spec_helper.rb",
|
28
|
+
"spec/user_stamps_spec.rb",
|
29
|
+
"userstamps.gemspec"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/stephankaag/userstamps}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
35
|
+
s.summary = %q{Extending ActiveRecord with stamping for created_by and updated_by fields}
|
36
|
+
s.test_files = [
|
37
|
+
"spec/spec_helper.rb",
|
38
|
+
"spec/user_stamps_spec.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
else
|
47
|
+
end
|
48
|
+
else
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: userstamps
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Stephan Kaag
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-04 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Gem that extends ActiveRecord with stamping for created_by and updated_by fields
|
23
|
+
email: stephan.kaag@holder.nl
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- MIT-LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
- Rakefile
|
35
|
+
- VERSION
|
36
|
+
- lib/active_record/associations/user_stamps.rb
|
37
|
+
- lib/userstamps.rb
|
38
|
+
- spec/.gitignore
|
39
|
+
- spec/spec_helper.rb
|
40
|
+
- spec/user_stamps_spec.rb
|
41
|
+
- userstamps.gemspec
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/stephankaag/userstamps
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.3.7
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Extending ActiveRecord with stamping for created_by and updated_by fields
|
76
|
+
test_files:
|
77
|
+
- spec/spec_helper.rb
|
78
|
+
- spec/user_stamps_spec.rb
|