ubiquitous_user 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## NetBeans
5
+ nbproject
6
+
7
+ ## RubyMine
8
+ .idea
9
+
10
+ ## TEXTMATE
11
+ *.tmproj
12
+ tmtags
13
+
14
+ ## EMACS
15
+ *~
16
+ \#*
17
+ .\#*
18
+
19
+ ## VIM
20
+ *.swp
21
+
22
+ ## PROJECT::GENERAL
23
+ coverage
24
+ rdoc
25
+ pkg
26
+ .bundle
27
+ tmp
28
+ Gemfile.lock
29
+
30
+ ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright © 2011, José Pablo Fernández
3
+
4
+ source "http://rubygems.org"
5
+
6
+ # Install the dependencies defined on ubiquitous_user.gemspec
7
+ gemspec
@@ -0,0 +1,117 @@
1
+ Ubiquitous User
2
+ ===============
3
+
4
+ Many web applications required you to log in before being able to interact with
5
+ them; which poses a real barer of entry for new users. You need users to have
6
+ accounts for many tasks, but you don't need those accounts to be any more than
7
+ an id. No username, no password, no profile.
8
+
9
+ This library is an implementation of that. You add the UbiquitousUser::Usable
10
+ mixin to your ApplicationController and after that call user to get a
11
+ current_user. When a new user is saved, it'll automatically store the id in the
12
+ session[:user_id] in the controller to mark this new user as the logged in user.
13
+
14
+ When a user logs in what you have to do is set the user, which is just doing
15
+
16
+ current_user = userObject
17
+
18
+ The user model and how to authenticate is your responsibility; ubiquity_user
19
+ doesn't try solve those problem.
20
+
21
+ Since people just accessing your web site will have a user, people that is
22
+ already registered at your web site may have an anonymous user with activity in
23
+ it. You should try to merge it.
24
+
25
+ ubiquity_user is designed for and tested in Rails 3.X. It wight work on Rails
26
+ 2.X but it might also require some fixes (which might be welcome). It also
27
+ works fine with omni_auth.
28
+
29
+
30
+ Where?
31
+ ------
32
+
33
+ The canonical places for this gem are:
34
+
35
+ * http://github.com/pupeno/ubiquitous_user
36
+ * http://rubygems.org/gems/ubiquitous_user
37
+ * http://rdoc.info/projects/pupeno/ubiquitous_user
38
+
39
+
40
+ How to use it
41
+ -------------
42
+
43
+ In your application_controller.rb be sure to add the mixin to
44
+ ApplicationController, like this:
45
+
46
+ class ApplicationController < ActionController::Base
47
+ include UbiquitousUser::Usable
48
+
49
+ #...
50
+ end
51
+
52
+ After that you can use user anywhere, for example:
53
+
54
+ @item.recommender = current_user
55
+
56
+ or
57
+
58
+ <%=h current_user.name %>
59
+
60
+ You can use current_user= in the controllers, for example:
61
+
62
+ class SessionsController < ApplicationController
63
+ def destroy
64
+ self.current_user = nil
65
+ # ...
66
+ end
67
+
68
+ def create
69
+ # ...
70
+ self.current_user = user
71
+ end
72
+
73
+ # ...
74
+ end
75
+
76
+
77
+ The model
78
+ ---------
79
+
80
+ Ubiquitous User expects you to have a model for your users called User
81
+ (configurable). You could create such a model with the following command:
82
+
83
+ rails generate model User
84
+
85
+
86
+ Configuration
87
+ -------------
88
+
89
+ If your user model is not called User or the method to create a new one isn't
90
+ :new, then you can configure Ubiquity User to work with the alternatives:
91
+
92
+ UbiquitousUser::Config::user_model = :User
93
+ UbiquitousUser::Config::user_model_new = :new
94
+
95
+
96
+ API Documentation
97
+ -----------------
98
+
99
+ Up to date api documentation should be automatically generated on
100
+ http://rdoc.info/projects/pupeno/ubiquitous_user
101
+
102
+
103
+ Note on patches and pull requests
104
+ ---------------------------------
105
+
106
+ * Fork the project.
107
+ * Make your feature addition or bug fix.
108
+ * Add tests for it. This is important so I don't break it in a
109
+ future version unintentionally.
110
+ * Commit, do not mess with rakefile, version, or history.
111
+ (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)
112
+ * Send me a pull request. Bonus points for topic branches.
113
+
114
+ Copyright
115
+ ---------
116
+
117
+ Copyright (c) 2009, 2010, 2011 José Pablo Fernández. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,59 +1,32 @@
1
- # coding: utf-8
2
- # Copyright 2010 J. Pablo Fernández
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright © 2011, José Pablo Fernández
3
3
 
4
- require 'rubygems'
5
- require 'rake'
4
+ $:.unshift File.expand_path("../lib", __FILE__)
5
+ require "ubiquitous_user/version"
6
6
 
