switch_user 0.1.1 → 0.2.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.md CHANGED
@@ -7,20 +7,41 @@ switch_user promises that it only be activated in the development environment.
7
7
 
8
8
  It supports only devise now, but it will support authlogic in the future.
9
9
 
10
+ Example
11
+ -------
12
+
13
+ Visit here: [http://switch-user-example.heroku.com/][0], switch the current user in the select box.
14
+
10
15
  Install
11
16
  -------
12
17
 
13
- add in Gemfile.
18
+ Add in Gemfile.
14
19
 
15
20
  gem "switch_user", "~> 0.1.0"
16
21
 
17
22
  Usage
18
23
  -----
19
24
 
20
- add following code into your layout page.
25
+ Add following code into your layout page.
21
26
 
22
27
  = switch_user_select
23
28
 
29
+ Configuration
30
+ -------------
31
+
32
+ By default, you can switch between Guest and all users in users table.
33
+
34
+ But if you want to use different scope users in devise or you want to customize the users that can be switched, you should do like this
35
+
36
+ SwitchUser.setup do |config|
37
+ # model may be :devise or :authologic, but now we only support devise
38
+ config.mode = :devise
39
+ # avaliable_users is a hash, key is the scope of devise user, value is the proc that return the users that can be switched.
40
+ config.available_users = { :user => lambda { User.all }, :admin => lambda { Admin.all } }
41
+ end
42
+
24
43
 
25
44
  Copyright © 2010 Richard Huang (flyerhzm@gmail.com), released under the MIT license
26
45
 
46
+ [0]: http://switch-user-example.heroku.com/
47
+
@@ -3,11 +3,20 @@ class SwitchUserController < ApplicationController
3
3
  before_filter :developer_modes_only
4
4
 
5
5
  def set_current_user
6
- if params[:user_id].blank?
7
- warden.logout(:user)
6
+ if params[:scope_id].blank?
7
+ SwitchUser.available_users.keys.each do |s|
8
+ warden.logout(s)
9
+ end
8
10
  else
9
- current_user = User.find(params[:user_id])
10
- warden.set_user(current_user, :scope => :user)
11
+ scope, id = params[:scope_id].split('_')
12
+ SwitchUser.available_users.keys.each do |s|
13
+ if scope == s.to_s
14
+ user = scope.classify.constantize.find(id)
15
+ warden.set_user(user, :scope => scope)
16
+ else
17
+ warden.logout(s)
18
+ end
19
+ end
11
20
  end
12
21
  redirect_to(request.env["HTTP_REFERER"] ? :back : root_path)
13
22
  end
@@ -2,14 +2,22 @@ module SwitchUserHelper
2
2
  def switch_user_select
3
3
  if Rails.env == "development"
4
4
  if current_user
5
- options = "<option value="">Guest</option>"
6
- options += options_from_collection_for_select(User.all, :id, :email, current_user.id)
5
+ options = "<option value=''>Guest</option>"
7
6
  else
8
- options = "<option selected='selected' value="">Guest</option>"
9
- options += options_from_collection_for_select(User.all, :id, :email)
7
+ options = "<option selected='selected' value=''>Guest</option>"
8
+ end
9
+ SwitchUser.available_users.each do |scope, user_proc|
10
+ user_proc.call.each do |user|
11
+ current = send("current_#{scope}")
12
+ if current and current.id == user.id
13
+ options += "<option selected='selected' value='#{scope}_#{user.id}'>#{user.send(SwitchUser.display_field)}</option>"
14
+ else
15
+ options += "<option value='#{scope}_#{user.id}'>#{user.send(SwitchUser.display_field)}</option>"
16
+ end
17
+ end
10
18
  end
11
19
  select_tag "switch_user_id", options.html_safe,
12
- :onchange => "location.href = '/switch_user?user_id=' + encodeURIComponent(this.options[this.selectedIndex].value)"
20
+ :onchange => "location.href = '/switch_user?scope_id=' + encodeURIComponent(this.options[this.selectedIndex].value)"
13
21
  end
14
22
  end
15
23
  end
@@ -1,3 +1,3 @@
1
1
  module SwitchUser
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/switch_user.rb CHANGED
@@ -3,7 +3,20 @@ require "rails"
3
3
  module SwitchUser
4
4
  class Engine < Rails::Engine
5
5
  config.to_prepare do
6
- ApplicationController.helper(SwitchUserHelper)
6
+ ApplicationController.helper(SwitchUserHelper)
7
7
  end
8
8
  end
9
- end
9
+
10
+ mattr_accessor :mode
11
+ self.mode = 'devise'
12
+
13
+ mattr_accessor :available_users
14
+ self.available_users = { :user => lambda { User.all } }
15
+
16
+ mattr_accessor :display_field
17
+ self.display_field = 'email'
18
+
19
+ def self.setup
20
+ yield self
21
+ end
22
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switch_user
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Richard Huang