Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added a full example for ActiveRecord and HAML

  • Loading branch information...
commit 614f88514e9eec1ae1ecf3c08e38501c49de34cf 1 parent 4cbe53b
@blindgaenger authored
View
22 examples/active_record/app.rb
@@ -0,0 +1,22 @@
+require 'sinatra'
+
+
+# add some data the first time or use an already existing db
+configure :development do
+ load 'setup_activerecord.rb'
+
+ Person.create(:name => 'foo')
+ Person.create(:name => 'bar')
+ Person.create(:name => 'baz')
+end
+
+# sinatra-rest needs to be loaded after ActiveRecord
+$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../../lib')
+require 'sinatra/rest'
+
+rest Person
+
+get '/' do
+ redirect '/people'
+end
+
View
12 examples/active_record/db/migrate/20090924173700_create_people.rb
@@ -0,0 +1,12 @@
+class CreatePeople < ActiveRecord::Migration
+ def self.up
+ create_table :people do |t|
+ t.string :name
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :people
+ end
+end
View
15 examples/active_record/setup_activerecord.rb
@@ -0,0 +1,15 @@
+require 'activerecord'
+
+ActiveRecord::Base.logger = Logger.new(STDOUT)
+ActiveRecord::Base.logger.level = Logger::WARN
+
+config = {
+ :adapter => 'sqlite3',
+ :database => "db/#{Sinatra::Application.environment.to_s}.sqlite3"
+}
+ActiveRecord::Base.establish_connection(config)
+ActiveRecord::Migrator.up('db/migrate')
+
+class Person < ActiveRecord::Base
+end
+
View
50 examples/active_record/views/layout.haml
@@ -0,0 +1,50 @@
+!!! XML
+!!! Strict
+%html{html_attrs}
+ %head
+ %meta{:"http-equiv"=>"content-type", :content=>"text/html; charset=UTF-8"}/
+ %title People
+ %style{:type=>"text/css", :media=>"screen"}
+ :sass
+ body
+ font-family: "Lucida Grande", sans-serif
+ font-size: 12px
+ background-color: #E9ECEB
+ color: #555555
+
+ #container
+ width: 400px
+ margin: 25px auto
+ padding: 10px 25px
+ background-color: #FFFFFF
+ border: 1px solid #D4D4D4
+ :-moz-border-radius 8px
+ :-webkit-border-radius 8px
+
+ #footer
+ text-align: center
+ color: #909090
+
+ h1
+ color: #383838
+ font-size: 2em
+ font-weight: bold
+
+ a
+ color: #075FB2
+ text-decoration: none
+
+ .buttons *
+ padding: 0 0.5em
+ text-align: center
+ margin: 1em 0
+ display: inline
+
+ %body
+
+ #container
+ = yield
+
+ #footer
+ powered by
+ %a{:href=>'http://blindgaenger.github.com/sinatra-rest/'} sinatra-rest
View
9 examples/active_record/views/people/edit.haml
@@ -0,0 +1,9 @@
+%h1 Edit
+
+%form{:action=>url_for_people_update(@person), :method=>"post"}
+ %input{:type=>"hidden", :name=>"_method", :value=>"put"}
+ Name:
+ %input{:name=>"name", :value=>@person.name}
+ .buttons
+ %a{:href => url_for_people_show(@person)} Cancel
+ %button{:type => :submit} Update
View
11 examples/active_record/views/people/index.haml
@@ -0,0 +1,11 @@
+%h1 Index
+
+%ul
+ - @people.each do |person|
+ %li
+ = person.id
+ %a{:href => url_for_people_show(person)}
+ = person.name
+
+.buttons
+ %a{:href => url_for_people_new} New
View
9 examples/active_record/views/people/new.haml
@@ -0,0 +1,9 @@
+%h1 New
+
+%form{:action=>url_for_people_create, :method=>"post"}
+ Name:
+ %input{:name=>"name"}
+ .buttons
+ %a{:href => url_for_people_index} Cancel
+ %button{:type => :submit} Create
+
View
20 examples/active_record/views/people/show.haml
@@ -0,0 +1,20 @@
+%h1 Show
+
+%table
+ %tr
+ %th Name
+ %th Value
+ %tr
+ %td id
+ %td= @person.id
+ %tr
+ %td Name
+ %td= @person.name
+
+.buttons
+ %a{:href => url_for_people_index} « Index
+ %a{:href => url_for_people_edit(@person)} Edit
+ %form{:action=>url_for_people_update(@person), :method=>"post"}
+ %input{:type=>"hidden", :name=>"_method", :value=>"delete"}
+ %button{:type => :submit} Delete
+
Please sign in to comment.
Something went wrong with that request. Please try again.