7
- begin
8
- require 'jeweler'
9
- Jeweler::Tasks.new do |gem|
10
- gem.name = "ubiquitous_user"
11
- gem.summary = "Helpers to get and retrieve users ubiquitously"
12
- gem.description = "Create accounts for users right away, even when they are anonymous."
13
- gem.email = "pupeno@pupeno.com"
14
- gem.homepage = "http://github.com/pupeno/ubiquitous_user"
15
- gem.authors = ["J. Pablo Fernández"]
16
- gem.add_dependency "actionpack", ">= 2.0.0"
17
- gem.files = %w(LICENSE README.rdoc Rakefile VERSION) + Dir.glob("{lib,generators}/**/*")
18
- #gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
19
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
20
- end
21
- Jeweler::GemcutterTasks.new
22
- rescue LoadError
23
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
24
- end
7
+ require "bundler"
8
+ Bundler::GemHelper.install_tasks
25
9
 
26
- require 'rake/testtask'
10
+ require "rake/testtask"
27
11
  Rake::TestTask.new(:test) do |test|
28
- test.libs << 'lib' << 'test'
29
- test.pattern = 'test/**/test_*.rb'
12
+ test.libs << "lib" << "test"
13
+ test.pattern = "test/**/test_*.rb"
30
14
  test.verbose = true
31
15
  end
16
+ task :default => :test
32
17
 
33
- begin
34
- require 'rcov/rcovtask'
35
- Rcov::RcovTask.new do |test|
36
- test.libs << 'test'
37
- test.pattern = 'test/**/test_*.rb'
38
- test.verbose = true
39
- end
40
- rescue LoadError
41
- task :rcov do
42
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
43
- end
18
+ require "rcov/rcovtask"
19
+ Rcov::RcovTask.new do |test|
20
+ test.libs << "test"
21
+ test.pattern = "test/**/test_*.rb"
22
+ test.verbose = true
44
23
  end
45
24
 
46
- task :test => :check_dependencies
47
-
48
- task :default => :test
49
-
50
- require 'rake/rdoctask'
25
+ require "rake/rdoctask"
51
26
  Rake::RDocTask.new do |rdoc|
52
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
53
-
54
- rdoc.rdoc_dir = 'rdoc'
55
- rdoc.title = "Ubiquitous User #{version}"
56
- rdoc.rdoc_files.include('README*')
57
- rdoc.rdoc_files.include('LICENSE')
58
- rdoc.rdoc_files.include('lib/**/*.rb')
27
+ rdoc.rdoc_dir = "rdoc"
28
+ rdoc.title = "Ubiquitous User #{UbiquitousUser::VERSION}"
29
+ rdoc.rdoc_files.include("README*")
30
+ rdoc.rdoc_files.include("LICENSE")
31
+ rdoc.rdoc_files.include("lib/**/*.rb")
59
32
  end
@@ -1,6 +1,4 @@
1
1
  # Configure ubiquitous user. For more information see http://github.com/pupeno/ubiquitous_user
2
2
 
3
- # UsableConfig::user_model = :User
4
- # UsableConfig::user_model_new = :new
5
- # UsableConfig::user_model_save = :save
6
- # UsableConfig::user_model_name = :name
3
+ # UbiquitousUser::Config::user_model = :User
4
+ # UbiquitousUser::Config::user_model_new = :new
@@ -1,12 +1,12 @@
1
- # coding: utf-8
2
- # Copyright 2010 J. Pablo Fernández
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright © 2011, José Pablo Fernández
3
3
 
4
4
  class UbiquitousUserGenerator < Rails::Generator::Base
5
5
  def manifest
6
6
  record do |m|
7
7
  m.file 'initializer.rb', 'config/initializers/ubiquitous_user.rb'
8
-
8
+
9
9
  m.readme 'INSTALL'
10
10
  end
11
11
  end
12
- end
12
+ end
@@ -1,101 +1,6 @@
1
- # coding: utf-8
2
- # Copyright 2010 J. Pablo Fernández
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright © 2011, José Pablo Fernández
3
3
 
