sybil 0.1.3 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sybil/controller.rb +7 -8
- data/lib/sybil/railtie.rb +23 -7
- data/lib/sybil/user_picker.html.erb +83 -34
- data/lib/sybil/version.rb +1 -1
- data/rails/routes.rb +1 -2
- metadata +3 -3
data/lib/sybil/controller.rb
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
module Sybil
|
2
2
|
class Controller < ActionController::Base
|
3
|
-
def
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
render :nothing => true
|
3
|
+
def sybil
|
4
|
+
if params[:id] == 'logout'
|
5
|
+
instance_eval(&(Rails.configuration.sybil.logout.is_a?(Hash) ? Rails.configuration.sybil.logout[params[:layout]] : Rails.configuration.sybil.logout))
|
6
|
+
else
|
7
|
+
instance_eval(&(Rails.configuration.sybil.login.is_a?(Hash) ? Rails.configuration.sybil.login[params[:layout]] : Rails.configuration.sybil.login))
|
8
|
+
end
|
9
|
+
params[:redirect] ? redirect_to(params[:redirect]) : render(:nothing => true)
|
11
10
|
end
|
12
11
|
end
|
13
12
|
end
|
data/lib/sybil/railtie.rb
CHANGED
@@ -21,28 +21,44 @@ module Sybil
|
|
21
21
|
# inject_before - Either a string or a pattern that Sybil will inject itself before the last occurance of in response.body
|
22
22
|
options.inject_before ||= /<\/body>/i
|
23
23
|
|
24
|
+
# toggle_user_picker - This is a string of JS that, if true, toggles the user picker
|
25
|
+
# It is run in a keyup callback registered to the document body, 'e' contains the KeyboardEvent
|
26
|
+
options.toggle_user_picker ||= "e.ctrlKey && (e.keyCode==85)"
|
27
|
+
|
28
|
+
# close_user_picker - This is a string of JS that, if true, closes the user picker
|
29
|
+
# It is run in a keyup callback registered to the document body, 'e' contains the KeyboardEvent
|
30
|
+
options.close_user_picker ||= "e.keyCode == 27"
|
31
|
+
|
32
|
+
## The following options can also be Hashes, in the form of '_layout' => Proc, so you can use a different proc for each layout ##
|
33
|
+
|
24
34
|
# users - A proc that is run within the context of Sybil's ApplicationController :after_filter and returns any object that responds to #each
|
25
35
|
# and returns objects that respond to #[:id] and #[:name]
|
26
36
|
# This means that you can simply do something like @users = User.all, if your Users have a :name attribute.
|
27
37
|
# :id should be the id of the user, and :name will be what is displayed in Sybil's dropdown
|
28
38
|
options.users ||= proc { User.all }
|
29
39
|
|
30
|
-
# login - A proc that is run within the context of Sybil's #
|
40
|
+
# login - A proc that is run within the context of Sybil's #sybil action. Should log the user identified by params[:id] in.
|
31
41
|
options.login ||= proc { UserSession.create(User.find(params[:id])) }
|
32
42
|
|
33
|
-
# logout - A proc that is run within the context of Sybil's #
|
34
|
-
|
43
|
+
# logout - A proc that is run within the context of Sybil's #sybil action (used when 'Guest' is selected). Should log the current user out.
|
44
|
+
# Note: It is NOT guaranteed that this won't be called when the user is already logged out, therefore it is the developer's responsibility to account for that
|
45
|
+
options.logout ||= proc { UserSession.find.destroy if UserSession.find }
|
46
|
+
|
47
|
+
# Should return the current user session (needs to return false/nil if not logged in and respond to #id)
|
48
|
+
options.current_user ||= proc { current_user }
|
35
49
|
end
|
36
50
|
|
37
51
|
config.to_prepare do
|
38
52
|
|
39
|
-
|
53
|
+
ActionController::Base.class_eval do
|
40
54
|
after_filter :inject_sybil
|
41
|
-
|
55
|
+
|
42
56
|
private
|
43
57
|
def inject_sybil
|
44
|
-
if Rails.configuration.sybil.layouts.include?(_layout) and (response.content_type =~ Rails.configuration.sybil.content_type)
|
45
|
-
@users = instance_eval(&Rails.configuration.sybil.users)
|
58
|
+
if _layout and Rails.configuration.sybil.layouts.include?(_layout) and (response.content_type =~ Rails.configuration.sybil.content_type)
|
59
|
+
@users = instance_eval(&(Rails.configuration.sybil.users.is_a?(Hash) ? Rails.configuration.sybil.users[_layout] : Rails.configuration.sybil.users))
|
60
|
+
@current_user = instance_eval(&(Rails.configuration.sybil.current_user.is_a?(Hash) ? Rails.configuration.sybil.current_user[_layout] : Rails.configuration.sybil.current_user))
|
61
|
+
@layout = _layout
|
46
62
|
insert_index = response.body.rindex(Rails.configuration.sybil.inject_before)
|
47
63
|
if insert_index
|
48
64
|
response.body = response.body.insert(insert_index,ERB.new(File.read(File.join(File.dirname(__FILE__),'user_picker.html.erb'))).result(binding))
|
@@ -1,49 +1,98 @@
|
|
1
|
-
<
|
1
|
+
<form id="sybil_user_picker" style="display:none;position:fixed;left:25%;top:0;width:50%;padding: 20px; background-color: #000; background-color:rgba(0,0,0,.8); text-align:center;border-radius:5px;box-shadow: 0 0 5px 5px rgba(0,0,0,.5);z-index:999;" action="<%= sybil_path %>" method="post">
|
2
2
|
<h1 style="color:white;border-bottom:solid white 1px;padding-bottom:5px;font-size:200%;">Sybil</h1>
|
3
3
|
<br />
|
4
4
|
<select name="id">
|
5
|
-
<option value="logout" <%= "selected" unless current_user %>>Guest</option>
|
5
|
+
<option value="logout" <%= "selected" unless @current_user %>>Guest</option>
|
6
6
|
<% @users.each do |u| %>
|
7
|
-
<option value="<%= u[:id] %>" <%= "selected" if current_user and current_user.id == u[:id] %>><%= u[:name] %></option>
|
7
|
+
<option value="<%= u[:id] %>" <%= "selected" if @current_user and @current_user.id == u[:id] %>><%= u[:name] %></option>
|
8
8
|
<% end %>
|
9
9
|
</select>
|
10
|
+
<input type="hidden" name="layout" value="<%= @layout %>">
|
10
11
|
<input type="submit" value="Switch">
|
11
|
-
</
|
12
|
-
<script>
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
</form>
|
13
|
+
<script>
|
14
|
+
function sybilAddEvent(el, event, fn, c){
|
15
|
+
if(el.addEventListener)
|
16
|
+
el.addEventListener(event, fn, c);
|
17
|
+
else
|
18
|
+
el.attachEvent('on' + event, fn);
|
19
|
+
}
|
20
|
+
|
21
|
+
sybilAddEvent(window, 'load', function(){
|
22
|
+
var up = document.getElementById('sybil_user_picker');
|
23
|
+
|
24
|
+
// If jQuery is present, set up a var and position userpicker, since it'll be animated
|
25
|
+
if(window.jQuery){
|
26
|
+
var $up = $(up);
|
27
|
+
$up.css('top', -($up.height()+40));
|
19
28
|
}
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
29
|
+
|
30
|
+
// Hide UP, slide up if jQuery
|
31
|
+
function hideSybilUserPicker(){
|
32
|
+
if(window.jQuery)
|
33
|
+
$up.animate({top: -($up.height()+40)}, 200, function(){$up.hide();});
|
24
34
|
else
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
if(
|
31
|
-
|
35
|
+
up.style.display='none';
|
36
|
+
}
|
37
|
+
|
38
|
+
// Show UP, slide down if jQuery
|
39
|
+
function showSybilUserPicker(){
|
40
|
+
if(window.jQuery)
|
41
|
+
{
|
42
|
+
$up.show();
|
43
|
+
$up.animate({top: '0'}, 200);
|
44
|
+
}else{
|
45
|
+
up.style.display='block';
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
// Find submit button
|
50
|
+
for(var i=0;i<up.children.length;i++)
|
51
|
+
{
|
52
|
+
if(up.children[i].type=='submit')
|
53
|
+
{
|
54
|
+
up.children[i].onclick = function(){
|
55
|
+
// Create hidden redirect field, set value to current location, then hide the UP, and let the form submit
|
56
|
+
var redirectField = document.createElement('input');
|
57
|
+
redirectField.type="hidden";
|
58
|
+
redirectField.name="redirect";
|
59
|
+
redirectField.value=location.href;
|
60
|
+
up.appendChild(redirectField);
|
61
|
+
hideSybilUserPicker();
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
// Add key listener
|
67
|
+
sybilAddEvent(document, 'keyup', function(e){
|
68
|
+
if(<%= Rails.configuration.sybil.toggle_user_picker %>){
|
69
|
+
if(up.style.display == 'none')
|
32
70
|
{
|
33
|
-
|
34
|
-
$up.animate({top: '0'}, 200);
|
71
|
+
showSybilUserPicker();
|
35
72
|
}else{
|
36
73
|
hideSybilUserPicker();
|
37
74
|
}
|
38
75
|
}
|
39
|
-
|
40
|
-
if(
|
41
|
-
});
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
76
|
+
|
77
|
+
if(<%= Rails.configuration.sybil.close_user_picker %>) hideSybilUserPicker();
|
78
|
+
}, false);
|
79
|
+
|
80
|
+
// Add click listener to body, hide UP if click is anywhere but on the UP itself
|
81
|
+
sybilAddEvent(document.body, 'click', function(e){
|
82
|
+
var childOfUserPicker = false;
|
83
|
+
var curNode = (e.srcElement) ? e.srcElement : e.target;
|
84
|
+
// Iterate over parents of target (only check 3 nodes deep, that's as deep as the UP gets)
|
85
|
+
// If parent is UP, set boolean, if parent is body, break
|
86
|
+
for(var i=0;i<3;i++)
|
87
|
+
{
|
88
|
+
if(childOfUserPicker = (curNode === up)) break;
|
89
|
+
curNode = curNode.parentNode;
|
90
|
+
if(curNode == document.body) break;
|
91
|
+
}
|
92
|
+
|
93
|
+
// Hide the UP if target isn't a child of the UP or the UP itself
|
94
|
+
if(!childOfUserPicker)
|
95
|
+
hideSybilUserPicker();
|
96
|
+
}, false);
|
97
|
+
}, false);
|
49
98
|
</script>
|
data/lib/sybil/version.rb
CHANGED
data/rails/routes.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: sybil
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1
|
5
|
+
version: 0.2.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jim Ryan
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-07-06 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
58
|
requirements: []
|
59
59
|
|
60
60
|
rubyforge_project: sybil
|
61
|
-
rubygems_version: 1.
|
61
|
+
rubygems_version: 1.6.2
|
62
62
|
signing_key:
|
63
63
|
specification_version: 3
|
64
64
|
summary: Easily switch between users during development of a Rails application
|