Implement cropping; make uploaders DRY
This commit is contained in:
parent
9757c56a2b
commit
ff9741589d
|
@ -30,10 +30,10 @@ if window.URL? or window.webkitURL?
|
|||
preview = ($ '#profile-picture-preview')
|
||||
|
||||
updateVars = (data, action) ->
|
||||
($ '#crop_x').val Math.floor(data.x / data.scale)
|
||||
($ '#crop_y').val Math.floor(data.y / data.scale)
|
||||
($ '#crop_w').val Math.floor(data.w / data.scale)
|
||||
($ '#crop_h').val Math.floor(data.h / data.scale)
|
||||
($ '#profile_picture_x').val Math.floor(data.x / data.scale)
|
||||
($ '#profile_picture_y').val Math.floor(data.y / data.scale)
|
||||
($ '#profile_picture_w').val Math.floor(data.w / data.scale)
|
||||
($ '#profile_picture_h').val Math.floor(data.h / data.scale)
|
||||
# rx = 100 / data.w
|
||||
# ry = 100 / data.h
|
||||
# ($ '#profile-picture-preview').css
|
||||
|
@ -76,10 +76,10 @@ if window.URL? or window.webkitURL?
|
|||
preview = ($ '#profile-header-preview')
|
||||
|
||||
updateVars = (data, action) ->
|
||||
($ '#crop_h_x').val Math.floor(data.x / data.scale)
|
||||
($ '#crop_h_y').val Math.floor(data.y / data.scale)
|
||||
($ '#crop_h_w').val Math.floor(data.w / data.scale)
|
||||
($ '#crop_h_h').val Math.floor(data.h / data.scale)
|
||||
($ '#profile_header_x').val Math.floor(data.x / data.scale)
|
||||
($ '#profile_header_y').val Math.floor(data.y / data.scale)
|
||||
($ '#profile_header_w').val Math.floor(data.w / data.scale)
|
||||
($ '#profile_header_h').val Math.floor(data.h / data.scale)
|
||||
|
||||
cropper.on 'load', ->
|
||||
if ({}.toString).call(src) == "[object URL]"
|
||||
|
|
|
@ -28,8 +28,9 @@ class UserController < ApplicationController
|
|||
end
|
||||
|
||||
def update
|
||||
user_attributes = params.require(:user).permit(:display_name, :profile_picture, :profile_header, :motivation_header, :website,
|
||||
:location, :bio, :crop_x, :crop_y, :crop_w, :crop_h, :crop_h_x, :crop_h_y, :crop_h_w, :crop_h_h, :show_foreign_themes)
|
||||
user_attributes = params.require(:user).permit(:display_name, :motivation_header, :website, :show_foreign_themes, :location, :bio,
|
||||
:profile_picture_x, :profile_picture_y, :profile_picture_w, :profile_picture_h,
|
||||
:profile_header_x, :profile_header_y, :profile_header_w, :profile_header_h, :profile_picture, :profile_header)
|
||||
if current_user.update_attributes(user_attributes)
|
||||
text = t('flash.user.update.text')
|
||||
text += t('flash.user.update.avatar') if user_attributes[:profile_picture]
|
||||
|
|
|
@ -41,6 +41,9 @@ class User < ApplicationRecord
|
|||
|
||||
has_one :theme, dependent: :destroy
|
||||
|
||||
attr_accessor :profile_picture_x, :profile_picture_y, :profile_picture_w, :profile_picture_h,
|
||||
:profile_header_x, :profile_header_y, :profile_header_w, :profile_header_h
|
||||
|
||||
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
|
||||
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
|
||||
|
||||
|
@ -65,7 +68,7 @@ class User < ApplicationRecord
|
|||
end unless website.blank?
|
||||
end
|
||||
|
||||
# when a user deleted himself, all reports relating to the user are invalid
|
||||
# when a user has been deleted, all reports relating to the user become invalid
|
||||
before_destroy do
|
||||
rep = Report.where(target_id: self.id, type: 'Reports::User')
|
||||
rep.each do |r|
|
||||
|
@ -221,10 +224,6 @@ class User < ApplicationRecord
|
|||
end
|
||||
# endregion
|
||||
|
||||
def cropping?
|
||||
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
|
||||
end
|
||||
|
||||
# forwards fill
|
||||
def banned?
|
||||
self.permanently_banned? or ((not self.banned_until.nil?) and self.banned_until >= DateTime.current)
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
class BaseUploader < CarrierWave::Uploader::Base
|
||||
include CarrierWave::Compatibility::Paperclip
|
||||
include CarrierWave::MiniMagick
|
||||
|
||||
storage :fog
|
||||
|
||||
# Store original size
|
||||
version :original
|
||||
|
||||
# Process cropping on upload
|
||||
process :cropping
|
||||
|
||||
def store_dir
|
||||
"/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
|
||||
end
|
||||
|
||||
def paperclip_path
|
||||
"users/:attachment/:id_partition/:style/:basename.:extension"
|
||||
end
|
||||
|
||||
def cropping
|
||||
x = model.public_send("#{mounted_as}_x")
|
||||
y = model.public_send("#{mounted_as}_y")
|
||||
w = model.public_send("#{mounted_as}_w")
|
||||
h = model.public_send("#{mounted_as}_h")
|
||||
|
||||
manipulate! do |image|
|
||||
image.crop "#{w}x#{h}+#{x}+#{y}"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,30 +1,8 @@
|
|||
class ProfileHeaderUploader < CarrierWave::Uploader::Base
|
||||
include CarrierWave::Compatibility::Paperclip
|
||||
include CarrierWave::MiniMagick
|
||||
|
||||
# Choose what kind of storage to use for this uploader:
|
||||
storage :fog
|
||||
|
||||
# Override the directory where uploaded files will be stored.
|
||||
# This is a sensible default for uploaders that are meant to be mounted:
|
||||
def store_dir
|
||||
"/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
|
||||
end
|
||||
|
||||
def paperclip_path
|
||||
"users/:attachment/:id_partition/:style/:basename.:extension"
|
||||
end
|
||||
|
||||
# Provide a default URL as a default if there hasn't been a file uploaded:
|
||||
class ProfileHeaderUploader < BaseUploader
|
||||
def default_url(*args)
|
||||
# For Rails 3.1+ asset pipeline compatibility:
|
||||
# ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
|
||||
|
||||
"/images/header/" + [version_name, "no_header.jpg"].compact.join('/')
|
||||
end
|
||||
|
||||
version :original
|
||||
|
||||
version :web do
|
||||
process resize_to_fit: [1500, 350]
|
||||
end
|
||||
|
@ -34,16 +12,4 @@ class ProfileHeaderUploader < CarrierWave::Uploader::Base
|
|||
version :retina do
|
||||
process resize_to_fit: [900, 210]
|
||||
end
|
||||
|
||||
# Add a white list of extensions which are allowed to be uploaded.
|
||||
# For images you might use something like this:
|
||||
# def extension_whitelist
|
||||
# %w(jpg jpeg gif png)
|
||||
# end
|
||||
|
||||
# Override the filename of the uploaded files:
|
||||
# Avoid using model.id or version_name here, see uploader/store.rb for details.
|
||||
# def filename
|
||||
# "something.jpg" if original_filename
|
||||
# end
|
||||
end
|
||||
|
|
|
@ -1,30 +1,8 @@
|
|||
class ProfilePictureUploader < CarrierWave::Uploader::Base
|
||||
include CarrierWave::Compatibility::Paperclip
|
||||
include CarrierWave::MiniMagick
|
||||
|
||||
# Choose what kind of storage to use for this uploader:
|
||||
storage :fog
|
||||
|
||||
# Override the directory where uploaded files will be stored.
|
||||
# This is a sensible default for uploaders that are meant to be mounted:
|
||||
def store_dir
|
||||
"/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
|
||||
end
|
||||
|
||||
def paperclip_path
|
||||
"users/:attachment/:id_partition/:style/:basename.:extension"
|
||||
end
|
||||
|
||||
# Provide a default URL as a default if there hasn't been a file uploaded:
|
||||
class ProfilePictureUploader < BaseUploader
|
||||
def default_url(*args)
|
||||
# For Rails 3.1+ asset pipeline compatibility:
|
||||
# ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
|
||||
|
||||
"/images/" + [version_name, "no_avatar.png"].compact.join('/')
|
||||
end
|
||||
|
||||
version :original
|
||||
|
||||
version :large do
|
||||
process resize_to_fit: [500, 500]
|
||||
end
|
||||
|
@ -34,16 +12,4 @@ class ProfilePictureUploader < CarrierWave::Uploader::Base
|
|||
version :small do
|
||||
process resize_to_fit: [80, 80]
|
||||
end
|
||||
|
||||
# Add a white list of extensions which are allowed to be uploaded.
|
||||
# For images you might use something like this:
|
||||
# def extension_whitelist
|
||||
# %w(jpg jpeg gif png)
|
||||
# end
|
||||
|
||||
# Override the filename of the uploaded files:
|
||||
# Avoid using model.id or version_name here, see uploader/store.rb for details.
|
||||
# def filename
|
||||
# "something.jpg" if original_filename
|
||||
# end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue