Skip to content

Commit

Permalink
Merge pull request #217 from iaebots/216-bots-cover-image-size
Browse files Browse the repository at this point in the history
validate cover image size on bot's model
  • Loading branch information
Utzig26 committed Apr 23, 2021
2 parents 27e2f4e + 542ab73 commit 868508e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions app/models/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class Bot < ApplicationRecord
validates :avatar, file_size: { less_than_or_equal_to: 2.megabytes }
validates :cover, file_size: { less_than_or_equal_to: 2.megabytes }

# validates cover image sizes
validate :validate_minimum_cover_image_size
validate :validate_maximum_cover_image_size

validates_length_of :bio, minimum: 1, maximum: 512 # validates length of bot's bio

# ensure bot's username doesn't contain symbols nor special characters
Expand Down Expand Up @@ -72,8 +76,8 @@ def validate_username

# validates minimum and maximum number of tags and max tag length
def tag_list_count
errors[:tag_list] << '1 tags minimum' if tag_list.count < 1
errors[:tag_list] << '16 tags maximum' if tag_list.count > 16
errors.add(:tag_list, '1 tags minimum') if tag_list.count < 1
errors.add(:tag_list, '16 tags maximum') if tag_list.count > 16

self.tag_list.each do |tag|
errors.add(:tag_list, "#{tag} must be shorter than 32 characters maximum") if tag.length > 32
Expand All @@ -98,4 +102,22 @@ def self.generate_api_secret
break token unless Bot.exists?(api_secret: token)
end
end

def validate_minimum_cover_image_size
if cover.path
image = MiniMagick::Image.open(cover.path)
unless image[:width] > 640 && image[:height] > 180
errors.add :cover, "should be 640x180px minimum!"
end
end
end

def validate_maximum_cover_image_size
if cover.path
image = MiniMagick::Image.open(cover.path)
unless image[:width] <= 1280 && image[:height] <= 360
errors.add :cover, "should be 1280x360px maximum!"
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/developer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def validate_minimum_cover_image_size
def validate_maximum_cover_image_size
if cover.path
image = MiniMagick::Image.open(cover.path)
unless image[:width] < 1280 && image[:height] < 360
unless image[:width] <= 1280 && image[:height] <= 360
errors.add :cover, "should be 1280x360px maximum!"
end
end
Expand Down

0 comments on commit 868508e

Please sign in to comment.