sybil 0.1.3 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,12 @@
1
1
  module Sybil
2
2
  class Controller < ActionController::Base
3
- def login
4
- instance_eval(&Rails.configuration.sybil.login)
5
- render :nothing => true
6
- end
7
-
8
- def logout
9
- instance_eval(&Rails.configuration.sybil.logout)
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 #login action. Should log the user identified by params[:id] in.
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 #logout action (used when 'Guest' is selected). Should log the current user out.
34
- options.logout ||= proc { UserSession.find.destroy }
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
- ApplicationController.class_eval do
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
- <div id="sybil_user_picker" style="display:none;position:fixed;left:25%;width:50%;padding: 20px; 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;">
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
- </div>
12
- <script>
13
- // TODO: Either write non-jQuery version or include jQuery if it isn't already present
14
- $(document).ready(function(){
15
- $up = $('#sybil_user_picker');
16
- $up.css('top', -($up.height()+40))
17
- function hideSybilUserPicker(){
18
- $up.animate({top: -($up.height()+40)}, 200, function(){$up.hide();});
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
- $up.find('input[type=submit]').click(function(){
21
- id = $up.find('select[name=id]').val();
22
- if(id == 'logout')
23
- $.get('<%= sybil_logout_path %>', function(){ location.reload(true); });
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
- $.post('<%= sybil_login_path %>', { id: id }, function() { location.reload(true); });
26
- hideSybilUserPicker();
27
- });
28
- $(document).keyup(function(e){
29
- // TODO: Make key combination configurable
30
- if(e.ctrlKey && (e.keyCode==85)){
31
- if($up.filter(':visible').length==0)
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
- $up.show();
34
- $up.animate({top: '0'}, 200);
71
+ showSybilUserPicker();
35
72
  }else{
36
73
  hideSybilUserPicker();
37
74
  }
38
75
  }
39
-
40
- if(e.keyCode == 27) hideSybilUserPicker();
41
- });
42
- $('body').click(function(e){
43
- $target = $(e.target);
44
- if(!$target.is('#sybil_user_picker') && $target.parents('#sybil_user_picker').length==0)
45
- if($up.filter(':visible').length>0)
46
- hideSybilUserPicker();
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
@@ -1,3 +1,3 @@
1
1
  module Sybil
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.1"
3
3
  end
data/rails/routes.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  if defined?(Rails::Application)
2
2
  Rails.application.routes.draw do
3
- post '/sybil', :to => 'sybil#login', :as => :sybil_login
4
- get '/sybil/logout', :to => 'sybil#logout', :as => :sybil_logout
3
+ post '/sybil', :to => 'sybil#sybil', :as => :sybil
5
4
  end
6
5
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: sybil
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
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-05-26 00:00:00 -04:00
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.5.2
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