Commit bf6aa155 authored by Johann Pardanaud's avatar Johann Pardanaud

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

parent fb6d7df3
......@@ -53,7 +53,7 @@ class @Profile
$avatarInput.on "change", ->
form = $(this).closest("form")
filename = $(this).val().replace(/^.*[\\\/]/, '')
$filename.text(filename)
$filename.data('label', $filename.text()).text(filename)
reader = new FileReader
......
......@@ -99,9 +99,7 @@ class User < ActiveRecord::Base
attr_accessor :login
# Virtual attributes to define avatar cropping
[:avatar_crop_x, :avatar_crop_y, :avatar_crop_size].each do |field|
attr_accessor field
end
attr_accessor :avatar_crop_x, :avatar_crop_y, :avatar_crop_size
#
# Relations
......@@ -169,7 +167,7 @@ class User < ActiveRecord::Base
validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
[: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
before_validation :generate_password, on: :create
......
......@@ -10,15 +10,8 @@ class AvatarUploader < CarrierWave::Uploader::Base
process :cropper
def is_integer? string
true if Integer(string) rescue false
end
def cropper
is_compliant = model.kind_of?(User) && is_integer?(model.avatar_crop_size)
is_compliant = is_compliant && is_integer?(model.avatar_crop_x) && is_integer?(model.avatar_crop_y)
if is_compliant
if model.kind_of?(User) && model.valid?
manipulate! do |img|
img.crop "#{model.avatar_crop_size}x#{model.avatar_crop_size}+#{model.avatar_crop_x}+#{model.avatar_crop_y}"
end
......
......@@ -88,7 +88,7 @@
%i.fa.fa-paperclip
%span Choose File ...
&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.hidden_field :avatar_crop_x
= f.hidden_field :avatar_crop_y
......@@ -107,7 +107,7 @@
.modal-dialog
.modal-content
.modal-header
%button.close{:type => "button", :'data-dismiss' => "modal"}
%button.close{type: 'button', data: {dismiss: 'modal'}}
%span
&times;
%h4.modal-title
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment