clerk 0.1.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.8.7"
4
+ - "1.9.2"
5
+ - "1.9.3"
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ script: bundle exec rspec spec
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
1
  # Clerk
2
2
 
3
+ [![Build Status](https://travis-ci.org/house9/clerk.png)](https://travis-ci.org/house9/clerk) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/house9/clerk)
4
+
3
5
  Clerk adds the following attributes to your ActiveRecord objects
4
6
 
5
7
  * creator
6
8
  * updater
7
9
 
8
- Used in conjunction with the [`sentient_user`](https://github.com/bokmann/sentient_user) gem,
10
+ Used in conjunction with the [`sentient_user`](https://github.com/bokmann/sentient_user) gem,
9
11
  Clerk will automatically update these attributes using `before_create` and `before_update`
10
12
  ActiveRecord callback methods
11
13
 
@@ -13,8 +15,8 @@ The gem assumes the following:
13
15
 
14
16
  * you have a User model
15
17
  * 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
+ * you have 2 columns which are foreign keys of users.id
19
+ * the gems uses the following as defaults `created_by_id` and `updated_by_id`
18
20
 
19
21
  ## Installation
20
22
 
@@ -29,7 +31,7 @@ And then execute:
29
31
  Or install it yourself as:
30
32
 
31
33
  $ gem install clerk
32
-
34
+
33
35
  Then follow the directions under Setup
34
36
 
35
37
  ## Setup
@@ -39,22 +41,22 @@ Step 1: include the `sentient_user` SentientUser module on your User `/app/model
39
41
  class User < ActiveRecord::Base
40
42
  include SentientUser
41
43
  endis
42
-
44
+
43
45
  Step 2: include the `sentient_user` SentientController module on your ApplicationController `/app/controllers/application_controller.rb`
44
-
46
+
45
47
  class ApplicationController < ActionController::Base
46
48
  protect_from_forgery
47
49
  include SentientController
48
-
50
+
49
51
  # ...
50
- end
51
-
52
+ end
53
+
52
54
  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
-
55
+
54
56
  class Post < ActiveRecord::Base
55
57
  track_who_does_it
56
58
  end
57
-
59
+
58
60
  We probably want our User model to `track_who_does_it` as well
59
61
 
60
62
  class User < ActiveRecord::Base
@@ -62,15 +64,21 @@ We probably want our User model to `track_who_does_it` as well
62
64
  track_who_does_it
63
65
  end
64
66
 
65
- ## Usage
67
+ You can optionally override the default column names `created_by_id` and `updated_by_id`
66
68
 
67
- Once you have finished the Setup and restarted your rails server
69
+ class Post < ActiveRecord::Base
70
+ track_who_does_it :creator_foreign_key => "user_id", :updater_foreign_key => "updater_id"
71
+ end
72
+
73
+ ## Usage
74
+
75
+ Once you have finished the Setup and restarted your rails server
68
76
  your models should automatically update the creator and the updater attributes after being saved.
69
77
 
70
78
  Since your models have the creator and updater attributes you can display these in any views
71
79
 
72
80
  Updated by <%= @post.updater.full_name %> at <%= l(@post.updated_at, :format => :long) %>
73
-
81
+
74
82
  Since we are using `sentient_user` you can also set the updater or creator in tests or rake tasks
75
83
 
76
84
  user = User.find(5150)
data/clerk.gemspec CHANGED
@@ -2,12 +2,12 @@
2
2
  require File.expand_path('../lib/clerk/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
-
5
+
6
6
  summary = %q{
7
- For Rails applications - add creator and updater to your ActiveRecord models
7
+ For Rails applications - add creator and updater to your ActiveRecord models
8
8
  and automatically set these from current_user
9
9
  }
10
-
10
+
11
11
  gem.authors = ["Jesse House"]
12
12
  gem.email = ["jesse.house@gmail.com"]
13
13
  gem.description = "#{summary}\nSee the README on github for more info"
@@ -20,14 +20,20 @@ Gem::Specification.new do |gem|
20
20
  gem.name = "clerk"
21
21
  gem.require_paths = ["lib"]
22
22
  gem.version = Clerk::VERSION
23
-
23
+
24
24
  # gem.add_dependency 'rails', '~> 3'
25
25
  gem.add_dependency 'activerecord', '~> 3'
26
26
  gem.add_dependency 'railties', '~> 3'
27
27
  gem.add_dependency 'sentient_user', '~> 0.3.2'
28
28
 
29
-
29
+
30
30
  gem.add_development_dependency 'bundler', '>= 1.1.3'
31
31
  gem.add_development_dependency 'rspec', '~> 2.9.0'
32
- gem.add_development_dependency 'sqlite3'
32
+
33
+ if defined?(JRUBY_VERSION)
34
+ gem.add_development_dependency 'activerecord-jdbcsqlite3-adapter'
35
+ gem.add_development_dependency 'jruby-openssl'
36
+ else
37
+ gem.add_development_dependency 'sqlite3'
38
+ end
33
39
  end
data/lib/clerk.rb CHANGED
@@ -5,11 +5,14 @@ require 'clerk/callback'
5
5
  require 'clerk/railtie'
6
6
 
7
7
  module Clerk
8
- def track_who_does_it
8
+ def track_who_does_it(options = {})
9
9
  before_create Clerk::Callback.new
10
10
  before_update Clerk::Callback.new
11
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"
12
+ creator_fk = options.fetch(:creator_foreign_key) { "created_by_id" }
13
+ updater_fk = options.fetch(:updater_foreign_key) { "updated_by_id" }
14
+
15
+ belongs_to :creator, :class_name => "User", :foreign_key => creator_fk
16
+ belongs_to :updater, :class_name => "User", :foreign_key => updater_fk
14
17
  end
15
18
  end
data/lib/clerk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Clerk
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.1"
3
3
  end
data/spec/clerk_spec.rb CHANGED
@@ -9,37 +9,37 @@ describe Clerk do
9
9
  Post.extend Clerk
10
10
  Post.should respond_to :track_who_does_it
11
11
  end
12
-
12
+
13
13
  describe "extended objects" do
14
14
  Post.extend Clerk
15
-
15
+
16
16
  class PostExtended < Post
17
17
  track_who_does_it
18
18
  end
19
-
19
+
20
20
  it "should respond to creator" do
21
21
  post = PostExtended.new
22
22
  post.should respond_to :creator
23
23
  end
24
-
24
+
25
25
  it "should respond to updater" do
26
26
  post = PostExtended.new
27
27
  post.should respond_to :updater
28
- end
28
+ end
29
29
  end
30
-
30
+
31
31
  describe "clerical objects can be tracked automatically" do
32
32
  Post.extend Clerk
33
-
33
+
34
34
  class PostExtended < Post
35
35
  track_who_does_it
36
36
  end
37
-
37
+
38
38
  before(:each) do
39
39
  @creator = User.create!(:name => "creator")
40
40
  @updater = User.create!(:name => "updater")
41
41
  end
42
-
42
+
43
43
  it "by creator" do
44
44
  @creator.make_current
45
45
  this = PostExtended.new(:title => "Test")
@@ -52,11 +52,28 @@ describe Clerk do
52
52
  @creator.make_current
53
53
  this = PostExtended.new(:title => "Test2")
54
54
  this.save
55
-
55
+
56
56
  @updater.make_current
57
57
  this.update_attribute(:title, "Updated")
58
58
  this.creator.name.should == "creator"
59
- this.updater.name.should == "updater"
59
+ this.updater.name.should == "updater"
60
60
  end
61
- end
61
+ end
62
+
63
+ describe "specified foreign_keys" do
64
+ Foo.extend Clerk
65
+
66
+ class FooExtended < Foo
67
+ track_who_does_it :creator_foreign_key => "user_id", :updater_foreign_key => "updater_id"
68
+ end
69
+
70
+ it "can override default foreign_keys" do
71
+ creator = User.create!(:name => "creator")
72
+ creator.make_current
73
+ this = FooExtended.new(:bar => "Test")
74
+ this.save
75
+ this.creator.name.should == "creator"
76
+ this.updater.name.should == "creator"
77
+ end
78
+ end
62
79
  end
@@ -4,4 +4,8 @@ end
4
4
 
5
5
  class User < ActiveRecord::Base
6
6
  include SentientUser
7
+ end
8
+
9
+ class Foo < ActiveRecord::Base
10
+
7
11
  end
@@ -8,11 +8,18 @@ ActiveRecord::Schema.define do
8
8
  t.integer :updated_by_id
9
9
  t.timestamps
10
10
  end
11
-
11
+
12
+ create_table :foos, :force => true do |t|
13
+ t.string :bar
14
+ t.integer :user_id
15
+ t.integer :updater_id
16
+ t.timestamps
17
+ end
18
+
12
19
  create_table :users, :force => true do |t|
13
20
  t.string :name
14
21
  t.integer :created_by_id
15
22
  t.integer :updated_by_id
16
23
  t.timestamps
17
- end
24
+ end
18
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clerk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-25 00:00:00.000000000Z
12
+ date: 2012-12-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70277505884660 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70277505884660
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: railties
27
- requirement: &70277505883260 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '3'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70277505883260
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: sentient_user
38
- requirement: &70277505882440 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: 0.3.2
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *70277505882440
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.2
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: bundler
49
- requirement: &70277505881640 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: 1.1.3
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70277505881640
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.1.3
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: rspec
60
- requirement: &70277505880820 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ~>
@@ -65,10 +85,15 @@ dependencies:
65
85
  version: 2.9.0
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *70277505880820
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.9.0
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: sqlite3
71
- requirement: &70277505880280 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
99
  - - ! '>='
@@ -76,9 +101,14 @@ dependencies:
76
101
  version: '0'
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *70277505880280
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
80
110
  description: ! "\n For Rails applications - add creator and updater to your ActiveRecord
81
- models \n and automatically set these from current_user\n \nSee the README on
111
+ models\n and automatically set these from current_user\n \nSee the README on
82
112
  github for more info"
83
113
  email:
84
114
  - jesse.house@gmail.com
@@ -87,7 +117,9 @@ extensions: []
87
117
  extra_rdoc_files: []
88
118
  files:
89
119
  - .gitignore
120
+ - .rspec
90
121
  - .rvmrc
122
+ - .travis.yml
91
123
  - Gemfile
92
124
  - LICENSE
93
125
  - README.md
@@ -122,11 +154,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
154
  version: '0'
123
155
  requirements: []
124
156
  rubyforge_project:
125
- rubygems_version: 1.8.10
157
+ rubygems_version: 1.8.24
126
158
  signing_key:
127
159
  specification_version: 3
128
- summary: For Rails applications - add creator and updater to your ActiveRecord models and
129
- automatically set these from current_user
160
+ summary: For Rails applications - add creator and updater to your ActiveRecord models
161
+ and automatically set these from current_user
130
162
  test_files:
131
163
  - spec/clerk_spec.rb
132
164
  - spec/spec_helper.rb