Permalink
Browse files

Renders some basic forms

  • Loading branch information...
0 parents commit 4289e8873427598d949c305bc1b183ad1fcfeaaa @namelessjon committed Jun 29, 2010
Showing with 226 additions and 0 deletions.
  1. +67 −0 address.rb
  2. +84 −0 templates/edit.html
  3. +6 −0 templates/index.html
  4. +11 −0 templates/layout.html
  5. +6 −0 views.rb
  6. +35 −0 views/edit.rb
  7. +7 −0 views/index.rb
  8. +10 −0 views/layout.rb
@@ -0,0 +1,67 @@
+#!/usr/bin/ruby
+# Jonathan D. Stott <jonathan.stott@gmail.com>
+require 'sinatra'
+require 'mongo'
+require 'views'
+
+configure do
+ DB = Mongo::Connection.new('10.0.0.9').db('jon')
+end
+
+set :mustache, { :templates => 'templates' }
+
+helpers do
+ def addresses
+ DB['addresses']
+ end
+
+ def clean_params(param_set=params)
+ p param_set
+ case param_set
+ when Array
+ param_set.each do |v|
+ if Array === v or Hash === v
+ clean_params(v)
+ end
+ end
+ param_set.delete_if { |v| v.empty? or v =~ /\A\s*\z/ }
+ when Hash
+ param_set.each do |k, v|
+ if Array === v or Hash === v
+ clean_params(v)
+ end
+ end
+ param_set.delete_if { |k, v| v.empty? or v =~ /\A\s*\z/ }
+ else
+ param_set
+ end
+ end
+end
+
+get '/' do
+ @addresses = addresses.find({}, :fields => [:name, :page], :sort => [[:page, :asc]]).to_a
+ mustache :index
+end
+
+get '/new' do
+ @person = {}
+ mustache :edit
+end
+
+get '/:page/edit' do
+ @person = addresses.find_one(:page => params['page'])
+ not_found unless @person
+ p @person
+ mustache :edit
+end
+
+post '/:page' do |page|
+ # clean up the params
+ params.delete_if { |k,v| !%w|person|.include?(k) }
+ params['person'] and params['person'].delete_if { |k,v| !%w|name page numbers emails addresses|.include?(k) }
+ clean_params
+
+
+ p params
+ redirect "/#{page}/edit"
+end
@@ -0,0 +1,84 @@
+<h1>{{title}}</h1>
+<form action='{{action}}' method='post'>
+ <fieldset>
+ <legend>Details</legend>
+ <dl>
+ <dt><label for='name'>Name</label></dt>
+ <dd><input id='name' type='text' size='30' name='person[name]' value='{{name}}' /></dd>
+ <dt><label for='page'>Page</label></dt>
+ <dd><input id='page' type='text' size='30' name='person[page]' value='{{page}}' /></dd>
+ </dl>
+ </fieldset>
+ <fieldset id='numbers'>
+ <legend>Phone Numbers</legend>
+ <table>
+ <tr><th>Name</th><th>Number</th></tr>
+ {{#numbers}}
+ <tr>
+ <td><input type='text' size='20' name='person[numbers][][name]' value='{{name}}' /></td>
+ <td><input type='text' size='30' name='person[numbers][][number]' value='{{number}}' /></td>
+ </tr>
+ {{/numbers}}
+ <tr>
+ <td><input type='text' size='20' name='person[numbers][][name]' value='' /></td>
+ <td><input type='text' size='30' name='person[numbers][][number]' value='' /></td>
+ </tr>
+ </table>
+ <a id='add_number' href='#'>Add Number</a>
+ </fieldset>
+ <fieldset id='emails'>
+ <legend>Emails</legend>
+ <table>
+ <tr><th>Name</th><th>Email</th></tr>
+ {{#emails}}
+ <tr>
+ <td><input type='text' size='20' name='person[emails][][name]' value='{{name}}' /></td>
+ <td><input type='email' size='30' name='person[emails][][email]' value='{{email}}' /></td>
+ </tr>
+ {{/emails}}
+ <tr>
+ <td><input type='text' size='20' name='person[emails][][name]' value='' /></td>
+ <td><input type='email' size='30' name='person[emails][][email]' value='' /></td>
+ </tr>
+ </table>
+ <a id='add_email' href='#'>Add Email</a>
+ </fieldset>
+ <fieldset id='addresses'>
+ <legend>Addresses</legend>
+ <ul>
+ {{#adresses}}
+ <li>
+ <dl>
+ <dt>Name</dt>
+ <dd><input type='text' size='20' name='person[addresses][][name]' value='{{name}}' /></dd>
+ <dt>Address</dt>
+ <dd>
+ <textarea rows='5' cols='30' name='person[addresses][][address]'>{{address}}</textarea>
+ </dd>
+ <dt>Postcode</dt>
+ <dd><input type='text' size='10' name='person[addresses][][postcode]' value='{{postcode}}' /></dd>
+ <dt>Country</dt>
+ <dd><input type='text' size='10' name='person[addresses][][country]' value='{{country}}' /></dd>
+ </dl>
+ </li>
+ {{/adresses}}
+ <li>
+ <dl>
+ <dt>Name</dt>
+ <dd><input type='text' size='20' name='person[addresses][][name]' value='' /></dd>
+ <dt>Address</dt>
+ <dd>
+ <textarea rows='5' cols='30' name='person[addresses][][address]'></textarea>
+ </dd>
+ <dt>Postcode</dt>
+ <dd><input type='text' size='10' name='person[addresses][][postcode]' value='' /></dd>
+ <dt>Country</dt>
+ <dd><input type='text' size='10' name='person[addresses][][country]' value='' /></dd>
+ </dl>
+ </li>
+ </ul>
+ <a id='add_address' href='#'>Add Address</a>
+ </fieldset>
+ <input type='hidden' name='foo' value='bar' />
+ <input type='submit' value='{{button_text}}' />
+</form>
@@ -0,0 +1,6 @@
+<h1>{{title}}</h1>
+<ul>
+ {{#addresses}}
+ <li><a href='/{{page}}'>{{name}}</a></li>
+ {{/addresses}}
+</ul>
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>{{title}}</title>
+ </head>
+ <body>
+ <div id='content'>
+ {{{yield}}}
+ </div>
+ </body>
+</html>
@@ -0,0 +1,6 @@
+#!/usr/bin/ruby
+# Jonathan D. Stott <jonathan.stott@gmail.com>
+require 'mustache/sinatra'
+require 'views/layout'
+require 'views/index'
+require 'views/edit'
@@ -0,0 +1,35 @@
+#!/usr/bin/ruby
+# Jonathan D. Stott <jonathan.stott@gmail.com>
+class Edit < Layout
+ def action
+ page ? "/#{page}" : '/'
+ end
+
+ def button_text
+ page ? "Update" : "Create"
+ end
+
+ def title
+ page ? "Editing #{name} - #{super}" : "New - #{super}"
+ end
+
+ def name
+ @person['name']
+ end
+
+ def page
+ @person['page']
+ end
+
+ def emails
+ @person['emails']
+ end
+
+ def addresses
+ @person['addresses']
+ end
+
+ def numbers
+ @person['numbers']
+ end
+end
@@ -0,0 +1,7 @@
+#!/usr/bin/ruby
+# Jonathan D. Stott <jonathan.stott@gmail.com>
+class Index < Layout
+ def addresses
+ @addresses
+ end
+end
@@ -0,0 +1,10 @@
+#!/usr/bin/ruby
+# Jonathan D. Stott <jonathan.stott@gmail.com>
+class Layout < Mustache
+ self.template_extension = 'html'
+ self.template_path = '../templates'
+
+ def title
+ 'Addresses!'
+ end
+end

0 comments on commit 4289e88

Please sign in to comment.