4
- module UsableConfig
5
- @user_model = :User
6
- @user_model_new = :new
7
- @user_model_save = :save!
8
- @user_model_name = :name
9
-
10
- # Class that defines the user model.
11
- attr_accessor :user_model
12
- module_function :user_model, :user_model=
13
- # Method used to create a new user, of class user_model
14
- attr_accessor :user_model_new
15
- module_function :user_model_new, :user_model_new=
16
- # Method used to save the user.
17
- attr_accessor :user_model_save
18
- module_function :user_model_save, :user_model_save=
19
- # Method used to get the name of the user.
20
- attr_accessor :user_model_name
21
- module_function :user_model_name, :user_model_name=
22
-
23
- def user_model_class # :nodoc:
24
- Object.const_get(user_model)
25
- end
26
- module_function :user_model_class
27
- end
28
-
29
- module UsableHelpers
30
- # Helper method to get the current user. It will always return a user but the
31
- # user may not be in the database. If options[:create] is true, then the user
32
- # will be in the database (although it may be a ghost user).
33
- def user
34
- # Find the user in the database if session[:user_id] is defined and @ubiquitous_user is not.
35
- @ubiquitous_user = UsableConfig::user_model_class.find_by_id(session[:user_id]) if session[:user_id] != nil and @ubiquitous_user == nil
36
-
37
- # Create a new user object if @ubiquitous_user is not defined.
38
- @ubiquitous_user = UsableConfig::user_model_class.send(UsableConfig::user_model_new) if @ubiquitous_user == nil
39
-
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
- controller = self
43
- # Read more about this technique on http://stackoverflow.com/questions/2495550/define-a-method-that-is-a-closure-in-ruby
44
- klass = class << @ubiquitous_user; self; end
45
- klass.send(:define_method, :after_save) do
46
- super
47
- controller.session[:user_id] = self.id
48
- end
49
- end
50
-
51
- return @ubiquitous_user
52
- end
53
-
54
- # <b>DEPRECATED:</b> Please use <tt>user</tt> instead. Call
55
- # <tt>user.save!</tt> if you really needed it saved.
56
- def user!
57
- warn "[DEPRECATION] use 'user' instead, call 'user.save!' if you really needed it saved"
58
- return user
59
- end
60
-
61
- # Helper method to check whether a user is logged in or not
62
- def user_logged_in?
63
- user_id = session[:user_id]
64
- user_id != nil and UsableConfig::user_model_class.find_by_id(user_id) != nil and session[:user_name] != nil
65
- end
66
- end
67
-
68
- ActionController::Base.class_eval do
69
- helper UsableHelpers
70
- end
71
-
72
- module UsableClass
73
- def authorize(message = "Please log in.", key = :warning)
74
- Proc.new do |controller|
75
- unless controller.user_logged_in?
76
- # flash, redirect_to and new_session_url are protected. Thank god this is Ruby, not Java.
77
- controller.send(:flash)[key] = message
78
- begin
79
- controller.send(:redirect_to, :back)
80
- rescue ActionController::RedirectBackError
81
- controller.send(:redirect_to, controller.send(:new_session_url))
82
- end
83
- end
84
- end
85
- end
86
- module_function :authorize
87
- end
88
-
89
- module Usable
90
- include UsableHelpers
91
-
92
- def user=(new_user)
93
- session[:user_id] = new_user != nil ? new_user.id : nil
94
- session[:user_name] = new_user != nil ? new_user.send(UsableConfig::user_model_name) : nil
95
- @ubiquitous_user = new_user
96
- end
97
-
98
- def authorize
99
- ::UsableClass.authorize().call(self)
100
- end
101
- end
4
+ require "ubiquitous_user/config"
5
+ require "ubiquitous_user/ubiquitous_user"
6
+ require "ubiquitous_user/version"
@@ -0,0 +1,21 @@
1
+ # Copyright © 2011, José Pablo Fernández
2
+
3
+ module UbiquitousUser
4
+ module Config
5
+ @user_model = :User
6
+ @user_model_new = :new
7
+
8
+ # Class that defines the user model.
9
+ attr_accessor :user_model
10
+ module_function :user_model, :user_model=
11
+ # Method used to create a new user, of class user_model
12
+ attr_accessor :user_model_new
13
+ module_function :user_model_new, :user_model_new=
14
+
15
+ def user_model_class # :nodoc:
16
+ Object.const_get(user_model)
17
+ end
18
+
19
+ module_function :user_model_class
20
+ end
21
+ end
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright © 2011, José Pablo Fernández
3
+
4
+ module UbiquitousUser
5
+ module Helpers
6
+ # Helper method to get the current user. It will always return a user but the
7
+ # user may not be in the database. If options[:create] is true, then the user
8
+ # will be in the database (although it may be a ghost user).
9
+ def current_user
10
+ # Find the user in the database if session[:user_id] is defined and @ubiquitous_user is not.
11
+ @ubiquitous_user = UbiquitousUser::Config::user_model_class.find_by_id(session[:user_id]) if session[:user_id] != nil and @ubiquitous_user == nil
12
+
13
+ # Create a new user object if @ubiquitous_user is not defined.
14
+ @ubiquitous_user = UbiquitousUser::Config::user_model_class.send(UbiquitousUser::Config::user_model_new) if @ubiquitous_user == nil
15
+
16
+ # If the object is new, let's get ready to mark the user as logged in when saving.
17
+ if @ubiquitous_user.new_record? or @ubiquitous_user.id != session[:user_id]
18
+ if !@ubiquitous_user.respond_to? :mark_user_as_logged_in_in_the_session
19
+ UbiquitousUser::Config::user_model_class.class_eval do
20
+ after_save :mark_user_as_logged_in_in_the_session
21
+
22
+ def mark_user_as_logged_in_in_the_session
23
+ if !@session_reference_by_ubiquitous_user.nil?
24
+ @session_reference_by_ubiquitous_user[:user_id] = id
25
+ end
26
+ end
27
+ end
28
+ end
29
+ @ubiquitous_user.instance_variable_set "@session_reference_by_ubiquitous_user", self.session
30
+ end
31
+
32
+ return @ubiquitous_user
33
+ end
34
+ end
35
+
36
+ # TODO: should this really be top level?
37
+ ::ActionController::Base.class_eval do
38
+ helper UbiquitousUser::Helpers
39
+ end
40
+
41
+ module Usable
42
+ include UbiquitousUser::Helpers
43
+
44
+ def current_user=(new_user)
45
+ session[:user_id] = new_user != nil ? new_user.id : nil
46
+ @ubiquitous_user = new_user
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright © 2011, José Pablo Fernández
3
+
4
+ module UbiquitousUser
5
+ VERSION = "0.5.0" unless defined?(::UbiquitousUser::VERSION)
6
+ end
@@ -1,50 +1,84 @@
1
- # coding: utf-8
2
- # Copyright 2010 J. Pablo Fernández
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright © 2011, José Pablo Fernández
3
3
 
