Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Rosenfeld Rosas committed Oct 6, 2010
0 parents commit 1b1d397
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.swp
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2010 [Rodrigo Rosenfeld Rosas]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 changes: 12 additions & 0 deletions README
@@ -0,0 +1,12 @@
Console
=======

A Rails 3 plugin for running Ruby scripts on the browser in the context of a controller action.

Just put it in your vendor/plugins/console. For instance:

git submodule add git://github.com/rosenfeld/rails-web-console.git vendor/plugins/console

Then, access "/console" in your browser.

Copyright (c) 2010 [Rodrigo Rosenfeld Rosas], released under the MIT license
20 changes: 20 additions & 0 deletions app/controllers/console_controller.rb
@@ -0,0 +1,20 @@
require 'stringio'
class ConsoleController < ApplicationController
def index
end

def run
session[:script] = params[:script]
stdout_orig = $stdout
$stdout = StringIO.new
begin
result_eval = eval params[:script], binding
$stdout.rewind
result = %Q{<div class="stdout">#{$stdout.read}</div><div class="return">#{result_eval.inspect}</div>}
rescue Exception => e
result = e.to_s
end
$stdout = stdout_orig
render text: result.gsub("\n", "<br />\n")
end
end
3 changes: 3 additions & 0 deletions app/views/console/index.html.erb
@@ -0,0 +1,3 @@
<textarea id="script" rows="10" cols="80"><%= session[:script] %></textarea>
<div><input type="button" value="Clear" id="clearResults" /><input type="button" value="Run" id="runScript" /></div>
<div id="results"></div>
14 changes: 14 additions & 0 deletions app/views/layouts/console.html.erb
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Console</title>
<%= javascript_include_tag 'http://code.jquery.com/jquery-1.4.2.min.js' %>
<%= javascript_include_tag 'console' %>
<%= stylesheet_link_tag 'console' %>
</head>
<body>

<%= yield %>

</body>
</html>
4 changes: 4 additions & 0 deletions config/routes.rb
@@ -0,0 +1,4 @@
Rails::Application.routes.draw do
get 'console' => 'console#index'
post 'console/run' => 'console#run'
end
1 change: 1 addition & 0 deletions init.rb
@@ -0,0 +1 @@
config.middleware.use ::ActionDispatch::Static, "#{root}/public"
7 changes: 7 additions & 0 deletions lib/console.rb
@@ -0,0 +1,7 @@
module Console
class Engine < Rails::Engine
# initializer "static assets" do |app|
# app.middleware.use ::ActionDispatch::Static, "#{root}/public"
# end
end
end
8 changes: 8 additions & 0 deletions public/javascripts/console.js
@@ -0,0 +1,8 @@
jQuery(function($) {
$('#clearResults').click(function() { $('#results').empty() })
$('#runScript').click(function() {
$.post('/console/run', {script: $('#script').val()}, function(data) {
$('<div/>').attr('class', 'console_result').html(data).appendTo('#results')
})
})
})
10 changes: 10 additions & 0 deletions public/stylesheets/console.css
@@ -0,0 +1,10 @@
#script {width: 100%}
.console_result {
background-color: black;
color: white;
margin: 1em 0;
}

.return {
background-color: blue;
}

0 comments on commit 1b1d397

Please sign in to comment.