Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web Applications With Rails (not Ruby)

Web Applications With Rails (not Ruby)

Slides from my Arrrrcamp 2013 talk.

David Padilla

October 03, 2013
Tweet

More Decks by David Padilla

Other Decks in Programming

Transcript

  1. <% set conn=Server.CreateObject("ADODB.Connection") conn.Open "northwind" %> <html> <head> <title>Super App</title>

    <link href="style.css" /> </head> <body> <h1>Page 1</h1> <% ' Some code here that connects to the DB and ' displays stuff %> </body> </html> page1.asp
  2. <% set conn=Server.CreateObject("ADODB.Connection") conn.Open "northwind" %> <html> <head> <title>Super App</title>

    <link href="style.css" /> </head> <body> <h1>Another Page</h1> <% ' Some code here that connects to the DB and ' displays stuff %> </body> </html> AnotherPage.asp
  3. Index.asp <!--#include file="database.asp"--> <html> <head> <title>Super App</title> <link href="style.css" />

    </head> <body> <% page = Request.QueryString("page") Select Case page Case "page1" %> <!--#include file="page1.asp"--><% Case "AnotherPage" %> <!--#include file="AnotherPage.asp"--><% End Select %> </body> </html>
  4. MVC

  5. <% $greeting = "Hello World" %> <% require "includes/stylesheets.rb" %>

    <html> <head> <title>RHP Rocks!</title> <%= Stylesheets.render() %> </head> <body> <h1><%= $greeting %></h1> <p>RHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to RHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual.</p> </body> </html> cgi/index.erb
  6. <% $greeting = "Hello World" %> <% require "includes/stylesheets.rb" %>

    <html> <head> <title>RHP Rocks!</title> <%= Stylesheets.render() %> </head> <body> <h1><%= $greeting %></h1> <p>RHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to RHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual.</p> </body> </html>
  7. config.ru run -> (env) { [200, headers, [body]] } body

    = "<html><body><h1>Hello World</h1></body><html>" headers = { "Content-Length" => body.length.to_s }
  8. config.ru run -> (env) { [200, headers, [body]] } body

    = "<html><body><h1>Hello World</h1></body><html>" headers = { "Content-Length" => body.length.to_s }
  9. config.ru run -> (env) { [200, headers, [body]] } body

    = "<html><body><h1>Hello World</h1></body><html>" headers = { "Content-Length" => body.length.to_s }
  10. app.rb class App def call(env) body = "<html><body><h1>Hello World</h1></body><html>" headers

    = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end
  11. app.rb class App def call(env) body = "<html><body><h1>Hello World</h1></body><html>" headers

    = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end RootController.new.show end end
  12. app.rb class App def call(env) path = env["REQUEST_PATH"] case path

    when %r{^/$} RootController.new.show else [404, { "Content-Length" => "9"}, ["Not found"]] end end end
  13. app.rb class App def call(env) path = env["REQUEST_PATH"] case path

    when %r{^/$} RootController.new.show else [404, { "Content-Length" => "9"}, ["Not found"]] end end end
  14. controllers/root_controller.rb class RootController # ... end def render(view_path) template =

    File.open(view_path, 'r').read Haml::Engine.new(template).render end
  15. controllers/root_controller.rb class RootController def show headers = { "Content-Length" =>

    body.length.to_s } [200, headers, [body]] end def render # ... end end body = render('views/root/show.html.haml') body = "<html><body><h1>Hello World</h1></body><html>"
  16. controllers/root_controller.rb class RootController def render(view_path) template = File.open(view_path, 'r').read Haml::Engine.new(template).render

    end def render_with_layout(view_path) layout = File.open('views/layout/application.html.haml', 'r').re Haml::Engine.new(layout).render do render(view_path) end end end end
  17. controllers/root_controller.rb class RootController def show headers = { "Content-Length" =>

    body.length.to_s } [200, headers, [body]] end def render # ... end end body = render('views/root/show.html.haml') body = render_with_layout('views/root/show.html.haml')
  18. controllers/root_controller.rb class RootController def show body = render_with_layout('views/root/show.html.haml') headers =

    { "Content-Length" => body.length.to_s } [200, headers, [body]] end def render(view_path) template = File.open(view_path, 'r').read Haml::Engine.new(template).render end def render_with_layout(view_path) layout = File.open('views/layout/application.html.haml', 'r').re Haml::Engine.new(layout).render do render(view_path) end end end class RootController < Controller def show body = render_with_layout('views/root/show.html.haml') headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end
  19. controller.rb class Controller def render(view_path) template = load_template_file(view_path) Haml::Engine.new(template).render end

    def render_with_layout(view_path) layout_template = load_template_file(layout) Haml::Engine.new(layout_template).render do render_template(view_path) end end end
  20. controller.rb class Controller end attr_accessor :env def initialize(env) self.env =

    env end def params Rack::Utils.parse_nested_query(env["QUERY_STRING"]) end
  21. app.rb class App def call(env) path = env["REQUEST_PATH"] case path

    when %r{^/$} RootController.new(env).show else [404, { "Content-Length" => "9"}, ["Not found"]] end end end
  22. controller.rb class Controller def render(view_path) template = load_template_file(view_path) Haml::Engine.new(template).render end

    def render_with_layout(view_path) layout_template = load_template_file('... ') Haml::Engine.new(layout_template).render do render(view_path) end end end class Controller def render(view_path, context = self) template = load_template_file(view_path) Haml::Engine.new(template).render(context) end def render_with_layout(view_path, context = self) layout_template = load_template_file('... ') Haml::Engine.new(layout_template).render(context) do render(view_path, context) end end end `
  23. controllers/root_controller.rb class RootController < Controller def show body = render_with_layout('views/root/show.html.haml')

    headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end @name = params["name"] || "World"
  24. model/user.rb require 'pg' DB = PG.connect( dbname: 'test', host: 'localhost')

    class User def self.all result = DB.exec "SELECT * FROM users" fields = result.fields result.collect do |row| hash = {} row.each_with_index do |value, i| hash[fields[i]] = value end end end end