4
- require 'rubygems'
5
- require 'test/unit'
6
- require 'shoulda'
7
- require 'mocha'
8
-
9
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
- $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ require "rubygems"
5
+ require "test/unit"
6
+ require "shoulda"
7
+ require "mocha"
11
8
 
12
9
  # Minimum set ActionController required to test ubiquitous_user
13
10
  module ActionController
14
11
  class Base
15
12
  @@helpers = []
16
-
13
+
17
14
  def self.helper(h)
18
15
  @@helpers << h
19
16
  end
20
17
  end
21
-
18
+
22
19
  class RedirectBackError < Exception
23
20
  end
24
21
  end
25
22
 
23
+ class Model
24
+ attr_accessor :id
25
+ @@after_save_methods = []
26
+
27
+ def self.after_save(method)
28
+ @@after_save_methods << method
29
+ end
30
+
31
+ def self.find_by_id(user_id)
32
+ user = User.new
33
+ user.id = user_id
34
+ return user
35
+ end
36
+
37
+ def save
38
+ self.id = object_id if id.nil?
39
+ @@after_save_methods.each do |m|
40
+ send(m)
41
+ end
42
+ end
43
+
44
+ def save!
45
+ save
46
+ end
47
+
48
+ def new_record?
49
+ id.nil?
50
+ end
51
+ end
52
+
26
53
  # Default user model.
27
- class User
54
+ class User < Model
28
55
  end
29
56
 
30
57
  # An alternative user model.
31
- class Person
58
+ class Person < Model
59
+ class <<self
60
+ alias_method :new_person, :new
61
+
62
+ def new
63
+ raise "This method shouldn't ever be called."
64
+ end
65
+ end
32
66
  end
33
67
 
34
- require 'ubiquitous_user'
68
+ require "ubiquitous_user"
35
69
 
36
70
  class Controller
37
- include Usable
38
- extend UsableClass
39
-
71
+ include UbiquitousUser::Usable
72
+
40
73
  # Simulate session and flash
41
74
  def initialize
42
75
  @session = {}
43
76
  @flash = {}
44
77
  end
78
+
45
79
  attr_accessor :session
46
80
  attr_accessor :flash
47
-
81
+
48
82
  # Allow access to @ubiquitous_user, only for testing purposes.
49
83
  attr_accessor :ubiquitous_user
50
- end
84
+ end
@@ -1,224 +1,87 @@
1
- # coding: utf-8
2
- # Copyright 2010 J. Pablo Fernández
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright © 2011, José Pablo Fernández
3
3
 
4
- require 'helper'
4
+ require "helper"
5
5
 
6
6
  class TestUbiquitousUser < Test::Unit::TestCase
7
- context "A controller and a mock user" do
7
+ context "A controller" do
8
8
  setup do
9
9
  @controller = Controller.new
10
- @user = mock("User")
11
-
12
10
  # Just to be sure we are starting from scratch
13
11
  assert_nil @controller.ubiquitous_user
12
+ assert_nil @controller.session[:user_id]
14
13
  end
