Guest User

Untitled

a guest
Apr 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. class Documents < Application
  2.  
  3. before(:owner_required, :exclude => ["create_comment", "index", "index_category", "search", "show"])
  4.  
  5. provides(:xml, :js, :yaml)
  6.  
  7. def create
  8. @document = Document.new(params[:document])
  9. if @document.save
  10. redirect url(:document, @document)
  11. else
  12. render :action => :new
  13. end
  14. end
  15.  
  16. def create_category
  17. @category = Category.new(params[:category])
  18. if @category.save
  19. redirect url(:documents)
  20. else
  21. raise BadRequest
  22. end
  23. end
  24.  
  25. def create_comment
  26. @document = Document[params[:id]]
  27. @comment = @document.comments.build(params[:comment])
  28. if @comment.save
  29. redirect url(:document, @document)
  30. else
  31. raise BadRequest
  32. end
  33. end
  34.  
  35. def create_document_category
  36. @document = Document[params[:id]]
  37. unless params[:category_id].blank?
  38. @category = Category[params[:category_id]]
  39. else
  40. @category = Category.new(params[:category])
  41. end
  42. @category.documents << @document
  43. if @category.save
  44. redirect url(:document, @document)
  45. else
  46. raise BadRequest
  47. end
  48. end
  49.  
  50. def destroy
  51. @document = Document[params[:id]]
  52. if @document.destroy!
  53. redirect url(:documents)
  54. else
  55. raise BadRequest
  56. end
  57. end
  58.  
  59. def destroy_category
  60. @category = Category[params[:id]]
  61. if @category.destroy!
  62. redirect url(:documents)
  63. else
  64. raise BadRequest
  65. end
  66. end
  67.  
  68. def destroy_document_category
  69. @document = Document[params[:id]]
  70. @category = Category[params[:category_id]]
  71. if @category.documents.delete(@document)
  72. redirect url(:document, @document)
  73. else
  74. raise BadRequest
  75. end
  76. end
  77.  
  78. def destroy_comment
  79. @document = Document[params[:id]]
  80. @comment = Comment[params[:comment_id]]
  81. if @comment.destroy!
  82. redirect url(:document, @document)
  83. else
  84. raise BadRequest
  85. end
  86. end
  87.  
  88. def edit
  89. only_provides :html
  90. @document = Document[params[:id]]
  91. display @document
  92. end
  93.  
  94. def edit_category
  95. only_provides :html
  96. @category = Category[params[:id]]
  97. display @category
  98. end
  99.  
  100. def edit_comment
  101. only_provides :html
  102. @document = Document[params[:id]]
  103. @comment = Comment[params[:comment_id]]
  104. display @comment
  105. end
  106.  
  107. def index
  108. unless owners.blank?
  109. unless params[:id].blank?
  110. @category_selected = Category[params[:id]]
  111. @documents = @category_selected.documents
  112. else
  113. @documents = Document.all(:order => "updated_at desc")
  114. end
  115. unless owner?
  116. categories_with_documents = []
  117. Category.all.each do |category|
  118. unless category.documents.blank?
  119. categories_with_documents << category.id
  120. end
  121. end
  122. @categories = Category.all(:id => categories_with_documents, :order => "text collate nocase")
  123. else
  124. @categories = Category.all(:order => "text collate nocase")
  125. end
  126. @category = Category.new(params[:category])
  127. display @documents
  128. else
  129. redirect(url(:new_person))
  130. end
  131. end
  132.  
  133. def new
  134. only_provides :html
  135. @document = Document.new(params[:document])
  136. display @document
  137. end
  138.  
  139. def search
  140. @query = params[:query]
  141. if @query.blank?
  142. flash[:error] = "You have to enter keywords to search for."
  143. redirect(url(:home))
  144. else
  145. @title_documents = Document.all(:title.like => "%#{@query}%", :order => "updated_at desc")
  146. @text_documents = Document.all(:text.like => "%#{@query}%", :order => "updated_at desc")
  147. @documents = @title_documents | @text_documents
  148. if @documents.blank?
  149. flash.now[:warning] = "No documents matched <b>#{@query}</b>."
  150. end
  151. display @documents
  152. end
  153. end
  154.  
  155. def show
  156. @document = Document[params[:id]]
  157. @comment = Comment.new(params[:comment])
  158. categories = []
  159. @document.categories.each do |category|
  160. categories << category.id
  161. end
  162. @categories = Category.all(:id.not => categories, :order => "text collate nocase")
  163. @document_categories = Category.all(:id => categories, :order => "text collate nocase")
  164. @category = Category.new(params[:category])
  165. display @document
  166. end
  167.  
  168. def update
  169. @document = Document[params[:id]]
  170. if @document.update_attributes(params[:document])
  171. redirect(url(:document, @document))
  172. else
  173. raise BadRequest
  174. end
  175. end
  176.  
  177. def update_category
  178. @category = Category[params[:id]]
  179. if @category.update_attributes(params[:category])
  180. redirect(url(:documents))
  181. else
  182. raise BadRequest
  183. end
  184. end
  185.  
  186. def update_comment
  187. @document = Document[params[:id]]
  188. @comment = Comment[params[:comment_id]]
  189. if @comment.update_attributes(params[:comment])
  190. redirect url(:document, @document)
  191. else
  192. raise BadRequest
  193. end
  194. end
  195.  
  196. end
Add Comment
Please, Sign In to add comment