stampable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.md +60 -0
- data/Rakefile +1 -0
- data/lib/stampable/base.rb +22 -0
- data/lib/stampable/version.rb +3 -0
- data/lib/stampable.rb +5 -0
- data/spec/stampable_spec.rb +76 -0
- data/stampable.gemspec +24 -0
- metadata +84 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
Stampable gem
|
2
|
+
=============
|
3
|
+
|
4
|
+
Overview
|
5
|
+
--------
|
6
|
+
|
7
|
+
The Stampable gem is used to stamp ActiveRecord model with current user. This is a simple gem that stores who modified/created the record in a single field.
|
8
|
+
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
Installation of the plugin can be done using the built in Rails plugin script. Issue the following
|
14
|
+
command from the root of your Rails application:
|
15
|
+
|
16
|
+
$ ./script/rails plugin install git://github.com/anandagrawal84/stampable.git
|
17
|
+
|
18
|
+
or add it to your Gemfile:
|
19
|
+
|
20
|
+
gem 'stampable'
|
21
|
+
|
22
|
+
and run `bundle install` to install the new dependency.
|
23
|
+
|
24
|
+
Usage
|
25
|
+
-----
|
26
|
+
All you need to do is include Stampable module in the ActiveRecord model that you want to stamp.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class Blog < ActiveRecord::Base
|
30
|
+
include Stampable
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
and in case of rails set the current user in application controller
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class ApplicationController
|
38
|
+
before_filter :current_user
|
39
|
+
|
40
|
+
def current_user
|
41
|
+
Thread.current['current_user'] = <current user name or id>
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
The value saved in current_user (say user name or user id) gets saved in the model that is stampable.
|
47
|
+
|
48
|
+
Configuration
|
49
|
+
-------------
|
50
|
+
To save the stamp in `touched_by` instead of `modified_by` add following to environment.rb file
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
Stampable::Base.config = {:stamp_field => 'touched_by'}
|
54
|
+
```
|
55
|
+
|
56
|
+
To specify a default user add following
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
Stampable::Base.config = {:default_user => 'robot'}
|
60
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Stampable
|
2
|
+
module Base
|
3
|
+
def self.config= config
|
4
|
+
@config = self.config.merge(config)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.config
|
8
|
+
@config || {:stamp_field => :modified_by}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.included(klass)
|
12
|
+
klass.send :before_save, :set_modified_by
|
13
|
+
end
|
14
|
+
|
15
|
+
def set_modified_by
|
16
|
+
return unless changed?
|
17
|
+
user_name = Thread.current['current_user'] || Stampable::Base.config[:default_user]
|
18
|
+
stamp_writer = (Stampable::Base.config[:stamp_field].to_s + '=').to_sym
|
19
|
+
send(stamp_writer, user_name) if respond_to?(stamp_writer)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/stampable.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
describe Stampable do
|
2
|
+
class ActiveRecordModel
|
3
|
+
attr_accessor :modified_by
|
4
|
+
|
5
|
+
#stub activerecord method
|
6
|
+
def self.before_save msg
|
7
|
+
@msg = msg
|
8
|
+
end
|
9
|
+
|
10
|
+
include Stampable::Base
|
11
|
+
|
12
|
+
def invoke_before_save
|
13
|
+
send :set_modified_by
|
14
|
+
end
|
15
|
+
|
16
|
+
#stub activerecord method
|
17
|
+
def changed?
|
18
|
+
true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
before :all do
|
23
|
+
Stampable::Base.config = {:default_user => 'user'}
|
24
|
+
end
|
25
|
+
|
26
|
+
after :each do
|
27
|
+
Thread.current['current_user'] = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
it 'should set default current user if not set in thread local' do
|
32
|
+
model = ActiveRecordModel.new
|
33
|
+
model.invoke_before_save
|
34
|
+
model.modified_by.should == 'user'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should set user from thread local' do
|
38
|
+
Thread.current['current_user'] = 'user1'
|
39
|
+
model = ActiveRecordModel.new
|
40
|
+
model.invoke_before_save
|
41
|
+
|
42
|
+
model.modified_by.should == 'user1'
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should not set user if model is not changed' do
|
46
|
+
model = ActiveRecordModel.new
|
47
|
+
model.stub(:changed?).and_return(false)
|
48
|
+
model.invoke_before_save
|
49
|
+
|
50
|
+
model.modified_by.should == nil
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should not update user if model is not changed' do
|
54
|
+
model = ActiveRecordModel.new
|
55
|
+
model.modified_by = 'some user'
|
56
|
+
model.stub(:changed?).and_return(false)
|
57
|
+
model.invoke_before_save
|
58
|
+
|
59
|
+
model.modified_by.should == 'some user'
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
it 'should set user to the stamp_field' do
|
64
|
+
Stampable::Base.config = {:stamp_field => 'touched_by'}
|
65
|
+
|
66
|
+
class ActiveRecordModel
|
67
|
+
attr_accessor :touched_by
|
68
|
+
end
|
69
|
+
|
70
|
+
model = ActiveRecordModel.new
|
71
|
+
model.invoke_before_save
|
72
|
+
|
73
|
+
model.touched_by.should == 'user'
|
74
|
+
model.modified_by.should == nil
|
75
|
+
end
|
76
|
+
end
|
data/stampable.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "stampable/version"
|
4
|
+
require "stampable/base"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "stampable"
|
8
|
+
s.version = Stampable::VERSION
|
9
|
+
s.authors = ["Anand Agrawal"]
|
10
|
+
s.email = ["anand.agrawal84@gmail.com", "rajashreermalvade@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Stamp any ActiveRecord model with last created/updated by}
|
13
|
+
s.description = %q{Stamp any ActiveRecord model with last created/updated by}
|
14
|
+
|
15
|
+
s.rubyforge_project = "stampable"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stampable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Anand Agrawal
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70246285937140 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70246285937140
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70246285936660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70246285936660
|
36
|
+
description: Stamp any ActiveRecord model with last created/updated by
|
37
|
+
email:
|
38
|
+
- anand.agrawal84@gmail.com
|
39
|
+
- rajashreermalvade@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- lib/stampable.rb
|
49
|
+
- lib/stampable/base.rb
|
50
|
+
- lib/stampable/version.rb
|
51
|
+
- spec/stampable_spec.rb
|
52
|
+
- stampable.gemspec
|
53
|
+
homepage: ''
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
hash: 172601517271973622
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
hash: 172601517271973622
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project: stampable
|
79
|
+
rubygems_version: 1.8.12
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Stamp any ActiveRecord model with last created/updated by
|
83
|
+
test_files:
|
84
|
+
- spec/stampable_spec.rb
|