acts_as_blamable 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +2 -0
- data/CHANGELOG.rdoc +7 -0
- data/LICENSE +20 -0
- data/README.rdoc +45 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/acts_as_blamable.gemspec +62 -0
- data/lib/acts_as_blamable.rb +46 -0
- data/spec/acts_as_blamable_spec.rb +47 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +59 -0
- metadata +116 -0
data/.document
ADDED
data/.gitignore
ADDED
data/CHANGELOG.rdoc
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Create
|
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,45 @@
|
|
1
|
+
== Description
|
2
|
+
|
3
|
+
The <tt>acts_as_blamable</tt> plugin stores the creator and updater of an ActiveRecord model.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install acts_as_blamable
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
Add a <tt>current_user</tt> class method to the User model, which returns for example the logged in user.
|
12
|
+
|
13
|
+
class User < ActiveRecord::Base
|
14
|
+
def self.current_user
|
15
|
+
...
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Post < ActiveRecord::Base
|
20
|
+
acts_as_blamable
|
21
|
+
end
|
22
|
+
|
23
|
+
Add <tt>created_by</tt> and <tt>updated_by</tt> attributes to the <tt>posts</tt> table.
|
24
|
+
|
25
|
+
create_table :posts do |t|
|
26
|
+
t.integer :created_by, :updated_by
|
27
|
+
end
|
28
|
+
|
29
|
+
== Configuration
|
30
|
+
|
31
|
+
Defaults
|
32
|
+
|
33
|
+
{ :current_user_method => 'current_user', :class_name => 'User', :creator_foreign_key => :created_by, :updater_foreign_key => :updated_by }
|
34
|
+
|
35
|
+
Modify the defaults if necessary
|
36
|
+
|
37
|
+
class Person < ActiveRecord::Base
|
38
|
+
def self.current_person
|
39
|
+
Person.first
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Comment < ActiveRecord::Base
|
44
|
+
acts_as_blamable :class_name => 'Person', :creator_foreign_key => :creator_id, :updater_foreign_key => :updater_id, :current_user_method => 'current_person'
|
45
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = 'acts_as_blamable'
|
8
|
+
gem.summary = 'Stores the creator and updater of a database record'
|
9
|
+
gem.description = 'Automatically set created_by and updated_by fields'
|
10
|
+
gem.email = 'philipp.ullmann@create.at'
|
11
|
+
gem.homepage = 'http://github.com/create-philipp-ullmann/acts_as_blamable'
|
12
|
+
gem.authors = ['Philipp Ullmann']
|
13
|
+
gem.add_development_dependency 'rspec', '>= 1.3.0'
|
14
|
+
gem.add_development_dependency 'sqlite3-ruby', '>=1.2.5'
|
15
|
+
gem.add_development_dependency 'activerecord', '>= 2.3.5'
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "test #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
@@ -0,0 +1,62 @@
|
|
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{acts_as_blamable}
|
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 = ["Philipp Ullmann"]
|
12
|
+
s.date = %q{2010-04-20}
|
13
|
+
s.description = %q{Automatically set created_by and updated_by fields}
|
14
|
+
s.email = %q{philipp.ullmann@create.at}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"CHANGELOG.rdoc",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"acts_as_blamable.gemspec",
|
28
|
+
"lib/acts_as_blamable.rb",
|
29
|
+
"spec/acts_as_blamable_spec.rb",
|
30
|
+
"spec/spec.opts",
|
31
|
+
"spec/spec_helper.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/create-philipp-ullmann/acts_as_blamable}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.6}
|
37
|
+
s.summary = %q{Stores the creator and updater of a database record}
|
38
|
+
s.test_files = [
|
39
|
+
"spec/acts_as_blamable_spec.rb",
|
40
|
+
"spec/spec_helper.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
49
|
+
s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.2.5"])
|
50
|
+
s.add_development_dependency(%q<activerecord>, [">= 2.3.5"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
53
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 1.2.5"])
|
54
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
58
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 1.2.5"])
|
59
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ActsAsBlamable
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def acts_as_blamable(options={})
|
8
|
+
class_variable_set :@@bl_conf, { :current_user_method => :current_user, :class_name => 'User', :creator_foreign_key => :created_by, :updater_foreign_key => :updated_by }
|
9
|
+
bl_conf.update(options) if options.is_a? Hash
|
10
|
+
|
11
|
+
class_eval do
|
12
|
+
include ActsAsBlamable::InstanceMethods
|
13
|
+
|
14
|
+
with_options :class_name => bl_conf[:class_name] do |o|
|
15
|
+
o.belongs_to :creator, :foreign_key => bl_conf[:creator_foreign_key]
|
16
|
+
o.belongs_to :updater, :foreign_key => bl_conf[:updater_foreign_key]
|
17
|
+
end
|
18
|
+
|
19
|
+
before_create :save_creator
|
20
|
+
before_update :save_updater
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def bl_conf
|
25
|
+
class_variable_get :@@bl_conf
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
private
|
31
|
+
|
32
|
+
def save_creator
|
33
|
+
self.creator = current_user
|
34
|
+
end
|
35
|
+
|
36
|
+
def save_updater
|
37
|
+
self.updater = current_user
|
38
|
+
end
|
39
|
+
|
40
|
+
def current_user
|
41
|
+
Kernel.const_get(self.class.bl_conf[:class_name]).send("#{self.class.bl_conf[:current_user_method]}")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
ActiveRecord::Base.class_eval { include ActsAsBlamable }
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe 'A Post' do
|
4
|
+
before(:each) do
|
5
|
+
setup_db
|
6
|
+
@user = User.create!
|
7
|
+
@post = Post.create!
|
8
|
+
end
|
9
|
+
|
10
|
+
after(:each) do
|
11
|
+
teardown_db
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should have been created by an user' do
|
15
|
+
@post.creator.reload.should == @user
|
16
|
+
@post.updater.should be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should have been updated by an user' do
|
20
|
+
@post.update_attributes :content => 'content'
|
21
|
+
@post.creator.reload.should == @user
|
22
|
+
@post.updater.reload.should == @user
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'A Comment' do
|
27
|
+
before(:each) do
|
28
|
+
setup_db
|
29
|
+
@person = Person.create!
|
30
|
+
@comment = Comment.create!
|
31
|
+
end
|
32
|
+
|
33
|
+
after(:each) do
|
34
|
+
teardown_db
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should have been created by a person' do
|
38
|
+
@comment.creator.reload.should == @person
|
39
|
+
@comment.updater.should be_nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should have been updated by a person' do
|
43
|
+
@comment.update_attributes :content => 'content'
|
44
|
+
@comment.creator.reload.should == @person
|
45
|
+
@comment.updater.reload.should == @person
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'active_record'
|
4
|
+
require 'acts_as_blamable'
|
5
|
+
require 'spec'
|
6
|
+
require 'spec/autorun'
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
9
|
+
|
10
|
+
def setup_db
|
11
|
+
ActiveRecord::Schema.define(:version => 1) do
|
12
|
+
create_table :users do |t|
|
13
|
+
t.string :name
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :posts do |t|
|
17
|
+
t.integer :created_by, :updated_by
|
18
|
+
t.text :content
|
19
|
+
end
|
20
|
+
|
21
|
+
create_table :people do |t|
|
22
|
+
t.string :name
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table :comments do |t|
|
26
|
+
t.integer :creator_id, :updater_id
|
27
|
+
t.text :content
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def teardown_db
|
33
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
34
|
+
ActiveRecord::Base.connection.drop_table table
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class User < ActiveRecord::Base
|
39
|
+
def self.current_user
|
40
|
+
User.first
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Post < ActiveRecord::Base
|
45
|
+
acts_as_blamable
|
46
|
+
end
|
47
|
+
|
48
|
+
class Person < ActiveRecord::Base
|
49
|
+
def self.current_person
|
50
|
+
Person.first
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Comment < ActiveRecord::Base
|
55
|
+
acts_as_blamable :class_name => 'Person', :creator_foreign_key => :creator_id, :updater_foreign_key => :updater_id, :current_user_method => 'current_person'
|
56
|
+
end
|
57
|
+
|
58
|
+
Spec::Runner.configure do |config|
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_blamable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Philipp Ullmann
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-20 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
version: 1.3.0
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: sqlite3-ruby
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 1
|
43
|
+
- 2
|
44
|
+
- 5
|
45
|
+
version: 1.2.5
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: activerecord
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 2
|
57
|
+
- 3
|
58
|
+
- 5
|
59
|
+
version: 2.3.5
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
description: Automatically set created_by and updated_by fields
|
63
|
+
email: philipp.ullmann@create.at
|
64
|
+
executables: []
|
65
|
+
|
66
|
+
extensions: []
|
67
|
+
|
68
|
+
extra_rdoc_files:
|
69
|
+
- LICENSE
|
70
|
+
- README.rdoc
|
71
|
+
files:
|
72
|
+
- .document
|
73
|
+
- .gitignore
|
74
|
+
- CHANGELOG.rdoc
|
75
|
+
- LICENSE
|
76
|
+
- README.rdoc
|
77
|
+
- Rakefile
|
78
|
+
- VERSION
|
79
|
+
- acts_as_blamable.gemspec
|
80
|
+
- lib/acts_as_blamable.rb
|
81
|
+
- spec/acts_as_blamable_spec.rb
|
82
|
+
- spec/spec.opts
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
has_rdoc: true
|
85
|
+
homepage: http://github.com/create-philipp-ullmann/acts_as_blamable
|
86
|
+
licenses: []
|
87
|
+
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options:
|
90
|
+
- --charset=UTF-8
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
requirements: []
|
108
|
+
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.3.6
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Stores the creator and updater of a database record
|
114
|
+
test_files:
|
115
|
+
- spec/acts_as_blamable_spec.rb
|
116
|
+
- spec/spec_helper.rb
|