reststop 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +56 -52
- data/LICENSE.txt +21 -21
- data/Manifest.txt +14 -12
- data/{README.txt → README.rdoc} +125 -125
- data/Rakefile +56 -54
- data/examples/blog.rb +400 -400
- data/lib/reststop.rb +451 -450
- data/lib/reststop/version.rb +9 -9
- data/setup.rb +1585 -1585
- data/test/test.rb +31 -0
- metadata +45 -60
- data/.loadpath +0 -9
- data/.project +0 -17
data/examples/blog.rb
CHANGED
@@ -1,400 +1,400 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
#require 'ruby-debug' # @techarch : commented out since only needed for local debugging
|
5
|
-
|
6
|
-
require 'markaby' # @techarch : added explicit require
|
7
|
-
require 'camping' # @techarch
|
8
|
-
require 'camping/session' # @techarch : added explicit require since session has changed in Camping 2.0
|
9
|
-
|
10
|
-
gem 'RedCloth' # @techarch : added since it is referenced in the Posts model
|
11
|
-
require 'redcloth' # @techarch : added
|
12
|
-
|
13
|
-
#gem 'camping', '~> 2.0'
|
14
|
-
gem 'camping' , '>= 2.0' # @techarch : updated version
|
15
|
-
|
16
|
-
#gem 'reststop', '~> 0.3'
|
17
|
-
|
18
|
-
=begin # @techarch : commented out since only needed for local debugging
|
19
|
-
$: << '../../camping/lib'
|
20
|
-
$: << '../lib'
|
21
|
-
require 'camping-unabridged'
|
22
|
-
require 'camping/ar'
|
23
|
-
require 'camping/session'
|
24
|
-
=end
|
25
|
-
|
26
|
-
#begin # @techarch : commented out since only needed for local debugging
|
27
|
-
# try to use local copy of library
|
28
|
-
#require '../lib/reststop2'
|
29
|
-
$: << '../lib/'
|
30
|
-
require 'reststop.rb' # @techarch : adjusted so that it is located in the same current folder
|
31
|
-
#rescue LoadError
|
32
|
-
# # ... otherwise default to rubygem
|
33
|
-
# require 'reststop'
|
34
|
-
#end
|
35
|
-
|
36
|
-
Camping.goes :Blog
|
37
|
-
|
38
|
-
module Blog
|
39
|
-
include Camping::Session
|
40
|
-
include Reststop
|
41
|
-
|
42
|
-
Controllers.extend Reststop::Controllers
|
43
|
-
end
|
44
|
-
|
45
|
-
module Blog::Base
|
46
|
-
alias camping_render render
|
47
|
-
alias camping_lookup lookup # @techarch: required if camping > 2.0
|
48
|
-
alias camping_service service
|
49
|
-
include Reststop::Base
|
50
|
-
alias service reststop_service
|
51
|
-
alias render reststop_render
|
52
|
-
|
53
|
-
# Overrides the new Tilt-centric lookup method In camping
|
54
|
-
# RESTstop needs to have a first try at looking up the view
|
55
|
-
# located in the Views::HTML module.
|
56
|
-
def lookup(n)
|
57
|
-
T.fetch(n.to_sym) do |k|
|
58
|
-
t = Blog::Views::HTML.method_defined?(k) || camping_lookup(n)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
module Blog::Models
|
64
|
-
class Post < Base
|
65
|
-
belongs_to :user
|
66
|
-
|
67
|
-
before_save do |record|
|
68
|
-
cloth = RedCloth.new(record.body)
|
69
|
-
cloth.hard_breaks = false
|
70
|
-
record.html_body = cloth.to_html
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
class Comment < Base; belongs_to :user; end
|
75
|
-
class User < Base; end
|
76
|
-
|
77
|
-
class BasicFields < V 1.1
|
78
|
-
def self.up
|
79
|
-
create_table :blog_posts, :force => true do |t|
|
80
|
-
t.integer :user_id, :null => false
|
81
|
-
t.string :title, :limit => 255
|
82
|
-
t.text :body, :html_body
|
83
|
-
t.timestamps
|
84
|
-
end
|
85
|
-
create_table :blog_users, :force => true do |t|
|
86
|
-
t.string :username, :password
|
87
|
-
end
|
88
|
-
create_table :blog_comments, :force => true do |t|
|
89
|
-
t.integer :post_id, :null => false
|
90
|
-
t.string :username
|
91
|
-
t.text :body, :html_body
|
92
|
-
t.timestamps
|
93
|
-
end
|
94
|
-
User.create :username => 'admin', :password => 'camping'
|
95
|
-
end
|
96
|
-
|
97
|
-
def self.down
|
98
|
-
drop_table :blog_posts
|
99
|
-
drop_table :blog_users
|
100
|
-
drop_table :blog_comments
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
module Blog::Controllers
|
106
|
-
extend Reststop::Controllers
|
107
|
-
class Index
|
108
|
-
def get
|
109
|
-
redirect '/posts'
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
class Login < R '/login' # @techarch : added explicit login controller
|
114
|
-
def get
|
115
|
-
render :_login
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
class Posts < REST 'posts'
|
120
|
-
# POST /posts
|
121
|
-
def create
|
122
|
-
require_login!
|
123
|
-
@post = Post.create :title => (input.post_title || input.title), # @techarch : allow for REST-client based update
|
124
|
-
:body => (input.post_body || input.body), # @techarch : allow for REST-client based update
|
125
|
-
:user_id => @state.user_id
|
126
|
-
redirect R(@post)
|
127
|
-
end
|
128
|
-
|
129
|
-
# GET /posts/1
|
130
|
-
# GET /posts/1.xml
|
131
|
-
def read(post_id)
|
132
|
-
@post = Post.find(post_id)
|
133
|
-
@comments = Models::Comment.find(:all, :conditions => ['post_id = ?', post_id])
|
134
|
-
render :view
|
135
|
-
end
|
136
|
-
|
137
|
-
# PUT /posts/1
|
138
|
-
def update(post_id)
|
139
|
-
require_login!
|
140
|
-
@post = Post.find(post_id)
|
141
|
-
@post.update_attributes :title => (input.post_title || input.title), # @techarch : allow for REST-client based update
|
142
|
-
:body => (input.post_body || input.body) # @techarch : allow for REST-client based update
|
143
|
-
redirect R(@post)
|
144
|
-
end
|
145
|
-
|
146
|
-
# DELETE /posts/1
|
147
|
-
def delete(post_id)
|
148
|
-
require_login!
|
149
|
-
@post = Post.find post_id
|
150
|
-
|
151
|
-
if @post.destroy
|
152
|
-
redirect R(Posts)
|
153
|
-
else
|
154
|
-
_error("Unable to delete post #{@post.id}", 500)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
# GET /posts
|
159
|
-
# GET /posts.xml
|
160
|
-
def list
|
161
|
-
@posts = Post.all(:order => 'updated_at DESC')
|
162
|
-
s=render :index
|
163
|
-
s
|
164
|
-
end
|
165
|
-
|
166
|
-
# GET /posts/new
|
167
|
-
def new
|
168
|
-
#@state.user_id = 1 # @techarch : commented out as was probably hard-coded for testing purpose
|
169
|
-
require_login!
|
170
|
-
@user = User.find @state.user_id # @techarch : added since we need the user info
|
171
|
-
@post = Post.new
|
172
|
-
render :add
|
173
|
-
end
|
174
|
-
|
175
|
-
# GET /posts/1/edit
|
176
|
-
def edit(post_id)
|
177
|
-
require_login!
|
178
|
-
@user = User.find @state.user_id # @techarch : added since we need the user info
|
179
|
-
@post = Post.find(post_id)
|
180
|
-
render :edit
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
class Comments < REST 'comments'
|
185
|
-
# POST /comments
|
186
|
-
def create
|
187
|
-
Models::Comment.create(:username => (input.post_username || input.username), # @techarch : allow for REST-client based update
|
188
|
-
:body => (input.post_body || input.body), # @techarch : allow for REST-client based update
|
189
|
-
:post_id => input.post_id)
|
190
|
-
redirect R(Posts, input.post_id)
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
class Sessions < REST 'sessions'
|
195
|
-
# POST /sessions
|
196
|
-
def create
|
197
|
-
@user = User.find_by_username_and_password(input.username, input.password)
|
198
|
-
|
199
|
-
if @user
|
200
|
-
@state.user_id = @user.id
|
201
|
-
redirect R(Posts)
|
202
|
-
else
|
203
|
-
@info = 'Wrong username or password.'
|
204
|
-
end
|
205
|
-
render :login
|
206
|
-
end
|
207
|
-
|
208
|
-
# DELETE /sessions
|
209
|
-
def delete
|
210
|
-
@state.user_id = nil
|
211
|
-
redirect R(Posts) # @techarch : changed redirect from Index (does not exist) to Posts
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
# You can use old-fashioned Camping controllers too!
|
216
|
-
class Style < R '/styles.css'
|
217
|
-
def get
|
218
|
-
@headers["Content-Type"] = "text/css; charset=utf-8"
|
219
|
-
@body = %{
|
220
|
-
body {
|
221
|
-
font-family: Utopia, Georga, serif;
|
222
|
-
}
|
223
|
-
h1.header {
|
224
|
-
background-color: #fef;
|
225
|
-
margin: 0; padding: 10px;
|
226
|
-
}
|
227
|
-
div.content {
|
228
|
-
padding: 10px;
|
229
|
-
}
|
230
|
-
}
|
231
|
-
end
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
module Blog::Helpers
|
236
|
-
alias_method :_R, :R
|
237
|
-
remove_method :R
|
238
|
-
include Reststop::Helpers
|
239
|
-
|
240
|
-
def logged_in?
|
241
|
-
!!@state.user_id
|
242
|
-
end
|
243
|
-
|
244
|
-
def require_login!
|
245
|
-
unless logged_in?
|
246
|
-
redirect(R(Blog::Controllers::Login)) # @techarch: add explicit route
|
247
|
-
throw :halt
|
248
|
-
end
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
|
253
|
-
module Blog::Views
|
254
|
-
extend Reststop::Views
|
255
|
-
|
256
|
-
module HTML
|
257
|
-
include Blog::Controllers
|
258
|
-
include Blog::Views
|
259
|
-
|
260
|
-
def layout
|
261
|
-
html do
|
262
|
-
head do
|
263
|
-
title 'blog'
|
264
|
-
link :rel => 'stylesheet', :type => 'text/css',
|
265
|
-
:href => self/'/styles.css', :media => 'screen'
|
266
|
-
end
|
267
|
-
body do
|
268
|
-
h1.header { a 'blog', :href => R(Posts) }
|
269
|
-
div.content do
|
270
|
-
self << yield
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
def index
|
277
|
-
if @posts.empty?
|
278
|
-
p 'No posts found.'
|
279
|
-
else
|
280
|
-
for post in @posts
|
281
|
-
_post(post)
|
282
|
-
end
|
283
|
-
end
|
284
|
-
p { a 'Add', :href => R(Posts, 'new') }
|
285
|
-
end
|
286
|
-
|
287
|
-
def login
|
288
|
-
p { b @login }
|
289
|
-
p { a 'Continue', :href => R(Posts, 'new') }
|
290
|
-
end
|
291
|
-
|
292
|
-
def logout
|
293
|
-
p "You have been logged out."
|
294
|
-
p { a 'Continue', :href => R(Posts) }
|
295
|
-
end
|
296
|
-
|
297
|
-
def add
|
298
|
-
if @user
|
299
|
-
_form(@post, :action => R(Posts))
|
300
|
-
else
|
301
|
-
_login
|
302
|
-
end
|
303
|
-
end
|
304
|
-
|
305
|
-
def edit
|
306
|
-
if @user
|
307
|
-
_form(@post, :action => R(@post), :method => :put)
|
308
|
-
else
|
309
|
-
_login
|
310
|
-
end
|
311
|
-
end
|
312
|
-
|
313
|
-
def view
|
314
|
-
_post(@post)
|
315
|
-
|
316
|
-
p "Comment for this post:"
|
317
|
-
for c in @comments
|
318
|
-
h1 c.username
|
319
|
-
p c.body
|
320
|
-
end
|
321
|
-
|
322
|
-
form :action => R(Comments), :method => 'post' do
|
323
|
-
label 'Name', :for => 'post_username'; br
|
324
|
-
input :name => 'post_username', :type => 'text'; br
|
325
|
-
label 'Comment', :for => 'post_body'; br
|
326
|
-
textarea :name => 'post_body' do; end; br
|
327
|
-
input :type => 'hidden', :name => 'post_id', :value => @post.id
|
328
|
-
input :type => 'submit'
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
# partials
|
333
|
-
def _login
|
334
|
-
p do
|
335
|
-
"(default: admin/camping)"
|
336
|
-
end
|
337
|
-
form :action => R(Sessions), :method => 'post' do
|
338
|
-
label 'Username', :for => 'username'; br
|
339
|
-
input :name => 'username', :type => 'text'; br
|
340
|
-
|
341
|
-
label 'Password', :for => 'password'; br
|
342
|
-
input :name => 'password', :type => 'text'; br
|
343
|
-
|
344
|
-
input :type => 'submit', :name => 'login', :value => 'Login'
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
def _post(post)
|
349
|
-
h1 post.title
|
350
|
-
p post.body
|
351
|
-
p do
|
352
|
-
[a("Edit", :href => R(Posts, post.id, 'edit')), a("View", :href => R(Posts, post.id, 'edit'))].join " | "
|
353
|
-
end
|
354
|
-
end
|
355
|
-
|
356
|
-
def _form(post, opts)
|
357
|
-
form(:action => R(Sessions), :method => 'delete') do
|
358
|
-
p do
|
359
|
-
span "You are logged in as #{@user.username}"
|
360
|
-
span " | "
|
361
|
-
button(:type => 'submit') {'Logout'}
|
362
|
-
end
|
363
|
-
end
|
364
|
-
form({:method => 'post'}.merge(opts)) do
|
365
|
-
label 'Title', :for => 'post_title'; br
|
366
|
-
input :name => 'post_title', :type => 'text',
|
367
|
-
:value => post.title; br
|
368
|
-
|
369
|
-
label 'Body', :for => 'post_body'; br
|
370
|
-
textarea post.body, :name => 'post_body'; br
|
371
|
-
|
372
|
-
input :type => 'hidden', :name => 'post_id', :value => post.id
|
373
|
-
input :type => 'submit'
|
374
|
-
end
|
375
|
-
end
|
376
|
-
end
|
377
|
-
default_format :HTML
|
378
|
-
|
379
|
-
module XML
|
380
|
-
def layout
|
381
|
-
yield
|
382
|
-
end
|
383
|
-
|
384
|
-
def index
|
385
|
-
@posts.to_xml(:root => 'blog')
|
386
|
-
end
|
387
|
-
|
388
|
-
def view
|
389
|
-
@post.to_xml(:root => 'post')
|
390
|
-
end
|
391
|
-
end
|
392
|
-
end
|
393
|
-
|
394
|
-
def Blog.create
|
395
|
-
raise "You must configure the database first in 'config/database.yml'!" unless File.exist?('config/database.yml')
|
396
|
-
dbconfig = YAML.load(File.read('config/database.yml')) # @techarch
|
397
|
-
Camping::Models::Base.establish_connection dbconfig['development'] # @techarch
|
398
|
-
|
399
|
-
Blog::Models.create_schema :assume => (Blog::Models::Post.table_exists? ? 1.0 : 0.0)
|
400
|
-
end
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
#require 'ruby-debug' # @techarch : commented out since only needed for local debugging
|
5
|
+
|
6
|
+
require 'markaby' # @techarch : added explicit require
|
7
|
+
require 'camping' # @techarch
|
8
|
+
require 'camping/session' # @techarch : added explicit require since session has changed in Camping 2.0
|
9
|
+
|
10
|
+
gem 'RedCloth' # @techarch : added since it is referenced in the Posts model
|
11
|
+
require 'redcloth' # @techarch : added
|
12
|
+
|
13
|
+
#gem 'camping', '~> 2.0'
|
14
|
+
gem 'camping' , '>= 2.0' # @techarch : updated version
|
15
|
+
|
16
|
+
#gem 'reststop', '~> 0.3'
|
17
|
+
|
18
|
+
=begin # @techarch : commented out since only needed for local debugging
|
19
|
+
$: << '../../camping/lib'
|
20
|
+
$: << '../lib'
|
21
|
+
require 'camping-unabridged'
|
22
|
+
require 'camping/ar'
|
23
|
+
require 'camping/session'
|
24
|
+
=end
|
25
|
+
|
26
|
+
#begin # @techarch : commented out since only needed for local debugging
|
27
|
+
# try to use local copy of library
|
28
|
+
#require '../lib/reststop2'
|
29
|
+
$: << '../lib/'
|
30
|
+
require 'reststop.rb' # @techarch : adjusted so that it is located in the same current folder
|
31
|
+
#rescue LoadError
|
32
|
+
# # ... otherwise default to rubygem
|
33
|
+
# require 'reststop'
|
34
|
+
#end
|
35
|
+
|
36
|
+
Camping.goes :Blog
|
37
|
+
|
38
|
+
module Blog
|
39
|
+
include Camping::Session
|
40
|
+
include Reststop
|
41
|
+
|
42
|
+
Controllers.extend Reststop::Controllers
|
43
|
+
end
|
44
|
+
|
45
|
+
module Blog::Base
|
46
|
+
alias camping_render render
|
47
|
+
alias camping_lookup lookup # @techarch: required if camping > 2.0
|
48
|
+
alias camping_service service
|
49
|
+
include Reststop::Base
|
50
|
+
alias service reststop_service
|
51
|
+
alias render reststop_render
|
52
|
+
|
53
|
+
# Overrides the new Tilt-centric lookup method In camping
|
54
|
+
# RESTstop needs to have a first try at looking up the view
|
55
|
+
# located in the Views::HTML module.
|
56
|
+
def lookup(n)
|
57
|
+
T.fetch(n.to_sym) do |k|
|
58
|
+
t = Blog::Views::HTML.method_defined?(k) || camping_lookup(n)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
module Blog::Models
|
64
|
+
class Post < Base
|
65
|
+
belongs_to :user
|
66
|
+
|
67
|
+
before_save do |record|
|
68
|
+
cloth = RedCloth.new(record.body)
|
69
|
+
cloth.hard_breaks = false
|
70
|
+
record.html_body = cloth.to_html
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class Comment < Base; belongs_to :user; end
|
75
|
+
class User < Base; end
|
76
|
+
|
77
|
+
class BasicFields < V 1.1
|
78
|
+
def self.up
|
79
|
+
create_table :blog_posts, :force => true do |t|
|
80
|
+
t.integer :user_id, :null => false
|
81
|
+
t.string :title, :limit => 255
|
82
|
+
t.text :body, :html_body
|
83
|
+
t.timestamps
|
84
|
+
end
|
85
|
+
create_table :blog_users, :force => true do |t|
|
86
|
+
t.string :username, :password
|
87
|
+
end
|
88
|
+
create_table :blog_comments, :force => true do |t|
|
89
|
+
t.integer :post_id, :null => false
|
90
|
+
t.string :username
|
91
|
+
t.text :body, :html_body
|
92
|
+
t.timestamps
|
93
|
+
end
|
94
|
+
User.create :username => 'admin', :password => 'camping'
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.down
|
98
|
+
drop_table :blog_posts
|
99
|
+
drop_table :blog_users
|
100
|
+
drop_table :blog_comments
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
module Blog::Controllers
|
106
|
+
extend Reststop::Controllers
|
107
|
+
class Index
|
108
|
+
def get
|
109
|
+
redirect '/posts'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
class Login < R '/login' # @techarch : added explicit login controller
|
114
|
+
def get
|
115
|
+
render :_login
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class Posts < REST 'posts'
|
120
|
+
# POST /posts
|
121
|
+
def create
|
122
|
+
require_login!
|
123
|
+
@post = Post.create :title => (input.post_title || input.title), # @techarch : allow for REST-client based update
|
124
|
+
:body => (input.post_body || input.body), # @techarch : allow for REST-client based update
|
125
|
+
:user_id => @state.user_id
|
126
|
+
redirect R(@post)
|
127
|
+
end
|
128
|
+
|
129
|
+
# GET /posts/1
|
130
|
+
# GET /posts/1.xml
|
131
|
+
def read(post_id)
|
132
|
+
@post = Post.find(post_id)
|
133
|
+
@comments = Models::Comment.find(:all, :conditions => ['post_id = ?', post_id])
|
134
|
+
render :view
|
135
|
+
end
|
136
|
+
|
137
|
+
# PUT /posts/1
|
138
|
+
def update(post_id)
|
139
|
+
require_login!
|
140
|
+
@post = Post.find(post_id)
|
141
|
+
@post.update_attributes :title => (input.post_title || input.title), # @techarch : allow for REST-client based update
|
142
|
+
:body => (input.post_body || input.body) # @techarch : allow for REST-client based update
|
143
|
+
redirect R(@post)
|
144
|
+
end
|
145
|
+
|
146
|
+
# DELETE /posts/1
|
147
|
+
def delete(post_id)
|
148
|
+
require_login!
|
149
|
+
@post = Post.find post_id
|
150
|
+
|
151
|
+
if @post.destroy
|
152
|
+
redirect R(Posts)
|
153
|
+
else
|
154
|
+
_error("Unable to delete post #{@post.id}", 500)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# GET /posts
|
159
|
+
# GET /posts.xml
|
160
|
+
def list
|
161
|
+
@posts = Post.all(:order => 'updated_at DESC')
|
162
|
+
s=render :index
|
163
|
+
s
|
164
|
+
end
|
165
|
+
|
166
|
+
# GET /posts/new
|
167
|
+
def new
|
168
|
+
#@state.user_id = 1 # @techarch : commented out as was probably hard-coded for testing purpose
|
169
|
+
require_login!
|
170
|
+
@user = User.find @state.user_id # @techarch : added since we need the user info
|
171
|
+
@post = Post.new
|
172
|
+
render :add
|
173
|
+
end
|
174
|
+
|
175
|
+
# GET /posts/1/edit
|
176
|
+
def edit(post_id)
|
177
|
+
require_login!
|
178
|
+
@user = User.find @state.user_id # @techarch : added since we need the user info
|
179
|
+
@post = Post.find(post_id)
|
180
|
+
render :edit
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
class Comments < REST 'comments'
|
185
|
+
# POST /comments
|
186
|
+
def create
|
187
|
+
Models::Comment.create(:username => (input.post_username || input.username), # @techarch : allow for REST-client based update
|
188
|
+
:body => (input.post_body || input.body), # @techarch : allow for REST-client based update
|
189
|
+
:post_id => input.post_id)
|
190
|
+
redirect R(Posts, input.post_id)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
class Sessions < REST 'sessions'
|
195
|
+
# POST /sessions
|
196
|
+
def create
|
197
|
+
@user = User.find_by_username_and_password(input.username, input.password)
|
198
|
+
|
199
|
+
if @user
|
200
|
+
@state.user_id = @user.id
|
201
|
+
redirect R(Posts)
|
202
|
+
else
|
203
|
+
@info = 'Wrong username or password.'
|
204
|
+
end
|
205
|
+
render :login
|
206
|
+
end
|
207
|
+
|
208
|
+
# DELETE /sessions
|
209
|
+
def delete
|
210
|
+
@state.user_id = nil
|
211
|
+
redirect R(Posts) # @techarch : changed redirect from Index (does not exist) to Posts
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
# You can use old-fashioned Camping controllers too!
|
216
|
+
class Style < R '/styles.css'
|
217
|
+
def get
|
218
|
+
@headers["Content-Type"] = "text/css; charset=utf-8"
|
219
|
+
@body = %{
|
220
|
+
body {
|
221
|
+
font-family: Utopia, Georga, serif;
|
222
|
+
}
|
223
|
+
h1.header {
|
224
|
+
background-color: #fef;
|
225
|
+
margin: 0; padding: 10px;
|
226
|
+
}
|
227
|
+
div.content {
|
228
|
+
padding: 10px;
|
229
|
+
}
|
230
|
+
}
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
module Blog::Helpers
|
236
|
+
alias_method :_R, :R
|
237
|
+
remove_method :R
|
238
|
+
include Reststop::Helpers
|
239
|
+
|
240
|
+
def logged_in?
|
241
|
+
!!@state.user_id
|
242
|
+
end
|
243
|
+
|
244
|
+
def require_login!
|
245
|
+
unless logged_in?
|
246
|
+
redirect(R(Blog::Controllers::Login)) # @techarch: add explicit route
|
247
|
+
throw :halt
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
|
253
|
+
module Blog::Views
|
254
|
+
extend Reststop::Views
|
255
|
+
|
256
|
+
module HTML
|
257
|
+
include Blog::Controllers
|
258
|
+
include Blog::Views
|
259
|
+
|
260
|
+
def layout
|
261
|
+
html do
|
262
|
+
head do
|
263
|
+
title 'blog'
|
264
|
+
link :rel => 'stylesheet', :type => 'text/css',
|
265
|
+
:href => self/'/styles.css', :media => 'screen'
|
266
|
+
end
|
267
|
+
body do
|
268
|
+
h1.header { a 'blog', :href => R(Posts) }
|
269
|
+
div.content do
|
270
|
+
self << yield
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
def index
|
277
|
+
if @posts.empty?
|
278
|
+
p 'No posts found.'
|
279
|
+
else
|
280
|
+
for post in @posts
|
281
|
+
_post(post)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
p { a 'Add', :href => R(Posts, 'new') }
|
285
|
+
end
|
286
|
+
|
287
|
+
def login
|
288
|
+
p { b @login }
|
289
|
+
p { a 'Continue', :href => R(Posts, 'new') }
|
290
|
+
end
|
291
|
+
|
292
|
+
def logout
|
293
|
+
p "You have been logged out."
|
294
|
+
p { a 'Continue', :href => R(Posts) }
|
295
|
+
end
|
296
|
+
|
297
|
+
def add
|
298
|
+
if @user
|
299
|
+
_form(@post, :action => R(Posts))
|
300
|
+
else
|
301
|
+
_login
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
def edit
|
306
|
+
if @user
|
307
|
+
_form(@post, :action => R(@post), :method => :put)
|
308
|
+
else
|
309
|
+
_login
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
def view
|
314
|
+
_post(@post)
|
315
|
+
|
316
|
+
p "Comment for this post:"
|
317
|
+
for c in @comments
|
318
|
+
h1 c.username
|
319
|
+
p c.body
|
320
|
+
end
|
321
|
+
|
322
|
+
form :action => R(Comments), :method => 'post' do
|
323
|
+
label 'Name', :for => 'post_username'; br
|
324
|
+
input :name => 'post_username', :type => 'text'; br
|
325
|
+
label 'Comment', :for => 'post_body'; br
|
326
|
+
textarea :name => 'post_body' do; end; br
|
327
|
+
input :type => 'hidden', :name => 'post_id', :value => @post.id
|
328
|
+
input :type => 'submit'
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
# partials
|
333
|
+
def _login
|
334
|
+
p do
|
335
|
+
"(default: admin/camping)"
|
336
|
+
end
|
337
|
+
form :action => R(Sessions), :method => 'post' do
|
338
|
+
label 'Username', :for => 'username'; br
|
339
|
+
input :name => 'username', :type => 'text'; br
|
340
|
+
|
341
|
+
label 'Password', :for => 'password'; br
|
342
|
+
input :name => 'password', :type => 'text'; br
|
343
|
+
|
344
|
+
input :type => 'submit', :name => 'login', :value => 'Login'
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
def _post(post)
|
349
|
+
h1 post.title
|
350
|
+
p post.body
|
351
|
+
p do
|
352
|
+
[a("Edit", :href => R(Posts, post.id, 'edit')), a("View", :href => R(Posts, post.id, 'edit'))].join " | "
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
def _form(post, opts)
|
357
|
+
form(:action => R(Sessions), :method => 'delete') do
|
358
|
+
p do
|
359
|
+
span "You are logged in as #{@user.username}"
|
360
|
+
span " | "
|
361
|
+
button(:type => 'submit') {'Logout'}
|
362
|
+
end
|
363
|
+
end
|
364
|
+
form({:method => 'post'}.merge(opts)) do
|
365
|
+
label 'Title', :for => 'post_title'; br
|
366
|
+
input :name => 'post_title', :type => 'text',
|
367
|
+
:value => post.title; br
|
368
|
+
|
369
|
+
label 'Body', :for => 'post_body'; br
|
370
|
+
textarea post.body, :name => 'post_body'; br
|
371
|
+
|
372
|
+
input :type => 'hidden', :name => 'post_id', :value => post.id
|
373
|
+
input :type => 'submit'
|
374
|
+
end
|
375
|
+
end
|
376
|
+
end
|
377
|
+
default_format :HTML
|
378
|
+
|
379
|
+
module XML
|
380
|
+
def layout
|
381
|
+
yield
|
382
|
+
end
|
383
|
+
|
384
|
+
def index
|
385
|
+
@posts.to_xml(:root => 'blog')
|
386
|
+
end
|
387
|
+
|
388
|
+
def view
|
389
|
+
@post.to_xml(:root => 'post')
|
390
|
+
end
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
def Blog.create
|
395
|
+
raise "You must configure the database first in 'config/database.yml'!" unless File.exist?('config/database.yml')
|
396
|
+
dbconfig = YAML.load(File.read('config/database.yml')) # @techarch
|
397
|
+
Camping::Models::Base.establish_connection dbconfig['development'] # @techarch
|
398
|
+
|
399
|
+
Blog::Models.create_schema :assume => (Blog::Models::Post.table_exists? ? 1.0 : 0.0)
|
400
|
+
end
|