-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
137 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") %> |
27 changes: 27 additions & 0 deletions
27
db/migrate/20190328041452_create_active_storage_tables.active_storage.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters