sixarm_ruby_current_user 1.4.0 → 1.4.4
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/CHANGELOG.txt +1 -0
- data/README.rdoc +63 -4
- data/VERSION +1 -1
- data/lib/sixarm_ruby_current_user.rb +49 -2
- data/test/sixarm_ruby_current_user_test.rb +57 -4
- data.tar.gz.sig +0 -0
- metadata +3 -14
- metadata.gz.sig +0 -0
data/CHANGELOG.txt
CHANGED
data/README.rdoc
CHANGED
@@ -4,10 +4,69 @@ Author:: Joel Parker Henderson, joel@joelparkerhenderson.com
|
|
4
4
|
Copyright:: Copyright (c) 2005-2011 Joel Parker Henderson
|
5
5
|
License:: See LICENSE.txt file
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
Get and set the current user in the Rails session array.
|
8
|
+
|
9
|
+
When you set the current user, this does:
|
10
|
+
- @current_user = user
|
11
|
+
- @current_user_id = user.id
|
12
|
+
- session[:current_user_id] = user.id
|
13
|
+
|
14
|
+
|
15
|
+
== Example code
|
16
|
+
|
17
|
+
joe = User.find(123)
|
18
|
+
self.current_user = joe
|
19
|
+
=>
|
20
|
+
@current_user == joe
|
21
|
+
@current_user_id == 123
|
22
|
+
session[:current_user_id] == 123
|
23
|
+
|
24
|
+
|
25
|
+
== Example controller
|
26
|
+
|
27
|
+
class MyController < ApplicationController
|
28
|
+
|
29
|
+
def sign_in(user)
|
30
|
+
self.current_user = user
|
31
|
+
end
|
32
|
+
|
33
|
+
def sign_out
|
34
|
+
self.current_user = nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def is_anyone_using_this?
|
38
|
+
current_user?
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
== Example of reloading
|
45
|
+
|
46
|
+
For fast speed, we memoize current_user and current_user_id:
|
47
|
+
we use fast instance variables @current_user and @current_user_id
|
48
|
+
rather than reading the slower session[:current_user_id] each time.
|
49
|
+
|
50
|
+
To reload @current_user and @current_user_id from session[:current_user_id],
|
51
|
+
we use the :reload parameter like this:
|
52
|
+
|
53
|
+
current_user(:reload => true)
|
54
|
+
|
55
|
+
|
56
|
+
== Why use the self prefix?
|
57
|
+
|
58
|
+
When we set variables, we must use the "self" prefix because Ruby uses this to do method dispatch.
|
59
|
+
|
60
|
+
Right:
|
61
|
+
self.current_user = joe
|
62
|
+
|
63
|
+
Wrong:
|
64
|
+
current_user = joe
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
9
70
|
|
10
|
-
For speed, we memoize the current_user.
|
11
71
|
|
12
|
-
The current_user_find method also includes the user's roles.
|
13
72
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.4.
|
1
|
+
1.4.4
|
@@ -3,10 +3,17 @@
|
|
3
3
|
Please see README.rdoc
|
4
4
|
=end
|
5
5
|
|
6
|
-
require 'sixarm_ruby_current_user_id'
|
7
6
|
|
8
7
|
module CurrentUser
|
9
8
|
|
9
|
+
|
10
|
+
###
|
11
|
+
#
|
12
|
+
# Methods for the User model
|
13
|
+
#
|
14
|
+
###
|
15
|
+
|
16
|
+
|
10
17
|
# Get the current user.
|
11
18
|
#
|
12
19
|
# This calls User.find with the current_user_id
|
@@ -20,7 +27,7 @@ module CurrentUser
|
|
20
27
|
|
21
28
|
def current_user(ops={})
|
22
29
|
if ops[:reload] then @current_user=nil end
|
23
|
-
@current_user ||= (current_user_id ? User.find(current_user_id) : nil)
|
30
|
+
@current_user ||= (current_user_id(ops) ? User.find(current_user_id) : nil)
|
24
31
|
end
|
25
32
|
|
26
33
|
|
@@ -39,4 +46,44 @@ module CurrentUser
|
|
39
46
|
@current_user = user
|
40
47
|
end
|
41
48
|
|
49
|
+
|
50
|
+
###
|
51
|
+
#
|
52
|
+
# Methods for the User id
|
53
|
+
#
|
54
|
+
###
|
55
|
+
|
56
|
+
|
57
|
+
# Get the current user id in the Rails session array.
|
58
|
+
#
|
59
|
+
# The current user id is memoized as @current_user_id.
|
60
|
+
# To reload, pass :reload => true
|
61
|
+
#
|
62
|
+
# Return the session's current user id.
|
63
|
+
|
64
|
+
def current_user_id(ops={})
|
65
|
+
if ops[:reload] then @current_user_id=nil end
|
66
|
+
@current_user_id ||= session[:current_user_id]
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
# Is there a current user id in the Rails session array?
|
71
|
+
|
72
|
+
def current_user_id?
|
73
|
+
return session[:current_user_id] ? true : false
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
# Set the current user id in the Rails session array.
|
78
|
+
# Return the current user id, suitable for chaining.
|
79
|
+
|
80
|
+
def current_user_id=(id)
|
81
|
+
@current_user_id=session[:current_user_id]=id
|
82
|
+
end
|
83
|
+
|
84
|
+
|
42
85
|
end
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require 'test/unit'
|
3
3
|
require 'sixarm_ruby_current_user'
|
4
|
-
require 'sixarm_ruby_current_user_id'
|
5
4
|
require 'sixarm_ruby_active_record_mock'
|
6
5
|
require 'simplecov'
|
7
6
|
SimpleCov.start
|
@@ -17,13 +16,18 @@ class Testing < Test::Unit::TestCase
|
|
17
16
|
end
|
18
17
|
|
19
18
|
include CurrentUser
|
20
|
-
|
19
|
+
|
20
|
+
|
21
|
+
###
|
22
|
+
#
|
23
|
+
# Methods for the User model
|
24
|
+
#
|
25
|
+
###
|
21
26
|
|
22
27
|
ANNE = User.new(:id => 1, :name => 'Anne')
|
23
28
|
BETH = User.new(:id => 2, :name => 'Beth')
|
24
29
|
CATE = User.new(:id => 3, :name => 'Cate')
|
25
30
|
|
26
|
-
|
27
31
|
def test_blank_slate
|
28
32
|
assert_nil(current_user, "current_user")
|
29
33
|
assert_nil(current_user_id, "current_user_id")
|
@@ -74,9 +78,58 @@ class Testing < Test::Unit::TestCase
|
|
74
78
|
assert_equal(nil, actual, "current_user")
|
75
79
|
end
|
76
80
|
|
77
|
-
end
|
78
81
|
|
79
82
|
|
83
|
+
###
|
84
|
+
#
|
85
|
+
# Methods for the User id
|
86
|
+
#
|
87
|
+
###
|
88
|
+
|
89
|
+
ID=1234
|
90
|
+
|
91
|
+
def test_current_user_id_default_is_nil
|
92
|
+
assert_nil(current_user_id, "current_user_id is not set, so current_user_id should be nil")
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_current_user_id_equals
|
96
|
+
self.current_user_id=ID
|
97
|
+
assert_equal(ID, @current_user_id, "current_user_id is set, so @current_user_id should be an id")
|
98
|
+
assert_equal(ID, session[:current_user_id], "current_user_id is set, so session[:current_user_id] should be an id")
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_current_user_id_equals_with_round_trip
|
102
|
+
self.current_user_id=ID
|
103
|
+
assert_equal(ID, current_user_id, "current_user_id is set, so current_user_id should be an id")
|
104
|
+
end
|
80
105
|
|
81
106
|
|
107
|
+
def test_current_user_id_with_memoize
|
108
|
+
self.current_user_id=ID
|
109
|
+
session[:current_user_id]=ID+1
|
110
|
+
assert_equal(ID, current_user_id, "current_user_id is memoized, so current_user_id should be an id")
|
111
|
+
end
|
82
112
|
|
113
|
+
def test_current_user_id_reload_false
|
114
|
+
self.current_user_id=ID
|
115
|
+
session[:current_user_id]=ID+1
|
116
|
+
assert_equal(ID, current_user_id(:reload=>false), "current_user_id reload => false, so current_user_id should be an id")
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_current_user_id_reload_true
|
120
|
+
self.current_user_id=ID
|
121
|
+
session[:current_user_id]=ID+1
|
122
|
+
assert_equal(ID+1, current_user_id(:reload=>true), "current_user_id reload => true, so current_user_id should be an id")
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_current_user_id_question_is_false
|
126
|
+
assert_equal(false, current_user_id?, "current_user_id is not set, so current_user_id? should be false")
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_current_user_id_question_is_true
|
130
|
+
self.current_user_id=ID
|
131
|
+
assert_equal(true, current_user_id?, "current_user_id is set, so current_user_id? should be true")
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: sixarm_ruby_current_user
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.4.
|
5
|
+
version: 1.4.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- SixArm
|
@@ -33,28 +33,17 @@ cert_chain:
|
|
33
33
|
date: 2011-04-26 00:00:00 -07:00
|
34
34
|
default_executable:
|
35
35
|
dependencies:
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: sixarm_ruby_current_user_id
|
38
|
-
prerelease: false
|
39
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
40
|
-
none: false
|
41
|
-
requirements:
|
42
|
-
- - "="
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
version: 1.4.0
|
45
|
-
type: :runtime
|
46
|
-
version_requirements: *id001
|
47
36
|
- !ruby/object:Gem::Dependency
|
48
37
|
name: sixarm_ruby_active_record_mock
|
49
38
|
prerelease: false
|
50
|
-
requirement: &
|
39
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
51
40
|
none: false
|
52
41
|
requirements:
|
53
42
|
- - ">="
|
54
43
|
- !ruby/object:Gem::Version
|
55
44
|
version: 1.4.4
|
56
45
|
type: :runtime
|
57
|
-
version_requirements: *
|
46
|
+
version_requirements: *id001
|
58
47
|
description:
|
59
48
|
email: sixarm@sixarm.com
|
60
49
|
executables: []
|
metadata.gz.sig
CHANGED
Binary file
|