Guest User

Untitled

a guest
Apr 26th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. #!/ruby/bin/ruby -rubygems
  2.  
  3. $:.unshift File.dirname(__FILE__) + "/../../lib"
  4. require 'camping'
  5. require 'camping/session'
  6.  
  7. Camping.goes :Blog
  8.  
  9. module Blog
  10. include Camping::Session
  11. end
  12.  
  13. module Blog::Models
  14. class Post < Base; belongs_to :user; end
  15. class Comment < Base; belongs_to :user; end
  16. class User < Base; end
  17.  
  18. class CreateTheBasics < V 1.0
  19. def self.up
  20. create_table :blog_posts do |t|
  21. t.column :id, :integer, :null => false
  22. t.column :user_id, :integer, :null => false
  23. t.column :title, :string, :limit => 255
  24. t.column :body, :text
  25. end
  26. create_table :blog_users do |t|
  27. t.column :id, :integer, :null => false
  28. t.column :username, :string
  29. t.column :password, :string
  30. end
  31. create_table :blog_comments do |t|
  32. t.column :id, :integer, :null => false
  33. t.column :post_id, :integer, :null => false
  34. t.column :username, :string
  35. t.column :body, :text
  36. end
  37. User.create :username => 'admin', :password => 'camping'
  38. end
  39. def self.down
  40. drop_table :blog_posts
  41. drop_table :blog_users
  42. drop_table :blog_comments
  43. end
  44. end
  45. end
  46.  
  47. module Blog::Controllers
  48. class Index < R '/'
  49. def get
  50. @posts = Post.find :all
  51. render :index
  52. end
  53. end
  54.  
  55. class Add
  56. def get
  57. unless @state.user_id.blank?
  58. @user = User.find @state.user_id
  59. @post = Post.new
  60. end
  61. render :add
  62. end
  63. def post
  64. unless @state.user_id.blank?
  65. post = Post.create :title => input.post_title, :body => input.post_body,
  66. :user_id => @state.user_id
  67. redirect View, post
  68. end
  69. end
  70. end
  71.  
  72. class Info < R '/info/(\d+)', '/info/(\w+)/(\d+)', '/info', '/info/(\d+)/(\d+)/(\d+)/([\w-]+)'
  73. def get(*args)
  74. div do
  75. code args.inspect; br; br
  76. code @env.inspect; br
  77. code "Link: #{R(Info, 1, 2)}"
  78. end
  79. end
  80. end
  81.  
  82. class View < R '/view/(\d+)'
  83. def get post_id
  84. @post = Post.find post_id
  85. @comments = Models::Comment.find :all, :conditions => ['post_id = ?', post_id]
  86. render :view
  87. end
  88. end
  89.  
  90. class Edit < R '/edit/(\d+)', '/edit'
  91. def get post_id
  92. unless @state.user_id.blank?
  93. @user = User.find @state.user_id
  94. end
  95. @post = Post.find post_id
  96. render :edit
  97. end
  98.  
  99. def post
  100. unless @state.user_id.blank?
  101. @post = Post.find input.post_id
  102. @post.update_attributes :title => input.post_title, :body => input.post_body
  103. redirect View, @post
  104. end
  105. end
  106. end
  107.  
  108. class Comment
  109. def post
  110. Models::Comment.create(:username => input.post_username,
  111. :body => input.post_body, :post_id => input.post_id)
  112. redirect View, input.post_id
  113. end
  114. end
  115.  
  116. class Login
  117. def post
  118. @user = User.find :first, :conditions => ['username = ? AND password = ?', input.username, input.password]
  119.  
  120. if @user
  121. @login = 'login success !'
  122. @state.user_id = @user.id
  123. else
  124. @login = 'wrong user name or password'
  125. end
  126. render :login
  127. end
  128. end
  129.  
  130. class Logout
  131. def get
  132. @state.user_id = nil
  133. render :logout
  134. end
  135. end
  136.  
  137. class Style < R '/styles.css'
  138. def get
  139. @headers["Content-Type"] = "text/css; charset=utf-8"
  140. @body = %{
  141. body {
  142. font-family: Utopia, Georga, serif;
  143. }
  144. h1.header {
  145. background-color: #fef;
  146. margin: 0; padding: 10px;
  147. }
  148. div.content {
  149. padding: 10px;
  150. }
  151. }
  152. end
  153. end
  154. end
  155.  
  156. module Blog::Views
  157.  
  158. def layout
  159. html do
  160. head do
  161. title 'blog'
  162. link :rel => 'stylesheet', :type => 'text/css',
  163. :href => '/styles.css', :media => 'screen'
  164. end
  165. body do
  166. h1.header { a 'blog', :href => R(Index) }
  167. div.content do
  168. self << yield
  169. end
  170. end
  171. end
  172. end
  173.  
  174. def index
  175. if @posts.empty?
  176. p 'No posts found.'
  177. p { a 'Add', :href => R(Add) }
  178. else
  179. for post in @posts
  180. _post(post)
  181. end
  182. end
  183. end
  184.  
  185. def login
  186. p { b @login }
  187. p { a 'Continue', :href => R(Add) }
  188. end
  189.  
  190. def logout
  191. p "You have been logged out."
  192. p { a 'Continue', :href => R(Index) }
  193. end
  194.  
  195. def add
  196. if @user
  197. _form(@post, :action => R(Add))
  198. else
  199. _login
  200. end
  201. end
  202.  
  203. def edit
  204. if @user
  205. _form(@post, :action => R(Edit))
  206. else
  207. _login
  208. end
  209. end
  210.  
  211. def view
  212. _post(@post)
  213.  
  214. p "Comment for this post:"
  215. for c in @comments
  216. h1 c.username
  217. p c.body
  218. end
  219.  
  220. form :action => R(Comment), :method => 'post' do
  221. label 'Name', :for => 'post_username'; br
  222. input :name => 'post_username', :type => 'text'; br
  223. label 'Comment', :for => 'post_body'; br
  224. textarea :name => 'post_body' do; end; br
  225. input :type => 'hidden', :name => 'post_id', :value => @post.id
  226. input :type => 'submit'
  227. end
  228. end
  229.  
  230. # partials
  231. def _login
  232. form :action => R(Login), :method => 'post' do
  233. label 'Username', :for => 'username'; br
  234. input :name => 'username', :type => 'text'; br
  235.  
  236. label 'Password', :for => 'password'; br
  237. input :name => 'password', :type => 'text'; br
  238.  
  239. input :type => 'submit', :name => 'login', :value => 'Login'
  240. end
  241. end
  242.  
  243. def _post(post)
  244. h1 post.title
  245. p post.body
  246. p do
  247. [a("Edit", :href => R(Edit, post)), a("View", :href => R(View, post))].join " | "
  248. end
  249. end
  250.  
  251. def _form(post, opts)
  252. p { "You are logged in as #{@user.username} | #{a 'Logout', :href => R(Logout)}" }
  253. form({:method => 'post'}.merge(opts)) do
  254. label 'Title', :for => 'post_title'; br
  255. input :name => 'post_title', :type => 'text',
  256. :value => post.title; br
  257.  
  258. label 'Body', :for => 'post_body'; br
  259. textarea post.body, :name => 'post_body'; br
  260.  
  261. input :type => 'hidden', :name => 'post_id', :value => post.id
  262. input :type => 'submit'
  263. end
  264. end
  265. end
  266.  
  267. def Blog.create
  268. Camping::Models::Session.create_schema
  269. Blog::Models.create_schema :assume => (Blog::Models::Post.table_exists? ? 1.0 : 0.0)
  270. end
Add Comment
Please, Sign In to add comment