15
-
16
- should "create a new user object on Controller#user" do
17
- @user.expects(:new_record?).returns(true)
18
- User.expects(:new).returns(@user)
19
-
20
- user = @controller.user
21
-
22
- assert_equal @user, user
23
- assert_equal @user, @controller.ubiquitous_user
24
- end
25
-
26
- should "should return previous user object on Controller#user" do
27
- @user.expects(:new_record?).returns(true)
28
- @controller.ubiquitous_user = @user
29
-
30
- user = @controller.user
31
-
32
- assert_equal @user, user
14
+
15
+ should "create a new user object on current_user" do
16
+ user = @controller.current_user
17
+ assert_equal User, user.class
18
+ assert_equal @controller.ubiquitous_user, user
19
+ assert user.new_record?
33
20
  end
34
-
35
- should "find a user on Controller#user if there's a user_id on session" do
36
- user_id = 42
37
- @controller.session[:user_id] = user_id
38
- @user.expects(:new_record?).returns(true)
39
- User.expects(:find_by_id).with(user_id).returns(@user)
40
-
41
- user = @controller.user
42
-
43
- assert_equal @user, user
44
- assert_equal @user, @controller.ubiquitous_user
21
+
22
+ should "find a user on current_user if there's a user_id on session" do
23
+ @controller.session[:user_id] = 42
24
+ user = @controller.current_user
25
+ assert_equal @controller.session[:user_id], user.id
45
26
  end
46
-
47
- should "set the session user_id when saving a user" do
48
- user_id = 43
49
- User.expects(:new).returns(@user)
50
- @user.expects(:new_record?).returns(true)
51
- @user.expects(:save!)
52
- @user.expects(:id).returns(user_id)
53
- @user.expects(:after_save)
54
27
 
55
- user = @controller.user
28
+ should "set the session user_id when saving a user" do
29
+ user = @controller.current_user
56
30
  user.save!
57
- # save! should be calling after_save, but it isn't because it's a mock, so
58
- # let's call it manually
59
- user.after_save
60
-
61
- assert_equal @user, user
62
- assert_equal @user, @controller.ubiquitous_user
63
- assert_equal user_id, @controller.session[:user_id]
64
- end
65
- #
66
- # should "not save an existing user even when requested on Controller#user" do
67
- # user_id = 44
68
- # @controller.session[:user_id] = user_id
69
- # User.expects(:find_by_id).with(user_id).returns(@user)
70
- # @user.expects(:new_record?).returns(false)
71
- #
72
- # user = @controller.user!
73
- #
74
- # assert_equal @user, user
75
- # assert_equal @user, @controller.ubiquitous_user
76
- # end
77
-
78
- should "set user on Controller#user=" do
79
- user_id = 45
80
- user_name = "Alex"
81
- @user.expects(:id).returns(user_id)
82
- @user.expects(:name).returns(user_name)
83
-
84
- @controller.user = @user
85
-
86
- assert_equal @user, @controller.ubiquitous_user
87
- assert_equal user_id, @controller.session[:user_id]
88
- assert_equal user_name, @controller.session[:user_name]
31
+ assert_not_nil user.id
32
+ assert_not_nil @controller.session[:user_id]
33
+ assert_equal user.id, @controller.session[:user_id]
89
34
  end
90
-
91
- should "unset user on Controller#user=(nil)" do
92
- @controller.user = nil
93
-
35
+
36
+ should "unset user on current_user=(nil)" do
37
+ @controller.current_user = nil
38
+
94
39
  assert_equal nil, @controller.ubiquitous_user
95
40
  assert_equal nil, @controller.session[:user_id]
96
- assert_equal nil, @controller.session[:user_name]
97
- end
98
-
99
- should "say no user is logged in when none is on Controller#user_logged_in?" do
100
- user_logged_in = @controller.user_logged_in?
101
-
102
- assert !user_logged_in
103
- end
104
-
105
- should "say no user is logged in when an anonymously registered user is in on Controller#user_logged_in?" do
106
- user_id = 46
107
- @controller.session[:user_id] = user_id
108
- User.expects(:find_by_id).with(user_id).returns(@user)
109
-
110
- user_logged_in = @controller.user_logged_in?
111
-
112
- assert !user_logged_in
113
- end
114
-
115
- should "say a user is logged in when it is on Controller#user_logged_in?" do
116
- user_id = 46
117
- user_name = "Brad"
118
- @controller.session[:user_id] = user_id
119
- @controller.session[:user_name] = user_name
120
- User.expects(:find_by_id).with(user_id).returns(@user)
121
-
122
- user_logged_in = @controller.user_logged_in?
123
-
124
- assert user_logged_in
125
- end
126
-
127
- should "redirect back with a flash message when user not logged in on Controller.authorize" do
128
- msg = "Log in you user!"
129
- key = :error
130
-
131
- authorize = Controller.send(:authorize, msg, key)
132
-
133
- assert authorize.instance_of? Proc
134
-
135
- @controller.expects(:redirect_to).with(:back)
136
-
137
- authorize.call(@controller)
138
- assert_equal msg, @controller.flash[key]
139
- end
140
-
141
- should "redirect to new_session_url with a flash message when user not logged in and there's no :back on Controller.authorize" do
142
- msg = "Log in you user!"
143
- key = :error
144
-
145
- authorize = Controller.send(:authorize, msg, key)
146
-
147
- assert authorize.instance_of? Proc
148
-
149
- new_session_url = "/login"
150
- @controller.expects(:redirect_to).with(:back).raises(ActionController::RedirectBackError)
151
- @controller.expects(:new_session_url).returns(new_session_url)
152
- @controller.expects(:redirect_to).with(new_session_url)
153
-
154
- authorize.call(@controller)
155
- assert_equal msg, @controller.flash[key]
156
41
  end
