Improve avatar cropping internals, based on suggestions made by @rspeicher on !2773

This commit is contained in:
Johann Pardanaud 2016-02-17 18:35:16 +01:00
parent fb6d7df347
commit bf6aa15512
4 changed files with 6 additions and 15 deletions

View file

@ -53,7 +53,7 @@ class @Profile
$avatarInput.on "change", -> $avatarInput.on "change", ->
form = $(this).closest("form") form = $(this).closest("form")
filename = $(this).val().replace(/^.*[\\\/]/, '') filename = $(this).val().replace(/^.*[\\\/]/, '')
$filename.text(filename) $filename.data('label', $filename.text()).text(filename)
reader = new FileReader reader = new FileReader

View file

@ -99,9 +99,7 @@ class User < ActiveRecord::Base
attr_accessor :login attr_accessor :login
# Virtual attributes to define avatar cropping # Virtual attributes to define avatar cropping
[:avatar_crop_x, :avatar_crop_y, :avatar_crop_size].each do |field| attr_accessor :avatar_crop_x, :avatar_crop_y, :avatar_crop_size
attr_accessor field
end
# #
# Relations # Relations
@ -169,7 +167,7 @@ class User < ActiveRecord::Base
validates :avatar, file_size: { maximum: 200.kilobytes.to_i } validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
[:avatar_crop_x, :avatar_crop_y, :avatar_crop_size].each do |field| [:avatar_crop_x, :avatar_crop_y, :avatar_crop_size].each do |field|
validates field, numericality: { only_integer: true }, allow_blank: true validates field, numericality: { only_integer: true }, presence: true, if: ->(user) { user.avatar_changed? }
end end
before_validation :generate_password, on: :create before_validation :generate_password, on: :create

View file

@ -10,15 +10,8 @@ class AvatarUploader < CarrierWave::Uploader::Base
process :cropper process :cropper
def is_integer? string
true if Integer(string) rescue false
end
def cropper def cropper
is_compliant = model.kind_of?(User) && is_integer?(model.avatar_crop_size) if model.kind_of?(User) && model.valid?
is_compliant = is_compliant && is_integer?(model.avatar_crop_x) && is_integer?(model.avatar_crop_y)
if is_compliant
manipulate! do |img| manipulate! do |img|
img.crop "#{model.avatar_crop_size}x#{model.avatar_crop_size}+#{model.avatar_crop_x}+#{model.avatar_crop_y}" img.crop "#{model.avatar_crop_size}x#{model.avatar_crop_size}+#{model.avatar_crop_x}+#{model.avatar_crop_y}"
end end

View file

@ -88,7 +88,7 @@
%i.fa.fa-paperclip %i.fa.fa-paperclip
%span Choose File ... %span Choose File ...
&nbsp; &nbsp;
%span.file_name.js-avatar-filename{:'data-label' => 'File name...'} File name... %span.file_name.js-avatar-filename File name...
= f.file_field :avatar, class: "js-user-avatar-input hidden" = f.file_field :avatar, class: "js-user-avatar-input hidden"
= f.hidden_field :avatar_crop_x = f.hidden_field :avatar_crop_x
= f.hidden_field :avatar_crop_y = f.hidden_field :avatar_crop_y
@ -107,7 +107,7 @@
.modal-dialog .modal-dialog
.modal-content .modal-content
.modal-header .modal-header
%button.close{:type => "button", :'data-dismiss' => "modal"} %button.close{type: 'button', data: {dismiss: 'modal'}}
%span %span
&times; &times;
%h4.modal-title %h4.modal-title