| @@ -4,7 +4,7 @@ class DocumentsController < ApplicationController | |||||
| # GET /documents | # GET /documents | ||||
| # GET /documents.json | # GET /documents.json | ||||
| def index | def index | ||||
| @documents = Document.all | |||||
| @documents = User.find(session[:user_id]).documents | |||||
| end | end | ||||
| # GET /documents/1 | # GET /documents/1 | ||||
| @@ -27,6 +27,7 @@ class DocumentsController < ApplicationController | |||||
| @document = Document.new(document_params) | @document = Document.new(document_params) | ||||
| @document.name = @document.doc_file_name | @document.name = @document.doc_file_name | ||||
| @document.user = User.find(session[:user_id]) | |||||
| if Document.exists?(doc_fingerprint: @document.doc_fingerprint) | if Document.exists?(doc_fingerprint: @document.doc_fingerprint) | ||||
| end | end | ||||
| @@ -16,5 +16,6 @@ class SessionsController < ApplicationController | |||||
| def destroy | def destroy | ||||
| session[:user_id] = nil | session[:user_id] = nil | ||||
| redirect_to documents_url | |||||
| end | end | ||||
| end | end | ||||
| @@ -1,4 +1,12 @@ | |||||
| class Document < ApplicationRecord | class Document < ApplicationRecord | ||||
| has_attached_file :doc, adapter_options: { hash_digest: Digest::SHA256 } | |||||
| belongs_to :user | |||||
| has_and_belongs_to_many :tags | |||||
| has_attached_file :doc, | |||||
| { | |||||
| adapter_options: { hash_digest: Digest::SHA256 }, | |||||
| url: "/system/:class/:attachment/:id_partition/:style/:hash.:extension", | |||||
| hash_secret: Rails.application.secrets.secret_key_base | |||||
| } | |||||
| validates_attachment_content_type :doc, content_type: 'application/pdf' | validates_attachment_content_type :doc, content_type: 'application/pdf' | ||||
| end | end | ||||
| @@ -0,0 +1,3 @@ | |||||
| class Tag < ApplicationRecord | |||||
| has_and_belongs_to_many :documents | |||||
| end | |||||
| @@ -1,3 +1,5 @@ | |||||
| class User < ApplicationRecord | class User < ApplicationRecord | ||||
| has_secure_password | has_secure_password | ||||
| has_many :documents, dependent: :destroy | |||||
| end | end | ||||
| @@ -0,0 +1,10 @@ | |||||
| class CreateTags < ActiveRecord::Migration[5.0] | |||||
| def change | |||||
| create_table :tags do |t| | |||||
| t.string :value | |||||
| t.boolean :automatically_asigned | |||||
| t.timestamps | |||||
| end | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,5 @@ | |||||
| class AddUserToDocument < ActiveRecord::Migration[5.0] | |||||
| def change | |||||
| add_reference :documents, :user, index: true | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,5 @@ | |||||
| class CreateJoinTableDocumentsTags < ActiveRecord::Migration[5.0] | |||||
| def change | |||||
| create_join_table :documents, :tags | |||||
| end | |||||
| end | |||||
| @@ -10,7 +10,7 @@ | |||||
| # | # | ||||
| # It's strongly recommended that you check this file into your version control system. | # It's strongly recommended that you check this file into your version control system. | ||||
| ActiveRecord::Schema.define(version: 20161124142626) do | |||||
| ActiveRecord::Schema.define(version: 20161124174721) do | |||||
| create_table "documents", force: :cascade do |t| | create_table "documents", force: :cascade do |t| | ||||
| t.string "name" | t.string "name" | ||||
| @@ -21,7 +21,21 @@ ActiveRecord::Schema.define(version: 20161124142626) do | |||||
| t.integer "doc_file_size" | t.integer "doc_file_size" | ||||
| t.datetime "doc_updated_at" | t.datetime "doc_updated_at" | ||||
| t.string "doc_fingerprint" | t.string "doc_fingerprint" | ||||
| t.integer "user_id" | |||||
| t.index ["doc_fingerprint"], name: "index_documents_on_doc_fingerprint" | t.index ["doc_fingerprint"], name: "index_documents_on_doc_fingerprint" | ||||
| t.index ["user_id"], name: "index_documents_on_user_id" | |||||
| end | |||||
| create_table "documents_tags", id: false, force: :cascade do |t| | |||||
| t.integer "document_id", null: false | |||||
| t.integer "tag_id", null: false | |||||
| end | |||||
| create_table "tags", force: :cascade do |t| | |||||
| t.string "value" | |||||
| t.boolean "automatically_asigned" | |||||
| t.datetime "created_at", null: false | |||||
| t.datetime "updated_at", null: false | |||||
| end | end | ||||
| create_table "users", force: :cascade do |t| | create_table "users", force: :cascade do |t| | ||||
| @@ -0,0 +1,7 @@ | |||||
| # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | |||||
| one: | |||||
| value: MyString | |||||
| two: | |||||
| value: MyString | |||||
| @@ -0,0 +1,7 @@ | |||||
| require 'test_helper' | |||||
| class TagTest < ActiveSupport::TestCase | |||||
| # test "the truth" do | |||||
| # assert true | |||||
| # end | |||||
| end | |||||