From db045dbbf60b53dbe013ef25554fd013baf88134 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 24 Nov 2004 01:04:44 +0000 Subject: Initial git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/examples/.htaccess | 24 +++++++ actionpack/examples/address_book/index.rhtml | 33 +++++++++ actionpack/examples/address_book/layout.rhtml | 8 +++ actionpack/examples/address_book_controller.cgi | 9 +++ actionpack/examples/address_book_controller.fcgi | 6 ++ actionpack/examples/address_book_controller.rb | 52 ++++++++++++++ actionpack/examples/address_book_controller.rbx | 4 ++ actionpack/examples/benchmark.rb | 52 ++++++++++++++ actionpack/examples/benchmark_with_ar.fcgi | 89 ++++++++++++++++++++++++ actionpack/examples/blog_controller.cgi | 53 ++++++++++++++ actionpack/examples/debate/index.rhtml | 14 ++++ actionpack/examples/debate/new_topic.rhtml | 22 ++++++ actionpack/examples/debate/topic.rhtml | 32 +++++++++ actionpack/examples/debate_controller.cgi | 57 +++++++++++++++ 14 files changed, 455 insertions(+) create mode 100644 actionpack/examples/.htaccess create mode 100644 actionpack/examples/address_book/index.rhtml create mode 100644 actionpack/examples/address_book/layout.rhtml create mode 100755 actionpack/examples/address_book_controller.cgi create mode 100755 actionpack/examples/address_book_controller.fcgi create mode 100644 actionpack/examples/address_book_controller.rb create mode 100644 actionpack/examples/address_book_controller.rbx create mode 100644 actionpack/examples/benchmark.rb create mode 100755 actionpack/examples/benchmark_with_ar.fcgi create mode 100755 actionpack/examples/blog_controller.cgi create mode 100644 actionpack/examples/debate/index.rhtml create mode 100644 actionpack/examples/debate/new_topic.rhtml create mode 100644 actionpack/examples/debate/topic.rhtml create mode 100755 actionpack/examples/debate_controller.cgi (limited to 'actionpack/examples') 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 @@ + + RubyRequire apache/ruby-run + RubySafeLevel 0 + + + SetHandler ruby-object + RubyHandler Apache::RubyRun.instance + + + + +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 @@ +

Address Book

+ +<% if @people.empty? %> +

No people in the address book yet

+<% else %> + + + <% for person in @people %> + + <% end %> +
NameEmail AddressPhone Number
<%= person.name %><%= person.email_address %><%= person.phone_number %>
+<% end %> + +
+

+ Name:
+ +

+ +

+ Email address:
+ +

+ +

+ Phone number:
+ +

+ +

+ +

+
\ 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 @@ + + + <%= @title || "Untitled" %> + + +<%= @content_for_layout %> + + \ 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 { "
" + e.message + e.backtrace.join("\n") + "
" } + $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" + + <%= @flash["alert"] %> +

Posts

+ <% @posts.each do |post| %> +

<%= post.title %>
<%= post.body %>

+ <% end %> + +

Create post

+
+ Title:
+ Body:
+ +
+ + + 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 @@ + + +

Topics

+ +<%= link_to "New topic", :action => "new_topic" %> + + + + + \ 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 @@ + + +

New topic

+ +
" method="post"> +

+ Title:
+ +

+ +

+ Body:
+ +

+ +

+ +

+
+ + + \ 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 @@ + + +

<%= @topic.title %>

+ +

<%= @topic.body %>

+ +<%= link_to "Back to topics", :action => "index" %> + +<% unless @topic.replies.empty? %> +

Replies

+
    + <% for reply in @topic.replies %> +
  1. <%= reply.body %>
  2. + <% end %> +
+<% end %> + +

Reply to this topic

+ +
" method="post"> + +

+ +

+ +

+ +

+
+ + + \ 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 -- cgit v1.2.3