Permalink
Browse files

Added all the routes and views with specs.

  • Loading branch information...
rgraff committed Apr 25, 2010
1 parent 07bfdf5 commit a9845f929084e45c9922f219dbedd43f59a4d36e
View
39 app.rb
@@ -2,6 +2,45 @@
require 'sinatra'
require 'config/init'
+
get '/' do
+ @person = Person.rand
erb :home
end
+
+get '/people' do
+ @people = Person.all
+ erb :people_index
+end
+
+get '/people/new' do
+ @person = Person.new
+ erb :people_new
+end
+
+post '/people' do
+ person = Person.new(params).save or halt 400
+ redirect "/people/#{person.id}"
+end
+
+get '/people/:id' do
+ @person = Person[params[:id]] or halt 404
+ erb :people_show
+end
+
+get '/people/:id/edit' do
+ @person = Person[params[:id]] or halt 404
+ erb :people_edit
+end
+
+put '/people/:id' do
+ @person = Person[params[:id]] or halt 404
+ @person.update(params) or halt 400
+ redirect "/people/#{@person.id}"
+end
+
+delete '/people/:id' do
+ @person = Person[params[:id]] or halt 404
+ @person.delete
+ redirect '/people'
+end
View
@@ -2,14 +2,36 @@
require 'sqlite3'
configure :development do
- set :database, 'sqlite://development.sqlite'
+ set :database, 'sqlite://tmp/development.sqlite'
end
+
configure :test do
set :database, 'sqlite3::memory:'
end
require 'config/migrations'
+Sequel::Model.strict_param_setting = false
Dir["models/**/*.rb"].each{|model|
require model
}
+
+helpers do
+ include Rack::Utils
+ alias_method :h, :escape_html
+
+ def partial(template, *args)
+ template_array = template.to_s.split('/')
+ template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ options.merge!(:layout => false)
+ if collection = options.delete(:collection) then
+ collection.inject([]) do |buffer, member|
+ buffer << erb(:"#{template}", options.merge(:layout =>
+ false, :locals => {template_array[-1].to_sym => member}))
+ end.join("\n")
+ else
+ erb(:"#{template}", options)
+ end
+ end
+end
View
@@ -1,4 +1,9 @@
class Person < Sequel::Model
+ def self.rand
+ count = Person.count
+ Person.limit(1, Kernel.rand(count) ).first if (count > 0)
+ end
+
def validate
errors.add(:name, "can't be empty") if name.nil? || name.empty?
end
View
@@ -7,10 +7,109 @@ def app
@app ||= Sinatra::Application
end
- it "should respond to /" do
- get '/'
- last_response.should be_ok
- last_response.body.should include('Hello World')
+ describe "get /" do
+ it "should say Hello World if there are no people" do
+ Person.delete
+ get '/'
+ last_response.should be_ok
+ last_response.body.should match /Hello World/
+ end
+
+ it "should say Hello to someone if there is a person" do
+ Person.delete
+ Person.new(:name => 'Maggie').save
+ get '/'
+ last_response.should be_ok
+ last_response.body.should match /Hello Maggie/
+ end
+ end
+
+ describe "get /people" do
+ it "should show a list of people" do
+ Person.delete
+ Person.new(:name => 'Maggie').save
+ Person.new(:name => 'Audrey').save
+ get '/people'
+ last_response.should be_ok
+ last_response.body.should match /Maggie/
+ last_response.body.should match /Audrey/
+ end
+ end
+
+ describe "get /people/new" do
+ it "should have a form" do
+ get '/people/new'
+ last_response.should be_ok
+ last_response.should match /<form action="\/people" method="post">/
+ end
+ end
+
+ describe "post /people" do
+ it "should create a new person" do
+ Person.delete
+ post '/people', :name => 'Jane'
+
+ person = Person.first
+ person.should_not be_nil
+
+ follow_redirect!
+
+ last_request.url.should == "http://example.org/people/#{person.id}"
+ last_response.should be_ok
+ end
+ end
+
+ describe "get /people/:id" do
+ it 'should show the person' do
+ person = Person.new(:name => 'Maggie').save
+ get "/people/#{person.id}"
+
+ last_response.should be_ok
+ last_response.should match /Maggie/
+ end
+ end
+
+ describe "get /people/:id/edit" do
+ before :each do
+ @person = Person.new(:name => 'Maggie').save
+ get "/people/#{@person.id}/edit"
+ end
+
+ it "should have a form" do
+ last_response.should be_ok
+ last_response.should match /<form action="\/people/#{@person.id}" method="post">/
+ end
+
+ it "should have the fields defaulted" do
+ last_response.should be_ok
+ last_response.should match /<input type="text" name="name" value="Maggie"\/>/
+ end
+ end
+
+ describe "put /people/:id" do
+ it "should update the record" do
+ person = Person.new(:name => 'Maggie').save
+ put "/people/#{person.id}", :name => 'Margaret'
+ person.reload.name.should == 'Margaret'
+
+ follow_redirect!
+
+ last_request.url.should == "http://example.org/people/#{person.id}"
+ last_response.should be_ok
+ end
+ end
+
+ describe "delete /people/:id" do
+ it "should delete the record" do
+ person = Person.new(:name => 'Maggie').save
+ delete "/people/#{person.id}"
+ lambda{person.reload}.should raise_error
+
+ follow_redirect!
+
+ last_request.url.should == "http://example.org/people"
+ last_response.should be_ok
+ end
end
end
View
@@ -1,6 +1,34 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe Person do
+ before :each do
+ Person.delete
+ end
+ describe "#rand" do
+ describe "with no records" do
+ it "should return nil" do
+ Person.rand.should be_nil
+ end
+ end
+ describe "with one record" do
+ before :each do
+ @person = Person.new(:name => "Maggie").save
+ end
+ it "should return the one record" do
+ Person.rand.should == @person
+ end
+ end
+ describe "with two records" do
+ before :each do
+ @person = Person.new(:name => "Maggie").save
+ @another = Person.new(:name => "Audrey").save
+ end
+ it "should return one or the other" do
+ [@person, @another].should include(Person.rand)
+ end
+ end
+ end
+
describe "validations" do
it "should require a name" do
Person.new().should_not be_valid
View
@@ -3,9 +3,12 @@
require 'rack/test'
require 'ruby-debug'
require 'spec'
+require 'spec/autorun'
+require 'spec/interop/test'
# set test environment
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false
+
View
No changes.
View
@@ -0,0 +1,11 @@
+<form action="/people<%= "/#{person.id}" unless person.new? %>" method="post">
+<%= "<input type=\"hidden\" name=\"_method\" value=\"put\" />" unless person.new? %>
+ <p>
+ <label>Name</label><br />
+ <input type="text" name="name" value="<%=h person.name %>"/>
+ </p>
+ <p>
+ <input type="submit" /> | <a href="/people<%= "/#{person.id}" unless person.new? %>">back</a>
+ </p>
+</form>
+
View
@@ -1,3 +1,4 @@
<div style="text-align: center;">
- <h1>Hello World</h1>
+ <h1>Hello <%=h @person ? @person.name : 'World' %></h1>
+ <p><a href="/">Refresh</a> | <a href="/people">People</a></p>
</div>
View
@@ -0,0 +1,2 @@
+<h1>Edit person</h1>
+<%= partial :people_form, :locals => { :person => @person } %>
View
@@ -0,0 +1,18 @@
+<h1>People</h1>
+<p><a href="/">Say Hello</a></p>
+<table>
+ <tr>
+ <th>Name</td>
+ <th></td>
+ </tr>
+
+<% @people.each do |person| %>
+ <tr>
+ <td><%=h person.name %></td>
+ <td><form id="delete_<%= person.id%>" action="/people/<%= person.id %>" method="post"><input type="hidden" name="_method" value="delete" /></form>
+ <a href="/people/<%= person.id %>">Show</a> | <a href="/people/<%= person.id %>/edit">Edit</a> | <a href="javascript:document.forms['delete_<%= person.id %>'].submit()">Delete</a></td>
+ </tr>
+<% end %>
+</table>
+
+<p><a href="/people/new">New Person</a></p>
View
@@ -0,0 +1,2 @@
+<h1>New person</h1>
+<%= partial :people_form, :locals => {:action=>'/people', :method=>:'post', :back => '/people', :person => @person } %>
View
@@ -0,0 +1,5 @@
+<h1><%=h @person.name %></h1>
+
+<p><a href="/people/<%= @person.id %>/edit">Edit</a> | <a href="javascript:document.forms['delete_<%= @person.id%>'].submit();">Delete</a> | <a href="/people">List</a></p>
+<form id="delete_<%= @person.id%>" action="/people/<%= @person.id %>" method="post"><input type="hidden" name="_method" value="delete" /></form>
+

0 comments on commit a9845f9

Please sign in to comment.