Skip to content

Commit

Permalink
Install activestorage for storing user uploaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
hellcp-work committed Apr 18, 2024
1 parent c2ef1bd commit ee573fe
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,6 +9,7 @@
/src/api/config/options.yml
/src/api/config/secret.key
/src/api/config/thinking_sphinx.yml
/src/api/storage
/src/api/db/sphinx
/src/api/config/*.sphinx.conf
/src/api/coverage
Expand Down
1 change: 1 addition & 0 deletions src/api/app/controllers/webui/webui_controller.rb
Expand Up @@ -12,6 +12,7 @@ class Webui::WebuiController < ActionController::Base
include RescueAuthorizationHandler
include SetCurrentRequestDetails
include Webui::ElisionsHelper
include ActiveStorage::SetCurrent
protect_from_forgery

before_action :setup_view_path
Expand Down
1 change: 1 addition & 0 deletions src/api/config/application.rb
Expand Up @@ -7,6 +7,7 @@
require 'action_mailer/railtie'
require 'action_controller/railtie'
require 'action_view/railtie'
require 'active_storage/engine'
require 'sprockets/railtie'
require 'rails/test_unit/railtie'

Expand Down
2 changes: 1 addition & 1 deletion src/api/config/environments/development.rb
Expand Up @@ -45,7 +45,7 @@
end

# Store uploaded files on the local file system (see config/storage.yml for options).
# config.active_storage.service = :local
config.active_storage.service = :local

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
Expand Down
2 changes: 1 addition & 1 deletion src/api/config/environments/production.rb
Expand Up @@ -43,7 +43,7 @@
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

# Store uploaded files on the local file system (see config/storage.yml for options).
# config.active_storage.service = :local
config.active_storage.service = :local

# Mount Action Cable outside main process or domain.
# config.action_cable.mount_path = nil
Expand Down
2 changes: 1 addition & 1 deletion src/api/config/environments/test.rb
Expand Up @@ -36,7 +36,7 @@
# config.action_dispatch.show_exceptions = false

# Store uploaded files on the local file system in a temporary directory.
# config.active_storage.service = :test
config.active_storage.service = :test

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
Expand Down
7 changes: 7 additions & 0 deletions src/api/config/storage.yml
@@ -0,0 +1,7 @@
local:
service: Disk
root: <%= Rails.root.join("storage") %>

test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
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
38 changes: 34 additions & 4 deletions src/api/db/schema.rb
Expand Up @@ -10,7 +10,35 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_04_11_150057) do
ActiveRecord::Schema[7.0].define(version: 2024_04_18_065204) do
create_table "active_storage_attachments", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.integer "record_id", null: false
t.integer "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", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", 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", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.integer "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 "appeals", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.text "reason", null: false
t.integer "appellant_id", null: false
Expand Down Expand Up @@ -388,6 +416,7 @@
t.text "code_of_conduct"
t.string "contact_name"
t.string "contact_url"
t.text "logo"
end

create_table "data_migrations", id: false, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
Expand All @@ -400,6 +429,7 @@
t.integer "kind", default: 0
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "type", default: "DecisionCleared"
t.index ["moderator_id"], name: "index_decisions_on_moderator_id"
end

Expand Down Expand Up @@ -1140,7 +1170,7 @@
t.index ["state"], name: "index_users_on_state"
end

create_table "versions", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t|
create_table "versions", charset: "utf8mb4", force: :cascade do |t|
t.string "item_type", limit: 191, null: false
t.bigint "item_id", null: false
t.string "event", null: false
Expand Down Expand Up @@ -1213,8 +1243,8 @@
t.index ["user_id"], name: "index_workflow_token_users_on_user_id"
end

add_foreign_key "appeals", "decisions"
add_foreign_key "appeals", "users", column: "appellant_id"
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 "attrib_allowed_values", "attrib_types", name: "attrib_allowed_values_ibfk_1"
add_foreign_key "attrib_default_values", "attrib_types", name: "attrib_default_values_ibfk_1"
add_foreign_key "attrib_issues", "attribs", name: "attrib_issues_ibfk_1"
Expand Down

0 comments on commit ee573fe

Please sign in to comment.