diff --git a/.gitignore b/.gitignore
index 6809ebb..ce55809 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,6 +18,9 @@
# Ignore environment variables
.env
+# Ignore Active Storage
+/storage
+
# Ignore dragonfly uploads
/public/system
/public/packs
diff --git a/Gemfile b/Gemfile
index f7ae0a7..bf985e8 100644
--- a/Gemfile
+++ b/Gemfile
@@ -9,6 +9,7 @@ gem "rails", "5.2.1"
gem "delayed_job_active_record"
gem "dragonfly"
gem "http"
+gem "mini_magick"
gem "omniauth-github"
gem "pg"
gem "puma"
diff --git a/Gemfile.lock b/Gemfile.lock
index a10b1af..abd0884 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -121,6 +121,7 @@ GEM
mimemagic (~> 0.3.2)
method_source (0.9.1)
mimemagic (0.3.2)
+ mini_magick (4.9.2)
mini_mime (1.0.1)
mini_portile2 (2.3.0)
minitest (5.11.3)
@@ -270,6 +271,7 @@ DEPENDENCIES
geckodriver-helper
http
launchy
+ mini_magick
omniauth-github
pg
pry
@@ -289,4 +291,4 @@ RUBY VERSION
ruby 2.5.1p57
BUNDLED WITH
- 1.16.4
+ 1.16.5
diff --git a/app/helpers/image_helper.rb b/app/helpers/image_helper.rb
index 62e5ec7..0298353 100644
--- a/app/helpers/image_helper.rb
+++ b/app/helpers/image_helper.rb
@@ -1,9 +1,15 @@
module ImageHelper
+ def image_variant(image, variant)
+ image.variant(variant)
+ end
+
def image_thumb(image, size)
- if image.format == "jpeg"
- image.thumb(size).encode("jpg", "-quality 90")
- else
- image.thumb(size)
- end
+ image.variant(
+ combine_options: {
+ gravity: "center",
+ resize: "#{size}x#{size}^",
+ extent: "#{size}x#{size}",
+ },
+ )
end
end
diff --git a/app/models/image.rb b/app/models/image.rb
index 379ff1a..1e31197 100644
--- a/app/models/image.rb
+++ b/app/models/image.rb
@@ -1,5 +1,6 @@
class Image < ActiveRecord::Base
- dragonfly_accessor :image
+ dragonfly_accessor :df_image
+ has_one_attached :image
validates :image, presence: true
diff --git a/app/views/images/_image.html.erb b/app/views/images/_image.html.erb
index 43a3604..31ca994 100644
--- a/app/views/images/_image.html.erb
+++ b/app/views/images/_image.html.erb
@@ -1,17 +1,17 @@
- "
+ "
media="(min-width: 960px) and (-webkit-min-device-pixel-ratio: 2),
(min-width: 960px) and (min-resolution: 192dpi)">
- "
+ "
media="(min-width: 960px)">
- "
+ "
media="(min-width: 640px) and (-webkit-min-device-pixel-ratio: 2),
(min-width: 640px) and (min-resolution: 192dpi)">
- "
+ "
media="(min-width: 640px)">
- "
+ "
media="(-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi)">
- ">
-
" itemprop="image">
+ ">
+
" itemprop="image">
diff --git a/app/views/images/index.html.erb b/app/views/images/index.html.erb
index 803ed02..6b6d926 100644
--- a/app/views/images/index.html.erb
+++ b/app/views/images/index.html.erb
@@ -5,7 +5,7 @@
<% @images.each do |image| %>
-
- <%= link_to image_tag(image.image.thumb('200x200#').url),
+ <%= link_to image_tag(image_thumb(image.image, 200)),
user_image_path(image), class: :image %>
<%= link_to 'Delete', user_image_path(image),
method: :delete, data: { confirm: 'Are you sure?' } %>
diff --git a/config/environments/development.rb b/config/environments/development.rb
index 2a1934a..f78382a 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -16,6 +16,9 @@ MyImages::Application.configure do
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
+ # Store files locally.
+ config.active_storage.service = :local
+
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
diff --git a/config/environments/production.rb b/config/environments/production.rb
index c992235..8a46fbd 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -72,6 +72,9 @@ MyImages::Application.configure do
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
+ # Store files locally.
+ config.active_storage.service = :local
+
# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false
diff --git a/config/environments/test.rb b/config/environments/test.rb
index edb380f..240f562 100644
--- a/config/environments/test.rb
+++ b/config/environments/test.rb
@@ -33,4 +33,7 @@ MyImages::Application.configure do
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
+
+ # Store files locally.
+ config.active_storage.service = :test
end
diff --git a/config/storage.yml b/config/storage.yml
new file mode 100644
index 0000000..1f93f73
--- /dev/null
+++ b/config/storage.yml
@@ -0,0 +1,7 @@
+local:
+ service: Disk
+ root: <%= Rails.root.join("storage") %>
+
+test:
+ service: Disk
+ root: <%= Rails.root.join("tmp/storage") %>
diff --git a/db/migrate/20181019204358_create_active_storage_tables.active_storage.rb b/db/migrate/20181019204358_create_active_storage_tables.active_storage.rb
new file mode 100644
index 0000000..360e0d1
--- /dev/null
+++ b/db/migrate/20181019204358_create_active_storage_tables.active_storage.rb
@@ -0,0 +1,26 @@
+# 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
+ end
+ end
+end
diff --git a/db/migrate/20181019210305_rename_dragonfly_columns.rb b/db/migrate/20181019210305_rename_dragonfly_columns.rb
new file mode 100644
index 0000000..3a50e5e
--- /dev/null
+++ b/db/migrate/20181019210305_rename_dragonfly_columns.rb
@@ -0,0 +1,6 @@
+class RenameDragonflyColumns < ActiveRecord::Migration[5.2]
+ def change
+ rename_column :images, :image_uid, :df_image_uid
+ rename_column :images, :image_name, :df_image_name
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 491eb0b..254c7b0 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,11 +10,32 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2018_08_26_163510) do
+ActiveRecord::Schema.define(version: 2018_10_19_210305) 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 "delayed_jobs", force: :cascade do |t|
t.integer "priority", default: 0, null: false
t.integer "attempts", default: 0, null: false
@@ -32,8 +53,8 @@ ActiveRecord::Schema.define(version: 2018_08_26_163510) do
create_table "images", force: :cascade do |t|
t.integer "user_id"
- t.string "image_uid"
- t.string "image_name"
+ t.string "df_image_uid"
+ t.string "df_image_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "tags", default: [], array: true
diff --git a/lib/tasks/images_migrate.rake b/lib/tasks/images_migrate.rake
new file mode 100644
index 0000000..4f1f813
--- /dev/null
+++ b/lib/tasks/images_migrate.rake
@@ -0,0 +1,11 @@
+namespace :images do
+ desc "Migrate all the images to Active Storage"
+ task migrate: :environment do
+ Image.all.each do |image|
+ image.image.attach(
+ io: image.df_image.file,
+ filename: image.df_image.name,
+ )
+ end
+ end
+end