Add budget investment image dimmensions validator.

This commit is contained in:
Senén Rodero Rodríguez
2017-06-23 16:28:08 +02:00
parent d0a5deab5e
commit c914c772c0
11 changed files with 59 additions and 8 deletions

View File

@@ -43,6 +43,7 @@ class Budget
validates_attachment :image, content_type: { content_type: ["image/jpeg"] }
validates :image_title, presence: true, if: -> { image.present? }
validates :image_title, length: { in: 4..Budget::Investment.title_max_length }, if: -> { image.present? }
validate :check_image_dimensions
scope :sort_by_confidence_score, -> { reorder(confidence_score: :desc, id: :desc) }
scope :sort_by_ballots, -> { reorder(ballot_lines_count: :desc, id: :desc) }
@@ -271,5 +272,18 @@ class Budget
self.budget_id ||= heading.try(:group).try(:budget_id)
end
def check_image_dimensions
temp_file = image.queued_for_write[:original]
unless temp_file.nil?
dimensions = Paperclip::Geometry.from_file(temp_file)
width = dimensions.width
height = dimensions.height
if width < 475 || height < 475
errors['image'] << I18n.t("budgets.investments.edit_image.invalid_dimmensions")
end
end
end
end
end