rack-mason 0.1.2
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.
- data/.gitignore +4 -0
- data/CHANGELOG +24 -0
- data/Gemfile +16 -0
- data/LICENSE +21 -0
- data/README.rdoc +54 -0
- data/Rakefile +23 -0
- data/examples/middlewares/initial.rb +27 -0
- data/examples/middlewares/intro.rb +16 -0
- data/examples/middlewares/l337.rb +21 -0
- data/examples/middlewares/stylizer.rb +15 -0
- data/examples/rackapp/app.rb +105 -0
- data/examples/rackapp/config.ru +16 -0
- data/examples/railsapp/README +243 -0
- data/examples/railsapp/Rakefile +10 -0
- data/examples/railsapp/app/controllers/application_controller.rb +10 -0
- data/examples/railsapp/app/controllers/tommy_boy_controller.rb +9 -0
- data/examples/railsapp/app/helpers/application_helper.rb +3 -0
- data/examples/railsapp/app/helpers/tommy_boy_helper.rb +2 -0
- data/examples/railsapp/app/views/layouts/application.html.erb +13 -0
- data/examples/railsapp/app/views/tommy_boy/index.html.erb +44 -0
- data/examples/railsapp/app/views/tommy_boy/more.html.erb +12 -0
- data/examples/railsapp/config/boot.rb +110 -0
- data/examples/railsapp/config/database.yml +22 -0
- data/examples/railsapp/config/environment.rb +50 -0
- data/examples/railsapp/config/environments/development.rb +17 -0
- data/examples/railsapp/config/environments/production.rb +28 -0
- data/examples/railsapp/config/environments/test.rb +28 -0
- data/examples/railsapp/config/initializers/backtrace_silencers.rb +7 -0
- data/examples/railsapp/config/initializers/inflections.rb +10 -0
- data/examples/railsapp/config/initializers/mime_types.rb +5 -0
- data/examples/railsapp/config/initializers/new_rails_defaults.rb +21 -0
- data/examples/railsapp/config/initializers/session_store.rb +15 -0
- data/examples/railsapp/config/locales/en.yml +5 -0
- data/examples/railsapp/config/routes.rb +4 -0
- data/examples/railsapp/db/development.sqlite3 +0 -0
- data/examples/railsapp/db/seeds.rb +7 -0
- data/examples/railsapp/doc/README_FOR_APP +2 -0
- data/examples/railsapp/public/404.html +30 -0
- data/examples/railsapp/public/422.html +30 -0
- data/examples/railsapp/public/500.html +30 -0
- data/examples/railsapp/public/favicon.ico +0 -0
- data/examples/railsapp/public/images/rails.png +0 -0
- data/examples/railsapp/public/javascripts/application.js +2 -0
- data/examples/railsapp/public/javascripts/controls.js +963 -0
- data/examples/railsapp/public/javascripts/dragdrop.js +973 -0
- data/examples/railsapp/public/javascripts/effects.js +1128 -0
- data/examples/railsapp/public/javascripts/prototype.js +4320 -0
- data/examples/railsapp/public/robots.txt +5 -0
- data/examples/railsapp/script/about +4 -0
- data/examples/railsapp/script/console +3 -0
- data/examples/railsapp/script/dbconsole +3 -0
- data/examples/railsapp/script/destroy +3 -0
- data/examples/railsapp/script/generate +3 -0
- data/examples/railsapp/script/performance/benchmarker +3 -0
- data/examples/railsapp/script/performance/profiler +3 -0
- data/examples/railsapp/script/plugin +3 -0
- data/examples/railsapp/script/runner +3 -0
- data/examples/railsapp/script/server +3 -0
- data/examples/railsapp/test/functional/tommy_boy_controller_test.rb +8 -0
- data/examples/railsapp/test/performance/browsing_test.rb +9 -0
- data/examples/railsapp/test/test_helper.rb +38 -0
- data/examples/railsapp/test/unit/helpers/tommy_boy_helper_test.rb +4 -0
- data/examples/sinatraapp/app.rb +107 -0
- data/gem.yml +17 -0
- data/lib/mason_helper.rb +76 -0
- data/lib/mason_test_helper.rb +99 -0
- data/lib/rack-mason.rb +70 -0
- data/test/rack-mason_test.rb +21 -0
- data/test/test_helper.rb +13 -0
- data/vex/gem.rake +36 -0
- data/vex/gem.rb +95 -0
- metadata +197 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
0.1.2 (February 21, 2011)
|
2
|
+
* BUGFIX: process_html test helper was not working in Ruby 1.9.
|
3
|
+
* Moved test apps into the examples directory.
|
4
|
+
* Added a basic automated test.
|
5
|
+
* Added bundler support.
|
6
|
+
* Small tweaks such as require paths, gitignore updates, etc.
|
7
|
+
|
8
|
+
0.1.1 (July 18, 2010)
|
9
|
+
* Tweaked documentation but did not change code.
|
10
|
+
|
11
|
+
0.1.0 (July 18, 2010)
|
12
|
+
* Added convenience methods for testing middleware based on Rack::Mason.
|
13
|
+
|
14
|
+
0.0.3 (December 3, 2009)
|
15
|
+
* BUGFIX: 'require' problem.
|
16
|
+
|
17
|
+
0.0.2 (December 3, 2009)
|
18
|
+
* BUGFIX: Namespace error.
|
19
|
+
|
20
|
+
0.0.1 (December 3, 2009)
|
21
|
+
* BUGFIX: Wrong filename.
|
22
|
+
|
23
|
+
0.0.0 (December 3, 2009)
|
24
|
+
* Initial release.
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2009, 2010 Wyatt M. Greene
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
= Rack::Mason
|
2
|
+
|
3
|
+
== Copyright
|
4
|
+
|
5
|
+
Rack::Mason is (c) radiospiel@open-lab.org. It is licensed under the terms of the MIT License.
|
6
|
+
Substantial parts are based on https://github.com/techiferous/rack-mason (c) techiferous@gmail.com
|
7
|
+
|
8
|
+
== Description
|
9
|
+
|
10
|
+
If you are creating Rack middleware that changes the HTML response, use
|
11
|
+
Rack::Mason to get a head start. Rack::Mason takes care of the
|
12
|
+
boilerplate Rack glue so that you can focus on simply changing the HTML.
|
13
|
+
|
14
|
+
== Usage
|
15
|
+
|
16
|
+
There are two ways you can change the HTML: as a Nokogiri document or as
|
17
|
+
a string. Simply define one of the following methods:
|
18
|
+
|
19
|
+
def update_body(doc)
|
20
|
+
... insert code that changes the doc ...
|
21
|
+
end
|
22
|
+
|
23
|
+
Rack::Mason also provides some convenience methods for interacting with
|
24
|
+
Rack and Nokogiri as well as some convenience methods for testing.
|
25
|
+
|
26
|
+
== Examples
|
27
|
+
|
28
|
+
The examples/middlewares directory has examples of writing middleware using
|
29
|
+
Rack::Mason.
|
30
|
+
|
31
|
+
== Testing
|
32
|
+
|
33
|
+
Rack::Mason comes with some test helpers that make it easier for you to
|
34
|
+
write tests for your Rack middleware. Refer to the documentation for
|
35
|
+
MasonTestHelper for more details.
|
36
|
+
|
37
|
+
If you want to test Rack::Mason itself, this gem doesn't come with automated tests,
|
38
|
+
but it provides manual tests. Each of the example middlewares is inserted into a
|
39
|
+
Sinatra, Rails, and Rack test app.
|
40
|
+
|
41
|
+
To run the Rails test app:
|
42
|
+
* cd examples/railsapp
|
43
|
+
* script/server
|
44
|
+
* point your browser to http://localhost:3000
|
45
|
+
|
46
|
+
To run the Sinatra test app:
|
47
|
+
* cd examples/sinatraapp
|
48
|
+
* ruby app.rb
|
49
|
+
* point your browser to http://localhost:4567
|
50
|
+
|
51
|
+
To run the Rack test app:
|
52
|
+
* cd examples/rackapp
|
53
|
+
* rackup config.ru
|
54
|
+
* point your browser to http://localhost:9292
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Generate documentation for Rack::Mason.'
|
6
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
7
|
+
rdoc.rdoc_dir = 'rdoc'
|
8
|
+
rdoc.title = 'Rack::Mason'
|
9
|
+
rdoc.rdoc_files.include('README.rdoc')
|
10
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
11
|
+
end
|
12
|
+
|
13
|
+
desc 'Default: run unit tests.'
|
14
|
+
task :default => :test
|
15
|
+
|
16
|
+
desc 'Test Rack::Mason'
|
17
|
+
Rake::TestTask.new(:test) do |t|
|
18
|
+
t.libs << 'lib'
|
19
|
+
t.pattern = 'test/**/*_test.rb'
|
20
|
+
t.verbose = true
|
21
|
+
end
|
22
|
+
|
23
|
+
load "#{File.dirname(__FILE__)}/vex/gem.rake"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'rack-mason')
|
2
|
+
|
3
|
+
# This middleware emphasizes the first character in every paragraph, not unlike
|
4
|
+
# the initials in illuminated manuscripts of the Middle Ages.
|
5
|
+
#
|
6
|
+
module Rack
|
7
|
+
class Initial < Mason
|
8
|
+
|
9
|
+
def update_body(doc)
|
10
|
+
doc.css("p").each do |p|
|
11
|
+
p.traverse do |node|
|
12
|
+
if node.text?
|
13
|
+
if node.content =~ /(.*?)(\S)(.*)/m
|
14
|
+
initial_whitespace = $1
|
15
|
+
initial_character = $2
|
16
|
+
rest_of_text = $3
|
17
|
+
text = initial_whitespace + '<span style="font-size: 150%; font-weight: bold;">' + initial_character +
|
18
|
+
'</span>' + rest_of_text
|
19
|
+
node.replace text
|
20
|
+
end
|
21
|
+
break
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'rack-mason')
|
2
|
+
|
3
|
+
# This middleware demonstrates how you would add an HTML element to the beginning
|
4
|
+
# of a web page.
|
5
|
+
#
|
6
|
+
module Rack
|
7
|
+
class Intro < Mason
|
8
|
+
|
9
|
+
def update_body(doc)
|
10
|
+
h1 = create_node(doc, "h1", "Holy shnikes!")
|
11
|
+
h1['style'] = "text-align: center;"
|
12
|
+
add_first_child(doc.at_css("body"), h1)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'rack-mason')
|
2
|
+
|
3
|
+
# This middleware converts the text of every paragraph to leetspeak.
|
4
|
+
#
|
5
|
+
module Rack
|
6
|
+
class L337 < Mason
|
7
|
+
|
8
|
+
def update_body(doc)
|
9
|
+
doc.css("p").each do |p|
|
10
|
+
p.traverse do |node|
|
11
|
+
if node.text?
|
12
|
+
l337_text = node.content.upcase.tr('ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
13
|
+
'48CD3F6H1JKLMN0P9R57UVWxY2')
|
14
|
+
update_text(node, l337_text)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'rack-mason')
|
2
|
+
|
3
|
+
# This middleware demonstrates how to add inline CSS styles to the web page.
|
4
|
+
#
|
5
|
+
module Rack
|
6
|
+
class Stylizer < Mason
|
7
|
+
|
8
|
+
def update_body(doc)
|
9
|
+
doc.at_css("body")["style"] = "font-family: Georgia, serif; font-style: italic;"
|
10
|
+
doc.at_css("div#container")["style"] =
|
11
|
+
"margin-left:auto;margin-right:auto;width:500px;position:relative"
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
class App
|
2
|
+
|
3
|
+
def call(env)
|
4
|
+
response = Rack::Response.new
|
5
|
+
request = Rack::Request.new(env)
|
6
|
+
response['Content-Type'] = 'text/html'
|
7
|
+
if request.path =~ /more/
|
8
|
+
response.write more
|
9
|
+
else
|
10
|
+
response.write front_page
|
11
|
+
end
|
12
|
+
response.finish
|
13
|
+
end
|
14
|
+
|
15
|
+
def front_page
|
16
|
+
%Q{
|
17
|
+
<!DOCTYPE html
|
18
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
19
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
20
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
21
|
+
<head>
|
22
|
+
<title>The Greatest Movie of All Time</title>
|
23
|
+
</head>
|
24
|
+
<body>
|
25
|
+
<div id="container">
|
26
|
+
<p>
|
27
|
+
What my associate is trying to say is that our new brake pads are really cool.
|
28
|
+
You're not even gonna believe it.
|
29
|
+
</p>
|
30
|
+
<p>
|
31
|
+
Like, um, let's say you're driving along
|
32
|
+
the road, with your family.<br />
|
33
|
+
And you're driving along...la de da...woo...<br />
|
34
|
+
And then all of a sudden
|
35
|
+
there's a truck tire
|
36
|
+
in the middle of the road
|
37
|
+
and you hit the brakes.<br />
|
38
|
+
Screeeee!
|
39
|
+
</p>
|
40
|
+
<p>
|
41
|
+
Woah, that was close.
|
42
|
+
</p>
|
43
|
+
<p>
|
44
|
+
Now let's see what happens when you're
|
45
|
+
driving with "the other guy's brake pads".
|
46
|
+
</p>
|
47
|
+
<p>
|
48
|
+
You're driving along,<br />
|
49
|
+
you're driving along,<br />
|
50
|
+
and all of a sudden your kids are
|
51
|
+
yelling from the back seat:<br />
|
52
|
+
"I gotta go to the bathroom, daddy!"<br />
|
53
|
+
"Not now, dammit!"<br />
|
54
|
+
"Truck tire! I can't stop! Aaaaa! Help!"<br />
|
55
|
+
"There's a cliff! Aaaaa!"<br />
|
56
|
+
And your family screaming:<br />
|
57
|
+
"Oh my God, we're burning alive!"<br />
|
58
|
+
"No! I can't feel my legs!"<br />
|
59
|
+
Here comes the meat-wagon! Woo woo woo!<br />
|
60
|
+
And the medic gets out and says:<br />
|
61
|
+
"Oh! My! God!"<br />
|
62
|
+
New guy's in the corner puking his guts out:<br />
|
63
|
+
Blllleeeeeeeaaaaaaaaaaah!<br />
|
64
|
+
Blllleeeeeeeaaaaaaaaaaah!<br />
|
65
|
+
</p>
|
66
|
+
<p>
|
67
|
+
All because you wanna save a coupla extra pennies.
|
68
|
+
</p>
|
69
|
+
<a href="/more">« inflict me with more »</a>
|
70
|
+
</div>
|
71
|
+
</body>
|
72
|
+
</html>
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
def more
|
77
|
+
%Q{
|
78
|
+
<!DOCTYPE html
|
79
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
80
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
81
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
82
|
+
<head>
|
83
|
+
<title>More of the Greatest Movie of All Time</title>
|
84
|
+
</head>
|
85
|
+
<body>
|
86
|
+
<div id="container">
|
87
|
+
<p>
|
88
|
+
D+? Oh my God! I passed! I passed! Oh, man i got a D+! I'm gonna graduate!
|
89
|
+
I'm gonna graduate! D+!
|
90
|
+
</p>
|
91
|
+
<p>
|
92
|
+
Hey guys, do i look different now that i'm a college grad?
|
93
|
+
</p>
|
94
|
+
<p>
|
95
|
+
Apparently they give a lot fewer D+'s than D-'s.
|
96
|
+
It's not a grade they like to give out, i'll tell ya that right now.
|
97
|
+
</p>
|
98
|
+
<a href="/">« take me back »</a>
|
99
|
+
</div>
|
100
|
+
</body>
|
101
|
+
</html>
|
102
|
+
}
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'app')
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'middlewares', 'initial')
|
3
|
+
require File.join(File.dirname(__FILE__), '..', 'middlewares', 'intro')
|
4
|
+
require File.join(File.dirname(__FILE__), '..', 'middlewares', 'l337')
|
5
|
+
require File.join(File.dirname(__FILE__), '..', 'middlewares', 'stylizer')
|
6
|
+
|
7
|
+
# Note that these middlewares will seem to be applied in backwards order.
|
8
|
+
# In other words, Rack::Stylizer parses the resulting HTML first, then
|
9
|
+
# passes it to Rack::Initial, then to Rack::L337, and finally to Rack::Intro.
|
10
|
+
|
11
|
+
use Rack::Intro
|
12
|
+
use Rack::L337
|
13
|
+
use Rack::Initial
|
14
|
+
use Rack::Stylizer
|
15
|
+
|
16
|
+
run App.new
|
@@ -0,0 +1,243 @@
|
|
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" templates
|
7
|
+
that are primarily responsible for inserting pre-built data in between HTML tags.
|
8
|
+
The model contains the "smart" domain objects (such as Account, Product, Person,
|
9
|
+
Post) that holds all the business logic and knows how to persist themselves to
|
10
|
+
a database. The controller handles the incoming requests (such as Save New Account,
|
11
|
+
Update Product, Show Post) by manipulating the model and directing data to the view.
|
12
|
+
|
13
|
+
In Rails, the model is handled by what's called an object-relational mapping
|
14
|
+
layer entitled Active Record. This layer allows you to present the data from
|
15
|
+
database rows as objects and embellish these data objects with business logic
|
16
|
+
methods. You can read more about Active Record in
|
17
|
+
link:files/vendor/rails/activerecord/README.html.
|
18
|
+
|
19
|
+
The controller and view are handled by the Action Pack, which handles both
|
20
|
+
layers by its two parts: Action View and Action Controller. These two layers
|
21
|
+
are bundled in a single package due to their heavy interdependence. This is
|
22
|
+
unlike the relationship between the Active Record and Action Pack that is much
|
23
|
+
more separate. Each of these packages can be used independently outside of
|
24
|
+
Rails. You can read more about Action Pack in
|
25
|
+
link:files/vendor/rails/actionpack/README.html.
|
26
|
+
|
27
|
+
|
28
|
+
== Getting Started
|
29
|
+
|
30
|
+
1. At the command prompt, start a new Rails application using the <tt>rails</tt> command
|
31
|
+
and your application name. Ex: rails myapp
|
32
|
+
2. Change directory into myapp and start the web server: <tt>script/server</tt> (run with --help for options)
|
33
|
+
3. Go to http://localhost:3000/ and get "Welcome aboard: You're riding the Rails!"
|
34
|
+
4. Follow the guidelines to start developing your application
|
35
|
+
|
36
|
+
|
37
|
+
== Web Servers
|
38
|
+
|
39
|
+
By default, Rails will try to use Mongrel if it's are installed when started with script/server, otherwise Rails will use WEBrick, the webserver that ships with Ruby. But you can also use Rails
|
40
|
+
with a variety of other web servers.
|
41
|
+
|
42
|
+
Mongrel is a Ruby-based webserver with a C component (which requires compilation) that is
|
43
|
+
suitable for development and deployment of Rails applications. If you have Ruby Gems installed,
|
44
|
+
getting up and running with mongrel is as easy as: <tt>gem install mongrel</tt>.
|
45
|
+
More info at: http://mongrel.rubyforge.org
|
46
|
+
|
47
|
+
Say other Ruby web servers like Thin and Ebb or regular web servers like Apache or LiteSpeed or
|
48
|
+
Lighttpd or IIS. The Ruby web servers are run through Rack and the latter can either be setup to use
|
49
|
+
FCGI or proxy to a pack of Mongrels/Thin/Ebb servers.
|
50
|
+
|
51
|
+
== Apache .htaccess example for FCGI/CGI
|
52
|
+
|
53
|
+
# General Apache options
|
54
|
+
AddHandler fastcgi-script .fcgi
|
55
|
+
AddHandler cgi-script .cgi
|
56
|
+
Options +FollowSymLinks +ExecCGI
|
57
|
+
|
58
|
+
# If you don't want Rails to look in certain directories,
|
59
|
+
# use the following rewrite rules so that Apache won't rewrite certain requests
|
60
|
+
#
|
61
|
+
# Example:
|
62
|
+
# RewriteCond %{REQUEST_URI} ^/notrails.*
|
63
|
+
# RewriteRule .* - [L]
|
64
|
+
|
65
|
+
# Redirect all requests not available on the filesystem to Rails
|
66
|
+
# By default the cgi dispatcher is used which is very slow
|
67
|
+
#
|
68
|
+
# For better performance replace the dispatcher with the fastcgi one
|
69
|
+
#
|
70
|
+
# Example:
|
71
|
+
# RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
|
72
|
+
RewriteEngine On
|
73
|
+
|
74
|
+
# If your Rails application is accessed via an Alias directive,
|
75
|
+
# then you MUST also set the RewriteBase in this htaccess file.
|
76
|
+
#
|
77
|
+
# Example:
|
78
|
+
# Alias /myrailsapp /path/to/myrailsapp/public
|
79
|
+
# RewriteBase /myrailsapp
|
80
|
+
|
81
|
+
RewriteRule ^$ index.html [QSA]
|
82
|
+
RewriteRule ^([^.]+)$ $1.html [QSA]
|
83
|
+
RewriteCond %{REQUEST_FILENAME} !-f
|
84
|
+
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
|
85
|
+
|
86
|
+
# In case Rails experiences terminal errors
|
87
|
+
# Instead of displaying this message you can supply a file here which will be rendered instead
|
88
|
+
#
|
89
|
+
# Example:
|
90
|
+
# ErrorDocument 500 /500.html
|
91
|
+
|
92
|
+
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
|
93
|
+
|
94
|
+
|
95
|
+
== Debugging Rails
|
96
|
+
|
97
|
+
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
98
|
+
will help you debug it and get it back on the rails.
|
99
|
+
|
100
|
+
First area to check is the application log files. Have "tail -f" commands running
|
101
|
+
on the server.log and development.log. Rails will automatically display debugging
|
102
|
+
and runtime information to these files. Debugging info will also be shown in the
|
103
|
+
browser on requests from 127.0.0.1.
|
104
|
+
|
105
|
+
You can also log your own messages directly into the log file from your code using
|
106
|
+
the Ruby logger class from inside your controllers. Example:
|
107
|
+
|
108
|
+
class WeblogController < ActionController::Base
|
109
|
+
def destroy
|
110
|
+
@weblog = Weblog.find(params[:id])
|
111
|
+
@weblog.destroy
|
112
|
+
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
The result will be a message in your log file along the lines of:
|
117
|
+
|
118
|
+
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1
|
119
|
+
|
120
|
+
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
121
|
+
|
122
|
+
Also, Ruby documentation can be found at http://www.ruby-lang.org/ including:
|
123
|
+
|
124
|
+
* The Learning Ruby (Pickaxe) Book: http://www.ruby-doc.org/docs/ProgrammingRuby/
|
125
|
+
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
126
|
+
|
127
|
+
These two online (and free) books will bring you up to speed on the Ruby language
|
128
|
+
and also on programming in general.
|
129
|
+
|
130
|
+
|
131
|
+
== Debugger
|
132
|
+
|
133
|
+
Debugger support is available through the debugger command when you start your Mongrel or
|
134
|
+
Webrick server with --debugger. This means that you can break out of execution at any point
|
135
|
+
in the code, investigate and change the model, AND then resume execution!
|
136
|
+
You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'
|
137
|
+
Example:
|
138
|
+
|
139
|
+
class WeblogController < ActionController::Base
|
140
|
+
def index
|
141
|
+
@posts = Post.find(:all)
|
142
|
+
debugger
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
So the controller will accept the action, run the first line, then present you
|
147
|
+
with a IRB prompt in the server window. Here you can do things like:
|
148
|
+
|
149
|
+
>> @posts.inspect
|
150
|
+
=> "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
|
151
|
+
#<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
|
152
|
+
>> @posts.first.title = "hello from a debugger"
|
153
|
+
=> "hello from a debugger"
|
154
|
+
|
155
|
+
...and even better is that you can examine how your runtime objects actually work:
|
156
|
+
|
157
|
+
>> f = @posts.first
|
158
|
+
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
159
|
+
>> f.
|
160
|
+
Display all 152 possibilities? (y or n)
|
161
|
+
|
162
|
+
Finally, when you're ready to resume execution, you enter "cont"
|
163
|
+
|
164
|
+
|
165
|
+
== Console
|
166
|
+
|
167
|
+
You can interact with the domain model by starting the console through <tt>script/console</tt>.
|
168
|
+
Here you'll have all parts of the application configured, just like it is when the
|
169
|
+
application is running. You can inspect domain models, change values, and save to the
|
170
|
+
database. Starting the script without arguments will launch it in the development environment.
|
171
|
+
Passing an argument will specify a different environment, like <tt>script/console production</tt>.
|
172
|
+
|
173
|
+
To reload your controllers and models after launching the console run <tt>reload!</tt>
|
174
|
+
|
175
|
+
== dbconsole
|
176
|
+
|
177
|
+
You can go to the command line of your database directly through <tt>script/dbconsole</tt>.
|
178
|
+
You would be connected to the database with the credentials defined in database.yml.
|
179
|
+
Starting the script without arguments will connect you to the development database. Passing an
|
180
|
+
argument will connect you to a different database, like <tt>script/dbconsole production</tt>.
|
181
|
+
Currently works for mysql, postgresql and sqlite.
|
182
|
+
|
183
|
+
== Description of Contents
|
184
|
+
|
185
|
+
app
|
186
|
+
Holds all the code that's specific to this particular application.
|
187
|
+
|
188
|
+
app/controllers
|
189
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
190
|
+
automated URL mapping. All controllers should descend from ApplicationController
|
191
|
+
which itself descends from ActionController::Base.
|
192
|
+
|
193
|
+
app/models
|
194
|
+
Holds models that should be named like post.rb.
|
195
|
+
Most models will descend from ActiveRecord::Base.
|
196
|
+
|
197
|
+
app/views
|
198
|
+
Holds the template files for the view that should be named like
|
199
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use eRuby
|
200
|
+
syntax.
|
201
|
+
|
202
|
+
app/views/layouts
|
203
|
+
Holds the template files for layouts to be used with views. This models the common
|
204
|
+
header/footer method of wrapping views. In your views, define a layout using the
|
205
|
+
<tt>layout :default</tt> and create a file named default.html.erb. Inside default.html.erb,
|
206
|
+
call <% yield %> to render the view using this layout.
|
207
|
+
|
208
|
+
app/helpers
|
209
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are generated
|
210
|
+
for you automatically when using script/generate for controllers. Helpers can be used to
|
211
|
+
wrap functionality for your views into methods.
|
212
|
+
|
213
|
+
config
|
214
|
+
Configuration files for the Rails environment, the routing map, the database, and other dependencies.
|
215
|
+
|
216
|
+
db
|
217
|
+
Contains the database schema in schema.rb. db/migrate contains all
|
218
|
+
the sequence of Migrations for your schema.
|
219
|
+
|
220
|
+
doc
|
221
|
+
This directory is where your application documentation will be stored when generated
|
222
|
+
using <tt>rake doc:app</tt>
|
223
|
+
|
224
|
+
lib
|
225
|
+
Application specific libraries. Basically, any kind of custom code that doesn't
|
226
|
+
belong under controllers, models, or helpers. This directory is in the load path.
|
227
|
+
|
228
|
+
public
|
229
|
+
The directory available for the web server. Contains subdirectories for images, stylesheets,
|
230
|
+
and javascripts. Also contains the dispatchers and the default HTML files. This should be
|
231
|
+
set as the DOCUMENT_ROOT of your web server.
|
232
|
+
|
233
|
+
script
|
234
|
+
Helper scripts for automation and generation.
|
235
|
+
|
236
|
+
test
|
237
|
+
Unit and functional tests along with fixtures. When using the script/generate scripts, template
|
238
|
+
test files will be generated for you and placed in this directory.
|
239
|
+
|
240
|
+
vendor
|
241
|
+
External libraries that the application depends on. Also includes the plugins subdirectory.
|
242
|
+
If the app has frozen rails, those gems also go here, under vendor/rails/.
|
243
|
+
This directory is in the load path.
|