diff --git a/app/models/account.rb b/app/models/account.rb index 7cd120c..98573cd 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -6,6 +6,8 @@ class Account < ApplicationRecord NICKNAME_RE = /\A[a-z][_a-z0-9]*[a-z0-9]\z/.freeze + AVATAR_MAX_SIZE = 1.megabyte + AVATAR_CONTENT_TYPES = %w[ image/png image/jpeg @@ -74,6 +76,7 @@ class Account < ApplicationRecord validates :biography, allow_nil: true, length: { in: 3..10_000 } + validate :avatar_size_is_valid validate :avatar_content_type_is_valid ########### @@ -135,6 +138,13 @@ private self.biography = biography&.strip end + def avatar_size_is_valid + return unless avatar.attached? + return if avatar.blob.byte_size <= AVATAR_MAX_SIZE + + errors.add :avatar, :size + end + def avatar_content_type_is_valid return unless avatar.attached? return if avatar.blob.content_type.in? AVATAR_CONTENT_TYPES diff --git a/config/locales/activerecord/en.yml b/config/locales/activerecord/en.yml index d57e962..a6e0052 100644 --- a/config/locales/activerecord/en.yml +++ b/config/locales/activerecord/en.yml @@ -112,4 +112,5 @@ en: account: attributes: avatar: + size: 'has too big size' format: 'has invalid format: %{content_type}' diff --git a/config/locales/activerecord/ru.yml b/config/locales/activerecord/ru.yml index 8ad19ce..6df8ee1 100644 --- a/config/locales/activerecord/ru.yml +++ b/config/locales/activerecord/ru.yml @@ -112,4 +112,5 @@ ru: account: attributes: avatar: + size: 'имеет слишком большой размер' format: 'имеет неверный формат: %{content_type}'