Commit 0701b70c authored by Johann Pardanaud's avatar Johann Pardanaud

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`.
parent bf6aa155
...@@ -166,9 +166,10 @@ class User < ActiveRecord::Base ...@@ -166,9 +166,10 @@ class User < ActiveRecord::Base
validate :owns_public_email, if: ->(user) { user.public_email_changed? } validate :owns_public_email, if: ->(user) { user.public_email_changed? }
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| validates :avatar_crop_x, :avatar_crop_y, :avatar_crop_size,
validates field, numericality: { only_integer: true }, presence: true, if: ->(user) { user.avatar_changed? } numericality: { only_integer: true },
end presence: true,
if: ->(user) { user.avatar_changed? }
before_validation :generate_password, on: :create before_validation :generate_password, on: :create
before_validation :restricted_signup_domains, on: :create before_validation :restricted_signup_domains, on: :create
......
...@@ -11,12 +11,12 @@ class AvatarUploader < CarrierWave::Uploader::Base ...@@ -11,12 +11,12 @@ class AvatarUploader < CarrierWave::Uploader::Base
process :cropper process :cropper
def cropper def cropper
if model.kind_of?(User) && model.valid? return unless model.respond_to?(:avatar_crop_size) && model.valid?
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
end end
end
def store_dir def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
......
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