ubiquitous_user 0.2.1 → 0.3.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/README.rdoc +23 -0
- data/Rakefile +37 -0
- data/VERSION +1 -1
- data/lib/ubiquitous_user.rb +32 -12
- data/test/helper.rb +40 -2
- data/test/test_ubiquitous_user.rb +213 -0
- metadata +3 -3
- data/test/test_ubiquitous-user.rb +0 -7
data/README.rdoc
CHANGED
@@ -119,6 +119,24 @@ You can use user= and authorize in the controllers, for example:
|
|
119
119
|
# ...
|
120
120
|
end
|
121
121
|
|
122
|
+
Another interesting method is the authorize generator, which allows you to
|
123
|
+
specify the message you want to give the authorization is not granted. For
|
124
|
+
example:
|
125
|
+
|
126
|
+
class ProfilesController < ApplicationController
|
127
|
+
before_filter :only => [:edit, :update], &authorize("You gotta log in to do that!")
|
128
|
+
# ...
|
129
|
+
end
|
130
|
+
|
131
|
+
But for that you also need to add an "extend", like this:
|
132
|
+
|
133
|
+
class ApplicationController < ActionController::Base
|
134
|
+
include Usable
|
135
|
+
extend UsableClass
|
136
|
+
|
137
|
+
#...
|
138
|
+
end
|
139
|
+
|
122
140
|
== The model
|
123
141
|
|
124
142
|
Ubiquitous User expects you to have a model for your users called User
|
@@ -147,6 +165,11 @@ example:
|
|
147
165
|
Up to date api documentation should be automatically generated on
|
148
166
|
http://rdoc.info/projects/pupeno/ubiquitous_user
|
149
167
|
|
168
|
+
== Metrics
|
169
|
+
|
170
|
+
You con find the metrics for this gem automatically generated at
|
171
|
+
http://getcaliper.com/caliper/project?repo=git%3A%2F%2Fgithub.com%2Fpupeno%2Fubiquitous_user.git
|
172
|
+
|
150
173
|
== Note on Patches/Pull Requests
|
151
174
|
|
152
175
|
* Fork the project.
|
data/Rakefile
CHANGED
@@ -54,3 +54,40 @@ Rake::RDocTask.new do |rdoc|
|
|
54
54
|
rdoc.rdoc_files.include('LICENSE')
|
55
55
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
56
|
end
|
57
|
+
|
58
|
+
begin
|
59
|
+
require 'metric_fu'
|
60
|
+
MetricFu::Configuration.run do |config|
|
61
|
+
#define which metrics you want to use
|
62
|
+
# config.metrics = [:churn, :saikuro, :stats, :flog, :flay, :reek, :roodi, :rcov]
|
63
|
+
config.metrics = [:rcov]
|
64
|
+
# config.graphs = [:flog, :flay, :reek, :roodi, :rcov]
|
65
|
+
config.graphs = [:rcov]
|
66
|
+
|
67
|
+
# config.flay = { :dirs_to_flay => ['app', 'lib'] }
|
68
|
+
# config.flog = { :dirs_to_flog => ['app', 'lib'] }
|
69
|
+
# config.reek = { :dirs_to_reek => ['app', 'lib'] }
|
70
|
+
# config.roodi = { :dirs_to_roodi => ['app', 'lib'] }
|
71
|
+
# config.saikuro = { :output_directory => 'scratch_directory/saikuro',
|
72
|
+
# :input_directory => ['app', 'lib'],
|
73
|
+
# :cyclo => "",
|
74
|
+
# :filter_cyclo => "0",
|
75
|
+
# :warn_cyclo => "5",
|
76
|
+
# :error_cyclo => "7",
|
77
|
+
# :formater => "text"} #this needs to be set to "text"
|
78
|
+
# config.churn = { :start_date => "1 year ago", :minimum_churn_count => 10}
|
79
|
+
config.rcov = { :test_files => ['test/**/test_*.rb'],
|
80
|
+
:rcov_opts => ["--text-report"
|
81
|
+
#"--sort coverage",
|
82
|
+
#"--no-html",
|
83
|
+
# "--text-coverage",
|
84
|
+
# "--no-color",
|
85
|
+
# "--profile",
|
86
|
+
# "--rails",
|
87
|
+
# "--exclude /gems/,/Library/,spec"
|
88
|
+
]
|
89
|
+
}
|
90
|
+
end
|
91
|
+
rescue LoadError
|
92
|
+
puts "Metric fu not available"
|
93
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/ubiquitous_user.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module UsableConfig
|
2
2
|
@user_model = :User
|
3
3
|
@user_model_new = :new
|
4
|
-
@user_model_save = :save
|
4
|
+
@user_model_save = :save!
|
5
5
|
@user_model_name = :name
|
6
6
|
|
7
7
|
# Class that defines the user model.
|
@@ -29,13 +29,13 @@ module UsableHelpers
|
|
29
29
|
# will be in the database (although it may be a ghost user).
|
30
30
|
def user(options = {:save => false})
|
31
31
|
# Find the user in the database if session[:user_id] is defined and @ubiquitous_user is not.
|
32
|
-
@ubiquitous_user = UsableConfig::user_model_class.
|
32
|
+
@ubiquitous_user = UsableConfig::user_model_class.find_by_id(session[:user_id]) if session[:user_id] != nil and @ubiquitous_user == nil
|
33
33
|
|
34
34
|
# Create a new user object if @ubiquitous_user is not defined.
|
35
|
-
@ubiquitous_user = UsableConfig::user_model_class.send(UsableConfig::user_model_new)
|
35
|
+
@ubiquitous_user = UsableConfig::user_model_class.send(UsableConfig::user_model_new) if @ubiquitous_user == nil
|
36
36
|
|
37
37
|
# If the object is new and we are asked to save, do it.
|
38
|
-
if @ubiquitous_user.new_record?
|
38
|
+
if options[:save] and @ubiquitous_user.new_record?
|
39
39
|
# Save the user in the database and set the session user_id for latter.
|
40
40
|
@ubiquitous_user.send(UsableConfig::user_model_save)
|
41
41
|
session[:user_id] = @ubiquitous_user.id
|
@@ -48,25 +48,45 @@ module UsableHelpers
|
|
48
48
|
def user!
|
49
49
|
return user(:save => true)
|
50
50
|
end
|
51
|
+
|
52
|
+
# Helper method to check whether a user is logged in or not
|
53
|
+
def user_logged_in?
|
54
|
+
user_id = session[:user_id]
|
55
|
+
user_id != nil and UsableConfig::user_model_class.find_by_id(user_id) != nil and session[:user_name] != nil
|
56
|
+
end
|
51
57
|
end
|
52
58
|
|
53
59
|
ActionController::Base.class_eval do
|
54
60
|
helper UsableHelpers
|
55
61
|
end
|
56
62
|
|
63
|
+
module UsableClass
|
64
|
+
def authorize(message = "Please log in.", key = :warning)
|
65
|
+
Proc.new do |controller|
|
66
|
+
unless controller.user_logged_in?
|
67
|
+
# flash, redirect_to and new_session_url are protected. Thank god this is Ruby, not Java.
|
68
|
+
controller.send(:flash)[key] = message
|
69
|
+
begin
|
70
|
+
controller.send(:redirect_to, :back)
|
71
|
+
rescue ActionController::RedirectBackError
|
72
|
+
controller.send(:redirect_to, controller.send(:new_session_url))
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
module_function :authorize
|
78
|
+
end
|
79
|
+
|
57
80
|
module Usable
|
58
81
|
include UsableHelpers
|
59
82
|
|
60
|
-
def user=(
|
61
|
-
session[:user_id] =
|
62
|
-
session[:user_name] =
|
63
|
-
|
83
|
+
def user=(new_user)
|
84
|
+
session[:user_id] = new_user != nil ? new_user.id : nil
|
85
|
+
session[:user_name] = new_user != nil ? new_user.send(UsableConfig::user_model_name) : nil
|
86
|
+
@ubiquitous_user = new_user
|
64
87
|
end
|
65
88
|
|
66
89
|
def authorize
|
67
|
-
|
68
|
-
flash[:notice] = "Please log in."
|
69
|
-
redirect_to new_session_url
|
70
|
-
end
|
90
|
+
::UsableClass.authorize().call(self)
|
71
91
|
end
|
72
92
|
end
|
data/test/helper.rb
CHANGED
@@ -1,10 +1,48 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'test/unit'
|
3
3
|
require 'shoulda'
|
4
|
+
require 'mocha'
|
5
|
+
require 'ruby-debug'
|
4
6
|
|
5
7
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
8
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
-
require 'ubiquitous_user'
|
8
9
|
|
9
|
-
|
10
|
+
# Minimum set ActionController required to test ubiquitous_user
|
11
|
+
module ActionController
|
12
|
+
class Base
|
13
|
+
@@helpers = []
|
14
|
+
|
15
|
+
def self.helper(h)
|
16
|
+
@@helpers << h
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class RedirectBackError < Exception
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Default user model.
|
25
|
+
class User
|
26
|
+
end
|
27
|
+
|
28
|
+
# An alternative user model.
|
29
|
+
class Person
|
10
30
|
end
|
31
|
+
|
32
|
+
require 'ubiquitous_user'
|
33
|
+
|
34
|
+
class Controller
|
35
|
+
include Usable
|
36
|
+
extend UsableClass
|
37
|
+
|
38
|
+
# Simulate session and flash
|
39
|
+
def initialize
|
40
|
+
@session = {}
|
41
|
+
@flash = {}
|
42
|
+
end
|
43
|
+
attr_accessor :session
|
44
|
+
attr_accessor :flash
|
45
|
+
|
46
|
+
# Allow access to @ubiquitous_user, only for testing purposes.
|
47
|
+
attr_accessor :ubiquitous_user
|
48
|
+
end
|
@@ -0,0 +1,213 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestUbiquitousUser < Test::Unit::TestCase
|
4
|
+
context "A controller and a mock user" do
|
5
|
+
setup do
|
6
|
+
@controller = Controller.new
|
7
|
+
@user = mock("User")
|
8
|
+
|
9
|
+
# Just to be sure we are starting from scratch
|
10
|
+
assert_nil @controller.ubiquitous_user
|
11
|
+
end
|
12
|
+
|
13
|
+
should "create a new user object on Controller#user" do
|
14
|
+
User.expects(:new).returns(@user)
|
15
|
+
|
16
|
+
user = @controller.user
|
17
|
+
|
18
|
+
assert_equal @user, user
|
19
|
+
assert_equal @user, @controller.ubiquitous_user
|
20
|
+
end
|
21
|
+
|
22
|
+
should "should return previous user object on Controller#user" do
|
23
|
+
@controller.ubiquitous_user = @user
|
24
|
+
|
25
|
+
user = @controller.user
|
26
|
+
|
27
|
+
assert_equal @user, user
|
28
|
+
end
|
29
|
+
|
30
|
+
should "find a user on Controller#user if there's a user_id on session" do
|
31
|
+
user_id = 42
|
32
|
+
@controller.session[:user_id] = user_id
|
33
|
+
User.expects(:find_by_id).with(user_id).returns(@user)
|
34
|
+
|
35
|
+
user = @controller.user
|
36
|
+
|
37
|
+
assert_equal @user, user
|
38
|
+
assert_equal @user, @controller.ubiquitous_user
|
39
|
+
end
|
40
|
+
|
41
|
+
should "save a new user when requested on Controller#user" do
|
42
|
+
user_id = 43
|
43
|
+
User.expects(:new).returns(@user)
|
44
|
+
@user.expects(:new_record?).returns(true)
|
45
|
+
@user.expects(:save!)
|
46
|
+
@user.expects(:id).returns(user_id)
|
47
|
+
|
48
|
+
user = @controller.user!
|
49
|
+
|
50
|
+
assert_equal @user, user
|
51
|
+
assert_equal @user, @controller.ubiquitous_user
|
52
|
+
assert_equal user_id, @controller.session[:user_id]
|
53
|
+
end
|
54
|
+
|
55
|
+
should "not save an existing user even when requested on Controller#user" do
|
56
|
+
user_id = 44
|
57
|
+
@controller.session[:user_id] = user_id
|
58
|
+
User.expects(:find_by_id).with(user_id).returns(@user)
|
59
|
+
@user.expects(:new_record?).returns(false)
|
60
|
+
|
61
|
+
user = @controller.user!
|
62
|
+
|
63
|
+
assert_equal @user, user
|
64
|
+
assert_equal @user, @controller.ubiquitous_user
|
65
|
+
end
|
66
|
+
|
67
|
+
should "set user on Controller#user=" do
|
68
|
+
user_id = 45
|
69
|
+
user_name = "Alex"
|
70
|
+
@user.expects(:id).returns(user_id)
|
71
|
+
@user.expects(:name).returns(user_name)
|
72
|
+
|
73
|
+
@controller.user = @user
|
74
|
+
|
75
|
+
assert_equal @user, @controller.ubiquitous_user
|
76
|
+
assert_equal user_id, @controller.session[:user_id]
|
77
|
+
assert_equal user_name, @controller.session[:user_name]
|
78
|
+
end
|
79
|
+
|
80
|
+
should "unset user on Controller#user=(nil)" do
|
81
|
+
@controller.user = nil
|
82
|
+
|
83
|
+
assert_equal nil, @controller.ubiquitous_user
|
84
|
+
assert_equal nil, @controller.session[:user_id]
|
85
|
+
assert_equal nil, @controller.session[:user_name]
|
86
|
+
end
|
87
|
+
|
88
|
+
should "say no user is logged in when none is on Controller#user_logged_in?" do
|
89
|
+
user_logged_in = @controller.user_logged_in?
|
90
|
+
|
91
|
+
assert !user_logged_in
|
92
|
+
end
|
93
|
+
|
94
|
+
should "say no user is logged in when an anonymously registered user is in on Controller#user_logged_in?" do
|
95
|
+
user_id = 46
|
96
|
+
@controller.session[:user_id] = user_id
|
97
|
+
User.expects(:find_by_id).with(user_id).returns(@user)
|
98
|
+
|
99
|
+
user_logged_in = @controller.user_logged_in?
|
100
|
+
|
101
|
+
assert !user_logged_in
|
102
|
+
end
|
103
|
+
|
104
|
+
should "say a user is logged in when it is on Controller#user_logged_in?" do
|
105
|
+
user_id = 46
|
106
|
+
user_name = "Brad"
|
107
|
+
@controller.session[:user_id] = user_id
|
108
|
+
@controller.session[:user_name] = user_name
|
109
|
+
User.expects(:find_by_id).with(user_id).returns(@user)
|
110
|
+
|
111
|
+
user_logged_in = @controller.user_logged_in?
|
112
|
+
|
113
|
+
assert user_logged_in
|
114
|
+
end
|
115
|
+
|
116
|
+
should "redirect back with a flash message when user not logged in on Controller.authorize" do
|
117
|
+
msg = "Log in you user!"
|
118
|
+
key = :error
|
119
|
+
|
120
|
+
authorize = Controller.send(:authorize, msg, key)
|
121
|
+
|
122
|
+
assert authorize.instance_of? Proc
|
123
|
+
|
124
|
+
@controller.expects(:redirect_to).with(:back)
|
125
|
+
|
126
|
+
authorize.call(@controller)
|
127
|
+
assert_equal msg, @controller.flash[key]
|
128
|
+
end
|
129
|
+
|
130
|
+
should "redirect to new_session_url with a flash message when user not logged in and there's no :back on Controller.authorize" do
|
131
|
+
msg = "Log in you user!"
|
132
|
+
key = :error
|
133
|
+
|
134
|
+
authorize = Controller.send(:authorize, msg, key)
|
135
|
+
|
136
|
+
assert authorize.instance_of? Proc
|
137
|
+
|
138
|
+
new_session_url = "/login"
|
139
|
+
@controller.expects(:redirect_to).with(:back).raises(ActionController::RedirectBackError)
|
140
|
+
@controller.expects(:new_session_url).returns(new_session_url)
|
141
|
+
@controller.expects(:redirect_to).with(new_session_url)
|
142
|
+
|
143
|
+
authorize.call(@controller)
|
144
|
+
assert_equal msg, @controller.flash[key]
|
145
|
+
end
|
146
|
+
|
147
|
+
should "redirect back with a flash message when user not logged in on Controller#authorize" do
|
148
|
+
@controller.expects(:redirect_to).with(:back)
|
149
|
+
|
150
|
+
@controller.authorize
|
151
|
+
assert_equal "Please log in.", @controller.flash[:warning]
|
152
|
+
end
|
153
|
+
|
154
|
+
context "with custom usable config" do
|
155
|
+
setup do
|
156
|
+
@orig_config = UsableConfig.clone
|
157
|
+
UsableConfig.user_model = :Person
|
158
|
+
UsableConfig.user_model_new = :new_person
|
159
|
+
UsableConfig.user_model_save = :save_person!
|
160
|
+
UsableConfig.user_model_name = :full_name
|
161
|
+
end
|
162
|
+
|
163
|
+
teardown do
|
164
|
+
UsableConfig.user_model = @orig_config.user_model
|
165
|
+
UsableConfig.user_model_new = @orig_config.user_model_new
|
166
|
+
UsableConfig.user_model_save = @orig_config.user_model_save
|
167
|
+
UsableConfig.user_model_name = @orig_config.user_model_name
|
168
|
+
end
|
169
|
+
|
170
|
+
should "create a new user object on #user" do
|
171
|
+
Person.expects(:new_person).returns(@user)
|
172
|
+
assert_equal @user, @controller.user
|
173
|
+
assert_equal @user, @controller.ubiquitous_user
|
174
|
+
end
|
175
|
+
|
176
|
+
should "save a new user when requested on #user" do
|
177
|
+
user_id = 43
|
178
|
+
Person.expects(:new_person).returns(@user)
|
179
|
+
@user.expects(:new_record?).returns(true)
|
180
|
+
@user.expects(:save_person!)
|
181
|
+
@user.expects(:id).returns(user_id)
|
182
|
+
assert_equal @user, @controller.user!
|
183
|
+
assert_equal @user, @controller.ubiquitous_user
|
184
|
+
assert_equal user_id, @controller.session[:user_id]
|
185
|
+
end
|
186
|
+
|
187
|
+
should "set user on Controller#user=" do
|
188
|
+
user_id = 45
|
189
|
+
user_name = "Alex"
|
190
|
+
@user.expects(:id).returns(user_id)
|
191
|
+
@user.expects(:full_name).returns(user_name)
|
192
|
+
|
193
|
+
@controller.user = @user
|
194
|
+
|
195
|
+
assert_equal @user, @controller.ubiquitous_user
|
196
|
+
assert_equal user_id, @controller.session[:user_id]
|
197
|
+
assert_equal user_name, @controller.session[:user_name]
|
198
|
+
end
|
199
|
+
|
200
|
+
should "say a user is logged in when it is on #user_logged_in?" do
|
201
|
+
user_id = 46
|
202
|
+
user_name = "Brad"
|
203
|
+
@controller.session[:user_id] = user_id
|
204
|
+
@controller.session[:user_name] = user_name
|
205
|
+
Person.expects(:find_by_id).with(user_id).returns(@user)
|
206
|
+
|
207
|
+
user_logged_in = @controller.user_logged_in?
|
208
|
+
|
209
|
+
assert user_logged_in
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ubiquitous_user
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "J. Pablo Fern\xC3\xA1ndez"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-26 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -70,4 +70,4 @@ specification_version: 3
|
|
70
70
|
summary: Helpers to get and retrieve users ubiquitously
|
71
71
|
test_files:
|
72
72
|
- test/helper.rb
|
73
|
-
- test/
|
73
|
+
- test/test_ubiquitous_user.rb
|