sybil 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sybil.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,15 @@
1
+ module Sybil
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
11
+ end
12
+ end
13
+ end
14
+
15
+ ::SybilController = Sybil::Controller
@@ -0,0 +1,50 @@
1
+ require 'rails'
2
+ require 'sybil/controller'
3
+
4
+ module Sybil
5
+ class Railtie < Rails::Railtie
6
+ config.sybil = ActiveSupport::OrderedOptions.new
7
+
8
+ initializer "sybil.configure_routes" do |app|
9
+ app.routes_reloader.paths << File.join(File.dirname(__FILE__), "..", "..", "rails", "routes.rb")
10
+ end
11
+
12
+ initializer 'sybil.set_default_options' do |app|
13
+ options = app.config.sybil
14
+
15
+ # templates - Array of string layout names that Sybil will inject into
16
+ options.layouts ||= %w{application}
17
+
18
+ # inject_before - Either a string or a pattern that Sybil will inject itself before the last occurance of in response.body
19
+ options.inject_before ||= /<\/body>/i
20
+
21
+ # users - A proc that is run within the context of Sybil's ApplicationController :after_filter and returns any object that responds to #each
22
+ # and returns objects that respond to #[:id] and #[:name]
23
+ # This means that you can simply do something like @users = User.all, if your Users have a :name attribute.
24
+ # :id should be the id of the user, and :name will be what is displayed in Sybil's dropdown
25
+ options.users ||= proc { User.all }
26
+
27
+ # login - A proc that is run within the context of Sybil's #login action. Should log the user identified by params[:id] in.
28
+ options.login ||= proc { UserSession.create(User.find(params[:id])) }
29
+
30
+ # 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.
31
+ options.logout ||= proc { UserSession.find.destroy }
32
+ end
33
+
34
+ config.to_prepare do
35
+
36
+ ApplicationController.class_eval do
37
+ after_filter :inject_sybil
38
+
39
+ private
40
+ def inject_sybil
41
+ if Rails.configuration.sybil.layouts.include?(_layout)
42
+ @users = instance_eval(&Rails.configuration.sybil.users)
43
+ response.body = response.body.insert(response.body.rindex(Rails.configuration.sybil.inject_before),ERB.new(File.read(File.join(File.dirname(__FILE__),'user_picker.html.erb'))).result(binding))
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,47 @@
1
+ <div id="sybil_user_picker" style="display:none;position:absolute;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;">
2
+ <h1 style="color:white;border-bottom:solid white 1px;padding-bottom:5px;font-size:200%;">Sybil</h1>
3
+ <br />
4
+ <select name="id">
5
+ <option value="logout" <%= "selected" unless current_user %>>Guest</option>
6
+ <% @users.each do |u| %>
7
+ <option value="<%= u[:id] %>" <%= "selected" if current_user and current_user.id == u[:id] %>><%= u[:name] %></option>
8
+ <% end %>
9
+ </select>
10
+ <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();});
19
+ }
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); });
24
+ else
25
+ $.post('<%= sybil_login_path %>', { id: id }, function() { location.reload(true); });
26
+ hideSybilUserPicker();
27
+ });
28
+ $(document).keypress(function(e){
29
+ // TODO: Make key combination configurable
30
+ if((e.keyCode==21) || (e.ctrlKey && (e.charCode==117))){
31
+ if($up.filter(':visible').length==0)
32
+ {
33
+ $up.show();
34
+ $up.animate({top: '0'}, 200);
35
+ }else{
36
+ hideSybilUserPicker();
37
+ }
38
+ }
39
+ });
40
+ $('body').click(function(e){
41
+ $target = $(e.target);
42
+ if(!$target.is('#sybil_user_picker') && $target.parents('#sybil_user_picker').length==0)
43
+ if($up.filter(':visible').length>0)
44
+ hideSybilUserPicker();
45
+ });
46
+ });
47
+ </script>
@@ -0,0 +1,3 @@
1
+ module Sybil
2
+ VERSION = "0.1.0"
3
+ end
data/lib/sybil.rb ADDED
@@ -0,0 +1 @@
1
+ require "sybil/railtie" if defined?(Rails) && Rails.version >= "3" && Rails.env == 'development'
data/rails/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ if defined?(Rails::Application)
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
5
+ end
6
+ end
data/sybil.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "sybil/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "sybil"
7
+ s.version = Sybil::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jim Ryan"]
10
+ s.email = ["jim@room118solutions.com"]
11
+ s.homepage = "http://github.com/jimryan/sybil"
12
+ s.summary = %q{Easily switch between users during development of a Rails application}
13
+ # s.description = %q{TODO: Write a gem description}
14
+
15
+ s.rubyforge_project = "sybil"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sybil
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jim Ryan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-23 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ - jim@room118solutions.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - Gemfile
29
+ - Rakefile
30
+ - lib/sybil.rb
31
+ - lib/sybil/controller.rb
32
+ - lib/sybil/railtie.rb
33
+ - lib/sybil/user_picker.html.erb
34
+ - lib/sybil/version.rb
35
+ - rails/routes.rb
36
+ - sybil.gemspec
37
+ has_rdoc: true
38
+ homepage: http://github.com/jimryan/sybil
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project: sybil
61
+ rubygems_version: 1.6.2
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Easily switch between users during development of a Rails application
65
+ test_files: []
66
+