web-console-rails3 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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +40 -0
- data/Rakefile +48 -0
- data/app/assets/javascripts/web_console/application.js +4 -0
- data/app/assets/javascripts/web_console/console_sessions.js +24 -0
- data/app/assets/stylesheets/web_console/application.css +13 -0
- data/app/assets/stylesheets/web_console/console_sessions.css +8 -0
- data/app/controllers/web_console/application_controller.rb +17 -0
- data/app/controllers/web_console/console_sessions_controller.rb +23 -0
- data/app/helpers/web_console/application_helper.rb +4 -0
- data/app/helpers/web_console/console_session_helper.rb +4 -0
- data/app/models/web_console/console_session.rb +110 -0
- data/app/views/layouts/web_console/application.html.erb +14 -0
- data/app/views/web_console/console_sessions/index.html.erb +4 -0
- data/config/routes.rb +5 -0
- data/lib/web-console-rails3.rb +1 -0
- data/lib/web-console.rb +1 -0
- data/lib/web_console.rb +21 -0
- data/lib/web_console/backport/active_model.rb +26 -0
- data/lib/web_console/engine.rb +40 -0
- data/lib/web_console/fiber.rb +48 -0
- data/lib/web_console/repl.rb +59 -0
- data/lib/web_console/repl/dummy.rb +38 -0
- data/lib/web_console/repl/irb.rb +62 -0
- data/lib/web_console/stream.rb +30 -0
- data/lib/web_console/version.rb +3 -0
- data/test/controllers/web_console/console_sessions_controller_test.rb +57 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +15 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +38 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +58 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/schema.rb +16 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +588 -0
- data/test/dummy/log/test.log +2122 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/dummy/tmp/cache/assets/C9E/AD0/sprockets%2F4a42d8916f3fa47867445f696497a92c +0 -0
- data/test/dummy/tmp/cache/assets/CA7/D70/sprockets%2F3e9e32bbc8c81a93002555265a36a852 +0 -0
- data/test/dummy/tmp/cache/assets/CBD/6E0/sprockets%2F3293dfc0984076f0e8371a9e7640f4a8 +0 -0
- data/test/dummy/tmp/cache/assets/CD0/FC0/sprockets%2Fbb2777627d42a216f3d9ced4322040b3 +0 -0
- data/test/dummy/tmp/cache/assets/CFA/E30/sprockets%2F35be1d26565dc0310c29f1a5e2f62f10 +0 -0
- data/test/dummy/tmp/cache/assets/D1D/FD0/sprockets%2Fa4a5ffe670666ce3d8d59179905201ef +0 -0
- data/test/dummy/tmp/cache/assets/D21/6B0/sprockets%2F9e242803fe56d6305274ff7e6487deda +0 -0
- data/test/dummy/tmp/cache/assets/D29/380/sprockets%2F92f1a0450bc4bea3364a07be42b4f645 +0 -0
- data/test/dummy/tmp/cache/assets/D3E/380/sprockets%2F434d98c8380bb9daf43810155aaf68ba +0 -0
- data/test/dummy/tmp/cache/assets/D69/670/sprockets%2Fc6b4a05846fd1b98fe9252ecdd1601c0 +0 -0
- data/test/dummy/tmp/cache/assets/D90/B30/sprockets%2Fe03fe93eb7fe888c9d21ee62816596ff +0 -0
- data/test/dummy/tmp/cache/assets/D95/C40/sprockets%2F09cb0a274209abf0391cbfce6ee67b82 +0 -0
- data/test/dummy/tmp/cache/assets/D9F/400/sprockets%2F7f60332f86073dc8ed80b4c2a9dfcbe1 +0 -0
- data/test/dummy/tmp/cache/assets/DB3/0C0/sprockets%2F689fb998e2a7add3e00db88df254c87a +0 -0
- data/test/dummy/tmp/cache/assets/DD4/440/sprockets%2Fa33d646ac00d8bc87a4a496af6eed96f +0 -0
- data/test/dummy/tmp/cache/assets/E5A/C40/sprockets%2F164cfa0fd9eb1ec1acdfaa278c83e9c5 +0 -0
- data/test/helpers/web_console/console_session_helper_test.rb +6 -0
- data/test/models/console_session_test.rb +111 -0
- data/test/test_helper.rb +15 -0
- data/test/web_console/repl/dummy_test.rb +54 -0
- data/test/web_console/repl/irb_test.rb +156 -0
- data/test/web_console/repl_test.rb +15 -0
- data/test/web_console_test.rb +93 -0
- data/vendor/assets/javascripts/jquery.console.js +727 -0
- metadata +249 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module WebConsole
|
|
2
|
+
# Poor Man's Fiber (API compatible Thread based Fiber implementation for Ruby 1.8)
|
|
3
|
+
# (c) 2008 Aman Gupta (tmm1)
|
|
4
|
+
#
|
|
5
|
+
# For the purposes of our REPL adapters there is a need for fiber invocation
|
|
6
|
+
# across threads. The native implementation does not support that.
|
|
7
|
+
class FiberError < StandardError; end
|
|
8
|
+
|
|
9
|
+
class Fiber
|
|
10
|
+
def initialize
|
|
11
|
+
raise ArgumentError, 'new Fiber requires a block' unless block_given?
|
|
12
|
+
|
|
13
|
+
@yield = Queue.new
|
|
14
|
+
@resume = Queue.new
|
|
15
|
+
|
|
16
|
+
@thread = Thread.new { @yield.push [ *yield(*@resume.pop) ] }
|
|
17
|
+
@thread.abort_on_exception = true
|
|
18
|
+
@thread[:fiber] = self
|
|
19
|
+
end
|
|
20
|
+
attr_reader :thread
|
|
21
|
+
|
|
22
|
+
def resume(*args)
|
|
23
|
+
raise FiberError, 'dead fiber called' unless @thread.alive?
|
|
24
|
+
@resume.push(args)
|
|
25
|
+
result = @yield.pop
|
|
26
|
+
result.size > 1 ? result : result.first
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def yield(*args)
|
|
30
|
+
@yield.push(args)
|
|
31
|
+
result = @resume.pop
|
|
32
|
+
result.size > 1 ? result : result.first
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.yield(*args)
|
|
36
|
+
raise FiberError, "can't yield from root fiber" unless fiber = Thread.current[:fiber]
|
|
37
|
+
fiber.yield(*args)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.current
|
|
41
|
+
Thread.current[:fiber] or raise FiberError, 'not inside a fiber'
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def inspect
|
|
45
|
+
"#<#{self.class}:0x#{self.object_id.to_s(16)}>"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
|
2
|
+
|
|
3
|
+
module WebConsole
|
|
4
|
+
module REPL
|
|
5
|
+
extend self
|
|
6
|
+
|
|
7
|
+
# Registry of REPL implementations mapped to their correspondent adapter
|
|
8
|
+
# classes.
|
|
9
|
+
#
|
|
10
|
+
# Don't manually alter the registry. Use WebConsole::REPL.register_adapter
|
|
11
|
+
# for adding entries.
|
|
12
|
+
def adapters
|
|
13
|
+
@adapters ||= {}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Register an adapter into the adapters registry.
|
|
17
|
+
#
|
|
18
|
+
# Registration maps and adapter class to an existing REPL implementation,
|
|
19
|
+
# that we call an adaptee constant. If the adaptee constant is not given,
|
|
20
|
+
# it is automatically derived from the adapter class name.
|
|
21
|
+
#
|
|
22
|
+
# For example, adapter named +WebConsole::REPL::IRB+ will derive the
|
|
23
|
+
# adaptee constant to +::IRB+.
|
|
24
|
+
#
|
|
25
|
+
# If a block is given, it would be evaluated right after the adapter
|
|
26
|
+
# registration.
|
|
27
|
+
def register_adapter(adapter_class, adaptee_constant = nil, options = {})
|
|
28
|
+
if adaptee_constant.is_a?(Hash)
|
|
29
|
+
options = adaptee_constant
|
|
30
|
+
adaptee_constant = nil
|
|
31
|
+
end
|
|
32
|
+
adaptee_constant = adapter_class if options[:standalone]
|
|
33
|
+
adaptee_constant ||= derive_adaptee_constant_from(adapter_class)
|
|
34
|
+
adapters[adaptee_constant] = adapter_class
|
|
35
|
+
yield if block_given?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Get the default adapter for the given application.
|
|
39
|
+
#
|
|
40
|
+
# By default the application will be Rails.application and the adapter
|
|
41
|
+
# will be chosen from Rails.application.config.console.
|
|
42
|
+
#
|
|
43
|
+
# If no suitible adapter is found for the configured Rails console, a dummy
|
|
44
|
+
# adapter will be used. You can evaluate code in it, but it won't support
|
|
45
|
+
# any advanced features, like multiline code evaluation.
|
|
46
|
+
def default(app = Rails.application)
|
|
47
|
+
adapters[(app.config.console || ::IRB rescue ::IRB)] || adapters[Dummy]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
def derive_adaptee_constant_from(cls, suffix = 'REPL')
|
|
52
|
+
"::#{cls.name.split('::').last.gsub(/#{suffix}$/i, '')}".constantize
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Require the builtin adapters.
|
|
58
|
+
require 'web_console/repl/irb'
|
|
59
|
+
require 'web_console/repl/dummy'
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require 'web_console/stream'
|
|
2
|
+
|
|
3
|
+
module WebConsole
|
|
4
|
+
module REPL
|
|
5
|
+
# == Dummy\ Adapter
|
|
6
|
+
#
|
|
7
|
+
# Dummy adapter that is used as a fallback for REPL with no adapters.
|
|
8
|
+
#
|
|
9
|
+
# It provides only the most basic code evaluation with no multiline code
|
|
10
|
+
# support.
|
|
11
|
+
class Dummy
|
|
12
|
+
def initialize(binding = TOPLEVEL_BINDING)
|
|
13
|
+
@binding = binding
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def prompt
|
|
17
|
+
'>> '
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def send_input(input)
|
|
21
|
+
eval_result = nil
|
|
22
|
+
streams_output = Stream.threadsafe_capture! do
|
|
23
|
+
eval_result = @binding.eval(input).inspect
|
|
24
|
+
end
|
|
25
|
+
"#{streams_output}=> #{eval_result}\n"
|
|
26
|
+
rescue Exception => exc
|
|
27
|
+
exc.backtrace.join("\n")
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
register_adapter Dummy, standalone: true do
|
|
32
|
+
require 'rails/console/app'
|
|
33
|
+
require 'rails/console/helpers'
|
|
34
|
+
|
|
35
|
+
TOPLEVEL_BINDING.eval('self').send(:include, Rails::ConsoleMethods)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require 'irb'
|
|
2
|
+
require 'web_console/fiber'
|
|
3
|
+
require 'web_console/stream'
|
|
4
|
+
|
|
5
|
+
module WebConsole
|
|
6
|
+
module REPL
|
|
7
|
+
# == IRB\ Adapter
|
|
8
|
+
#
|
|
9
|
+
# Adapter for the IRB REPL, which is the default Ruby on Rails console.
|
|
10
|
+
class IRB
|
|
11
|
+
# For some reason™ we have to be ::IRB::StdioInputMethod subclass to get
|
|
12
|
+
# #prompt populated.
|
|
13
|
+
#
|
|
14
|
+
# Not a pretty OOP, but for now, we just have to deal with it.
|
|
15
|
+
class FiberInputMethod < ::IRB::StdioInputMethod
|
|
16
|
+
def initialize; end
|
|
17
|
+
|
|
18
|
+
def gets
|
|
19
|
+
@previous = Fiber.yield
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def encoding
|
|
23
|
+
(@previous || '').encoding
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def initialize(binding = TOPLEVEL_BINDING)
|
|
28
|
+
initialize_irb_session!
|
|
29
|
+
@input = FiberInputMethod.new
|
|
30
|
+
@irb = ::IRB::Irb.new(::IRB::WorkSpace.new(binding), @input)
|
|
31
|
+
@fiber = Fiber.new { @irb.eval_input }.tap(&:resume)
|
|
32
|
+
finalize_irb_session!
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def prompt
|
|
36
|
+
@input.prompt
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def send_input(input)
|
|
40
|
+
Stream.threadsafe_capture! { @fiber.resume("#{input}\n") }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
def initialize_irb_session!(ap_path = nil)
|
|
45
|
+
::IRB.init_config(ap_path)
|
|
46
|
+
::IRB.run_config unless WebConsole.config.prevent_irbrc_execution
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def finalize_irb_session!
|
|
50
|
+
::IRB.conf[:MAIN_CONTEXT] = @irb.context
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
register_adapter IRB do
|
|
55
|
+
require 'rails/console/app'
|
|
56
|
+
require 'rails/console/helpers'
|
|
57
|
+
|
|
58
|
+
# Include all of the rails console helpers in the IRB session.
|
|
59
|
+
::IRB::ExtendCommandBundle.send(:include, Rails::ConsoleMethods)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'mutex_m'
|
|
2
|
+
|
|
3
|
+
module WebConsole
|
|
4
|
+
module Stream
|
|
5
|
+
extend Mutex_m
|
|
6
|
+
|
|
7
|
+
def self.threadsafe_capture!(*streams)
|
|
8
|
+
streams = [$stdout, $stderr] if streams.empty?
|
|
9
|
+
synchronize do
|
|
10
|
+
begin
|
|
11
|
+
streams_copy = streams.collect(&:dup)
|
|
12
|
+
replacement = Tempfile.new(name)
|
|
13
|
+
streams.each do |stream|
|
|
14
|
+
stream.reopen(replacement)
|
|
15
|
+
stream.sync = true
|
|
16
|
+
end
|
|
17
|
+
yield
|
|
18
|
+
streams.each(&:rewind)
|
|
19
|
+
replacement.read
|
|
20
|
+
ensure
|
|
21
|
+
replacement.close
|
|
22
|
+
replacement.unlink
|
|
23
|
+
streams.each_with_index do |stream, i|
|
|
24
|
+
stream.reopen(streams_copy[i])
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
module WebConsole
|
|
4
|
+
class ConsoleSessionsControllerTest < ActionController::TestCase
|
|
5
|
+
setup do
|
|
6
|
+
# Where does .stubs lives?
|
|
7
|
+
def @request.remote_ip; '127.0.0.1' end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
test 'index is successful' do
|
|
11
|
+
get :index, use_route: 'web_console'
|
|
12
|
+
assert_response :success
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
test 'index creates new console session' do
|
|
16
|
+
assert_difference 'ConsoleSession::INMEMORY_STORAGE.size' do
|
|
17
|
+
get :index, use_route: 'web_console'
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'update updates new console session' do
|
|
22
|
+
get :index, use_route: 'web_console'
|
|
23
|
+
assert_not_nil console_session = assigns(:console_session)
|
|
24
|
+
|
|
25
|
+
put :update, id: console_session.id, input: 42, use_route: 'web_console'
|
|
26
|
+
assert_match %r{42}, console_session.output
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test 'update failes when session is no longer available' do
|
|
30
|
+
get :index, use_route: 'web_console'
|
|
31
|
+
assert_not_nil console_session = assigns(:console_session)
|
|
32
|
+
|
|
33
|
+
ConsoleSession::INMEMORY_STORAGE.delete(console_session.id)
|
|
34
|
+
put :update, id: console_session.id, input: 42, use_route: 'web_console'
|
|
35
|
+
assert_response :gone
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
test 'blocks requests from non-whitelisted ips' do
|
|
39
|
+
def @request.remote_ip; '128.0.0.1' end
|
|
40
|
+
get :index, use_route: 'web_console'
|
|
41
|
+
assert_response :unauthorized
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
test 'index generated path' do
|
|
45
|
+
assert_generates mount_path, {
|
|
46
|
+
use_route: 'web_console',
|
|
47
|
+
controller: 'console_sessions'
|
|
48
|
+
}, {}, {controller: 'console_sessions'}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def mount_path
|
|
54
|
+
WebConsole::Engine.config.web_console.default_mount_path
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
== Welcome to Rails
|
|
2
|
+
|
|
3
|
+
Rails is a web-application framework that includes everything needed to create
|
|
4
|
+
database-backed web applications according to the Model-View-Control pattern.
|
|
5
|
+
|
|
6
|
+
This pattern splits the view (also called the presentation) into "dumb"
|
|
7
|
+
templates that are primarily responsible for inserting pre-built data in between
|
|
8
|
+
HTML tags. The model contains the "smart" domain objects (such as Account,
|
|
9
|
+
Product, Person, Post) that holds all the business logic and knows how to
|
|
10
|
+
persist themselves to a database. The controller handles the incoming requests
|
|
11
|
+
(such as Save New Account, Update Product, Show Post) by manipulating the model
|
|
12
|
+
and directing data to the view.
|
|
13
|
+
|
|
14
|
+
In Rails, the model is handled by what's called an object-relational mapping
|
|
15
|
+
layer entitled Active Record. This layer allows you to present the data from
|
|
16
|
+
database rows as objects and embellish these data objects with business logic
|
|
17
|
+
methods. You can read more about Active Record in
|
|
18
|
+
link:files/vendor/rails/activerecord/README.html.
|
|
19
|
+
|
|
20
|
+
The controller and view are handled by the Action Pack, which handles both
|
|
21
|
+
layers by its two parts: Action View and Action Controller. These two layers
|
|
22
|
+
are bundled in a single package due to their heavy interdependence. This is
|
|
23
|
+
unlike the relationship between the Active Record and Action Pack that is much
|
|
24
|
+
more separate. Each of these packages can be used independently outside of
|
|
25
|
+
Rails. You can read more about Action Pack in
|
|
26
|
+
link:files/vendor/rails/actionpack/README.html.
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
== Getting Started
|
|
30
|
+
|
|
31
|
+
1. At the command prompt, create a new Rails application:
|
|
32
|
+
<tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
|
|
33
|
+
|
|
34
|
+
2. Change directory to <tt>myapp</tt> and start the web server:
|
|
35
|
+
<tt>cd myapp; rails server</tt> (run with --help for options)
|
|
36
|
+
|
|
37
|
+
3. Go to http://localhost:3000/ and you'll see:
|
|
38
|
+
"Welcome aboard: You're riding Ruby on Rails!"
|
|
39
|
+
|
|
40
|
+
4. Follow the guidelines to start developing your application. You can find
|
|
41
|
+
the following resources handy:
|
|
42
|
+
|
|
43
|
+
* The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
|
|
44
|
+
* Ruby on Rails Tutorial Book: http://www.railstutorial.org/
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
== Debugging Rails
|
|
48
|
+
|
|
49
|
+
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
|
50
|
+
will help you debug it and get it back on the rails.
|
|
51
|
+
|
|
52
|
+
First area to check is the application log files. Have "tail -f" commands
|
|
53
|
+
running on the server.log and development.log. Rails will automatically display
|
|
54
|
+
debugging and runtime information to these files. Debugging info will also be
|
|
55
|
+
shown in the browser on requests from 127.0.0.1.
|
|
56
|
+
|
|
57
|
+
You can also log your own messages directly into the log file from your code
|
|
58
|
+
using the Ruby logger class from inside your controllers. Example:
|
|
59
|
+
|
|
60
|
+
class WeblogController < ActionController::Base
|
|
61
|
+
def destroy
|
|
62
|
+
@weblog = Weblog.find(params[:id])
|
|
63
|
+
@weblog.destroy
|
|
64
|
+
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
The result will be a message in your log file along the lines of:
|
|
69
|
+
|
|
70
|
+
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
|
|
71
|
+
|
|
72
|
+
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
|
73
|
+
|
|
74
|
+
Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
|
|
75
|
+
several books available online as well:
|
|
76
|
+
|
|
77
|
+
* Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
|
|
78
|
+
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
|
79
|
+
|
|
80
|
+
These two books will bring you up to speed on the Ruby language and also on
|
|
81
|
+
programming in general.
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
== Debugger
|
|
85
|
+
|
|
86
|
+
Debugger support is available through the debugger command when you start your
|
|
87
|
+
Mongrel or WEBrick server with --debugger. This means that you can break out of
|
|
88
|
+
execution at any point in the code, investigate and change the model, and then,
|
|
89
|
+
resume execution! You need to install ruby-debug to run the server in debugging
|
|
90
|
+
mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
|
|
91
|
+
|
|
92
|
+
class WeblogController < ActionController::Base
|
|
93
|
+
def index
|
|
94
|
+
@posts = Post.all
|
|
95
|
+
debugger
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
So the controller will accept the action, run the first line, then present you
|
|
100
|
+
with a IRB prompt in the server window. Here you can do things like:
|
|
101
|
+
|
|
102
|
+
>> @posts.inspect
|
|
103
|
+
=> "[#<Post:0x14a6be8
|
|
104
|
+
@attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
|
|
105
|
+
#<Post:0x14a6620
|
|
106
|
+
@attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
|
|
107
|
+
>> @posts.first.title = "hello from a debugger"
|
|
108
|
+
=> "hello from a debugger"
|
|
109
|
+
|
|
110
|
+
...and even better, you can examine how your runtime objects actually work:
|
|
111
|
+
|
|
112
|
+
>> f = @posts.first
|
|
113
|
+
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
|
114
|
+
>> f.
|
|
115
|
+
Display all 152 possibilities? (y or n)
|
|
116
|
+
|
|
117
|
+
Finally, when you're ready to resume execution, you can enter "cont".
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
== Console
|
|
121
|
+
|
|
122
|
+
The console is a Ruby shell, which allows you to interact with your
|
|
123
|
+
application's domain model. Here you'll have all parts of the application
|
|
124
|
+
configured, just like it is when the application is running. You can inspect
|
|
125
|
+
domain models, change values, and save to the database. Starting the script
|
|
126
|
+
without arguments will launch it in the development environment.
|
|
127
|
+
|
|
128
|
+
To start the console, run <tt>rails console</tt> from the application
|
|
129
|
+
directory.
|
|
130
|
+
|
|
131
|
+
Options:
|
|
132
|
+
|
|
133
|
+
* Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
|
|
134
|
+
made to the database.
|
|
135
|
+
* Passing an environment name as an argument will load the corresponding
|
|
136
|
+
environment. Example: <tt>rails console production</tt>.
|
|
137
|
+
|
|
138
|
+
To reload your controllers and models after launching the console run
|
|
139
|
+
<tt>reload!</tt>
|
|
140
|
+
|
|
141
|
+
More information about irb can be found at:
|
|
142
|
+
link:http://www.rubycentral.org/pickaxe/irb.html
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
== dbconsole
|
|
146
|
+
|
|
147
|
+
You can go to the command line of your database directly through <tt>rails
|
|
148
|
+
dbconsole</tt>. You would be connected to the database with the credentials
|
|
149
|
+
defined in database.yml. Starting the script without arguments will connect you
|
|
150
|
+
to the development database. Passing an argument will connect you to a different
|
|
151
|
+
database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
|
|
152
|
+
PostgreSQL and SQLite 3.
|
|
153
|
+
|
|
154
|
+
== Description of Contents
|
|
155
|
+
|
|
156
|
+
The default directory structure of a generated Ruby on Rails application:
|
|
157
|
+
|
|
158
|
+
|-- app
|
|
159
|
+
| |-- assets
|
|
160
|
+
| | |-- images
|
|
161
|
+
| | |-- javascripts
|
|
162
|
+
| | `-- stylesheets
|
|
163
|
+
| |-- controllers
|
|
164
|
+
| |-- helpers
|
|
165
|
+
| |-- mailers
|
|
166
|
+
| |-- models
|
|
167
|
+
| `-- views
|
|
168
|
+
| `-- layouts
|
|
169
|
+
|-- config
|
|
170
|
+
| |-- environments
|
|
171
|
+
| |-- initializers
|
|
172
|
+
| `-- locales
|
|
173
|
+
|-- db
|
|
174
|
+
|-- doc
|
|
175
|
+
|-- lib
|
|
176
|
+
| |-- assets
|
|
177
|
+
| `-- tasks
|
|
178
|
+
|-- log
|
|
179
|
+
|-- public
|
|
180
|
+
|-- script
|
|
181
|
+
|-- test
|
|
182
|
+
| |-- fixtures
|
|
183
|
+
| |-- functional
|
|
184
|
+
| |-- integration
|
|
185
|
+
| |-- performance
|
|
186
|
+
| `-- unit
|
|
187
|
+
|-- tmp
|
|
188
|
+
| `-- cache
|
|
189
|
+
| `-- assets
|
|
190
|
+
`-- vendor
|
|
191
|
+
|-- assets
|
|
192
|
+
| |-- javascripts
|
|
193
|
+
| `-- stylesheets
|
|
194
|
+
`-- plugins
|
|
195
|
+
|
|
196
|
+
app
|
|
197
|
+
Holds all the code that's specific to this particular application.
|
|
198
|
+
|
|
199
|
+
app/assets
|
|
200
|
+
Contains subdirectories for images, stylesheets, and JavaScript files.
|
|
201
|
+
|
|
202
|
+
app/controllers
|
|
203
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
|
204
|
+
automated URL mapping. All controllers should descend from
|
|
205
|
+
ApplicationController which itself descends from ActionController::Base.
|
|
206
|
+
|
|
207
|
+
app/models
|
|
208
|
+
Holds models that should be named like post.rb. Models descend from
|
|
209
|
+
ActiveRecord::Base by default.
|
|
210
|
+
|
|
211
|
+
app/views
|
|
212
|
+
Holds the template files for the view that should be named like
|
|
213
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use
|
|
214
|
+
eRuby syntax by default.
|
|
215
|
+
|
|
216
|
+
app/views/layouts
|
|
217
|
+
Holds the template files for layouts to be used with views. This models the
|
|
218
|
+
common header/footer method of wrapping views. In your views, define a layout
|
|
219
|
+
using the <tt>layout :default</tt> and create a file named default.html.erb.
|
|
220
|
+
Inside default.html.erb, call <% yield %> to render the view using this
|
|
221
|
+
layout.
|
|
222
|
+
|
|
223
|
+
app/helpers
|
|
224
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are
|
|
225
|
+
generated for you automatically when using generators for controllers.
|
|
226
|
+
Helpers can be used to wrap functionality for your views into methods.
|
|
227
|
+
|
|
228
|
+
config
|
|
229
|
+
Configuration files for the Rails environment, the routing map, the database,
|
|
230
|
+
and other dependencies.
|
|
231
|
+
|
|
232
|
+
db
|
|
233
|
+
Contains the database schema in schema.rb. db/migrate contains all the
|
|
234
|
+
sequence of Migrations for your schema.
|
|
235
|
+
|
|
236
|
+
doc
|
|
237
|
+
This directory is where your application documentation will be stored when
|
|
238
|
+
generated using <tt>rake doc:app</tt>
|
|
239
|
+
|
|
240
|
+
lib
|
|
241
|
+
Application specific libraries. Basically, any kind of custom code that
|
|
242
|
+
doesn't belong under controllers, models, or helpers. This directory is in
|
|
243
|
+
the load path.
|
|
244
|
+
|
|
245
|
+
public
|
|
246
|
+
The directory available for the web server. Also contains the dispatchers and the
|
|
247
|
+
default HTML files. This should be set as the DOCUMENT_ROOT of your web
|
|
248
|
+
server.
|
|
249
|
+
|
|
250
|
+
script
|
|
251
|
+
Helper scripts for automation and generation.
|
|
252
|
+
|
|
253
|
+
test
|
|
254
|
+
Unit and functional tests along with fixtures. When using the rails generate
|
|
255
|
+
command, template test files will be generated for you and placed in this
|
|
256
|
+
directory.
|
|
257
|
+
|
|
258
|
+
vendor
|
|
259
|
+
External libraries that the application depends on. Also includes the plugins
|
|
260
|
+
subdirectory. If the app has frozen rails, those gems also go here, under
|
|
261
|
+
vendor/rails/. This directory is in the load path.
|