From 493973bdd34d6dbf2b00f38d5a17e0a0754f46d4 Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Thu, 24 Nov 2016 18:53:38 +0100 Subject: [PATCH] Some more functions. --- app/controllers/documents_controller.rb | 3 ++- app/controllers/sessions_controller.rb | 1 + app/models/document.rb | 10 +++++++++- app/models/tag.rb | 3 +++ app/models/user.rb | 2 ++ db/migrate/20161124165646_create_tags.rb | 10 ++++++++++ db/migrate/20161124171050_add_user_to_document.rb | 5 +++++ .../20161124174721_create_join_table_documents_tags.rb | 5 +++++ db/schema.rb | 16 +++++++++++++++- test/fixtures/tags.yml | 7 +++++++ test/models/tag_test.rb | 7 +++++++ 11 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 app/models/tag.rb create mode 100644 db/migrate/20161124165646_create_tags.rb create mode 100644 db/migrate/20161124171050_add_user_to_document.rb create mode 100644 db/migrate/20161124174721_create_join_table_documents_tags.rb create mode 100644 test/fixtures/tags.yml create mode 100644 test/models/tag_test.rb diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 931c0b2..ee9616c 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -4,7 +4,7 @@ class DocumentsController < ApplicationController # GET /documents # GET /documents.json def index - @documents = Document.all + @documents = User.find(session[:user_id]).documents end # GET /documents/1 @@ -27,6 +27,7 @@ class DocumentsController < ApplicationController @document = Document.new(document_params) @document.name = @document.doc_file_name + @document.user = User.find(session[:user_id]) if Document.exists?(doc_fingerprint: @document.doc_fingerprint) end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index e941571..2eeb9a9 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -16,5 +16,6 @@ class SessionsController < ApplicationController def destroy session[:user_id] = nil + redirect_to documents_url end end diff --git a/app/models/document.rb b/app/models/document.rb index ddda517..5a6a4f8 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -1,4 +1,12 @@ 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' end diff --git a/app/models/tag.rb b/app/models/tag.rb new file mode 100644 index 0000000..72ce84d --- /dev/null +++ b/app/models/tag.rb @@ -0,0 +1,3 @@ +class Tag < ApplicationRecord + has_and_belongs_to_many :documents +end diff --git a/app/models/user.rb b/app/models/user.rb index d67da20..26dfd33 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,5 @@ class User < ApplicationRecord has_secure_password + + has_many :documents, dependent: :destroy end diff --git a/db/migrate/20161124165646_create_tags.rb b/db/migrate/20161124165646_create_tags.rb new file mode 100644 index 0000000..41d2187 --- /dev/null +++ b/db/migrate/20161124165646_create_tags.rb @@ -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 diff --git a/db/migrate/20161124171050_add_user_to_document.rb b/db/migrate/20161124171050_add_user_to_document.rb new file mode 100644 index 0000000..491e76b --- /dev/null +++ b/db/migrate/20161124171050_add_user_to_document.rb @@ -0,0 +1,5 @@ +class AddUserToDocument < ActiveRecord::Migration[5.0] + def change + add_reference :documents, :user, index: true + end +end diff --git a/db/migrate/20161124174721_create_join_table_documents_tags.rb b/db/migrate/20161124174721_create_join_table_documents_tags.rb new file mode 100644 index 0000000..ee8359d --- /dev/null +++ b/db/migrate/20161124174721_create_join_table_documents_tags.rb @@ -0,0 +1,5 @@ +class CreateJoinTableDocumentsTags < ActiveRecord::Migration[5.0] + def change + create_join_table :documents, :tags + end +end diff --git a/db/schema.rb b/db/schema.rb index f35f97e..5536e32 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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| t.string "name" @@ -21,7 +21,21 @@ ActiveRecord::Schema.define(version: 20161124142626) do t.integer "doc_file_size" t.datetime "doc_updated_at" t.string "doc_fingerprint" + t.integer "user_id" 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 create_table "users", force: :cascade do |t| diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml new file mode 100644 index 0000000..de1506a --- /dev/null +++ b/test/fixtures/tags.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + value: MyString + +two: + value: MyString diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb new file mode 100644 index 0000000..b8498a1 --- /dev/null +++ b/test/models/tag_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TagTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end