Skip to content

Commit

Permalink
use ActiveStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyf committed Mar 28, 2019
1 parent 6210d2a commit c53a694
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 21 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ gem 'seed-fu', '~> 2.3'
gem 'acts-as-taggable-on', '~> 6.0'
gem 'aws-sdk-s3' #used for digital ocean integration
gem 'mini_magick'
gem 'carrierwave'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
8 changes: 0 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (~> 1.2)
xpath (~> 3.2)
carrierwave (1.3.1)
activemodel (>= 4.0.0)
activesupport (>= 4.0.0)
mime-types (>= 1.16)
childprocess (0.9.0)
ffi (~> 1.0, >= 1.0.11)
chromedriver-helper (2.1.1)
Expand Down Expand Up @@ -181,9 +177,6 @@ GEM
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.2)
mime-types (3.2.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2018.0812)
mimemagic (0.3.3)
mini_magick (4.9.3)
mini_mime (1.0.1)
Expand Down Expand Up @@ -303,7 +296,6 @@ DEPENDENCIES
bootstrap (~> 4.1.3)
byebug
capybara (>= 2.15)
carrierwave
chromedriver-helper
coffee-rails (~> 4.2)
devise
Expand Down
61 changes: 55 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
class User < ApplicationRecord
mount_uploader :avatar, AvatarUploader
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :crop_avatar

def crop_avatar
avatar.recreate_versions! if crop_x.present?

#after_update :crop_avatar

has_one_attached :avatar

before_save :check_cropping

def check_cropping
self.crop_settings = {x: crop_x, y: crop_y, w: crop_w, h: crop_h} if cropping?
end


def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end

def cropped_avatar
if avatar.attached?
if crop_settings.is_a? Hash
dimensions = "#{crop_settings['w']}x#{crop_settings['h']}"
coord = "#{crop_settings['x']}+#{crop_settings['y']}"
avatar.variant(
crop: "#{dimensions}+#{coord}"
).processed
else
avatar
end
end
end

def thumbnail(size = '100x100')
if avatar.attached?
if crop_settings.is_a? Hash
dimensions = "#{crop_settings['w']}x#{crop_settings['h']}"
coord = "#{crop_settings['x']}+#{crop_settings['y']}"
avatar.variant(
crop: "#{dimensions}+#{coord}",
resize: size
).processed
else
avatar.variant(resize: size).processed
end
end
end


# def crop_avatar
# avatar.recreate_versions! if crop_x.present?
# end

private

def delete_avatar
self.avatar = nil
self.save
end

end
4 changes: 2 additions & 2 deletions app/views/users/crop.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<h1>Crop Avatar</h1>

<%= image_tag @user.avatar_url(:large), id: "cropbox" %>
<%= image_tag @user.avatar, id: "cropbox" %>

<h4>Preview</h4>
<div style="width:100px; height:100px; overflow:hidden">
<%= image_tag @user.avatar.url(:large), :id => "preview" %>
<%= image_tag @user.avatar, :id => "preview" %>
</div>

<%= form_for @user do |f| %>
Expand Down
7 changes: 5 additions & 2 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

<ul>
<% @users.each do |user| %>
<li><%= link_to user.name, user %></li>
<li>
<%= image_tag user.thumbnail if user.avatar.attached? %>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>

<p><%= link_to "New User", new_user_path %></p>
<p><%= link_to "New User", new_user_path %></p>
8 changes: 7 additions & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
<%= @user.name %>
</p>
<p>
<%= image_tag @user.avatar_url(:thumb) if @user.avatar? %>
<%= image_tag @user.thumbnail if @user.avatar.attached? %>
</p>
<p>
<%= image_tag @user.cropped_avatar if @user.avatar.attached? %>
</p>
<p>
<%= image_tag @user.avatar if @user.avatar.attached? %>
</p>
<p>
<%= link_to "Edit", edit_user_path(@user) %> |
Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
# number of complex assets.
config.assets.debug = true

config.active_storage.service = :local

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

Expand Down
2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false

config.active_storage.service = :local

# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
Expand Down
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr

config.active_storage.service = :test

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
7 changes: 7 additions & 0 deletions config/storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

local:
service: Disk
root: <%= Rails.root.join("storage") %>
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
5 changes: 5 additions & 0 deletions db/migrate/20190328042854_add_crop_settings_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCropSettingsToUser < ActiveRecord::Migration[5.2]
def change
add_column :users, :crop_settings, :JSON
end
end
24 changes: 23 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,35 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2016_05_09_210056) do
ActiveRecord::Schema.define(version: 2019_03_28_042854) do

create_table "active_storage_attachments", 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", 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 "users", force: :cascade do |t|
t.string "name"
t.string "avatar"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.json "crop_settings"
end

end

0 comments on commit c53a694

Please sign in to comment.