diff --git a/.gitignore b/.gitignore index c9c85604..185b2f74 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ tmp public/assets public/system public/uploads +storage vendor/bundle # no configuration diff --git a/Dockerfile b/Dockerfile index 0b4f9aa1..49180276 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,6 +39,7 @@ RUN export DATABASE_URL=mysql2://localhost/temp?encoding=utf8 && \ mariadb -e "CREATE DATABASE temp" && \ cp config/app_config.yml.SAMPLE config/app_config.yml && \ cp config/database.yml.MySQL_SAMPLE config/database.yml && \ + cp config/storage.yml.SAMPLE config/storage.yml && \ bundle exec rake db:setup assets:precompile && \ rm -Rf tmp/* && \ /etc/init.d/mysql stop && \ @@ -56,6 +57,8 @@ USER nobody EXPOSE 3000 +VOLUME /usr/src/app/storage + # cleanup, and by default start web process from Procfile ENTRYPOINT ["./docker-entrypoint.sh"] CMD ["./proc-start", "web"] diff --git a/config/environments/development.rb.SAMPLE b/config/environments/development.rb.SAMPLE index 035a7e86..50787eca 100644 --- a/config/environments/development.rb.SAMPLE +++ b/config/environments/development.rb.SAMPLE @@ -31,6 +31,9 @@ Rails.application.configure do config.cache_store = :null_store end + # Store uploaded files on the local file system (see config/storage.yml for options) + config.active_storage.service = :local + # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false diff --git a/config/environments/production.rb b/config/environments/production.rb index 8e8dcf25..0560b38d 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -40,6 +40,9 @@ Rails.application.configure do # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache # 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 + # Mount Action Cable outside main process or domain # config.action_cable.mount_path = nil # config.action_cable.url = 'wss://example.com/cable' diff --git a/config/environments/test.rb b/config/environments/test.rb index b10aeee0..ccf3767f 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -31,6 +31,10 @@ Rails.application.configure do # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false + + # Store uploaded files on the local file system in a temporary directory + config.active_storage.service = :test + config.action_mailer.perform_caching = false # Tell Action Mailer not to deliver emails to the real world. diff --git a/config/initializers/active_storage_foodcoop_path.rb b/config/initializers/active_storage_foodcoop_path.rb new file mode 100644 index 00000000..bebb33d2 --- /dev/null +++ b/config/initializers/active_storage_foodcoop_path.rb @@ -0,0 +1,15 @@ +require 'active_storage/service/disk_service' + +module FoodsoftActiveStorageDiskService + def self.included(base) # :nodoc: + base.class_eval do + def path_for(key) + File.join root, FoodsoftConfig.scope, folder_for(key), key + end + end + end +end + +ActiveSupport.on_load(:after_initialize) do + ActiveStorage::Service::DiskService.include FoodsoftActiveStorageDiskService +end diff --git a/config/storage.yml.SAMPLE b/config/storage.yml.SAMPLE new file mode 100644 index 00000000..d32f76e8 --- /dev/null +++ b/config/storage.yml.SAMPLE @@ -0,0 +1,34 @@ +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + +local: + service: Disk + root: <%= Rails.root.join("storage") %> + +# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) +# amazon: +# service: S3 +# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> +# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> +# region: us-east-1 +# bucket: your_own_bucket + +# Remember not to checkin your GCS keyfile to a repository +# google: +# service: GCS +# project: your_project +# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> +# bucket: your_own_bucket + +# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) +# microsoft: +# service: AzureStorage +# storage_account_name: your_account_name +# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> +# container: your_container_name + +# mirror: +# service: Mirror +# primary: local +# mirrors: [ amazon, google, microsoft ] diff --git a/db/migrate/20190101000000_create_active_storage_tables.active_storage.rb b/db/migrate/20190101000000_create_active_storage_tables.active_storage.rb new file mode 100644 index 00000000..3739c2e8 --- /dev/null +++ b/db/migrate/20190101000000_create_active_storage_tables.active_storage.rb @@ -0,0 +1,27 @@ +# This migration comes from active_storage (originally 20170806125915) +class CreateActiveStorageTables < ActiveRecord::Migration[4.2][5.2] + def change + create_table :active_storage_blobs do |t| + t.string :key, null: false + t.string :filename, null: false + t.string :content_type + t.text :metadata + t.bigint :byte_size, null: false + t.string :checksum, null: false + t.datetime :created_at, null: false + + t.index [:key], unique: true + end + + create_table :active_storage_attachments do |t| + t.string :name, null: false + t.references :record, null: false, polymorphic: true, index: false + t.references :blob, null: false + + t.datetime :created_at, null: false + + 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 + end +end diff --git a/db/schema.rb b/db/schema.rb index 6e85c5d6..0fcc5161 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,6 +12,27 @@ ActiveRecord::Schema.define(version: 2021_02_05_090257) do + create_table "active_storage_attachments", id: :integer, 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", id: :integer, force: :cascade do |t| + t.string "key", null: false + t.string "filename", null: false + t.string "content_type" + t.text "metadata" + t.bigint "byte_size", null: false + t.string "checksum", null: false + t.datetime "created_at", null: false + t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true + end + create_table "article_categories", id: :integer, force: :cascade do |t| t.string "name", default: "", null: false t.string "description" diff --git a/lib/tasks/foodsoft_setup.rake b/lib/tasks/foodsoft_setup.rake index 9dc2c7d0..baa483d1 100644 --- a/lib/tasks/foodsoft_setup.rake +++ b/lib/tasks/foodsoft_setup.rake @@ -33,6 +33,7 @@ namespace :foodsoft do setup_app_config setup_development setup_database + setup_storage start_mailcatcher puts yellow "All done! Your foodsoft setup should be running smoothly." start_server @@ -43,6 +44,7 @@ namespace :foodsoft do puts yellow "This task will help you get your foodcoop running in development via docker." setup_app_config setup_development + setup_storage setup_run_rake_db_setup puts yellow "All done! Your foodsoft setup should be running smoothly via docker." end @@ -112,6 +114,15 @@ def setup_development reminder(file) end +def setup_storage + file = 'config/storage.yml' + return nil if skip?(file) + + puts yellow "Copying #{file}..." + %x(cp -p #{Rails.root.join("#{file}.SAMPLE")} #{Rails.root.join(file)}) + reminder(file) +end + def start_mailcatcher return nil if ENV['MAILCATCHER_PORT'] # skip when it has an existing Docker container