diff options
author | Johann Pardanaud <pardanaud.j@gmail.com> | 2016-02-18 13:09:51 +0100 |
---|---|---|
committer | Johann Pardanaud <pardanaud.j@gmail.com> | 2016-02-18 13:12:39 +0100 |
commit | 0701b70c9c47e90a96100cefe5b93ec5954f9ec1 (patch) | |
tree | 9a4994634f74c23c8143d08acd920323b832852a | |
parent | bf6aa15512cc6d31be320204fbc3c115bfd11b1b (diff) | |
download | gitlab-ce-0701b70c9c47e90a96100cefe5b93ec5954f9ec1.tar.gz |
Minor changes on avatar cropping internals
- Avoid multiple calls to `validates` for the avatar attributes.
- In a cropping process, don't check if the model inherits `User`, check if it responds to `:avatar_crop_size`.
-rw-r--r-- | app/models/user.rb | 7 | ||||
-rw-r--r-- | app/uploaders/avatar_uploader.rb | 8 |
2 files changed, 8 insertions, 7 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 37285c2a9d4..cd6e4332a76 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -166,9 +166,10 @@ class User < ActiveRecord::Base validate :owns_public_email, if: ->(user) { user.public_email_changed? } 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 }, presence: true, if: ->(user) { user.avatar_changed? } - end + validates :avatar_crop_x, :avatar_crop_y, :avatar_crop_size, + numericality: { only_integer: true }, + presence: true, + if: ->(user) { user.avatar_changed? } before_validation :generate_password, on: :create before_validation :restricted_signup_domains, on: :create diff --git a/app/uploaders/avatar_uploader.rb b/app/uploaders/avatar_uploader.rb index c8d9ab072e8..2c72df44ff0 100644 --- a/app/uploaders/avatar_uploader.rb +++ b/app/uploaders/avatar_uploader.rb @@ -11,10 +11,10 @@ class AvatarUploader < CarrierWave::Uploader::Base process :cropper def cropper - 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 + return unless model.respond_to?(:avatar_crop_size) && 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 end |