Guest User

Untitled

a guest
May 3rd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'rubygems'
  4. require 'ruby-debug'
  5. #gem 'camping', '~> 2.0'
  6. #gem 'reststop', '~> 0.3'
  7.  
  8. $: << '../../camping-camping/lib'
  9. $: << '../lib'
  10. require 'camping-unabridged'
  11. require 'camping/ar'
  12. require 'camping/session'
  13.  
  14. #begin
  15. # try to use local copy of library
  16. require '../lib/reststop2'
  17. #rescue LoadError
  18. # # ... otherwise default to rubygem
  19. # require 'reststop'
  20. #end
  21.  
  22.  
  23.  
  24. Camping.goes :Blog
  25.  
  26. module Blog
  27. include Camping::Session
  28. include Reststop
  29.  
  30. Controllers.extend Reststop::Controllers
  31. end
  32.  
  33. module Blog::Base
  34. alias camping_render render
  35. alias camping_service service
  36. include Reststop::Base
  37. alias service reststop_service
  38. alias render reststop_render
  39. end
  40.  
  41. module Blog::Models
  42. class Post < Base
  43. belongs_to :user
  44.  
  45. before_save do |record|
  46. cloth = RedCloth.new(record.body)
  47. cloth.hard_breaks = false
  48. record.html_body = cloth.to_html
  49. end
  50. end
  51.  
  52. class Comment < Base; belongs_to :user; end
  53. class User < Base; end
  54.  
  55. class BasicFields < V 1.1
  56. def self.up
  57. create_table :blog_posts, :force => true do |t|
  58. t.integer :user_id, :null => false
  59. t.string :title, :limit => 255
  60. t.text :body, :html_body
  61. t.timestamps
  62. end
  63. create_table :blog_users, :force => true do |t|
  64. t.string :username, :password
  65. end
  66. create_table :blog_comments, :force => true do |t|
  67. t.integer :post_id, :null => false
  68. t.string :username
  69. t.text :body, :html_body
  70. t.timestamps
  71. end
  72. User.create :username => 'admin', :password => 'camping'
  73. end
  74.  
  75. def self.down
  76. drop_table :blog_posts
  77. drop_table :blog_users
  78. drop_table :blog_comments
  79. end
  80. end
  81. end
  82.  
  83. module Blog::Controllers
  84. extend Reststop::Controllers
  85.  
  86. class Posts < REST 'posts'
  87. # POST /posts
  88. def create
  89. require_login!
  90. @post = Post.create :title => input.post_title, :body => input.post_body,
  91. :user_id => @state.user_id
  92. redirect R(@post)
  93. end
  94.  
  95. # GET /posts/1
  96. # GET /posts/1.xml
  97. def read(post_id)
  98. @post = Post.find(post_id)
  99. @comments = Models::Comment.find(:all, :conditions => ['post_id = ?', post_id])
  100. render :view
  101. end
  102.  
  103. # PUT /posts/1
  104. def update(post_id)
  105. require_login!
  106. @post = Post.find(post_id)
  107. @post.update_attributes :title => input.post_title, :body => input.post_body
  108. redirect R(@post)
  109. end
  110.  
  111. # DELETE /posts/1
  112. def delete(post_id)
  113. require_login!
  114. @post = Post.find post_id
  115.  
  116. if @post.destroy
  117. redirect R(Posts)
  118. else
  119. _error("Unable to delete post #{@post.id}", 500)
  120. end
  121. end
  122.  
  123. # GET /posts
  124. # GET /posts.xml
  125. def list
  126. @posts = Post.all(:order => 'updated_at DESC')
  127. render :index
  128. end
  129.  
  130. # GET /posts/new
  131. def new
  132. @state.user_id = 1
  133. require_login!
  134. @post = Post.new
  135. render :add
  136. end
  137.  
  138. # GET /posts/1/edit
  139. def edit(post_id)
  140. require_login!
  141. @post = Post.find(post_id)
  142. render :edit
  143. end
  144. end
  145.  
  146. class Comments < REST 'comments'
  147. # POST /comments
  148. def create
  149. Models::Comment.create(:username => input.post_username,
  150. :body => input.post_body, :post_id => input.post_id)
  151. redirect R(Posts, input.post_id)
  152. end
  153. end
  154.  
  155. class Sessions < REST 'sessions'
  156. # POST /sessions
  157. def create
  158. @user = User.find_by_username_and_password(input.username, input.password)
  159.  
  160. if @user
  161. @state.user_id = @user.id
  162. redirect R(Posts)
  163. else
  164. @info = 'Wrong username or password.'
  165. end
  166. render :login
  167. end
  168.  
  169. # DELETE /sessions
  170. def delete
  171. @state.user_id = nil
  172. redirect Index
  173. end
  174. end
  175.  
  176. # You can use old-fashioned Camping controllers too!
  177. class Style < R '/styles.css'
  178. def get
  179. @headers["Content-Type"] = "text/css; charset=utf-8"
  180. @body = %{
  181. body {
  182. font-family: Utopia, Georga, serif;
  183. }
  184. h1.header {
  185. background-color: #fef;
  186. margin: 0; padding: 10px;
  187. }
  188. div.content {
  189. padding: 10px;
  190. }
  191. }
  192. end
  193. end
  194. end
  195.  
  196. module Blog::Helpers
  197. alias_method :_R, :R
  198. remove_method :R
  199. include Reststop::Helpers
  200.  
  201. def logged_in?
  202. !!@state.user_id
  203. end
  204.  
  205. def require_login!
  206. unless logged_in?
  207. redirect Controllers::Login
  208. throw :halt
  209. end
  210. end
  211. end
  212.  
  213.  
  214. module Blog::Views
  215. extend Reststop::Views
  216.  
  217. module HTML
  218. include Blog::Controllers
  219. include Blog::Views
  220.  
  221. def layout
  222. html do
  223. head do
  224. title 'blog'
  225. link :rel => 'stylesheet', :type => 'text/css',
  226. :href => self/'/styles.css', :media => 'screen'
  227. end
  228. body do
  229. h1.header { a 'blog', :href => R(Posts) }
  230. div.content do
  231. self << yield
  232. end
  233. end
  234. end
  235. end
  236.  
  237. def index
  238. if @posts.empty?
  239. p 'No posts found.'
  240. else
  241. for post in @posts
  242. _post(post)
  243. end
  244. end
  245. p { a 'Add', :href => R(Posts, 'new') }
  246. end
  247.  
  248. def login
  249. p { b @login }
  250. p { a 'Continue', :href => R(Posts, 'new') }
  251. end
  252.  
  253. def logout
  254. p "You have been logged out."
  255. p { a 'Continue', :href => R(Posts) }
  256. end
  257.  
  258. def add
  259. if @user
  260. _form(@post, :action => R(Posts))
  261. else
  262. _login
  263. end
  264. end
  265.  
  266. def edit
  267. if @user
  268. _form(@post, :action => R(@post), :method => :put)
  269. else
  270. _login
  271. end
  272. end
  273.  
  274. def view
  275. _post(@post)
  276.  
  277. p "Comment for this post:"
  278. for c in @comments
  279. h1 c.username
  280. p c.body
  281. end
  282.  
  283. form :action => R(Comments), :method => 'post' do
  284. label 'Name', :for => 'post_username'; br
  285. input :name => 'post_username', :type => 'text'; br
  286. label 'Comment', :for => 'post_body'; br
  287. textarea :name => 'post_body' do; end; br
  288. input :type => 'hidden', :name => 'post_id', :value => @post.id
  289. input :type => 'submit'
  290. end
  291. end
  292.  
  293. # partials
  294. def _login
  295. form :action => R(Sessions), :method => 'post' do
  296. label 'Username', :for => 'username'; br
  297. input :name => 'username', :type => 'text'; br
  298.  
  299. label 'Password', :for => 'password'; br
  300. input :name => 'password', :type => 'text'; br
  301.  
  302. input :type => 'submit', :name => 'login', :value => 'Login'
  303. end
  304. end
  305.  
  306. def _post(post)
  307. h1 post.title
  308. p post.body
  309. p do
  310. [a("Edit", :href => R(Posts, post.id, 'edit')), a("View", :href => R(Posts, post.id, 'edit'))].join " | "
  311. end
  312. end
  313.  
  314. def _form(post, opts)
  315. form(:action => R(Sessions), :method => 'delete') do
  316. p do
  317. span "You are logged in as #{@user.username}"
  318. span " | "
  319. button(:type => 'submit') {'Logout'}
  320. end
  321. end
  322. form({:method => 'post'}.merge(opts)) do
  323. label 'Title', :for => 'post_title'; br
  324. input :name => 'post_title', :type => 'text',
  325. :value => post.title; br
  326.  
  327. label 'Body', :for => 'post_body'; br
  328. textarea post.body, :name => 'post_body'; br
  329.  
  330. input :type => 'hidden', :name => 'post_id', :value => post.id
  331. input :type => 'submit'
  332. end
  333. end
  334. end
  335. default_format :HTML
  336.  
  337. module XML
  338. def layout
  339. yield
  340. end
  341.  
  342. def index
  343. @posts.to_xml(:root => 'blog')
  344. end
  345.  
  346. def view
  347. @post.to_xml(:root => 'post')
  348. end
  349. end
  350. end
  351.  
  352. def Blog.create
  353. Blog::Models.create_schema :assume => (Blog::Models::Post.table_exists? ? 1.0 : 0.0)
  354. end
Add Comment
Please, Sign In to add comment