clerk 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -5
- data/README.md +13 -0
- data/lib/clerk.rb +1 -0
- data/lib/clerk/callback.rb +19 -7
- data/lib/clerk/configuration.rb +22 -0
- data/lib/clerk/railtie.rb +5 -0
- data/lib/clerk/version.rb +1 -1
- data/spec/clerk/configuration_spec.rb +44 -0
- data/spec/clerk_spec.rb +65 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2ee29689040f515048d33262d9717d47e4572ea
|
4
|
+
data.tar.gz: cfc014bf93106d3b0c82709f03da3ba3546b81e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d42c93804677c2df22c6706e1a716d1da502d0b0f73b4e9f5dfb9237f03cc1bb8c92f2b7ab629b9ae5f7c6bd7f4649626785a8050eea53cc0db3b4f83209c07
|
7
|
+
data.tar.gz: 4c8e491a147aff637ab02c7a4d7bb9b42c345b154ac59a2989e2e3c857008f418f3c9a2e4d6bda985a10b497f252b390458e732595e94f743ba8855e2118d593
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -86,6 +86,18 @@ Since we are using `sentient_user` you can also set the updater or creator in te
|
|
86
86
|
Post.create!(:title => "New Post")
|
87
87
|
Post.last.creator == user # => true
|
88
88
|
|
89
|
+
## Configuration
|
90
|
+
|
91
|
+
In case you need to customize Clerk. Just create `config/initializers/clerk.rb` and set up your configuration:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
Clerk.configure do |config|
|
95
|
+
config.silence_warnings = true | false # Defaults: false (true in test env)
|
96
|
+
config.logger = MyCustomLogger.new() # Defaults: Rails.logger
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
|
89
101
|
## Resources used in the development of this gem
|
90
102
|
|
91
103
|
* [http://ryanbigg.com/2011/01/extending-active-record/](http://ryanbigg.com/2011/01/extending-active-record/)
|
@@ -94,3 +106,4 @@ Since we are using `sentient_user` you can also set the updater or creator in te
|
|
94
106
|
* [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/)
|
95
107
|
* [http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html](http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)
|
96
108
|
* [https://github.com/bokmann/sentient_user](https://github.com/bokmann/sentient_user)
|
109
|
+
* [https://robots.thoughtbot.com/mygem-configure-block](https://robots.thoughtbot.com/mygem-configure-block)
|
data/lib/clerk.rb
CHANGED
data/lib/clerk/callback.rb
CHANGED
@@ -4,21 +4,33 @@ module Clerk
|
|
4
4
|
record.creator = current_user if record.respond_to?(:creator) && current_user
|
5
5
|
record.updater = current_user if record.respond_to?(:updater) && current_user
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
def before_update(record)
|
9
9
|
record.updater = current_user if record.respond_to?(:updater) && current_user
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
# relies on `include SentientUser` on User
|
13
13
|
def current_user
|
14
|
-
|
15
|
-
|
14
|
+
warn "User#current is not defined, are you including SentientUser on your User model?" unless User.respond_to?(:current)
|
15
|
+
warn "User#current is nil, are you including SentientController on your ApplicationController?" unless User.current
|
16
16
|
|
17
17
|
User.current
|
18
|
-
end
|
19
|
-
|
18
|
+
end
|
19
|
+
|
20
|
+
#######
|
21
|
+
private
|
22
|
+
#######
|
23
|
+
|
24
|
+
def warn(message)
|
25
|
+
logger.warn "WARNING: #{message}" unless configuration.silence_warnings
|
26
|
+
end
|
27
|
+
|
20
28
|
def logger
|
21
|
-
|
29
|
+
configuration.logger
|
30
|
+
end
|
31
|
+
|
32
|
+
def configuration
|
33
|
+
Clerk.configuration
|
22
34
|
end
|
23
35
|
end
|
24
36
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_support/logger'
|
2
|
+
|
3
|
+
module Clerk
|
4
|
+
class << self
|
5
|
+
attr_accessor :configuration
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.configure
|
9
|
+
self.configuration ||= Configuration.new
|
10
|
+
yield(configuration)
|
11
|
+
end
|
12
|
+
|
13
|
+
class Configuration
|
14
|
+
attr_accessor :silence_warnings,
|
15
|
+
:logger
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@silence_warnings = false
|
19
|
+
@logger = ActiveSupport::Logger.new(STDOUT)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/clerk/railtie.rb
CHANGED
@@ -5,6 +5,11 @@ module Clerk
|
|
5
5
|
class Railtie < Rails::Railtie
|
6
6
|
initializer 'clerk.ar_extensions' do |app|
|
7
7
|
ActiveRecord::Base.extend Clerk
|
8
|
+
|
9
|
+
Clerk.configure do |config|
|
10
|
+
config.silence_warnings = Rails.env.test?
|
11
|
+
config.logger = Rails.logger
|
12
|
+
end
|
8
13
|
end
|
9
14
|
end
|
10
15
|
end
|
data/lib/clerk/version.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clerk::Configuration do
|
4
|
+
before(:each) do
|
5
|
+
Clerk.configure {}
|
6
|
+
end
|
7
|
+
|
8
|
+
after(:each) do
|
9
|
+
Clerk.configuration = Clerk::Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should have a configuration" do
|
13
|
+
Clerk.configuration.should be_a(Clerk::Configuration)
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'silence_warnings' do
|
17
|
+
it 'should be false by default' do
|
18
|
+
Clerk.configuration.silence_warnings.should be_false
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'can be toggled to true' do
|
22
|
+
Clerk.configure { |c| c.silence_warnings = true }
|
23
|
+
Clerk.configuration.silence_warnings.should be_true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'logger' do
|
28
|
+
it 'should exist by default' do
|
29
|
+
Clerk.configuration.logger.should be_a ActiveSupport::Logger
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should be configurable' do
|
33
|
+
class FakeLogger
|
34
|
+
def warn(msg)
|
35
|
+
# noop
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Clerk.configure { |c| c.logger = FakeLogger.new() }
|
40
|
+
|
41
|
+
Clerk.configuration.logger.should be_a FakeLogger
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/clerk_spec.rb
CHANGED
@@ -76,4 +76,69 @@ describe Clerk do
|
|
76
76
|
this.updater.name.should == "creator"
|
77
77
|
end
|
78
78
|
end
|
79
|
+
|
80
|
+
describe "warning messages" do
|
81
|
+
Post.extend Clerk
|
82
|
+
|
83
|
+
class PostExtended < Post
|
84
|
+
track_who_does_it
|
85
|
+
end
|
86
|
+
|
87
|
+
before(:each) do
|
88
|
+
@logger = double("logger", warn: true)
|
89
|
+
Clerk.configure { |c| c.logger = @logger }
|
90
|
+
end
|
91
|
+
|
92
|
+
after(:each) do
|
93
|
+
# Reset configuration
|
94
|
+
Clerk.configuration = Clerk::Configuration.new
|
95
|
+
end
|
96
|
+
|
97
|
+
context "when missing SentientUser" do
|
98
|
+
before do
|
99
|
+
User.stub(:respond_to?).with(:current).and_return(false)
|
100
|
+
end
|
101
|
+
it "should log a message" do
|
102
|
+
@logger.should_receive(:warn).with("WARNING: User#current is not defined, are you including SentientUser on your User model?")
|
103
|
+
this = FooExtended.new(:bar => "Test")
|
104
|
+
this.save
|
105
|
+
end
|
106
|
+
|
107
|
+
context "amd warning are silenced" do
|
108
|
+
before do
|
109
|
+
Clerk.configure { |c| c.silence_warnings = true }
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should not show a log message" do
|
113
|
+
@logger.should_not_receive(:warn).with("WARNING: User#current is not defined, are you including SentientUser on your User model?")
|
114
|
+
this = FooExtended.new(:bar => "Test")
|
115
|
+
this.save
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "when missing User.current" do
|
121
|
+
before do
|
122
|
+
User.current = nil
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should log a message" do
|
126
|
+
@logger.should_receive(:warn).with("WARNING: User#current is nil, are you including SentientController on your ApplicationController?")
|
127
|
+
this = FooExtended.new(:bar => "Test")
|
128
|
+
this.save
|
129
|
+
end
|
130
|
+
|
131
|
+
context "amd warning are silenced" do
|
132
|
+
before do
|
133
|
+
Clerk.configure { |c| c.silence_warnings = true }
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should not show a log message" do
|
137
|
+
@logger.should_not_receive(:warn).with("WARNING: User#current is nil, are you including SentientController on your ApplicationController?")
|
138
|
+
this = FooExtended.new(:bar => "Test")
|
139
|
+
this.save
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
79
144
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clerk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesse House
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -114,8 +114,10 @@ files:
|
|
114
114
|
- clerk.gemspec
|
115
115
|
- lib/clerk.rb
|
116
116
|
- lib/clerk/callback.rb
|
117
|
+
- lib/clerk/configuration.rb
|
117
118
|
- lib/clerk/railtie.rb
|
118
119
|
- lib/clerk/version.rb
|
120
|
+
- spec/clerk/configuration_spec.rb
|
119
121
|
- spec/clerk_spec.rb
|
120
122
|
- spec/spec_helper.rb
|
121
123
|
- spec/support/data.rb
|
@@ -141,12 +143,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
143
|
version: '0'
|
142
144
|
requirements: []
|
143
145
|
rubyforge_project:
|
144
|
-
rubygems_version: 2.
|
146
|
+
rubygems_version: 2.5.1
|
145
147
|
signing_key:
|
146
148
|
specification_version: 4
|
147
149
|
summary: For Rails applications - add creator and updater to your ActiveRecord models
|
148
150
|
and automatically set these from current_user
|
149
151
|
test_files:
|
152
|
+
- spec/clerk/configuration_spec.rb
|
150
153
|
- spec/clerk_spec.rb
|
151
154
|
- spec/spec_helper.rb
|
152
155
|
- spec/support/data.rb
|