houdini-rails 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/MIT-LICENSE +20 -0
- data/README +13 -0
- data/Rakefile +33 -0
- data/VERSION +1 -0
- data/app/controllers/houdini/postbacks_controller.rb +11 -0
- data/config/routes.rb +7 -0
- data/houdini-rails.gemspec +129 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/houdini.rb +6 -0
- data/lib/houdini/base.rb +26 -0
- data/lib/houdini/model.rb +41 -0
- data/lib/houdini/task.rb +18 -0
- data/spec/controllers/houdini/postbacks_controller_spec.rb +27 -0
- data/spec/integration/postbacks_test.rb +11 -0
- data/spec/rails_app/README +243 -0
- data/spec/rails_app/Rakefile +10 -0
- data/spec/rails_app/app/controllers/application_controller.rb +10 -0
- data/spec/rails_app/app/helpers/application_helper.rb +3 -0
- data/spec/rails_app/app/models/post.rb +25 -0
- data/spec/rails_app/app/views/posts/houdini_template.html.haml +6 -0
- data/spec/rails_app/config/boot.rb +110 -0
- data/spec/rails_app/config/database.yml +22 -0
- data/spec/rails_app/config/environment.rb +23 -0
- data/spec/rails_app/config/environments/development.rb +17 -0
- data/spec/rails_app/config/environments/production.rb +28 -0
- data/spec/rails_app/config/environments/test.rb +28 -0
- data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails_app/config/initializers/inflections.rb +10 -0
- data/spec/rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/rails_app/config/initializers/new_rails_defaults.rb +21 -0
- data/spec/rails_app/config/initializers/session_store.rb +15 -0
- data/spec/rails_app/config/locales/en.yml +5 -0
- data/spec/rails_app/config/routes.rb +43 -0
- data/spec/rails_app/db/migrate/20100517231810_create_posts.rb +16 -0
- data/spec/rails_app/db/seeds.rb +7 -0
- data/spec/rails_app/doc/README_FOR_APP +2 -0
- data/spec/rails_app/public/404.html +30 -0
- data/spec/rails_app/public/422.html +30 -0
- data/spec/rails_app/public/500.html +30 -0
- data/spec/rails_app/public/favicon.ico +0 -0
- data/spec/rails_app/public/images/rails.png +0 -0
- data/spec/rails_app/public/index.html +275 -0
- data/spec/rails_app/public/javascripts/application.js +2 -0
- data/spec/rails_app/public/javascripts/controls.js +963 -0
- data/spec/rails_app/public/javascripts/dragdrop.js +973 -0
- data/spec/rails_app/public/javascripts/effects.js +1128 -0
- data/spec/rails_app/public/javascripts/prototype.js +4320 -0
- data/spec/rails_app/public/robots.txt +5 -0
- data/spec/rails_app/script/about +4 -0
- data/spec/rails_app/script/console +3 -0
- data/spec/rails_app/script/dbconsole +3 -0
- data/spec/rails_app/script/destroy +3 -0
- data/spec/rails_app/script/generate +3 -0
- data/spec/rails_app/script/performance/benchmarker +3 -0
- data/spec/rails_app/script/performance/profiler +3 -0
- data/spec/rails_app/script/plugin +3 -0
- data/spec/rails_app/script/runner +3 -0
- data/spec/rails_app/script/server +3 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/test_helper.rb +24 -0
- data/tasks/houdini_tasks.rake +4 -0
- data/uninstall.rb +1 -0
- metadata +161 -0
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
5
|
+
spec.libs << 'lib' << 'spec'
|
6
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rake/testtask'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'spec/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
task :default => [:spec, :test]
|
18
|
+
|
19
|
+
begin
|
20
|
+
require 'jeweler'
|
21
|
+
Jeweler::Tasks.new do |gem|
|
22
|
+
gem.name = "houdini-rails"
|
23
|
+
gem.summary = %Q{Rails plugin for interacting with the Houdini Mechanical Turk API}
|
24
|
+
gem.description = %Q{Rails plugin for interacting with the Houdini Mechanical Turk API}
|
25
|
+
gem.email = "chris@chrisconley.me"
|
26
|
+
gem.homepage = "http://github.com/chrisconley/houdini-rails"
|
27
|
+
gem.authors = ["Chris Conley"]
|
28
|
+
gem.add_development_dependency "spec", ">= 1.3.0"
|
29
|
+
end
|
30
|
+
Jeweler::GemcutterTasks.new
|
31
|
+
rescue LoadError
|
32
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
33
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.2
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Houdini::PostbacksController < ApplicationController
|
2
|
+
def create
|
3
|
+
subject_class = params[:subject_class].classify.constantize
|
4
|
+
subject = subject_class.find(params[:subject_id])
|
5
|
+
if subject.process_postback(params[:answer])
|
6
|
+
render :json => {:success => true}
|
7
|
+
else
|
8
|
+
render :json => {:success => false}, :status => 422
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{houdini-rails}
|
8
|
+
s.version = "0.1.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Chris Conley"]
|
12
|
+
s.date = %q{2010-08-06}
|
13
|
+
s.description = %q{Rails plugin for interacting with the Houdini Mechanical Turk API}
|
14
|
+
s.email = %q{chris@chrisconley.me}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"app/controllers/houdini/postbacks_controller.rb",
|
25
|
+
"config/routes.rb",
|
26
|
+
"houdini-rails.gemspec",
|
27
|
+
"init.rb",
|
28
|
+
"install.rb",
|
29
|
+
"lib/houdini.rb",
|
30
|
+
"lib/houdini/base.rb",
|
31
|
+
"lib/houdini/model.rb",
|
32
|
+
"lib/houdini/task.rb",
|
33
|
+
"spec/controllers/houdini/postbacks_controller_spec.rb",
|
34
|
+
"spec/integration/postbacks_test.rb",
|
35
|
+
"spec/rails_app/README",
|
36
|
+
"spec/rails_app/Rakefile",
|
37
|
+
"spec/rails_app/app/controllers/application_controller.rb",
|
38
|
+
"spec/rails_app/app/helpers/application_helper.rb",
|
39
|
+
"spec/rails_app/app/models/post.rb",
|
40
|
+
"spec/rails_app/app/views/posts/houdini_template.html.haml",
|
41
|
+
"spec/rails_app/config/boot.rb",
|
42
|
+
"spec/rails_app/config/database.yml",
|
43
|
+
"spec/rails_app/config/environment.rb",
|
44
|
+
"spec/rails_app/config/environments/development.rb",
|
45
|
+
"spec/rails_app/config/environments/production.rb",
|
46
|
+
"spec/rails_app/config/environments/test.rb",
|
47
|
+
"spec/rails_app/config/initializers/backtrace_silencers.rb",
|
48
|
+
"spec/rails_app/config/initializers/inflections.rb",
|
49
|
+
"spec/rails_app/config/initializers/mime_types.rb",
|
50
|
+
"spec/rails_app/config/initializers/new_rails_defaults.rb",
|
51
|
+
"spec/rails_app/config/initializers/session_store.rb",
|
52
|
+
"spec/rails_app/config/locales/en.yml",
|
53
|
+
"spec/rails_app/config/routes.rb",
|
54
|
+
"spec/rails_app/db/migrate/20100517231810_create_posts.rb",
|
55
|
+
"spec/rails_app/db/seeds.rb",
|
56
|
+
"spec/rails_app/doc/README_FOR_APP",
|
57
|
+
"spec/rails_app/log/development.log",
|
58
|
+
"spec/rails_app/log/production.log",
|
59
|
+
"spec/rails_app/log/server.log",
|
60
|
+
"spec/rails_app/public/404.html",
|
61
|
+
"spec/rails_app/public/422.html",
|
62
|
+
"spec/rails_app/public/500.html",
|
63
|
+
"spec/rails_app/public/favicon.ico",
|
64
|
+
"spec/rails_app/public/images/rails.png",
|
65
|
+
"spec/rails_app/public/index.html",
|
66
|
+
"spec/rails_app/public/javascripts/application.js",
|
67
|
+
"spec/rails_app/public/javascripts/controls.js",
|
68
|
+
"spec/rails_app/public/javascripts/dragdrop.js",
|
69
|
+
"spec/rails_app/public/javascripts/effects.js",
|
70
|
+
"spec/rails_app/public/javascripts/prototype.js",
|
71
|
+
"spec/rails_app/public/robots.txt",
|
72
|
+
"spec/rails_app/script/about",
|
73
|
+
"spec/rails_app/script/console",
|
74
|
+
"spec/rails_app/script/dbconsole",
|
75
|
+
"spec/rails_app/script/destroy",
|
76
|
+
"spec/rails_app/script/generate",
|
77
|
+
"spec/rails_app/script/performance/benchmarker",
|
78
|
+
"spec/rails_app/script/performance/profiler",
|
79
|
+
"spec/rails_app/script/plugin",
|
80
|
+
"spec/rails_app/script/runner",
|
81
|
+
"spec/rails_app/script/server",
|
82
|
+
"spec/spec.opts",
|
83
|
+
"spec/spec_helper.rb",
|
84
|
+
"spec/test_helper.rb",
|
85
|
+
"tasks/houdini_tasks.rake",
|
86
|
+
"uninstall.rb"
|
87
|
+
]
|
88
|
+
s.homepage = %q{http://github.com/chrisconley/houdini-rails}
|
89
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
90
|
+
s.require_paths = ["lib"]
|
91
|
+
s.rubygems_version = %q{1.3.6}
|
92
|
+
s.summary = %q{Rails plugin for interacting with the Houdini Mechanical Turk API}
|
93
|
+
s.test_files = [
|
94
|
+
"spec/controllers/houdini/postbacks_controller_spec.rb",
|
95
|
+
"spec/integration/postbacks_test.rb",
|
96
|
+
"spec/rails_app/app/controllers/application_controller.rb",
|
97
|
+
"spec/rails_app/app/helpers/application_helper.rb",
|
98
|
+
"spec/rails_app/app/models/post.rb",
|
99
|
+
"spec/rails_app/config/boot.rb",
|
100
|
+
"spec/rails_app/config/environment.rb",
|
101
|
+
"spec/rails_app/config/environments/development.rb",
|
102
|
+
"spec/rails_app/config/environments/production.rb",
|
103
|
+
"spec/rails_app/config/environments/test.rb",
|
104
|
+
"spec/rails_app/config/initializers/backtrace_silencers.rb",
|
105
|
+
"spec/rails_app/config/initializers/inflections.rb",
|
106
|
+
"spec/rails_app/config/initializers/mime_types.rb",
|
107
|
+
"spec/rails_app/config/initializers/new_rails_defaults.rb",
|
108
|
+
"spec/rails_app/config/initializers/session_store.rb",
|
109
|
+
"spec/rails_app/config/routes.rb",
|
110
|
+
"spec/rails_app/db/migrate/20100517231810_create_posts.rb",
|
111
|
+
"spec/rails_app/db/seeds.rb",
|
112
|
+
"spec/spec_helper.rb",
|
113
|
+
"spec/test_helper.rb"
|
114
|
+
]
|
115
|
+
|
116
|
+
if s.respond_to? :specification_version then
|
117
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
118
|
+
s.specification_version = 3
|
119
|
+
|
120
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
121
|
+
s.add_development_dependency(%q<spec>, [">= 1.3.0"])
|
122
|
+
else
|
123
|
+
s.add_dependency(%q<spec>, [">= 1.3.0"])
|
124
|
+
end
|
125
|
+
else
|
126
|
+
s.add_dependency(%q<spec>, [">= 1.3.0"])
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'houdini'
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/houdini.rb
ADDED
data/lib/houdini/base.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Houdini
|
2
|
+
class Base
|
3
|
+
Undefined = Class.new(NameError)
|
4
|
+
HoudiniRequestError = Class.new(NameError)
|
5
|
+
|
6
|
+
def self.request(api, params)
|
7
|
+
validate_constants
|
8
|
+
return ["200", '{success:"true"}'] if HOST == 'test'
|
9
|
+
url = URI.parse("http://#{HOST}/api/v0/#{api}/tasks/")
|
10
|
+
puts url.inspect
|
11
|
+
response, body = Net::HTTP.post_form(url, params)
|
12
|
+
raise HoudiniRequestError, "The request to houdini failed with code #{response.code}: #{body}" if response.code != "200"
|
13
|
+
[response.code, body]
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def self.validate_constants
|
19
|
+
raise Undefined, "Houdini::KEY is not defined" if Houdini::KEY.blank?
|
20
|
+
raise Undefined, "Houdini::HOST is not defined" if Houdini::HOST.blank?
|
21
|
+
raise Undefined, "Houdini::RAILS_HOST is not defined" if Houdini::RAILS_HOST.blank?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Houdini
|
2
|
+
module Model
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def houdini(name, options)
|
9
|
+
include ActionController::UrlWriter
|
10
|
+
cattr_accessor :houdini_task
|
11
|
+
self.houdini_task = Houdini::Task.new(name, options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def send_to_houdini
|
16
|
+
result = Houdini::Base.request(houdini_task.api,
|
17
|
+
:api_key => Houdini::KEY,
|
18
|
+
:identifier => houdini_task.identifier,
|
19
|
+
:price => houdini_task.price,
|
20
|
+
:title => houdini_task.title,
|
21
|
+
:form_html => generate_form_html(houdini_task.form_template),
|
22
|
+
:postback_url => houdini_postbacks_url(self.class.name, self.id, self.houdini_task.name, :host => Houdini::RAILS_HOST))
|
23
|
+
call_on_submit(*result)
|
24
|
+
end
|
25
|
+
|
26
|
+
def process_postback(answer)
|
27
|
+
self.send(houdini_task.on_postback, answer)
|
28
|
+
end
|
29
|
+
|
30
|
+
def call_on_submit(response, body)
|
31
|
+
self.send(houdini_task.on_submit, response, body) if houdini_task.on_submit
|
32
|
+
end
|
33
|
+
|
34
|
+
def generate_form_html(template)
|
35
|
+
#TODO: look into including Rails::Renderer
|
36
|
+
template = File.read(template)
|
37
|
+
haml_engine = Haml::Engine.new(template)
|
38
|
+
haml_engine.render(Object.new, self.class.name.underscore => self)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/houdini/task.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Houdini
|
2
|
+
class Task
|
3
|
+
attr_accessor :name, :api, :on, :if, :identifier, :price, :title, :form_template, :on_submit, :on_postback
|
4
|
+
|
5
|
+
def initialize(name, options)
|
6
|
+
@name = name
|
7
|
+
@api = "simple" # options[:strategy]
|
8
|
+
@on = options[:on] || :after_create
|
9
|
+
@if = options[:if] || true
|
10
|
+
@identifier = options[:identifier] || name
|
11
|
+
@price = options[:price]
|
12
|
+
@title = options[:title]
|
13
|
+
@form_template = options[:form_template]
|
14
|
+
@on_submit = options[:on_submit]
|
15
|
+
@on_postback = options[:on_postback] || :update_attributes
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Foo < ActiveRecord::Base
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Houdini::PostbacksController do
|
8
|
+
before do
|
9
|
+
@subject = mock_model(Foo)
|
10
|
+
Foo.stub!(:find).and_return(@subject)
|
11
|
+
@subject.stub!(:process_postback)
|
12
|
+
end
|
13
|
+
|
14
|
+
def do_post
|
15
|
+
post :create, :subject_class => 'foo', :subject_id => '1', :task_name => 'review_image'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should find the subject" do
|
19
|
+
Foo.should_receive(:find).with("1").and_return(@subject)
|
20
|
+
do_post
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should process the answer" do
|
24
|
+
@subject.should_receive(:process_postback)
|
25
|
+
do_post
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec/test_helper'
|
2
|
+
|
3
|
+
class PostbacksTest < ActionController::IntegrationTest
|
4
|
+
def test_should_do_something
|
5
|
+
post = Post.create(:image_url => 'http://google.com', :flagged => nil)
|
6
|
+
visit 'houdini/post/1/review_image/postbacks', :post, :answer => {:flagged => "yes"}
|
7
|
+
assert_response :success
|
8
|
+
assert_equal true, post.reload.flagged
|
9
|
+
assert_equal Time.now.to_date, post.houdini_request_sent_at.to_date
|
10
|
+
end
|
11
|
+
end
|
@@ -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.
|