Guest User

Untitled

a guest
May 26th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. require 'rubygems'
  2. require 'sinatra'
  3. require 'ohm'
  4. require 'lunar'
  5. require 'pagination'
  6. require 'haml'
  7.  
  8. helpers Pagination::Helpers
  9.  
  10. class Post < Ohm::Model
  11. attribute :body
  12. attribute :tags
  13. attribute :votes
  14.  
  15. def create
  16. if super
  17. index
  18. return true
  19. end
  20. end
  21.  
  22. protected
  23. def index
  24. Lunar.index Post do |i|
  25. i.id(id)
  26. i.text :body, body
  27. i.text :tags, tags
  28.  
  29. i.sortable :votes, votes
  30. end
  31. end
  32. end
  33.  
  34. get '/' do
  35. @posts = paginate Post.all,
  36. :per_page => 10, :page => params[:page],
  37. :order => "DESC"
  38.  
  39. haml :home
  40. end
  41.  
  42. post '/post' do
  43. Post.create(:body => params[:body], :tags => params[:tags])
  44. redirect '/'
  45. end
  46.  
  47. get '/search' do
  48. results = Lunar.search(Post, :q => params[:q], :tags => params[:tags])
  49.  
  50. @posts = paginate results, :page => params[:page], :per_page => 10,
  51. :sort_by => :votes, :order => "DESC"
  52.  
  53. haml :home
  54. end
  55.  
  56. if Post.all.size == 0
  57. Post.create(:body => "The all new macbook pro 17inch", :tags => "apple macbook pro 17", :votes => 20)
  58. Post.create(:body => "apple iPad 3G", :tags => "apple ipad 3G tablet", :votes => 11)
  59. Post.create(:body => "apple iPad", :tags => "apple ipad tablet", :votes => 2)
  60. Post.create(:body => "apple iPhone 3GS", :tags => "apple iphone mobile", :votes => 5)
  61. Post.create(:body => "apple iPhone 3G", :tags => "apple iphone mobile", :votes => 10)
  62. end
  63.  
  64. __END__
  65. @@ home
  66. %h1 All Posts (#{@posts.total})
  67.  
  68. %form(action='/search' method='get')
  69. %fieldset
  70. %legend Search Posts
  71. %label
  72. %span Keywords:
  73. %input(type='text' name='q')
  74.  
  75. %label
  76. %span Tags:
  77. %input(type='text' name='tags')
  78.  
  79. %button(type='submit') Go
  80.  
  81. %ul
  82. - @posts.each do |post|
  83. %li
  84. %p= post.body
  85. %p
  86. Tagged: #{post.tags}
  87. |
  88. Votes: #{post.votes}
  89.  
  90. %form(action='/post' method='post')
  91. %fieldset
  92. %label(for='body')
  93. %span Body
  94. %br
  95. %textarea(name='body' cols=50 rows=5)
  96.  
  97. %br
  98.  
  99. %label(for='tags')
  100. %span Tags
  101. %br
  102. %input(type='text' name='tags')
  103.  
  104. %fieldset
  105. %button(type='submit') Post!
  106.  
  107. = pagination @posts
Add Comment
Please, Sign In to add comment