current_fu 3.2.0
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 +3 -0
- data/.travis.yml +3 -0
- data/CHANGELOG +5 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +21 -0
- data/README.md +27 -0
- data/Rakefile +13 -0
- data/lib/current_fu.rb +8 -0
- data/lib/current_fu/controller_concerns.rb +28 -0
- data/lib/current_fu/errors.rb +7 -0
- data/lib/current_fu/getter.rb +22 -0
- data/lib/current_fu/object.rb +52 -0
- data/lib/current_fu/version.rb +3 -0
- data/test/cases/base_test.rb +75 -0
- data/test/helper.rb +69 -0
- metadata +119 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2008-2012 Ken Collins
|
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.
|
21
|
+
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
# CurrentFu: The most evil gem you will hate to love.
|
3
|
+
|
4
|
+
CurrentFu gives a standard interface to the current controller instance in your models with automatic delegation to #current_* named methods.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem with bundler. We follow a semantic versioning format that tracks Rails' minor version. So this means to use the latest 3.2.x version of CurrentFu with any 3.2 Rails version.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'current_fu', '~> 3.2.0'
|
13
|
+
```
|
14
|
+
|
15
|
+
|
16
|
+
## Testing
|
17
|
+
|
18
|
+
Simple! Just clone the repo, then run `bundle install` and `bundle exec rake`. The tests will begin to run. We also use Travis CI to run our tests too. Current build status is:
|
19
|
+
|
20
|
+
[](http://travis-ci.org/metaskills/current_fu)
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
## License
|
25
|
+
|
26
|
+
Released under the MIT license.
|
27
|
+
Copyright (c) 2008-2012 Ken Collins
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
desc 'Test the CurrentFu gem.'
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
t.libs = ['lib','test']
|
9
|
+
t.test_files = Dir.glob("test/**/*_test.rb").sort
|
10
|
+
t.verbose = true
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => [:test]
|
data/lib/current_fu.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module CurrentFu
|
4
|
+
module ControllerConcerns
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
around_filter :set_current
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
def current
|
15
|
+
@current ||= CurrentFu::Object.new(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_current
|
19
|
+
CurrentFu::Object.instance = current
|
20
|
+
yield
|
21
|
+
ensure
|
22
|
+
CurrentFu::Object.instance = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
ActiveSupport.on_load(:action_controller) { include CurrentFu::ControllerConcerns }
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module CurrentFu
|
4
|
+
module Getter
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def current
|
11
|
+
CurrentFu::Object.instance
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def current
|
17
|
+
self.class.current
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module CurrentFu
|
2
|
+
class Object
|
3
|
+
|
4
|
+
def initialize(current_owner)
|
5
|
+
@current_owner = current_owner
|
6
|
+
end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def instance
|
11
|
+
Thread.current[:current_fu_instance]
|
12
|
+
end
|
13
|
+
|
14
|
+
def instance=(current_instance)
|
15
|
+
Thread.current[:current_fu_instance] = current_instance
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def instance
|
21
|
+
@current_owner
|
22
|
+
end
|
23
|
+
|
24
|
+
def respond_to?(method)
|
25
|
+
current_owner_respond_to?(method) || super
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def current_method(method)
|
32
|
+
:"current_#{method}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def current_owner_respond_to?(method)
|
36
|
+
@current_owner.respond_to? current_method(method)
|
37
|
+
end
|
38
|
+
|
39
|
+
def method_missing(method, *args)
|
40
|
+
if current_owner_respond_to?(method)
|
41
|
+
if block_given?
|
42
|
+
@current_owner.send(current_method(method),*args) { |*block_args| yield(*block_args) }
|
43
|
+
else
|
44
|
+
@current_owner.send(current_method(method),*args)
|
45
|
+
end
|
46
|
+
else
|
47
|
+
block_given? ? super { |*block_args| yield(*block_args) } : super
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
|
2
|
+
require 'helper'
|
3
|
+
|
4
|
+
class CurrentFu::BaseTest < CurrentFu::TestCase
|
5
|
+
|
6
|
+
let(:base_klass) { ActionController::Base }
|
7
|
+
let(:base_instance) { base_klass.new }
|
8
|
+
let(:base_current) { CurrentFu::Object.new(base_instance) }
|
9
|
+
let(:app_instance) { TestApplicationController.new }
|
10
|
+
let(:app_current) { CurrentFu::Object.new(app_instance) }
|
11
|
+
let(:employee_klass) { Employee }
|
12
|
+
let(:employee_instance) { employee_klass.new }
|
13
|
+
|
14
|
+
describe 'ControllerConcerns' do
|
15
|
+
|
16
|
+
it 'mixins in #current method' do
|
17
|
+
base_klass.protected_instance_methods.map(&:to_s).must_include 'current'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'wraps self via #current' do
|
21
|
+
base_instance.send(:current).instance.must_equal base_instance
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'sets up an around filter to #set_current' do
|
25
|
+
callback = base_klass._process_action_callbacks.detect { |cb| cb.kind == :around && cb.filter == :set_current }
|
26
|
+
assert callback
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'sets the current instance with #set_current and always returns current instance to nil' do
|
30
|
+
CurrentFu::Object.instance.must_be_nil
|
31
|
+
base_instance.send :set_current do
|
32
|
+
CurrentFu::Object.instance.must_be_instance_of CurrentFu::Object
|
33
|
+
CurrentFu::Object.instance.instance.must_equal base_instance
|
34
|
+
end
|
35
|
+
CurrentFu::Object.instance.must_be_nil
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'Getter' do
|
41
|
+
|
42
|
+
it 'has both a class and instance for current instance object' do
|
43
|
+
employee_klass.current.must_be_nil
|
44
|
+
employee_instance.send(:current).must_be_nil
|
45
|
+
CurrentFu::Object.instance = base_current
|
46
|
+
employee_klass.current.must_be_instance_of CurrentFu::Object
|
47
|
+
employee_klass.current.instance.must_equal base_instance
|
48
|
+
employee_instance.current.instance.must_equal base_instance
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'Object' do
|
54
|
+
|
55
|
+
it 'delegates via #respond_to? and #method_missing to current_* methods of the instance' do
|
56
|
+
user = Employee.first
|
57
|
+
app_instance.current_user = user
|
58
|
+
CurrentFu::Object.instance = app_current
|
59
|
+
employee_instance.current.respond_to?(:user)
|
60
|
+
employee_instance.current.user.must_equal user
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'is thread safe' do
|
64
|
+
bk1 = base_klass.new
|
65
|
+
bk2 = base_klass.new
|
66
|
+
t1 = Thread.new { CurrentFu::Object.instance = CurrentFu::Object.new(bk1) }.join
|
67
|
+
t2 = Thread.new { CurrentFu::Object.instance = CurrentFu::Object.new(bk2) }.join
|
68
|
+
t1[:current_fu_instance].instance.must_equal bk1
|
69
|
+
t2[:current_fu_instance].instance.must_equal bk2
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require "bundler/setup"
|
4
|
+
Bundler.require
|
5
|
+
require 'current_fu'
|
6
|
+
require 'active_record/base'
|
7
|
+
require 'action_controller/base'
|
8
|
+
require 'minitest/autorun'
|
9
|
+
require 'logger'
|
10
|
+
|
11
|
+
|
12
|
+
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__),'debug.log'))
|
13
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
14
|
+
|
15
|
+
|
16
|
+
module CurrentFu
|
17
|
+
class TestCase < MiniTest::Spec
|
18
|
+
|
19
|
+
before { setup_environment }
|
20
|
+
after { cleanup }
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def cleanup
|
25
|
+
CurrentFu::Object.instance = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def setup_environment
|
29
|
+
setup_database
|
30
|
+
setup_data
|
31
|
+
end
|
32
|
+
|
33
|
+
def setup_database
|
34
|
+
ActiveRecord::Base.class_eval do
|
35
|
+
silence do
|
36
|
+
connection.create_table :employees, :force => true do |t|
|
37
|
+
t.column :name, :string
|
38
|
+
t.column :email, :string
|
39
|
+
t.column :age, :integer
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def setup_data
|
46
|
+
Employee.create :name => 'Ken Collins', :email => 'ken@metaskills.net', :age => 39
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Employee < ActiveRecord::Base
|
53
|
+
include CurrentFu::Getter
|
54
|
+
end
|
55
|
+
|
56
|
+
module TestAuthSystem
|
57
|
+
include CurrentFu::Getter
|
58
|
+
def current_user
|
59
|
+
@current_user ||= Employee.first
|
60
|
+
end
|
61
|
+
def current_user=(user)
|
62
|
+
@current_user = user
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class TestApplicationController < ActionController::Base
|
67
|
+
include TestAuthSystem
|
68
|
+
end
|
69
|
+
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: current_fu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ken Collins
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70214029672660 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70214029672660
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sqlite3
|
27
|
+
requirement: &70214029721340 !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: *70214029721340
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70214029720880 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70214029720880
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: minitest
|
49
|
+
requirement: &70214029720460 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70214029720460
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: m
|
60
|
+
requirement: &70214029720040 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70214029720040
|
69
|
+
description: ! 'CurrentFu gives a standard interface to the current controller instance
|
70
|
+
in your models with automatic delegation to #current_* named methods.'
|
71
|
+
email:
|
72
|
+
- ken@metaskills.net
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- .travis.yml
|
79
|
+
- CHANGELOG
|
80
|
+
- Gemfile
|
81
|
+
- MIT-LICENSE
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/current_fu.rb
|
85
|
+
- lib/current_fu/controller_concerns.rb
|
86
|
+
- lib/current_fu/errors.rb
|
87
|
+
- lib/current_fu/getter.rb
|
88
|
+
- lib/current_fu/object.rb
|
89
|
+
- lib/current_fu/version.rb
|
90
|
+
- test/cases/base_test.rb
|
91
|
+
- test/helper.rb
|
92
|
+
homepage: http://github.com/metaskills/current_fu/
|
93
|
+
licenses: []
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options:
|
96
|
+
- --charset=UTF-8
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 1.8.17
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: The most evil gem you will hate to love.
|
117
|
+
test_files:
|
118
|
+
- test/cases/base_test.rb
|
119
|
+
- test/helper.rb
|