ubiquitous_user 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,8 +6,9 @@ accounts for many tasks, but you don't need those accounts to be any more than
6
6
  an id. No username, no password, no profile.
7
7
 
8
8
  This library is an implementation of that. You add the Usable mixin to your
9
- ApplicationController and after that call user to get a user, or user! to get a
10
- user that exists on the database (it'll be stored if needed).
9
+ ApplicationController and after that call user to get a user. When a new user
10
+ is saved, it'll automatically store the session[:user_id] in the controller to
11
+ mark this new user as the logged in user.
11
12
 
12
13
  When a user log ins what you have to do is set the user, which is just doing
13
14
  user = userObject.
@@ -99,10 +100,12 @@ ApplicationController, like this:
99
100
  #...
100
101
  end
101
102
 
102
- After that you can use user and user! anywhere, for example:
103
+ After that you can use user anywhere, for example:
104
+
105
+ @item.recommender = user
106
+
107
+ or
103
108
 
104
- @item.recommender = user!
105
-
106
109
  <%=h user.name %>
107
110
 
108
111
  You can use user= and authorize in the controllers, for example:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -1,3 +1,6 @@
1
+ # coding: utf-8
2
+ # Copyright 2010 J. Pablo Fernández
3
+
1
4
  class UbiquitousUserGenerator < Rails::Generator::Base
2
5
  def manifest
3
6
  record do |m|
@@ -30,26 +30,30 @@ module UsableHelpers
30
30
  # Helper method to get the current user. It will always return a user but the
31
31
  # user may not be in the database. If options[:create] is true, then the user
32
32
  # will be in the database (although it may be a ghost user).
33
- def user(options = {:save => false})
33
+ def user
34
34
  # Find the user in the database if session[:user_id] is defined and @ubiquitous_user is not.
35
35
  @ubiquitous_user = UsableConfig::user_model_class.find_by_id(session[:user_id]) if session[:user_id] != nil and @ubiquitous_user == nil
36
36
 
37
37
  # Create a new user object if @ubiquitous_user is not defined.
38
38
  @ubiquitous_user = UsableConfig::user_model_class.send(UsableConfig::user_model_new) if @ubiquitous_user == nil
39
39
 
40
- # If the object is new and we are asked to save, do it.
41
- if options[:save] and @ubiquitous_user.new_record?
42
- # Save the user in the database and set the session user_id for latter.
43
- @ubiquitous_user.send(UsableConfig::user_model_save)
44
- session[:user_id] = @ubiquitous_user.id
40
+ # If the object is new, let's get ready to mark the user as logged in when saving.
41
+ if @ubiquitous_user.new_record? or @ubiquitous_user.id != session[:user_id]
42
+ @ubiquitous_user.instance_variable_set :@ubiquitous_user_controller, self
43
+ # TODO: save a previous after_save and call it.
44
+ def @ubiquitous_user.after_save
45
+ @ubiquitous_user_controller.session[:user_id] = self.id
46
+ end
45
47
  end
46
48
 
47
49
  return @ubiquitous_user
48
50
  end
49
51
 
50
- # Helper method to get a user that for sure exists on the database.
52
+ # <b>DEPRECATED:</b> Please use <tt>user</tt> instead. Call
53
+ # <tt>user.save!</tt> if you really needed it saved.
51
54
  def user!
52
- return user(:save => true)
55
+ warn "[DEPRECATION] use 'user' instead, call 'user.save!' if you really needed it saved"
56
+ return user
53
57
  end
54
58
 
55
59
  # Helper method to check whether a user is logged in or not
@@ -14,6 +14,7 @@ class TestUbiquitousUser < Test::Unit::TestCase
14
14
  end
15
15
 
16
16
  should "create a new user object on Controller#user" do
17
+ @user.expects(:new_record?).returns(true)
17
18
  User.expects(:new).returns(@user)
18
19
 
19
20
  user = @controller.user
@@ -23,6 +24,7 @@ class TestUbiquitousUser < Test::Unit::TestCase
23
24
  end
24
25
 
25
26
  should "should return previous user object on Controller#user" do
27
+ @user.expects(:new_record?).returns(true)
26
28
  @controller.ubiquitous_user = @user
27
29
 
28
30
  user = @controller.user
@@ -33,6 +35,7 @@ class TestUbiquitousUser < Test::Unit::TestCase
33
35
  should "find a user on Controller#user if there's a user_id on session" do
34
36
  user_id = 42
35
37
  @controller.session[:user_id] = user_id
38
+ @user.expects(:new_record?).returns(true)
36
39
  User.expects(:find_by_id).with(user_id).returns(@user)
37
40
 
38
41
  user = @controller.user
@@ -41,31 +44,35 @@ class TestUbiquitousUser < Test::Unit::TestCase
41
44
  assert_equal @user, @controller.ubiquitous_user
42
45
  end
43
46
 
44
- should "save a new user when requested on Controller#user" do
47
+ should "set the session user_id when saving a user" do
45
48
  user_id = 43
46
49
  User.expects(:new).returns(@user)
47
50
  @user.expects(:new_record?).returns(true)
48
51
  @user.expects(:save!)
49
52
  @user.expects(:id).returns(user_id)
50
-
51
- user = @controller.user!
52
-
53
+
54
+ user = @controller.user
55
+ user.save!
56
+ # save! should be calling after_save, but it isn't because it's a mock, so
57
+ # let's call it manually
58
+ user.after_save
59
+
53
60
  assert_equal @user, user
54
61
  assert_equal @user, @controller.ubiquitous_user
55
62
  assert_equal user_id, @controller.session[:user_id]
56
63
  end
57
-
58
- should "not save an existing user even when requested on Controller#user" do
59
- user_id = 44
60
- @controller.session[:user_id] = user_id
61
- User.expects(:find_by_id).with(user_id).returns(@user)
62
- @user.expects(:new_record?).returns(false)
63
-
64
- user = @controller.user!
65
-
66
- assert_equal @user, user
67
- assert_equal @user, @controller.ubiquitous_user
68
- end
64
+ #
65
+ # should "not save an existing user even when requested on Controller#user" do
66
+ # user_id = 44
67
+ # @controller.session[:user_id] = user_id
68
+ # User.expects(:find_by_id).with(user_id).returns(@user)
69
+ # @user.expects(:new_record?).returns(false)
70
+ #
71
+ # user = @controller.user!
72
+ #
73
+ # assert_equal @user, user
74
+ # assert_equal @user, @controller.ubiquitous_user
75
+ # end
69
76
 
70
77
  should "set user on Controller#user=" do
71
78
  user_id = 45
@@ -171,21 +178,22 @@ class TestUbiquitousUser < Test::Unit::TestCase
171
178
  end
172
179
 
173
180
  should "create a new user object on #user" do
181
+ @user.expects(:new_record?).returns(true)
174
182
  Person.expects(:new_person).returns(@user)
175
183
  assert_equal @user, @controller.user
176
184
  assert_equal @user, @controller.ubiquitous_user
177
185
  end
178
186
 
179
- should "save a new user when requested on #user" do
180
- user_id = 43
181
- Person.expects(:new_person).returns(@user)
182
- @user.expects(:new_record?).returns(true)
183
- @user.expects(:save_person!)
184
- @user.expects(:id).returns(user_id)
185
- assert_equal @user, @controller.user!
186
- assert_equal @user, @controller.ubiquitous_user
187
- assert_equal user_id, @controller.session[:user_id]
188
- end
187
+ # should "save a new user when requested on #user" do
188
+ # user_id = 43
189
+ # Person.expects(:new_person).returns(@user)
190
+ # @user.expects(:new_record?).returns(true)
191
+ # @user.expects(:save_person!)
192
+ # @user.expects(:id).returns(user_id)
193
+ # assert_equal @user, @controller.user!
194
+ # assert_equal @user, @controller.ubiquitous_user
195
+ # assert_equal user_id, @controller.session[:user_id]
196
+ # end
189
197
 
190
198
  should "set user on Controller#user=" do
191
199
  user_id = 45
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 1
9
- version: 0.3.1
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "J. Pablo Fern\xC3\xA1ndez"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-07 00:00:00 +01:00
17
+ date: 2010-03-22 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency