--- a/sinatra/events.rb
+++ b/sinatra/events.rb
@@ -8,8 +8,16 @@ DataMapper::Database.setup({
:database => 'events.db'
})
+class Hash
+ # Cool hack by Evan Phoenix
+ def &(keys)
+ reject{|k,v| !keys.include?(k)}
+ end
+end
+
class Event < DataMapper::Base
property :name, :string
+ property :url, :string
property :description, :text
end
Event.table.create!
@@ -20,20 +28,6 @@ get '/events' do
puts Event.all.inspect
@events = Event.all
haml(layout('Events', %q{
- %h1= "Events"
- %form{:action => '/events', :method => 'POST'}
- %fieldset
- %legend New Event
-
- %label{:for => 'name'} Name
- %input{:type => 'text', :name => 'name', :id => 'name', :size => 30}
- %br
-
- %label{:for => 'description'} Description
- %textarea{:type => 'description', :name => 'description', :id => 'description', :cols => 30, :rows => 4}
- %br
-
- %input{:type => 'submit', :class => 'submit', :value => 'Create'}
- if @events.empty?
%p No events found.
- else
@@ -47,19 +41,30 @@ get '/events' do
%td{:class => 'name'}
%a{:href => '#'}= e.name
= e.description
+ %form{:action => '/events', :method => 'POST'}
+ %fieldset
+ %legend New Event
+
+ %label{:for => 'name'} Name
+ %input{:type => 'text', :name => 'name', :id => 'name', :size => 30}
+ %br
+
+ %label{:for => 'description'} Description
+ %textarea{:type => 'description', :name => 'description', :id => 'description', :cols => 30, :rows => 4}
+ %br
+
+ %input{:type => 'submit', :class => 'submit', :value => 'Create', :name => 'foo[bar]'}
+
}))
end
post '/events' do
- name = params[:name].to_s.strip
- description = params[:description].to_s.strip
- x = Event.new(:name => name, :description => description).save
+ Event.new(params & [:name, :url, :description]).save
redirect '/events'
end
def layout(title, content)
- @saved = $ticgit.config['list_options'].keys rescue []
%Q(
%html
%head
@@ -91,17 +96,21 @@ a
form
label
+ width: 120px
float: left
- text-align: right
- width: 200px
- :font
- weight: bold
+ font-weight: bold
input, textarea
- margin-left: 210px
- display: block
- margin-bottom: 0pt
- width: 300px
+ width: 180px
+ margin-bottom: 5px
+
+ input.submit
+ width: 90px
+ margin-left: 120px
+ margin-top: 5px
+
+ br
+ clear: left
table
width: 100%
@@ -134,19 +143,3 @@ table
display: block
:font
weight: bold
-
-.submit
- font-size: large
- font-weight: bold
-
-.page_title
- font-size: xx-large
-
-.edit_link
- color: black
- font-size: 14px
- font-weight: bold
- background-color: #e0e0e0
- font-variant: small-caps
- text-decoration: none
- |