You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

93 line
2.4 KiB

  1. class DocumentsController < ApplicationController
  2. before_action :set_document, only: [:show, :edit, :update, :destroy]
  3. # GET /documents
  4. # GET /documents.json
  5. def index
  6. if params[:search]
  7. @documents = User.find(session[:user_id]).documents.contains_word(params[:search])
  8. elsif params[:category]
  9. @documents = User.find(session[:user_id]).documents.belongs_to_category(params[:category])
  10. else
  11. @documents = User.find(session[:user_id]).documents
  12. end
  13. end
  14. # GET /documents/1
  15. # GET /documents/1.json
  16. def show
  17. end
  18. # GET /documents/new
  19. def new
  20. @document = Document.new
  21. end
  22. # GET /documents/1/edit
  23. def edit
  24. end
  25. # POST /documents
  26. # POST /documents.json
  27. def create
  28. @document = Document.new(document_params)
  29. @document.name = @document.doc_file_name
  30. @document.user = User.find(session[:user_id])
  31. if Document.exists?(doc_fingerprint: @document.doc_fingerprint)
  32. end
  33. respond_to do |format|
  34. if @document.save
  35. format.html { redirect_to @document, notice: 'Document was successfully created.' }
  36. format.json { render :show, status: :created, location: @document }
  37. else
  38. format.html { render :new }
  39. format.json { render json: @document.errors, status: :unprocessable_entity }
  40. end
  41. end
  42. end
  43. # PATCH/PUT /documents/1
  44. # PATCH/PUT /documents/1.json
  45. def update
  46. respond_to do |format|
  47. if @document.update(document_params)
  48. format.html { redirect_to @document, notice: 'Document was successfully updated.' }
  49. format.json { render :show, status: :ok, location: @document }
  50. else
  51. format.html { render :edit }
  52. format.json { render json: @document.errors, status: :unprocessable_entity }
  53. end
  54. end
  55. end
  56. # DELETE /documents/1
  57. # DELETE /documents/1.json
  58. def destroy
  59. @document.destroy
  60. respond_to do |format|
  61. format.html { redirect_to documents_url, notice: 'Document was successfully destroyed.' }
  62. format.json { head :no_content }
  63. end
  64. end
  65. # GET /documents/search
  66. def search
  67. end
  68. # GET
  69. private
  70. # Use callbacks to share common setup or constraints between actions.
  71. def set_document
  72. @document = Document.find(params[:id])
  73. end
  74. # Never trust parameters from the scary internet, only allow the white list through.
  75. def document_params
  76. params.require(:document).permit(:doc, :category_id)
  77. end
  78. end