clerk 0.1.1
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 +18 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +88 -0
- data/Rakefile +2 -0
- data/clerk.gemspec +30 -0
- data/lib/clerk.rb +15 -0
- data/lib/clerk/callback.rb +24 -0
- data/lib/clerk/railtie.rb +10 -0
- data/lib/clerk/version.rb +3 -0
- data/spec/clerk_spec.rb +62 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/data.rb +1 -0
- data/spec/support/models.rb +7 -0
- data/spec/support/schema.rb +18 -0
- metadata +165 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.2@clerk --create
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jesse House
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Clerk
|
2
|
+
|
3
|
+
Clerk adds the following attributes to your ActiveRecord objects
|
4
|
+
|
5
|
+
* creator
|
6
|
+
* updater
|
7
|
+
|
8
|
+
Used in conjunction with the [`sentient_user`](https://github.com/bokmann/sentient_user) gem,
|
9
|
+
Clerk will automatically update these attributes using `before_create` and `before_update`
|
10
|
+
ActiveRecord callback methods
|
11
|
+
|
12
|
+
The gem assumes the following:
|
13
|
+
|
14
|
+
* you have a User model
|
15
|
+
* you have a `current_user` method on your ApplicationController
|
16
|
+
* you are using columns named `created_by_id` and `updated_by_id` on your database tables
|
17
|
+
* these columns are foreign keys to users.id
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
gem 'clerk'
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install clerk
|
32
|
+
|
33
|
+
Then follow the directions under Setup
|
34
|
+
|
35
|
+
## Setup
|
36
|
+
|
37
|
+
Step 1: include the `sentient_user` SentientUser module on your User `/app/models/user.rb`
|
38
|
+
|
39
|
+
class User < ActiveRecord::Base
|
40
|
+
include SentientUser
|
41
|
+
endis
|
42
|
+
|
43
|
+
Step 2: include the `sentient_user` SentientController module on your ApplicationController `/app/controllers/application_controller.rb`
|
44
|
+
|
45
|
+
class ApplicationController < ActionController::Base
|
46
|
+
protect_from_forgery
|
47
|
+
include SentientController
|
48
|
+
|
49
|
+
# ...
|
50
|
+
end
|
51
|
+
|
52
|
+
Step 3: Add the `track_who_does_it` macro to any ActiveRecord models that have `created_by_id` and `updated_by_id` database columns
|
53
|
+
|
54
|
+
class Post < ActiveRecord::Base
|
55
|
+
track_who_does_it
|
56
|
+
end
|
57
|
+
|
58
|
+
We probably want our User model to `track_who_does_it` as well
|
59
|
+
|
60
|
+
class User < ActiveRecord::Base
|
61
|
+
include SentientUser
|
62
|
+
track_who_does_it
|
63
|
+
end
|
64
|
+
|
65
|
+
## Usage
|
66
|
+
|
67
|
+
Once you have finished the Setup and restarted your rails server
|
68
|
+
your models should automatically update the creator and the updater attributes after being saved.
|
69
|
+
|
70
|
+
Since your models have the creator and updater attributes you can display these in any views
|
71
|
+
|
72
|
+
Updated by <%= @post.updater.full_name %> at <%= l(@post.updated_at, :format => :long) %>
|
73
|
+
|
74
|
+
Since we are using `sentient_user` you can also set the updater or creator in tests or rake tasks
|
75
|
+
|
76
|
+
user = User.find(5150)
|
77
|
+
user.make_current # now all models marked as track_who_does_it will save with this user tracked
|
78
|
+
Post.create!(:title => "New Post")
|
79
|
+
Post.last.creator == user # => true
|
80
|
+
|
81
|
+
## Resources used in the development of this gem
|
82
|
+
|
83
|
+
* [http://ryanbigg.com/2011/01/extending-active-record/](http://ryanbigg.com/2011/01/extending-active-record/)
|
84
|
+
* [http://charlotteruby.org/gem_workshop_tutorial/](http://charlotteruby.org/gem_workshop_tutorial/)
|
85
|
+
* [http://yehudakatz.com/2009/11/12/better-ruby-idioms/](http://yehudakatz.com/2009/11/12/better-ruby-idioms/)
|
86
|
+
* [http://www.cowboycoded.com/2011/01/31/developing-is_able-or-acts_as-plugins-for-rails/](http://www.cowboycoded.com/2011/01/31/developing-is_able-or-acts_as-plugins-for-rails/)
|
87
|
+
* [http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html](http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)
|
88
|
+
* [https://github.com/bokmann/sentient_user](https://github.com/bokmann/sentient_user)
|
data/Rakefile
ADDED
data/clerk.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/clerk/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jesse House"]
|
6
|
+
gem.email = ["jesse.house@gmail.com"]
|
7
|
+
gem.description = File.open('README.md') { |f| f.read }
|
8
|
+
gem.summary = %q{
|
9
|
+
For Rails applications - add creator and updater to your ActiveRecord models
|
10
|
+
and automatically set these from current_user
|
11
|
+
}
|
12
|
+
gem.homepage = "https://github.com/house9/clerk"
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($\)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.name = "clerk"
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.version = Clerk::VERSION
|
20
|
+
|
21
|
+
# gem.add_dependency 'rails', '~> 3'
|
22
|
+
gem.add_dependency 'activerecord', '~> 3'
|
23
|
+
gem.add_dependency 'railties', '~> 3'
|
24
|
+
gem.add_dependency 'sentient_user', '~> 0.3.2'
|
25
|
+
|
26
|
+
|
27
|
+
gem.add_development_dependency 'bundler', '>= 1.1.3'
|
28
|
+
gem.add_development_dependency 'rspec', '~> 2.9.0'
|
29
|
+
gem.add_development_dependency 'sqlite3'
|
30
|
+
end
|
data/lib/clerk.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'sentient_user'
|
3
|
+
require 'clerk/version'
|
4
|
+
require 'clerk/callback'
|
5
|
+
require 'clerk/railtie'
|
6
|
+
|
7
|
+
module Clerk
|
8
|
+
def track_who_does_it
|
9
|
+
before_create Clerk::Callback.new
|
10
|
+
before_update Clerk::Callback.new
|
11
|
+
|
12
|
+
belongs_to :creator, :class_name => "User", :foreign_key => "created_by_id"
|
13
|
+
belongs_to :updater, :class_name => "User", :foreign_key => "updated_by_id"
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Clerk
|
2
|
+
class Callback
|
3
|
+
def before_create(record)
|
4
|
+
record.creator = current_user if record.respond_to?(:creator) && current_user
|
5
|
+
record.updater = current_user if record.respond_to?(:updater) && current_user
|
6
|
+
end
|
7
|
+
|
8
|
+
def before_update(record)
|
9
|
+
record.updater = current_user if record.respond_to?(:updater) && current_user
|
10
|
+
end
|
11
|
+
|
12
|
+
# relies on `include SentientUser` on User
|
13
|
+
def current_user
|
14
|
+
logger.warn "WARNING: User#current is not defined, are you including SentientUser on your User model?" unless User.respond_to?(:current)
|
15
|
+
logger.warn "WARNING: User#current is nil, are you including SentientController on your ApplicationController?" unless User.current
|
16
|
+
|
17
|
+
User.current
|
18
|
+
end
|
19
|
+
|
20
|
+
def logger
|
21
|
+
Rails.logger
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/clerk_spec.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clerk do
|
4
|
+
it "should have a version" do
|
5
|
+
Clerk::VERSION.length.should > 0
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should expose track_who_does_it" do
|
9
|
+
Post.extend Clerk
|
10
|
+
Post.should respond_to :track_who_does_it
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "extended objects" do
|
14
|
+
Post.extend Clerk
|
15
|
+
|
16
|
+
class PostExtended < Post
|
17
|
+
track_who_does_it
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should respond to creator" do
|
21
|
+
post = PostExtended.new
|
22
|
+
post.should respond_to :creator
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should respond to updater" do
|
26
|
+
post = PostExtended.new
|
27
|
+
post.should respond_to :updater
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "clerical objects can be tracked automatically" do
|
32
|
+
Post.extend Clerk
|
33
|
+
|
34
|
+
class PostExtended < Post
|
35
|
+
track_who_does_it
|
36
|
+
end
|
37
|
+
|
38
|
+
before(:each) do
|
39
|
+
@creator = User.create!(:name => "creator")
|
40
|
+
@updater = User.create!(:name => "updater")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "by creator" do
|
44
|
+
@creator.make_current
|
45
|
+
this = PostExtended.new(:title => "Test")
|
46
|
+
this.save
|
47
|
+
this.creator.name.should == "creator"
|
48
|
+
this.updater.name.should == "creator"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "by updater" do
|
52
|
+
@creator.make_current
|
53
|
+
this = PostExtended.new(:title => "Test2")
|
54
|
+
this.save
|
55
|
+
|
56
|
+
@updater.make_current
|
57
|
+
this.update_attribute(:title, "Updated")
|
58
|
+
this.creator.name.should == "creator"
|
59
|
+
this.updater.name.should == "updater"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
require 'clerk'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => File.dirname(__FILE__) + "/clerk_test_db.sqlite3")
|
4
|
+
load File.dirname(__FILE__) + '/support/schema.rb'
|
5
|
+
load File.dirname(__FILE__) + '/support/models.rb'
|
6
|
+
load File.dirname(__FILE__) + '/support/data.rb'
|
@@ -0,0 +1 @@
|
|
1
|
+
Post.create!(:title => "TEST POST")
|
@@ -0,0 +1,18 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
self.verbose = false
|
3
|
+
|
4
|
+
create_table :posts, :force => true do |t|
|
5
|
+
t.string :title
|
6
|
+
t.string :body
|
7
|
+
t.integer :created_by_id
|
8
|
+
t.integer :updated_by_id
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :users, :force => true do |t|
|
13
|
+
t.string :name
|
14
|
+
t.integer :created_by_id
|
15
|
+
t.integer :updated_by_id
|
16
|
+
t.timestamps
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clerk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jesse House
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-25 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70208941589700 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70208941589700
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: railties
|
27
|
+
requirement: &70208941588800 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70208941588800
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sentient_user
|
38
|
+
requirement: &70208941587760 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.3.2
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70208941587760
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: &70208941586920 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.1.3
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70208941586920
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rspec
|
60
|
+
requirement: &70208941586240 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 2.9.0
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70208941586240
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: &70208941585700 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70208941585700
|
80
|
+
description: ! "# Clerk\n\nClerk adds the following attributes to your ActiveRecord
|
81
|
+
objects\n\n* creator\n* updater\n\nUsed in conjunction with the [`sentient_user`](https://github.com/bokmann/sentient_user)
|
82
|
+
gem, \nClerk will automatically update these attributes using `before_create` and
|
83
|
+
`before_update`\nActiveRecord callback methods\n\nThe gem assumes the following:\n\n*
|
84
|
+
you have a User model\n* you have a `current_user` method on your ApplicationController\n*
|
85
|
+
you are using columns named `created_by_id` and `updated_by_id` on your database
|
86
|
+
tables\n* these columns are foreign keys to users.id\n\n## Installation\n\nAdd this
|
87
|
+
line to your application's Gemfile:\n\n gem 'clerk'\n\nAnd then execute:\n\n
|
88
|
+
\ $ bundle\n\nOr install it yourself as:\n\n $ gem install clerk\n \nThen
|
89
|
+
follow the directions under Setup\n\n## Setup\n\nStep 1: include the `sentient_user`
|
90
|
+
SentientUser module on your User `/app/models/user.rb`\n\n class User < ActiveRecord::Base\n
|
91
|
+
\ include SentientUser\n endis\n \nStep 2: include the `sentient_user`
|
92
|
+
SentientController module on your ApplicationController `/app/controllers/application_controller.rb`\n
|
93
|
+
\ \n class ApplicationController < ActionController::Base\n protect_from_forgery\n
|
94
|
+
\ include SentientController\n \n # ...\n end \n \nStep 3:
|
95
|
+
Add the `track_who_does_it` macro to any ActiveRecord models that have `created_by_id`
|
96
|
+
and `updated_by_id` database columns\n \n class Post < ActiveRecord::Base\n
|
97
|
+
\ track_who_does_it\n end\n \nWe probably want our User model to `track_who_does_it`
|
98
|
+
as well\n\n class User < ActiveRecord::Base\n include SentientUser\n track_who_does_it\n
|
99
|
+
\ end\n\n## Usage \n\nOnce you have finished the Setup and restarted your rails
|
100
|
+
server \nyour models should automatically update the creator and the updater attributes
|
101
|
+
after being saved.\n\nSince your models have the creator and updater attributes
|
102
|
+
you can display these in any views\n\n Updated by <%= @post.updater.full_name
|
103
|
+
%> at <%= l(@post.updated_at, :format => :long) %>\n \nSince we are using `sentient_user`
|
104
|
+
you can also set the updater or creator in tests or rake tasks\n\n user = User.find(5150)\n
|
105
|
+
\ user.make_current # now all models marked as track_who_does_it will save with
|
106
|
+
this user tracked\n Post.create!(:title => \"New Post\")\n Post.last.creator
|
107
|
+
== user # => true\n\n## Resources used in the development of this gem\n\n* [http://ryanbigg.com/2011/01/extending-active-record/](http://ryanbigg.com/2011/01/extending-active-record/)\n*
|
108
|
+
[http://charlotteruby.org/gem_workshop_tutorial/](http://charlotteruby.org/gem_workshop_tutorial/)\n*
|
109
|
+
[http://yehudakatz.com/2009/11/12/better-ruby-idioms/](http://yehudakatz.com/2009/11/12/better-ruby-idioms/)\n*
|
110
|
+
[http://www.cowboycoded.com/2011/01/31/developing-is_able-or-acts_as-plugins-for-rails/](http://www.cowboycoded.com/2011/01/31/developing-is_able-or-acts_as-plugins-for-rails/)\n*
|
111
|
+
[http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html](http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)\n*
|
112
|
+
[https://github.com/bokmann/sentient_user](https://github.com/bokmann/sentient_user)\n"
|
113
|
+
email:
|
114
|
+
- jesse.house@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- .gitignore
|
120
|
+
- .rvmrc
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- clerk.gemspec
|
126
|
+
- lib/clerk.rb
|
127
|
+
- lib/clerk/callback.rb
|
128
|
+
- lib/clerk/railtie.rb
|
129
|
+
- lib/clerk/version.rb
|
130
|
+
- spec/clerk_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/support/data.rb
|
133
|
+
- spec/support/models.rb
|
134
|
+
- spec/support/schema.rb
|
135
|
+
homepage: https://github.com/house9/clerk
|
136
|
+
licenses: []
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ! '>='
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ! '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 1.8.10
|
156
|
+
signing_key:
|
157
|
+
specification_version: 3
|
158
|
+
summary: For Rails applications - add creator and updater to your ActiveRecord models and
|
159
|
+
automatically set these from current_user
|
160
|
+
test_files:
|
161
|
+
- spec/clerk_spec.rb
|
162
|
+
- spec/spec_helper.rb
|
163
|
+
- spec/support/data.rb
|
164
|
+
- spec/support/models.rb
|
165
|
+
- spec/support/schema.rb
|