Guest User

Untitled

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