mongoid_userstamp 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -0
- data/.travis.yml +2 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.md +20 -0
- data/README.md +55 -0
- data/Rakefile +33 -0
- data/init.rb +3 -0
- data/lib/mongoid/userstamp.rb +51 -0
- data/lib/mongoid/userstamp/config.rb +33 -0
- data/lib/mongoid/userstamp/railtie.rb +20 -0
- data/lib/mongoid/userstamp/user.rb +40 -0
- data/lib/mongoid/userstamp/version.rb +14 -0
- data/lib/mongoid_userstamp.rb +4 -0
- data/mongoid_userstamp.gemspec +62 -0
- metadata +110 -0
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.3-head@mongoid_userstamp --create
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.2.3)
|
5
|
+
activesupport (= 3.2.3)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
activesupport (3.2.3)
|
8
|
+
i18n (~> 0.6)
|
9
|
+
multi_json (~> 1.0)
|
10
|
+
bson (1.6.2)
|
11
|
+
builder (3.0.0)
|
12
|
+
git (1.2.5)
|
13
|
+
i18n (0.6.0)
|
14
|
+
jeweler (1.8.3)
|
15
|
+
bundler (~> 1.0)
|
16
|
+
git (>= 1.2.5)
|
17
|
+
rake
|
18
|
+
rdoc
|
19
|
+
json (1.6.6)
|
20
|
+
mongo (1.6.2)
|
21
|
+
bson (~> 1.6.2)
|
22
|
+
mongoid (2.4.8)
|
23
|
+
activemodel (~> 3.1)
|
24
|
+
mongo (~> 1.3)
|
25
|
+
tzinfo (~> 0.3.22)
|
26
|
+
multi_json (1.2.0)
|
27
|
+
rake (0.9.2.2)
|
28
|
+
rdoc (3.12)
|
29
|
+
json (~> 1.4)
|
30
|
+
redcarpet (2.1.1)
|
31
|
+
tzinfo (0.3.33)
|
32
|
+
yard (0.7.5)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
jeweler (~> 1.8.3)
|
39
|
+
mongoid (~> 2.4.7)
|
40
|
+
redcarpet (~> 2.1.0)
|
41
|
+
yard (~> 0.7.5)
|
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Langwhich GmbH <http://www.langwhich.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.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# MongoidUserstamp [![Build Status](https://secure.travis-ci.org/Langwhich/mongoid_userstamp.png)](https://secure.travis-ci.org/Langwhich/mongoid_userstamp)
|
2
|
+
|
3
|
+
MongoidUserstamp adds stamp columns for created by and updated by
|
4
|
+
informations within rails applications.
|
5
|
+
|
6
|
+
## Install
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'mongoid_userstamp'
|
10
|
+
```
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
# Default config
|
16
|
+
Mongoid::Userstamp.configure do |c|
|
17
|
+
c.user_reader = :current_user
|
18
|
+
c.user_model = :user
|
19
|
+
|
20
|
+
c.created_column = :created_by
|
21
|
+
c.created_accessor = :creator
|
22
|
+
|
23
|
+
c.updated_column = :updator_by
|
24
|
+
c.updated_accessor = :updator
|
25
|
+
end
|
26
|
+
|
27
|
+
# Example model
|
28
|
+
class Person
|
29
|
+
include Mongoid::Document
|
30
|
+
include Mongoid::Userstamp
|
31
|
+
end
|
32
|
+
|
33
|
+
# Create instance
|
34
|
+
p = Person.create
|
35
|
+
|
36
|
+
# Updater ObjectID or nil
|
37
|
+
p.created_by
|
38
|
+
# => BSON::ObjectId('4f7c719f476da850ba000039')
|
39
|
+
|
40
|
+
# Updater instance or nil
|
41
|
+
p.creator
|
42
|
+
# => <User _id: 4f7c719f476da850ba000039>
|
43
|
+
|
44
|
+
# Creater ObjectID or nil
|
45
|
+
p.updated_by
|
46
|
+
# => BSON::ObjectId('4f7c719f476da850ba000039')
|
47
|
+
|
48
|
+
# Creater instance or nil
|
49
|
+
p.updator
|
50
|
+
# => <User _id: 4f7c719f476da850ba000039>
|
51
|
+
```
|
52
|
+
|
53
|
+
## Credits
|
54
|
+
|
55
|
+
Copyright (c) 2012 Langwhich GmbH <http://www.langwhich.com>
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler'
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rake'
|
13
|
+
require 'jeweler'
|
14
|
+
require 'yard'
|
15
|
+
|
16
|
+
$:.push File.expand_path('../lib', __FILE__)
|
17
|
+
require 'mongoid/userstamp/version'
|
18
|
+
|
19
|
+
Jeweler::Tasks.new do |gem|
|
20
|
+
gem.name = 'mongoid_userstamp'
|
21
|
+
gem.version = Mongoid::Userstamp::Version::STRING
|
22
|
+
gem.homepage = 'https://github.com/Langwhich/mongoid_userstamp'
|
23
|
+
gem.license = 'MIT'
|
24
|
+
gem.summary = %Q{Userstamp for created and updated columns within mongoid}
|
25
|
+
gem.email = 'thomas.boerger@langwhich.com'
|
26
|
+
gem.authors = ['Thomas Boerger', 'Tim Rudat']
|
27
|
+
end
|
28
|
+
|
29
|
+
Jeweler::RubygemsDotOrgTasks.new
|
30
|
+
YARD::Rake::YardocTask.new
|
31
|
+
|
32
|
+
task :default => :version
|
33
|
+
|
data/init.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Mongoid
|
3
|
+
module Userstamp
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
autoload :Version, 'mongoid/userstamp/version'
|
7
|
+
autoload :Railtie, 'mongoid/userstamp/railtie'
|
8
|
+
autoload :Config, 'mongoid/userstamp/config'
|
9
|
+
autoload :User, 'mongoid/userstamp/user'
|
10
|
+
|
11
|
+
included do
|
12
|
+
include Mongoid::Timestamps
|
13
|
+
|
14
|
+
field Mongoid::Userstamp.configuration.updated_column, :type => Object
|
15
|
+
field Mongoid::Userstamp.configuration.created_column, :type => Object
|
16
|
+
|
17
|
+
before_save :set_updator
|
18
|
+
before_create :set_creator
|
19
|
+
|
20
|
+
define_method Mongoid::Userstamp.configuration.updated_accessor do
|
21
|
+
Mongoid::Userstamp.configuration.user_model.find(self.send(Mongoid::Userstamp.configuration.updated_column))
|
22
|
+
end
|
23
|
+
|
24
|
+
define_method Mongoid::Userstamp.configuration.created_accessor do
|
25
|
+
Mongoid::Userstamp.configuration.user_model.find(self.send(Mongoid::Userstamp.configuration.created_column))
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
def set_updator
|
30
|
+
column = "#{Mongoid::Userstamp.configuration.updated_column.to_s}=".to_sym
|
31
|
+
self.send(column, Mongoid::Userstamp.configuration.user_model.current.try(:id))
|
32
|
+
end
|
33
|
+
|
34
|
+
def set_creator
|
35
|
+
column = "#{Mongoid::Userstamp.configuration.created_column.to_s}=".to_sym
|
36
|
+
self.send(column, Mongoid::Userstamp.configuration.user_model.current.try(:id))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class << self
|
41
|
+
def configure(&block)
|
42
|
+
@configuration = Mongoid::Userstamp::Config.new(&block)
|
43
|
+
end
|
44
|
+
|
45
|
+
def configuration
|
46
|
+
@configuration ||= Mongoid::Userstamp::Config.new
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Mongoid
|
3
|
+
module Userstamp
|
4
|
+
class Config
|
5
|
+
attr_accessor :user_reader
|
6
|
+
attr_accessor :user_model
|
7
|
+
|
8
|
+
attr_accessor :created_column
|
9
|
+
attr_accessor :created_accessor
|
10
|
+
|
11
|
+
attr_accessor :updated_column
|
12
|
+
attr_accessor :updated_accessor
|
13
|
+
|
14
|
+
def initialize(&block)
|
15
|
+
@user_reader = :current_user
|
16
|
+
@user_model = :user
|
17
|
+
|
18
|
+
@created_column = :created_by
|
19
|
+
@created_accessor = :creator
|
20
|
+
|
21
|
+
@updated_column = :updated_by
|
22
|
+
@updated_accessor = :updator
|
23
|
+
|
24
|
+
instance_eval(&block) if block_given?
|
25
|
+
end
|
26
|
+
|
27
|
+
def user_model
|
28
|
+
@user_model.to_s.classify.constantize
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Mongoid
|
3
|
+
module Userstamp
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
ActiveSupport.on_load :action_controller do
|
6
|
+
unless Mongoid::Userstamp.configuration.user_model.respond_to? :current
|
7
|
+
Mongoid::Userstamp.configuration.user_model.send(
|
8
|
+
:include,
|
9
|
+
Mongoid::Userstamp::User
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
before_filter do |c|
|
14
|
+
Mongoid::Userstamp.configuration.user_model.current = c.send(Mongoid::Userstamp.configuration.user_reader)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Mongoid
|
3
|
+
module Userstamp
|
4
|
+
module User
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Mongoid::Timestamps
|
9
|
+
|
10
|
+
def current?
|
11
|
+
!Thread.current[:user].nil? && self.id == Thread.current[:user].id
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def current
|
17
|
+
Thread.current[:user]
|
18
|
+
end
|
19
|
+
|
20
|
+
def current=(value)
|
21
|
+
Thread.current[:user] = value
|
22
|
+
end
|
23
|
+
|
24
|
+
def do_as(user, &block)
|
25
|
+
old = self.current
|
26
|
+
|
27
|
+
begin
|
28
|
+
self.current = user
|
29
|
+
response = block.call unless block.nil?
|
30
|
+
ensure
|
31
|
+
self.current = old
|
32
|
+
end
|
33
|
+
|
34
|
+
response
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "mongoid_userstamp"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Thomas Boerger", "Tim Rudat"]
|
12
|
+
s.date = "2012-04-11"
|
13
|
+
s.email = "thomas.boerger@langwhich.com"
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE.md",
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".rvmrc",
|
20
|
+
".travis.yml",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.md",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"init.rb",
|
27
|
+
"lib/mongoid/userstamp.rb",
|
28
|
+
"lib/mongoid/userstamp/config.rb",
|
29
|
+
"lib/mongoid/userstamp/railtie.rb",
|
30
|
+
"lib/mongoid/userstamp/user.rb",
|
31
|
+
"lib/mongoid/userstamp/version.rb",
|
32
|
+
"lib/mongoid_userstamp.rb",
|
33
|
+
"mongoid_userstamp.gemspec"
|
34
|
+
]
|
35
|
+
s.homepage = "https://github.com/Langwhich/mongoid_userstamp"
|
36
|
+
s.licenses = ["MIT"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = "1.8.10"
|
39
|
+
s.summary = "Userstamp for created and updated columns within mongoid"
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<mongoid>, ["~> 2.4.7"])
|
46
|
+
s.add_development_dependency(%q<redcarpet>, ["~> 2.1.0"])
|
47
|
+
s.add_development_dependency(%q<yard>, ["~> 0.7.5"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<mongoid>, ["~> 2.4.7"])
|
51
|
+
s.add_dependency(%q<redcarpet>, ["~> 2.1.0"])
|
52
|
+
s.add_dependency(%q<yard>, ["~> 0.7.5"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<mongoid>, ["~> 2.4.7"])
|
57
|
+
s.add_dependency(%q<redcarpet>, ["~> 2.1.0"])
|
58
|
+
s.add_dependency(%q<yard>, ["~> 0.7.5"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_userstamp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thomas Boerger
|
9
|
+
- Tim Rudat
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-04-11 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mongoid
|
17
|
+
requirement: &70241191732520 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.4.7
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70241191732520
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: redcarpet
|
28
|
+
requirement: &70241191731220 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.1.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70241191731220
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: yard
|
39
|
+
requirement: &70241191729080 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 0.7.5
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70241191729080
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: jeweler
|
50
|
+
requirement: &70241191727440 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.8.3
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *70241191727440
|
59
|
+
description:
|
60
|
+
email: thomas.boerger@langwhich.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files:
|
64
|
+
- LICENSE.md
|
65
|
+
- README.md
|
66
|
+
files:
|
67
|
+
- .rvmrc
|
68
|
+
- .travis.yml
|
69
|
+
- Gemfile
|
70
|
+
- Gemfile.lock
|
71
|
+
- LICENSE.md
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- init.rb
|
75
|
+
- lib/mongoid/userstamp.rb
|
76
|
+
- lib/mongoid/userstamp/config.rb
|
77
|
+
- lib/mongoid/userstamp/railtie.rb
|
78
|
+
- lib/mongoid/userstamp/user.rb
|
79
|
+
- lib/mongoid/userstamp/version.rb
|
80
|
+
- lib/mongoid_userstamp.rb
|
81
|
+
- mongoid_userstamp.gemspec
|
82
|
+
homepage: https://github.com/Langwhich/mongoid_userstamp
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
hash: -3961653981712842111
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.8.10
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Userstamp for created and updated columns within mongoid
|
110
|
+
test_files: []
|