From 3928da4577bbe668514a704ec4bbb5ec8df55b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Thu, 21 Jul 2022 10:28:07 -0300 Subject: [PATCH] add ck editor file upload endpoint --- Gemfile | 3 +- Gemfile.lock | 7 + .../ck_editor_uploads_controller.rb | 14 ++ app/models/ck_editor_upload.rb | 11 + ...20220721132022_create_ck_editor_uploads.rb | 7 + ...te_active_storage_tables.active_storage.rb | 57 +++++ db/schema.rb | 37 ++- erd.svg | 229 +++++++++++++++--- 8 files changed, 326 insertions(+), 39 deletions(-) create mode 100644 app/controllers/ck_editor_uploads_controller.rb create mode 100644 app/models/ck_editor_upload.rb create mode 100644 db/migrate/20220721132022_create_ck_editor_uploads.rb create mode 100644 db/migrate/20220721132556_create_active_storage_tables.active_storage.rb diff --git a/Gemfile b/Gemfile index 8fc90fa..37594a0 100644 --- a/Gemfile +++ b/Gemfile @@ -23,8 +23,7 @@ gem "redis", "~> 4.0" gem "bootsnap", require: false -# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] -# gem "image_processing", "~> 1.2" +gem "image_processing", "~> 1.2" gem "pundit", "~> 2.2" gem "enumerize", "~> 2.5" diff --git a/Gemfile.lock b/Gemfile.lock index f640a14..82bf802 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -130,6 +130,9 @@ GEM hashie (5.0.0) i18n (1.12.0) concurrent-ruby (~> 1.0) + image_processing (1.12.2) + mini_magick (>= 4.9.5, < 5) + ruby-vips (>= 2.0.17, < 3) importmap-rails (1.1.5) actionpack (>= 6.0.0) railties (>= 6.0.0) @@ -168,6 +171,7 @@ GEM mini_mime (>= 0.1.1) marcel (1.0.2) method_source (1.0.0) + mini_magick (4.11.0) mini_mime (1.1.2) minitest (5.16.2) msgpack (1.5.3) @@ -279,6 +283,8 @@ GEM rspec-support (3.11.0) ruby-graphviz (1.2.5) rexml + ruby-vips (2.1.4) + ffi (~> 1.12) ruby2_keywords (0.0.5) sassc (2.4.0) ffi (~> 1.9) @@ -334,6 +340,7 @@ DEPENDENCIES enumerize (~> 2.5) factory_bot_rails (~> 6.2) faker (~> 2.21) + image_processing (~> 1.2) importmap-rails jbuilder omniauth (~> 1.9.1) diff --git a/app/controllers/ck_editor_uploads_controller.rb b/app/controllers/ck_editor_uploads_controller.rb new file mode 100644 index 0000000..00160b9 --- /dev/null +++ b/app/controllers/ck_editor_uploads_controller.rb @@ -0,0 +1,14 @@ +class CkEditorUploadsController < ApplicationController + def create + @upload = CkEditorUpload.new(attachment: params[:attachment]) + + if @upload.save! + render(json: { + uploaded: true, + url: rails_blob_url(@upload.attachment, only_path: false), + }) + else + render(json: { uploaded: false }) + end + end +end diff --git a/app/models/ck_editor_upload.rb b/app/models/ck_editor_upload.rb new file mode 100644 index 0000000..4074a1f --- /dev/null +++ b/app/models/ck_editor_upload.rb @@ -0,0 +1,11 @@ +# == Schema Information +# +# Table name: ck_editor_uploads +# +# id :bigint not null, primary key +# created_at :datetime not null +# updated_at :datetime not null +# +class CkEditorUpload < ApplicationRecord + has_one_attached :attachment +end diff --git a/db/migrate/20220721132022_create_ck_editor_uploads.rb b/db/migrate/20220721132022_create_ck_editor_uploads.rb new file mode 100644 index 0000000..ed7d0d3 --- /dev/null +++ b/db/migrate/20220721132022_create_ck_editor_uploads.rb @@ -0,0 +1,7 @@ +class CreateCkEditorUploads < ActiveRecord::Migration[7.0] + def change + create_table :ck_editor_uploads do |t| + t.timestamps + end + end +end diff --git a/db/migrate/20220721132556_create_active_storage_tables.active_storage.rb b/db/migrate/20220721132556_create_active_storage_tables.active_storage.rb new file mode 100644 index 0000000..8a7bfe1 --- /dev/null +++ b/db/migrate/20220721132556_create_active_storage_tables.active_storage.rb @@ -0,0 +1,57 @@ +# This migration comes from active_storage (originally 20170806125915) +class CreateActiveStorageTables < ActiveRecord::Migration[5.2] + def change + # Use Active Record's configured type for primary and foreign keys + primary_key_type, foreign_key_type = primary_and_foreign_key_types + + create_table :active_storage_blobs, id: primary_key_type do |t| + t.string :key, null: false + t.string :filename, null: false + t.string :content_type + t.text :metadata + t.string :service_name, null: false + t.bigint :byte_size, null: false + t.string :checksum + + if connection.supports_datetime_with_precision? + t.datetime :created_at, precision: 6, null: false + else + t.datetime :created_at, null: false + end + + t.index [ :key ], unique: true + end + + create_table :active_storage_attachments, id: primary_key_type do |t| + t.string :name, null: false + t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type + t.references :blob, null: false, type: foreign_key_type + + if connection.supports_datetime_with_precision? + t.datetime :created_at, precision: 6, null: false + else + t.datetime :created_at, null: false + end + + t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true + t.foreign_key :active_storage_blobs, column: :blob_id + end + + create_table :active_storage_variant_records, id: primary_key_type do |t| + t.belongs_to :blob, null: false, index: false, type: foreign_key_type + t.string :variation_digest, null: false + + t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true + t.foreign_key :active_storage_blobs, column: :blob_id + end + end + + private + def primary_and_foreign_key_types + config = Rails.configuration.generators + setting = config.options[config.orm][:primary_key_type] + primary_key_type = setting || :primary_key + foreign_key_type = setting || :bigint + [primary_key_type, foreign_key_type] + end +end diff --git a/db/schema.rb b/db/schema.rb index 967d517..7c0f6bb 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[7.0].define(version: 2022_07_21_124944) do +ActiveRecord::Schema[7.0].define(version: 2022_07_21_132556) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,6 +28,34 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_21_124944) do t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource" end + create_table "active_storage_attachments", force: :cascade do |t| + t.string "name", null: false + t.string "record_type", null: false + t.bigint "record_id", null: false + t.bigint "blob_id", null: false + t.datetime "created_at", null: false + t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" + t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true + end + + create_table "active_storage_blobs", force: :cascade do |t| + t.string "key", null: false + t.string "filename", null: false + t.string "content_type" + t.text "metadata" + t.string "service_name", null: false + t.bigint "byte_size", null: false + t.string "checksum" + t.datetime "created_at", null: false + t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true + end + + create_table "active_storage_variant_records", force: :cascade do |t| + t.bigint "blob_id", null: false + t.string "variation_digest", null: false + t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true + end + create_table "axes", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false @@ -42,6 +70,11 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_21_124944) do t.index ["name"], name: "index_categories_on_name", unique: true end + create_table "ck_editor_uploads", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "questions", force: :cascade do |t| t.bigint "user_id", null: false t.bigint "subject_id" @@ -88,6 +121,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_21_124944) do t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" + add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "questions", "subjects" add_foreign_key "questions", "users" add_foreign_key "subjects", "axes" diff --git a/erd.svg b/erd.svg index 1f5f115..f6ee7d4 100644 --- a/erd.svg +++ b/erd.svg @@ -4,49 +4,206 @@ - - + + ProgressTest - -Progress Test - Models + +Progress Test - Models m_ActiveAdmin::Comment - -ActiveAdmin::Comment - -author_id -integer (8) -author_type -string -body -text ∗ -namespace -string ∗ -resource_id -integer (8) -resource_type -string + +ActiveAdmin::Comment + +author_id +integer (8) +author_type +string +body +text ∗ +namespace +string ∗ +resource_id +integer (8) +resource_type +string + + + +m_ActiveStorage::Attachment + +ActiveStorage::Attachment + +name +string ∗ +record_type +string ∗ + + + +m_ActiveStorage::Blob + +ActiveStorage::Blob + +byte_size +integer (8) ∗ +checksum +string ∗ +content_type +string +filename +string ∗ +key +string ∗ +metadata +text +service_name +string ∗ + + + +m_ActiveStorage::Blob->m_ActiveStorage::Attachment + + + + +m_ActiveStorage::Blob->m_ActiveStorage::Blob + + + + +m_ActiveStorage::VariantRecord + +ActiveStorage::VariantRecord + +variation_digest +string ∗ + + + +m_ActiveStorage::VariantRecord->m_ActiveStorage::Attachment + + + + +m_ActiveStorage::VariantRecord->m_ActiveStorage::Blob + + + + + +m_Axis + +Axis + +name +string ∗ U + + + +m_Subject + +Subject + +name +string ∗ U + + + +m_Axis->m_Subject + + + + + +m_Category + +Category + +name +string ∗ U + + + +m_Category->m_Subject + + + + + +m_CkEditorUpload + +CkEditorUpload + + + +m_CkEditorUpload->m_ActiveStorage::Attachment + + + + +m_Question + +Question + +alternatives +jsonb ∗ +authorship +string +authorship_year +string +bloom_taxonomy +string +body +text +check_type +string +difficulty +string +explanation +text +instruction +text +intention +text +references +text +status +string ∗ +support +text + + + +m_Subject->m_Question + + - + m_User - -User - -email -string ∗ U -encrypted_password -string ∗ -name -string ∗ -remember_created_at -datetime (6,0) -reset_password_sent_at -datetime (6,0) -reset_password_token -string + +User + +email +string ∗ U +encrypted_password +string ∗ +name +string ∗ +remember_created_at +datetime (6,0) +reset_password_sent_at +datetime (6,0) +reset_password_token +string + + + +m_User->m_Question + +