summaryrefslogtreecommitdiff
path: root/app/models/concerns/avatarable.rb
diff options
context:
space:
mode:
authorMicaël Bergeron <mbergeron@gitlab.com>2018-01-29 12:57:34 -0500
committerMicaël Bergeron <mbergeron@gitlab.com>2018-02-01 12:14:46 -0500
commit2057a6acdee7c1f6824ff6289b0d979e8cb15f35 (patch)
tree25b911b147bfa0797dfee4cacf99f23ae5f55281 /app/models/concerns/avatarable.rb
parent402f3dfc0a962dc89d8334d4d61410e67e14a55f (diff)
downloadgitlab-ce-2057a6acdee7c1f6824ff6289b0d979e8cb15f35.tar.gz
port of 594e6a0a625^..f74c90f68c6
Diffstat (limited to 'app/models/concerns/avatarable.rb')
-rw-r--r--app/models/concerns/avatarable.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/app/models/concerns/avatarable.rb b/app/models/concerns/avatarable.rb
index 10659030910..d35e37935fb 100644
--- a/app/models/concerns/avatarable.rb
+++ b/app/models/concerns/avatarable.rb
@@ -1,6 +1,30 @@
module Avatarable
extend ActiveSupport::Concern
+ included do
+ prepend ShadowMethods
+
+ validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
+ validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
+
+ mount_uploader :avatar, AvatarUploader
+ end
+
+ module ShadowMethods
+ def avatar_url(**args)
+ # We use avatar_path instead of overriding avatar_url because of carrierwave.
+ # See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11001/diffs#note_28659864
+
+ avatar_path(only_path: args.fetch(:only_path, true)) || super
+ end
+ end
+
+ def avatar_type
+ unless self.avatar.image?
+ self.errors.add :avatar, "only images allowed"
+ end
+ end
+
def avatar_path(only_path: true)
return unless self[:avatar].present?