/
wakeup.rb
137 lines (113 loc) · 3.08 KB
/
wakeup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'rubygems'
require 'sinatra'
require 'activerecord'
ActiveRecord::Base.establish_connection(:adapter=>"sqlite3", :database=>"blog.sqlite3.db")
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
get '/' do
@posts = Post.all()
haml :index
end
get '/posts/new' do
@post = Post.new()
haml :post_new
end
post '/posts' do
Post.create(:title=>params[:title], :body=>params[:body]).save
redirect '/'
end
get '/rss' do
@posts = Post.all({:order=>"created_at DESC"})
content_type 'application/xml', :charset => 'utf-8'
builder({:layout=>false}) do |xml|
xml.instruct! :xml, :version => '1.0'
xml.rss :version => "2.0", "xmlns:atom"=>"http://www.w3.org/2005/Atom" do
xml.channel do
xml.title "Rubysztán"
xml.description "Minden ami Ruby."
xml.link "http://0.0.0.0:4567/"
xml.atom :link, :href => "http://0.0.0.0:4567/rss", :rel => "self", :type => "application/rss+xml"
@posts.each do |post|
xml.item do
xml.title post.title
xml.link "/posts/#{post.id}"
xml.description post.body
xml.pubDate Time.parse(post.created_at.to_s).rfc822()
xml.guid "/posts/#{post.id}"
end
end
end
end
end
end
get '/posts/:id/edit' do
@post = Post.find(params[:id])
haml :post_edit
end
get '/posts/:id' do
@post = Post.find(params[:id])
haml :post_show
end
put '/posts/:id' do
Post.find(params[:id]).update_attributes(:title=>params[:title], :body=>params[:body])
redirect "/posts/#{params[:id]}"
end
get '/posts/:id/delete' do
Post.find(params[:id]).destroy
redirect '/'
end
post '/posts/:id/comments' do
Comment.create(:post_id=>params[:id], :email=>params[:email], :body=>params[:body])
redirect "/posts/#{params[:id]}"
end
use_in_file_templates!
__END__
@@ layout
!!! Strict
%head
%title Sinatra - I did it my way
%body
%h1
%a{:href=>"/"} Sinatra - I did it my way
= yield
@@ index
-for post in @posts
%h2
%a{:href=>"/posts/#{post.id}"}= post.title
%a{:href=>"/posts/#{post.id}/edit"} / Edit
%a{:href=>"/posts/#{post.id}/delete"} / Destroy
%a{:href=>"/posts/new"} Add a new post!
%a{:href=>"rss"} RSS
@@ post_show
%h2= @post.title
#body= @post.body
- @post.comments.each do |comment|
%comment
%p= "#{comment.email} @ #{comment.created_at}"
%div= comment.body
%h3 Please, comment it!
%form{:action=>"/posts/#{@post.id}/comments", :method=>"post"}
%label{:for=>"email"}E-mail
%input{:type=>"text", :name=>"email"}
%label{:for=>"body"} Comment
%textarea{:name=>"body"}
%input{:type=>"submit", :value=>"Comment it!"}
@@ post_new
%form{:action=>"/posts", :method=>"post"}
= haml :post_form, :layout=>false
%input{:type=>"submit", :value=>"Post it!"}
@@ post_edit
%form{:action=>"/posts/#{@post.id}", :method=>"post"}
= haml :post_form, :layout=>false
%input{:type=>"hidden", :name=>"_method", :value=>"put"}
%input{:type=>"submit", :value=>"Edit & Post it!"}
@@ post_form
%h1 Create a new post
%label Title
%input{:name=>"title", :type=>"text", :value=>@post.title}
%label Body
%textarea{:name=>"body"}= @post.body