diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-11-24 01:04:44 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-11-24 01:04:44 +0000 |
commit | db045dbbf60b53dbe013ef25554fd013baf88134 (patch) | |
tree | 257830e3c76458c8ff3d1329de83f32b23926028 /actionpack/examples | |
download | rails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.gz rails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.bz2 rails-db045dbbf60b53dbe013ef25554fd013baf88134.zip |
Initial
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/examples')
-rw-r--r-- | actionpack/examples/.htaccess | 24 | ||||
-rw-r--r-- | actionpack/examples/address_book/index.rhtml | 33 | ||||
-rw-r--r-- | actionpack/examples/address_book/layout.rhtml | 8 | ||||
-rwxr-xr-x | actionpack/examples/address_book_controller.cgi | 9 | ||||
-rwxr-xr-x | actionpack/examples/address_book_controller.fcgi | 6 | ||||
-rw-r--r-- | actionpack/examples/address_book_controller.rb | 52 | ||||
-rw-r--r-- | actionpack/examples/address_book_controller.rbx | 4 | ||||
-rw-r--r-- | actionpack/examples/benchmark.rb | 52 | ||||
-rwxr-xr-x | actionpack/examples/benchmark_with_ar.fcgi | 89 | ||||
-rwxr-xr-x | actionpack/examples/blog_controller.cgi | 53 | ||||
-rw-r--r-- | actionpack/examples/debate/index.rhtml | 14 | ||||
-rw-r--r-- | actionpack/examples/debate/new_topic.rhtml | 22 | ||||
-rw-r--r-- | actionpack/examples/debate/topic.rhtml | 32 | ||||
-rwxr-xr-x | actionpack/examples/debate_controller.cgi | 57 |
14 files changed, 455 insertions, 0 deletions
diff --git a/actionpack/examples/.htaccess b/actionpack/examples/.htaccess new file mode 100644 index 0000000000..fb59fa105e --- /dev/null +++ b/actionpack/examples/.htaccess @@ -0,0 +1,24 @@ +<IfModule mod_ruby.c> + RubyRequire apache/ruby-run + RubySafeLevel 0 + + <Files *.rbx> + SetHandler ruby-object + RubyHandler Apache::RubyRun.instance + </Files> +</IfModule> + + +RewriteEngine On +RewriteRule ^fcgi/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([0-9]+)$ /$1_controller.fcgi?action=$2&id=$3 [QSA] +RewriteRule ^fcgi/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /$1_controller.fcgi?action=$2 [QSA] +RewriteRule ^fcgi/([-_a-zA-Z0-9]+)/$ /$1_controller.fcgi?action=index [QSA] + +RewriteRule ^modruby/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([0-9]+)$ /$1_controller.rbx?action=$2&id=$3 [QSA] +RewriteRule ^modruby/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /$1_controller.rbx?action=$2 [QSA] +RewriteRule ^modruby/([-_a-zA-Z0-9]+)/$ /$1_controller.rbx?action=index [QSA] + +RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([0-9]+)$ /$1_controller.cgi?action=$2&id=$3 [QSA] +RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /$1_controller.cgi?action=$2 [QSA] +RewriteRule ^([-_a-zA-Z0-9]+)/$ /$1_controller.cgi?action=index [QSA] + diff --git a/actionpack/examples/address_book/index.rhtml b/actionpack/examples/address_book/index.rhtml new file mode 100644 index 0000000000..217d39075c --- /dev/null +++ b/actionpack/examples/address_book/index.rhtml @@ -0,0 +1,33 @@ +<h1>Address Book</h1> + +<% if @people.empty? %> + <p>No people in the address book yet</p> +<% else %> + <table> + <tr><th>Name</th><th>Email Address</th><th>Phone Number</th></tr> + <% for person in @people %> + <tr><td><%= person.name %></td><td><%= person.email_address %></td><td><%= person.phone_number %></td></tr> + <% end %> + </table> +<% end %> + +<form action="create_person"> + <p> + Name:<br /> + <input type="text" name="person[name]"> + </p> + + <p> + Email address:<br /> + <input type="text" name="person[email_address]"> + </p> + + <p> + Phone number:<br /> + <input type="text" name="person[phone_number]"> + </p> + + <p> + <input type="submit" value="Create Person"> + </p> +</form>
\ No newline at end of file diff --git a/actionpack/examples/address_book/layout.rhtml b/actionpack/examples/address_book/layout.rhtml new file mode 100644 index 0000000000..931e141c01 --- /dev/null +++ b/actionpack/examples/address_book/layout.rhtml @@ -0,0 +1,8 @@ +<html> +<head> + <title><%= @title || "Untitled" %></title> +</head> +<body> +<%= @content_for_layout %> +</body> +</html>
\ No newline at end of file diff --git a/actionpack/examples/address_book_controller.cgi b/actionpack/examples/address_book_controller.cgi new file mode 100755 index 0000000000..2e15467285 --- /dev/null +++ b/actionpack/examples/address_book_controller.cgi @@ -0,0 +1,9 @@ +#!/usr/local/bin/ruby + +require "address_book_controller" + +begin + AddressBookController.process_cgi(CGI.new) +rescue => e + CGI.new.out { "#{e.class}: #{e.message}" } +end
\ No newline at end of file diff --git a/actionpack/examples/address_book_controller.fcgi b/actionpack/examples/address_book_controller.fcgi new file mode 100755 index 0000000000..39947b4444 --- /dev/null +++ b/actionpack/examples/address_book_controller.fcgi @@ -0,0 +1,6 @@ +#!/usr/local/bin/ruby + +require "address_book_controller" +require "fcgi" + +FCGI.each_cgi { |cgi| AddressBookController.process_cgi(cgi) }
\ No newline at end of file diff --git a/actionpack/examples/address_book_controller.rb b/actionpack/examples/address_book_controller.rb new file mode 100644 index 0000000000..01d498e1bc --- /dev/null +++ b/actionpack/examples/address_book_controller.rb @@ -0,0 +1,52 @@ +$:.unshift(File.dirname(__FILE__) + "/../lib") + +require "action_controller" +require "action_controller/test_process" + +Person = Struct.new("Person", :id, :name, :email_address, :phone_number) + +class AddressBookService + attr_reader :people + + def initialize() @people = [] end + def create_person(data) people.unshift(Person.new(next_person_id, data["name"], data["email_address"], data["phone_number"])) end + def find_person(topic_id) people.select { |person| person.id == person.to_i }.first end + def next_person_id() people.first.id + 1 end +end + +class AddressBookController < ActionController::Base + layout "address_book/layout" + + before_filter :initialize_session_storage + + # Could also have used a proc + # before_filter proc { |c| c.instance_variable_set("@address_book", c.session["address_book"] ||= AddressBookService.new) } + + def index + @title = "Address Book" + @people = @address_book.people + end + + def person + @person = @address_book.find_person(@params["id"]) + end + + def create_person + @address_book.create_person(@params["person"]) + redirect_to :action => "index" + end + + private + def initialize_session_storage + @address_book = @session["address_book"] ||= AddressBookService.new + end +end + +ActionController::Base.template_root = File.dirname(__FILE__) +# ActionController::Base.logger = Logger.new("debug.log") # Remove first comment to turn on logging in current dir + +begin + AddressBookController.process_cgi(CGI.new) if $0 == __FILE__ +rescue => e + CGI.new.out { "#{e.class}: #{e.message}" } +end
\ No newline at end of file diff --git a/actionpack/examples/address_book_controller.rbx b/actionpack/examples/address_book_controller.rbx new file mode 100644 index 0000000000..8c04eeccc8 --- /dev/null +++ b/actionpack/examples/address_book_controller.rbx @@ -0,0 +1,4 @@ +#!/usr/local/bin/ruby + +require "address_book_controller" +AddressBookController.process_cgi(CGI.new)
\ No newline at end of file diff --git a/actionpack/examples/benchmark.rb b/actionpack/examples/benchmark.rb new file mode 100644 index 0000000000..1e10a0c962 --- /dev/null +++ b/actionpack/examples/benchmark.rb @@ -0,0 +1,52 @@ +$:.unshift(File.dirname(__FILE__) + "/../lib") + +require "action_controller" +require 'action_controller/test_process' + +Person = Struct.new("Person", :name, :address, :age) + +class BenchmarkController < ActionController::Base + def message + render_text "hello world" + end + + def list + @people = [ Person.new("David"), Person.new("Mary") ] + render_template "hello: <% for person in @people %>Name: <%= person.name %><% end %>" + end + + def form_helper + @person = Person.new "david", "hyacintvej", 24 + render_template( + "<% person = Person.new 'Mary', 'hyacintvej', 22 %> " + + "change the name <%= text_field 'person', 'name' %> and <%= text_field 'person', 'address' %> and <%= text_field 'person', 'age' %>" + ) + end +end + +#ActionController::Base.template_root = File.dirname(__FILE__) + +require "benchmark" + +RUNS = ARGV[0] ? ARGV[0].to_i : 50 + +require "profile" if ARGV[1] + +runtime = Benchmark.measure { + RUNS.times { BenchmarkController.process_test(ActionController::TestRequest.new({ "action" => "list" })) } +} + +puts "List: #{RUNS / runtime.real}" + + +runtime = Benchmark.measure { + RUNS.times { BenchmarkController.process_test(ActionController::TestRequest.new({ "action" => "message" })) } +} + +puts "Message: #{RUNS / runtime.real}" + +runtime = Benchmark.measure { + RUNS.times { BenchmarkController.process_test(ActionController::TestRequest.new({ "action" => "form_helper" })) } +} + +puts "Form helper: #{RUNS / runtime.real}" diff --git a/actionpack/examples/benchmark_with_ar.fcgi b/actionpack/examples/benchmark_with_ar.fcgi new file mode 100755 index 0000000000..b9de370e24 --- /dev/null +++ b/actionpack/examples/benchmark_with_ar.fcgi @@ -0,0 +1,89 @@ +#!/usr/local/bin/ruby + +begin + +$:.unshift(File.dirname(__FILE__) + "/../lib") +$:.unshift(File.dirname(__FILE__) + "/../../../edge/activerecord/lib") + +require 'fcgi' +require 'action_controller' +require 'action_controller/test_process' + +require 'active_record' + +class Post < ActiveRecord::Base; end + +ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "basecamp") + +SESSION_OPTIONS = { "database_manager" => CGI::Session::MemoryStore } + +class TestController < ActionController::Base + def index + render_template <<-EOT + <% for post in Post.find_all(nil,nil,100) %> + <%= post.title %> + <% end %> + EOT + end + + def show_one + render_template <<-EOT + <%= Post.find_first.title %> + EOT + end + + def text + render_text "hello world" + end + + def erb_text + render_template "hello <%= 'world' %>" + end + + def erb_loop + render_template <<-EOT + <% for post in 1..100 %> + <%= post %> + <% end %> + EOT + end + + def rescue_action(e) puts e.message + e.backtrace.join("\n") end +end + +if ARGV.empty? && ENV["REQUEST_URI"] + FCGI.each_cgi do |cgi| + TestController.process(ActionController::CgiRequest.new(cgi, SESSION_OPTIONS), ActionController::CgiResponse.new(cgi)).out + end +else + if ARGV.empty? + cgi = CGI.new + end + + require 'benchmark' + require 'profile' if ARGV[2] == "profile" + + RUNS = ARGV[1] ? ARGV[1].to_i : 50 + + runtime = Benchmark::measure { + RUNS.times { + if ARGV.empty? + TestController.process(ActionController::CgiRequest.new(cgi, SESSION_OPTIONS), ActionController::CgiResponse.new(cgi)) + else + response = TestController.process_test( + ActionController::TestRequest.new({"action" => ARGV[0]}) + ) + puts(response.body) if ARGV[2] == "show" + end + } + } + + puts "Runs: #{RUNS}" + puts "Avg. runtime: #{runtime.real / RUNS}" + puts "Requests/second: #{RUNS / runtime.real}" +end + +rescue Exception => e + # CGI.new.out { "<pre>" + e.message + e.backtrace.join("\n") + "</pre>" } + $stderr << e.message + e.backtrace.join("\n") +end
\ No newline at end of file diff --git a/actionpack/examples/blog_controller.cgi b/actionpack/examples/blog_controller.cgi new file mode 100755 index 0000000000..e64fe85f0c --- /dev/null +++ b/actionpack/examples/blog_controller.cgi @@ -0,0 +1,53 @@ +#!/usr/local/bin/ruby + +$:.unshift(File.dirname(__FILE__) + "/../lib") + +require "action_controller" + +Post = Struct.new("Post", :title, :body) + +class BlogController < ActionController::Base + before_filter :initialize_session_storage + + def index + @posts = @session["posts"] + + render_template <<-"EOF" + <html><body> + <%= @flash["alert"] %> + <h1>Posts</h1> + <% @posts.each do |post| %> + <p><b><%= post.title %></b><br /><%= post.body %></p> + <% end %> + + <h1>Create post</h1> + <form action="create"> + Title: <input type="text" name="post[title]"><br> + Body: <textarea name="post[body]"></textarea><br> + <input type="submit" value="save"> + </form> + + </body></html> + EOF + end + + def create + @session["posts"].unshift(Post.new(@params["post"]["title"], @params["post"]["body"])) + flash["alert"] = "New post added!" + redirect_to :action => "index" + end + + private + def initialize_session_storage + @session["posts"] = [] if @session["posts"].nil? + end +end + +ActionController::Base.template_root = File.dirname(__FILE__) +# ActionController::Base.logger = Logger.new("debug.log") # Remove first comment to turn on logging in current dir + +begin + BlogController.process_cgi(CGI.new) if $0 == __FILE__ +rescue => e + CGI.new.out { "#{e.class}: #{e.message}" } +end
\ No newline at end of file diff --git a/actionpack/examples/debate/index.rhtml b/actionpack/examples/debate/index.rhtml new file mode 100644 index 0000000000..ddaa87da57 --- /dev/null +++ b/actionpack/examples/debate/index.rhtml @@ -0,0 +1,14 @@ +<html> +<body> +<h1>Topics</h1> + +<%= link_to "New topic", :action => "new_topic" %> + +<ul> +<% for topic in @topics %> + <li><%= link_to "#{topic.title} (#{topic.replies.length} replies)", :action => "topic", :path_params => { "id" => topic.id } %></li> +<% end %> +</ul> + +</body> +</html>
\ No newline at end of file diff --git a/actionpack/examples/debate/new_topic.rhtml b/actionpack/examples/debate/new_topic.rhtml new file mode 100644 index 0000000000..f52a69cc31 --- /dev/null +++ b/actionpack/examples/debate/new_topic.rhtml @@ -0,0 +1,22 @@ +<html> +<body> +<h1>New topic</h1> + +<form action="<%= url_for(:action => "create_topic") %>" method="post"> + <p> + Title:<br> + <input type="text" name="topic[title]"> + </p> + + <p> + Body:<br> + <textarea name="topic[body]" style="width: 200px; height: 200px"></textarea> + </p> + + <p> + <input type="submit" value="Create topic"> + </p> +</form> + +</body> +</html>
\ No newline at end of file diff --git a/actionpack/examples/debate/topic.rhtml b/actionpack/examples/debate/topic.rhtml new file mode 100644 index 0000000000..e247c00f0d --- /dev/null +++ b/actionpack/examples/debate/topic.rhtml @@ -0,0 +1,32 @@ +<html> +<body> +<h1><%= @topic.title %></h1> + +<p><%= @topic.body %></p> + +<%= link_to "Back to topics", :action => "index" %> + +<% unless @topic.replies.empty? %> + <h2>Replies</h2> + <ol> + <% for reply in @topic.replies %> + <li><%= reply.body %></li> + <% end %> + </ol> +<% end %> + +<h2>Reply to this topic</h2> + +<form action="<%= url_for(:action => "create_reply") %>" method="post"> + <input type="hidden" name="reply[topic_id]" value="<%= @topic.id %>"> + <p> + <textarea name="reply[body]" style="width: 200px; height: 200px"></textarea> + </p> + + <p> + <input type="submit" value="Create reply"> + </p> +</form> + +</body> +</html>
\ No newline at end of file diff --git a/actionpack/examples/debate_controller.cgi b/actionpack/examples/debate_controller.cgi new file mode 100755 index 0000000000..b82ac6259d --- /dev/null +++ b/actionpack/examples/debate_controller.cgi @@ -0,0 +1,57 @@ +#!/usr/local/bin/ruby + +$:.unshift(File.dirname(__FILE__) + "/../lib") + +require "action_controller" + +Topic = Struct.new("Topic", :id, :title, :body, :replies) +Reply = Struct.new("Reply", :body) + +class DebateService + attr_reader :topics + + def initialize() @topics = [] end + def create_topic(data) topics.unshift(Topic.new(next_topic_id, data["title"], data["body"], [])) end + def create_reply(data) find_topic(data["topic_id"]).replies << Reply.new(data["body"]) end + def find_topic(topic_id) topics.select { |topic| topic.id == topic_id.to_i }.first end + def next_topic_id() topics.first.id + 1 end +end + +class DebateController < ActionController::Base + before_filter :initialize_session_storage + + def index + @topics = @debate.topics + end + + def topic + @topic = @debate.find_topic(@params["id"]) + end + + # def new_topic() end <-- This is not needed as the template doesn't require any assigns + + def create_topic + @debate.create_topic(@params["topic"]) + redirect_to :action => "index" + end + + def create_reply + @debate.create_reply(@params["reply"]) + redirect_to :action => "topic", :path_params => { "id" => @params["reply"]["topic_id"] } + end + + private + def initialize_session_storage + @session["debate"] = DebateService.new if @session["debate"].nil? + @debate = @session["debate"] + end +end + +ActionController::Base.template_root = File.dirname(__FILE__) +# ActionController::Base.logger = Logger.new("debug.log") # Remove first comment to turn on logging in current dir + +begin + DebateController.process_cgi(CGI.new) if $0 == __FILE__ +rescue => e + CGI.new.out { "#{e.class}: #{e.message}" } +end
\ No newline at end of file |