Skip to content

Commit

Permalink
Add support for ActiveStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
paroga committed Mar 7, 2021
1 parent 4b7cbf1 commit 45039bf
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tmp
public/assets
public/system
public/uploads
storage
vendor/bundle

# no configuration
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand All @@ -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"]
3 changes: 3 additions & 0 deletions config/environments/development.rb.SAMPLE
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
# 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'
Expand Down
4 changes: 4 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

# 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.
Expand Down
15 changes: 15 additions & 0 deletions config/initializers/active_storage_foodcoop_path.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions config/storage.yml.SAMPLE
Original file line number Diff line number Diff line change
@@ -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 ]
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions lib/tasks/foodsoft_setup.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 45039bf

Please sign in to comment.