Skip to content

Commit

Permalink
WIP adding user profile pics stored on S3 via ActiveStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
denny committed Dec 10, 2019
1 parent de1cedc commit daa25a2
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 39 deletions.
2 changes: 0 additions & 2 deletions .env.development

This file was deleted.

7 changes: 7 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DATABASE_URL=postgres://user:password@localhost/shinycms
SECRET_KEY_BASE=abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234

AWS_ACCESS_KEY_ID=01234567890123456789
AWS_SECRET_ACCESS_KEY=0123456789012345678901234567890123456789
AWS_REGION=eu-west-2
AWS_BUCKET=shinycms-files
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ source 'https://rubygems.org' do
gem 'devise-pwned_password'
gem 'pundit'

# WYSIWYG editor for admin area
# CKEditor: WYSIWYG editor for admin area
gem 'ckeditor'
# Image storage for CKEditor
gem 'image_magick'

# Pagination
gem 'kaminari'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
image_magick (0.1.9)
jaro_winkler (1.5.4)
json (2.2.0)
kaminari (1.1.1)
Expand Down Expand Up @@ -292,6 +293,7 @@ DEPENDENCIES
devise-pwned_password!
factory_bot_rails!
faker!
image_magick!
kaminari!
listen (>= 3.0.5, < 3.3)!
pg (>= 0.18, < 2.0)!
Expand Down
8 changes: 5 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class User < ApplicationRecord
# Restrict the character set for usernames to letters, numbers, and - . _
validates :username, format: ANCHORED_USERNAME_REGEX

# User profile pic (powered by ActiveStorage)
has_one_attached :profile_pic

# Authorisation (powered by Pundit)
has_many :user_capabilities, dependent: :restrict_with_error

has_many :capabilities, through: :user_capabilities,
dependent: :restrict_with_error
has_many :capabilities, dependent: :restrict_with_error,
through: :user_capabilities

def can?( capability )
t_can? I18n.t( "capability.#{capability}" )
Expand Down
9 changes: 8 additions & 1 deletion app/views/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<%= f.label :display_name %>
<br><%= f.text_field :display_name %>
</p>

<p>
<%= f.label :email %>
<br><%= f.text_field :email %>
Expand All @@ -17,9 +18,15 @@
<%= f.label :display_email %>
<br><%= f.text_field :display_email %>
</p>

<p>
<%= f.label :password %>
<br><%= f.text_field :password %>
<br><%= f.password_field :password %>
</p>

<p>
<%= f.label :profile_pic %>
<br><%= f.file_field :profile_pic %>
</p>

<p>
Expand Down
27 changes: 7 additions & 20 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
require_relative 'boot'

# Replace this:
# require 'rails/all'
# ... with this stack of individual require statements (so I can get selective)

# Load Rails components selectively
require 'rails'

# require 'action_cable/engine'
require 'action_controller/railtie' # Pretty sure we need this though
# require 'action_mailbox/engine' # Won't need for a long time: inbound email
require 'action_mailer/railtie' # Registration emails, etc
require 'action_controller/railtie'
# require 'action_mailbox/engine' # Won't need for a long time (inbound email)
require 'action_mailer/railtie' # Outbound email (registration, etc)
# require 'action_text/engine'
require 'action_view/railtie' # Also seems quite fundamental
require 'action_view/railtie'
require 'active_job/railtie' # Queue (for mailer tasks)
require 'active_record/railtie'
# require 'active_storage/engine' # Will need soon: user profile pics, etc
require 'active_storage/engine' # File storage (CKEditor image uploads, etc)
# require 'rails/test_unit/railtie'
# require 'sprockets/railtie'
require 'sprockets/railtie'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand All @@ -33,15 +29,6 @@ class Application < Rails::Application
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.

# Remove routes for Active Storage
initializer(
:remove_activestorage_routes, after: :add_routing_paths
) do |app|
app.routes_reloader.paths.delete_if do |path|
path =~ /activestorage/
end
end

# Remote routes for Action Mailbox
initializer(
:remove_actionmailbox_routes, after: :add_routing_paths
Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

# Store uploaded files on local file system (config/storage.yml for options)
# config.active_storage.service = :local
# Use AWS S3 to store files
config.active_storage.service = :amazon

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

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

# Mount Action Cable outside main process or domain.
# config.action_cable.mount_path = nil
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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.active_storage.service = :test

# config.action_mailer.perform_caching = false

Expand Down
13 changes: 6 additions & 7 deletions config/storage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ 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
amazon:
service: S3
bucket: <%= ENV['AWS_BUCKET'] %>
region: <%= ENV['AWS_REGION'] %>
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>

# Remember not to checkin your GCS keyfile to a repository
# google:
Expand Down
1 change: 0 additions & 1 deletion db/migrate/20191014193220_devise_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def change

t.string :display_name
t.string :display_email
t.string :profile_pic
t.text :bio
t.string :website
t.string :location
Expand Down
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[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
25 changes: 23 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,32 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_12_06_184835) do
ActiveRecord::Schema.define(version: 2019_12_10_211946) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

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.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 "capabilities", force: :cascade do |t|
t.string "name", null: false
t.integer "category_id"
Expand Down Expand Up @@ -137,7 +158,6 @@
t.string "encrypted_password", default: "", null: false
t.string "display_name"
t.string "display_email"
t.string "profile_pic"
t.text "bio"
t.string "website"
t.string "location"
Expand Down Expand Up @@ -166,6 +186,7 @@
t.index ["username"], name: "index_users_on_username", unique: true
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "capabilities", "capability_categories", column: "category_id"
add_foreign_key "page_elements", "pages"
add_foreign_key "page_sections", "page_sections", column: "section_id"
Expand Down

0 comments on commit daa25a2

Please sign in to comment.