157
-
158
- should "redirect back with a flash message when user not logged in on Controller#authorize" do
159
- @controller.expects(:redirect_to).with(:back)
160
-
161
- @controller.authorize
162
- assert_equal "Please log in.", @controller.flash[:warning]
163
- end
164
-
165
- context "with custom usable config" do
42
+
43
+ context "and a user" do
166
44
  setup do
167
- @orig_config = UsableConfig.clone
168
- UsableConfig.user_model = :Person
169
- UsableConfig.user_model_new = :new_person
170
- UsableConfig.user_model_save = :save_person!
171
- UsableConfig.user_model_name = :full_name
45
+ @user = User.new
46
+ assert_nil @user.id
172
47
  end
173
-
174
- teardown do
175
- UsableConfig.user_model = @orig_config.user_model
176
- UsableConfig.user_model_new = @orig_config.user_model_new
177
- UsableConfig.user_model_save = @orig_config.user_model_save
178
- UsableConfig.user_model_name = @orig_config.user_model_name
48
+
49
+ should "should return previous user object on current_user" do
50
+ @controller.ubiquitous_user = @user
51
+ assert_equal @user, @controller.current_user
52
+ assert_nil @controller.session[:user_id]
53
+ end
54
+
55
+ context "that is saved" do
56
+ setup do
57
+ @user.save!
58
+ assert_not_nil @user.id
59
+ end
60
+
61
+ should "set user on current_user=" do
62
+ @controller.current_user = @user
63
+
64
+ assert_equal @user, @controller.ubiquitous_user
65
+ assert_equal @user.id, @controller.session[:user_id]
66
+ end
179
67
  end
180
-
181
- should "create a new user object on #user" do
182
- @user.expects(:new_record?).returns(true)
183
- Person.expects(:new_person).returns(@user)
184
- assert_equal @user, @controller.user
185
- assert_equal @user, @controller.ubiquitous_user
68
+ end
69
+
70
+ context "with custom config" do
71
+ setup do
72
+ @orig_config = UbiquitousUser::Config.clone
73
+ UbiquitousUser::Config.user_model = :Person
74
+ UbiquitousUser::Config.user_model_new = :new_person
186
75
  end
187
-
188
- # should "save a new user when requested on #user" do
189
- # user_id = 43
190
- # Person.expects(:new_person).returns(@user)
191
- # @user.expects(:new_record?).returns(true)
192
- # @user.expects(:save_person!)
193
- # @user.expects(:id).returns(user_id)
194
- # assert_equal @user, @controller.user!
195
- # assert_equal @user, @controller.ubiquitous_user
196
- # assert_equal user_id, @controller.session[:user_id]
197
- # end
198
-
199
- should "set user on Controller#user=" do
200
- user_id = 45
201
- user_name = "Alex"
202
- @user.expects(:id).returns(user_id)
203
- @user.expects(:full_name).returns(user_name)
204
-
205
- @controller.user = @user
206
-
207
- assert_equal @user, @controller.ubiquitous_user
208
- assert_equal user_id, @controller.session[:user_id]
209
- assert_equal user_name, @controller.session[:user_name]
76
+
77
+ teardown do
78
+ UbiquitousUser::Config.user_model = @orig_config.user_model
79
+ UbiquitousUser::Config.user_model_new = @orig_config.user_model_new
210
80
  end
211
-
212
- should "say a user is logged in when it is on #user_logged_in?" do
213
- user_id = 46
214
- user_name = "Brad"
215
- @controller.session[:user_id] = user_id
216
- @controller.session[:user_name] = user_name
217
- Person.expects(:find_by_id).with(user_id).returns(@user)
218
-
219
- user_logged_in = @controller.user_logged_in?
220
-
221
- assert user_logged_in
81
+
82
+ should "create a new user object on #current_user" do
83
+ person = @controller.current_user
84
+ assert_equal Person, person.class
222
85
  end
223
86
  end
224
87
  end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # Copyright © 2011, José Pablo Fernández
