ubiquitous_user 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +150 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/lib/ubiquitous_user.rb +52 -0
- data/test/helper.rb +13 -0
- data/test/test_ubiquitous-user.rb +7 -0
- metadata +74 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 J. Pablo Fernández
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
= ubiquitous_user
|
2
|
+
|
3
|
+
Many web applications required you to log in before being able to interact with
|
4
|
+
them; which poses a real barer of entry for new users. You need users to have
|
5
|
+
accounts for many tasks, but you don't need those accounts to be any more than
|
6
|
+
an id. No username, no password, no profile.
|
7
|
+
|
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).
|
11
|
+
|
12
|
+
When a user log ins what you have to do is set the user, which is just doing
|
13
|
+
user = userObject.
|
14
|
+
|
15
|
+
This library also provides an authorize method, to use as a filter for those
|
16
|
+
actions that really require a logged in user.
|
17
|
+
|
18
|
+
An important consideration is that users should be mergeable now. If you have
|
19
|
+
a user with an account, and that user comes back a month latter, he may start
|
20
|
+
to operate your site with the new ghost account and only log in latter on. At
|
21
|
+
that moment the ghost account and the real account will be merged, basically by
|
22
|
+
calling real_account.merge(ghost_account_id)
|
23
|
+
|
24
|
+
== Installation
|
25
|
+
|
26
|
+
This gem is provided through Gemcutter so you need to have gem configured to
|
27
|
+
pull gems from Gemcutter.
|
28
|
+
|
29
|
+
=== Enabling Gemcutter
|
30
|
+
|
31
|
+
A properly configured environment would be like this:
|
32
|
+
|
33
|
+
$ gem sources
|
34
|
+
*** CURRENT SOURCES ***
|
35
|
+
|
36
|
+
http://gemcutter.org
|
37
|
+
http://gems.rubyforge.org/
|
38
|
+
http://gems.github.com
|
39
|
+
|
40
|
+
If you don't have http://gemcutter.org in your sources then you need to add. I
|
41
|
+
know two ways to do. One is installing Gemcutter and running gem tumble:
|
42
|
+
|
43
|
+
$ sudo gem install gemcutter
|
44
|
+
$ gem tumble
|
45
|
+
|
46
|
+
Be careful that gem tumble will remove Gemcutter from your repositories if it's
|
47
|
+
already there.
|
48
|
+
|
49
|
+
The other way is by hand like this:
|
50
|
+
|
51
|
+
$ gem source -a http://gemcutter.org
|
52
|
+
|
53
|
+
I'm not sure if there's any difference. I think there isn't one.
|
54
|
+
|
55
|
+
=== Installing ubiquitous_user manually
|
56
|
+
|
57
|
+
It's simple a matter of running:
|
58
|
+
|
59
|
+
$ gem install ubiquitous_user
|
60
|
+
|
61
|
+
and that's it. Let me know if something breaks.
|
62
|
+
|
63
|
+
=== Installing through your Ruby on Rails project
|
64
|
+
|
65
|
+
In the +environment.rb+ file of your Ruby on Rails project you'll have some
|
66
|
+
commented out lines like this:
|
67
|
+
|
68
|
+
# config.gem "bj"
|
69
|
+
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
|
70
|
+
# config.gem "sqlite3-ruby", :lib => "sqlite3"
|
71
|
+
# config.gem "aws-s3", :lib => "aws/s3"
|
72
|
+
|
73
|
+
After those you can just add
|
74
|
+
|
75
|
+
config.gem "ubiquitous_user"
|
76
|
+
|
77
|
+
and then run
|
78
|
+
|
79
|
+
$ rake gems:install
|
80
|
+
|
81
|
+
and you'll get this and all the gems your Rails project need installed.
|
82
|
+
Configuring your Rails project like that is something you'll need anyway, so
|
83
|
+
this is my recommended way.
|
84
|
+
|
85
|
+
== How to use it
|
86
|
+
|
87
|
+
In your application_controller.rb be sure to add the mixin to
|
88
|
+
ApplicationController, like this:
|
89
|
+
|
90
|
+
class ApplicationController < ActionController::Base
|
91
|
+
include Usable
|
92
|
+
|
93
|
+
#...
|
94
|
+
end
|
95
|
+
|
96
|
+
After that you can use user and user! anywhere, for example:
|
97
|
+
|
98
|
+
@item.recommender = user!
|
99
|
+
|
100
|
+
<%=h user.name %>
|
101
|
+
|
102
|
+
You can use user= and authorize in the controllers, for example:
|
103
|
+
|
104
|
+
class SessionsController < ApplicationController
|
105
|
+
def destroy
|
106
|
+
self.user = nil
|
107
|
+
# ...
|
108
|
+
end
|
109
|
+
|
110
|
+
def create
|
111
|
+
# ...
|
112
|
+
self.user = user
|
113
|
+
end
|
114
|
+
|
115
|
+
# ...
|
116
|
+
end
|
117
|
+
|
118
|
+
class ProfilesController < ApplicationController
|
119
|
+
before_filter :authorize
|
120
|
+
# ...
|
121
|
+
end
|
122
|
+
|
123
|
+
== Customize
|
124
|
+
|
125
|
+
ubiquitous_user expects your user model to be named User. That's not
|
126
|
+
configurable yet. What is configurable is the two methods it calls on user. You
|
127
|
+
can do this kind of configuration in config/initializers/ubiquitous_user.rb for
|
128
|
+
example:
|
129
|
+
|
130
|
+
Usable::UserModelSave = :save_bypassing_non_essential_validation
|
131
|
+
Usable::UserModelName = :name_or_else
|
132
|
+
|
133
|
+
== API Documentation
|
134
|
+
|
135
|
+
Up to date api documentation should be automatically generated on
|
136
|
+
http://rdoc.info/projects/pupeno/ubiquitous_user
|
137
|
+
|
138
|
+
== Note on Patches/Pull Requests
|
139
|
+
|
140
|
+
* Fork the project.
|
141
|
+
* Make your feature addition or bug fix.
|
142
|
+
* Add tests for it. This is important so I don't break it in a
|
143
|
+
future version unintentionally.
|
144
|
+
* Commit, do not mess with rakefile, version, or history.
|
145
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
146
|
+
* Send me a pull request. Bonus points for topic branches.
|
147
|
+
|
148
|
+
== Copyright
|
149
|
+
|
150
|
+
Copyright (c) 2009 J. Pablo Fernández. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ubiquitous_user"
|
8
|
+
gem.summary = "Helpers to get and retrieve users ubiquitously"
|
9
|
+
gem.description = "Create accounts for users right away, even when they are anonymous."
|
10
|
+
gem.email = "pupeno@pupeno.com"
|
11
|
+
gem.homepage = "http://github.com/pupeno/ubiquitous_user"
|
12
|
+
gem.authors = ["J. Pablo Fernández"]
|
13
|
+
gem.add_dependency "actionpack", ">= 2.0.0"
|
14
|
+
#gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "Ubiquitous User #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('LICENSE')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module UsableHelpers
|
2
|
+
# Helper method to get the current user. It will always return a user but the
|
3
|
+
# user may not be in the database. If options[:create] is true, then the user
|
4
|
+
# will be in the database (although it may be a ghost user).
|
5
|
+
def user(options = {:create => false})
|
6
|
+
# If we already have a user object, return that.
|
7
|
+
return @ubiquitous_user if @ubiquitous_user != nil
|
8
|
+
|
9
|
+
# Try to find the user in the database if session[:user_id] is defined.
|
10
|
+
@ubiquitous_user = User.find(session[:user_id]) if session[:user_id] != nil
|
11
|
+
return @ubiquitous_user if @ubiquitous_user != nil
|
12
|
+
|
13
|
+
# Create a new user object.
|
14
|
+
@ubiquitous_user = User.new()
|
15
|
+
if options[:create]
|
16
|
+
# Save the user in the database and set the session user_id for latter.
|
17
|
+
# TODO use UsableConfig::UserModelSave
|
18
|
+
@ubiquitous_user.send(Usable::UserModelSave)
|
19
|
+
session[:user_id] = @ubiquitous_user.id
|
20
|
+
end
|
21
|
+
return @ubiquitous_user
|
22
|
+
end
|
23
|
+
|
24
|
+
# Helper method to get a user that for sure exists on the database.
|
25
|
+
def user!
|
26
|
+
return user(:create => true)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
ActionController::Base.class_eval do
|
31
|
+
helper UsableHelpers
|
32
|
+
end
|
33
|
+
|
34
|
+
module Usable
|
35
|
+
include UsableHelpers
|
36
|
+
|
37
|
+
UserModelSave = :save
|
38
|
+
UserModelName = :name
|
39
|
+
|
40
|
+
def user=(u)
|
41
|
+
session[:user_id] = u != nil ? u.id : nil
|
42
|
+
session[:user_name] = u != nil ? u.send(UserModelName) : nil
|
43
|
+
user
|
44
|
+
end
|
45
|
+
|
46
|
+
def authorize
|
47
|
+
unless User.find_by_id(session[:user_id]) and session[:user_name] != nil
|
48
|
+
flash[:notice] = "Please log in."
|
49
|
+
redirect_to new_session_url
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
require 'active_support'
|
6
|
+
require 'action_pack'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
|
+
require 'ubiquitous_user'
|
11
|
+
|
12
|
+
class Test::Unit::TestCase
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ubiquitous_user
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "J. Pablo Fern\xC3\xA1ndez"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-29 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.0.0
|
24
|
+
version:
|
25
|
+
description: Create accounts for users right away, even when they are anonymous.
|
26
|
+
email: pupeno@pupeno.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/ubiquitous_user.rb
|
42
|
+
- test/helper.rb
|
43
|
+
- test/test_ubiquitous-user.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/pupeno/ubiquitous_user
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.5
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Helpers to get and retrieve users ubiquitously
|
72
|
+
test_files:
|
73
|
+
- test/helper.rb
|
74
|
+
- test/test_ubiquitous-user.rb
|