3
+
4
+ $:.unshift File.expand_path("../lib", __FILE__)
5
+ require "ubiquitous_user/version"
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "ubiquitous_user"
9
+ s.version = UbiquitousUser::VERSION
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = ["J. Pablo Fernández"]
12
+ s.email = ["pupeno@pupeno.com"]
13
+ s.homepage = "http://github.com/pupeno/ubiquitous_user"
14
+ s.summary = "Helpers to get and retrieve users ubiquitously"
15
+ s.description = "Create accounts for users right away, even when they are anonymous."
16
+
17
+ s.required_rubygems_version = ">= 1.3.6"
18
+ s.rubyforge_project = "ubiquitous_user"
19
+
20
+ s.add_development_dependency "shoulda"
21
+ s.add_development_dependency "rcov"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test}/*`.split("\n")
25
+
26
+ #s.rdoc_options = ["--charset=UTF-8"]
27
+ end
28
+
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ubiquitous_user
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 11
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
- - 4
8
- - 1
9
- version: 0.4.1
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - "J. Pablo Fern\xC3\xA1ndez"
@@ -14,71 +15,97 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-03-23 00:00:00 +01:00
18
+ date: 2011-04-23 00:00:00 +02:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
- name: actionpack
22
+ name: shoulda
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
- - 2
29
- - 0
30
31
  - 0
31
- version: 2.0.0
32
- type: :runtime
32
+ version: "0"
33
+ type: :development
33
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rcov
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
34
49
  description: Create accounts for users right away, even when they are anonymous.
35
- email: pupeno@pupeno.com
50
+ email:
51
+ - pupeno@pupeno.com
36
52
  executables: []
37
53
 
38
54
  extensions: []
39
55
 
40
- extra_rdoc_files:
41
- - LICENSE
42
- - README.rdoc
56
+ extra_rdoc_files: []
57
+
43
58
  files:
59
+ - .gitignore
60
+ - Gemfile
44
61
  - LICENSE
45
- - README.rdoc
62
+ - README.md
46
63
  - Rakefile
47
- - VERSION
48
64
  - generators/ubiquitous_user/templates/INSTALL
49
65
  - generators/ubiquitous_user/templates/initializer.rb
50
66
  - generators/ubiquitous_user/ubiquitous_user_generator.rb
51
67
  - lib/ubiquitous_user.rb
68
+ - lib/ubiquitous_user/config.rb
69
+ - lib/ubiquitous_user/ubiquitous_user.rb
70
+ - lib/ubiquitous_user/version.rb
71
+ - test/helper.rb
72
+ - test/test_ubiquitous_user.rb
73
+ - ubiquitous_user.gemspec
52
74
  has_rdoc: true
53
75
  homepage: http://github.com/pupeno/ubiquitous_user
54
76
  licenses: []
55
77
 
56
78
  post_install_message:
57
- rdoc_options:
58
- - --charset=UTF-8
79
+ rdoc_options: []
80
+
59
81
  require_paths:
60
82
  - lib
61
83
  required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
62
85
  requirements:
63
86
  - - ">="
64
87
  - !ruby/object:Gem::Version
88
+ hash: 3
65
89
  segments:
66
90
  - 0
67
91
  version: "0"
68
92
  required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
69
94
  requirements:
70
95
  - - ">="
71
96
  - !ruby/object:Gem::Version
97
+ hash: 23
72
98
  segments:
73
- - 0
74
- version: "0"
99
+ - 1
100
+ - 3
101
+ - 6
102
+ version: 1.3.6
75
103
  requirements: []
76
104
 
77
- rubyforge_project:
78
- rubygems_version: 1.3.6
105
+ rubyforge_project: ubiquitous_user
106
+ rubygems_version: 1.5.2
79
107
  signing_key:
80
108
  specification_version: 3
81
109
  summary: Helpers to get and retrieve users ubiquitously
82
- test_files:
83
- - test/helper.rb
84
- - test/test_ubiquitous_user.rb
110
+ test_files: []
111
+
@@ -1,195 +0,0 @@
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. 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.
12
-
13
- When a user log ins what you have to do is set the user, which is just doing
14
- user = userObject.
15
-
16
- This library also provides an authorize method, to use as a filter for those
17
- actions that really require a logged in user.
18
-
19
- The user model and how to authenticate is your responsibility; ubiquity_user
20
- doesn't try solve those problem (not yet at least). I may recommend
21
- {rails_openid}[http://github.com/pupeno/rails_openid] for authentication, but
22
- maybe it's only because I wrote it.
23
-
24
- Since people just accessing your web site will have a user, people that is
25
- already registered at your web site may have an anonymous user with activity in
26
- it. You should try to merge it.
27
-
28
- == Installation
29
-
30
- This gem is provided through RubyGems.org so you need to have gem configured to
31
- pull gems from RubyGems.org.
32
-
33
- === Enabling RubyGems.org (formerly known as GemCutter)
34
-
35
- You can skip this if you have RubyGems.org enabled (which is going to be the
36
- default in the future anyway). A properly configured environment should list
37
- rubygems.org or gemcutter.org in the gem sources, like:
38
-
39
- $ gem sources
40
- *** CURRENT SOURCES ***
41
-
42
- http://rubygems.org/
43
-
44
- If you don't have it, you can probably add it this way:
45
-
46
- $ gem source -a http://rubygems.org/
47
-
48
- === Installing ubiquitous_user manually
49
-
50
- It's simple a matter of running:
51
-
52
- $ gem install ubiquitous_user
53
-
54
- and that's it. Let me know if something breaks.
55
-
56
- === Installing through your Ruby on Rails 3 project
57
-
58
- In the +Gemfile+ file of your Ruby on Rails project you'll need to add:
59
-
60
- gem "ubiquitous_user"
61
-
62
- after that run
63
-
64
- bundle install
65
-
66
- and watch the magic happen, all required gems will be installed. Configuring
67
- your Rails project like that is something you'll need anyway, so this is my
68
- recommended way.
69
-
70
- === Installing through your Ruby on Rails 2.3 project
71
-
72
- In the +environment.rb+ file of your Ruby on Rails project you'll have some
73
- commented out lines like this:
74
-
75
- # config.gem "bj"
76
- # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
77
- # config.gem "sqlite3-ruby", :lib => "sqlite3"
78
- # config.gem "aws-s3", :lib => "aws/s3"
79
-
80
- After those you can just add
81
-
82
- config.gem "ubiquitous_user"
83
-
84
- and then run
85
-
86
- $ rake gems:install
87
-
88
- and you'll get this and all the gems your Rails project need installed.
89
- Configuring your Rails project like that is something you'll need anyway, so
90
- this is my recommended way.
91
-
92
- == How to use it
93
-
94
- In your application_controller.rb be sure to add the mixin to
95
- ApplicationController, like this:
96
-
97
- class ApplicationController < ActionController::Base
98
- include Usable
99
-
100
- #...
101
- end
102
-
103
- After that you can use user anywhere, for example:
104
-
105
- @item.recommender = user
106
-
107
- or
108
-
109
- <%=h user.name %>
110
-
111
- You can use user= and authorize in the controllers, for example:
112
-
113
- class SessionsController < ApplicationController
114
- def destroy
115
- self.user = nil
116
- # ...
117
- end
118
-
119
- def create
120
- # ...
121
- self.user = user
122
- end
123
-
124
- # ...
125
- end
126
-
127
- class ProfilesController < ApplicationController
128
- before_filter :authorize
129
- # ...
130
- end
131
-
132
- Another interesting method is the authorize generator, which allows you to
133
- specify the message you want to give when the authorization is not granted. For
134
- example:
135
-
136
- class ProfilesController < ApplicationController
137
- before_filter :only => [:edit, :update], &authorize("You gotta log in to do that!")
138
- # ...
139
- end
140
-
141
- But for that you also need to add an "extend", like this:
142
-
143
- class ApplicationController < ActionController::Base
144
- include Usable
145
- extend UsableClass
146
-
147
- #...
148
- end
149
-
150
- == The model
151
-
152
- Ubiquitous User expects you to have a model for your users called User
153
- (configurable) with a name field (also configurable). You could create such a
154
- model with the following command:
155
-
156
- ./script/generate model User name:string
157
-
158
- The name can be the real name, the username or anything. Ubiquitous user doesn't
159
- make any assumptions about how you authenticate users.
160
-
161
- == Customize
162
-
163
- ubiquitous_user expects your user model to be named User. That's not
164
- configurable yet. What is configurable is the two methods it calls on user. You
165
- can do this kind of configuration in config/initializers/ubiquitous_user.rb for
166
- example:
167
-
168
- UsableConfig::user_model = :Account
169
- UsableConfig::user_model_new = :new_account
170
- UsableConfig::user_model_save = :save_bypassing_non_essential_validation
171
- UsableConfig::user_model_name = :name_or_else
172
-
173
- == API Documentation
174
-
175
- Up to date api documentation should be automatically generated on
176
- http://rdoc.info/projects/pupeno/ubiquitous_user
177
-
178
- == Metrics
179
-
180
- You con find the metrics for this gem automatically generated at
181
- http://getcaliper.com/caliper/project?repo=git%3A%2F%2Fgithub.com%2Fpupeno%2Fubiquitous_user.git
182
-
183
- == Note on Patches/Pull Requests
184
-
185
- * Fork the project.
186
- * Make your feature addition or bug fix.
187
- * Add tests for it. This is important so I don't break it in a
188
- future version unintentionally.
189
- * Commit, do not mess with rakefile, version, or history.
190
- (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)
191
- * Send me a pull request. Bonus points for topic branches.
192
-
193
- == Copyright
194
-
195
- Copyright (c) 2009 J. Pablo Fernández. See LICENSE for details.
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.4.1