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

@@ -12,4 +12,8 @@ module InvestmentsHelper
investment.image.exists? ? "edit_image" : "add_image"
end
def errors_on_image(investment)
@investment.errors[:image].join(', ') if @investment.errors.has_key?(:image)
end
end

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

View File

@@ -19,6 +19,11 @@
<p><%= investment_image_file_name(@investment) %></p>
</div>
</div>
<div class="small-12 column">
<div class="image-errors">
<p><%= errors_on_image(@investment)%></p>
</div>
</div>
<div class="small-12 column">
<%= f.label :image_title, t("budgets.investments.edit_image.form.image_title") %>
<%= f.text_field :image_title, placeholder: t("budgets.investments.edit_image.form.image_title"), label: false %>

View File

@@ -121,6 +121,7 @@ en:
recommendation_title: Image recomendations
recommendation_one: Projects that have images attract more attention than those that do not. If you have a picture suitable for your project upload.
recommendation_two: Give a descriptive title to the image.
invalid_dimmensions: 'Image dimensions are too small. For a good quality please upload a larger image. Minimum width: 475px, minimum height: 475px.'
wrong_price_format: Only integer numbers
investment:
add: Vote

View File

@@ -121,6 +121,7 @@ es:
recommendation_title: Recomendaciones para cambiar la imagen
recommendation_one: Los proyectos que tienen imágenes llaman más la atención que las que no la tienen. Si tienes una imagen adecuada para tu proyecto súbela.
recommendation_two: Dale un título descriptivo a la imagen.
invalid_dimmensions: 'Las dimensiones de la imagen son demasiado pequeñas. Para una buena calidad de imagen suba una más grande. Ancho mínimo: 475px, altura mínima: 475px.'
wrong_price_format: Solo puede incluir caracteres numéricos
investment:
add: Votar

View File

@@ -320,7 +320,7 @@ FactoryGirl.define do
end
trait :with_descriptive_image do
image { File.new("spec/fixtures/files/logo_header.jpg") }
image { File.new("spec/fixtures/files/clippy.jpg") }
image_title "Lorem ipsum dolor sit amet"
end
end

View File

@@ -545,11 +545,11 @@ feature 'Budget Investments' do
visit edit_image_budget_investment_path(investment.budget, investment)
fill_in :budget_investment_image_title, with: "New image title"
attach_file :budget_investment_image, "spec/fixtures/files/logo_header.jpg"
attach_file :budget_investment_image, "spec/fixtures/files/clippy.jpg"
click_on "Save image"
within ".budget-investment-show" do
expect(page).to have_css("img[src*='logo_header.jpg']")
expect(page).to have_css("img[src*='clippy.jpg']")
end
expect(page).to have_content 'Investment project image updated succesfully. '
end
@@ -561,15 +561,27 @@ feature 'Budget Investments' do
visit edit_image_budget_investment_path(investment.budget, investment)
fill_in :budget_investment_image_title, with: "New image title"
attach_file :budget_investment_image, "spec/fixtures/files/logo_header.jpg"
attach_file :budget_investment_image, "spec/fixtures/files/clippy.jpg"
click_on "Save image"
within ".budget-investment-show" do
expect(page).to have_css("img[src*='logo_header.jpg']")
expect(page).to have_css("img[src*='clippy.jpg']")
end
expect(page).to have_content 'Investment project image updated succesfully. '
end
scenario "Add image with dimmenssions smaller than 475x475" do
investment = create(:budget_investment, heading: heading, author: author)
login_as(author)
visit edit_image_budget_investment_path(investment.budget, investment)
fill_in :budget_investment_image_title, with: "New image title"
attach_file :budget_investment_image, "spec/fixtures/files/logo_header.jpg"
click_on "Save image"
expect(page).to have_content 'Image dimensions are too small. For a good quality please upload a larger image. Minimum width: 475px, minimum height: 475px.'
end
context "Show (feasible budget investment)" do
let(:investment) do
create(:budget_investment,

BIN
spec/fixtures/files/clippy.gif vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
spec/fixtures/files/clippy.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
spec/fixtures/files/clippy.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File

@@ -38,24 +38,38 @@ describe Budget::Investment do
describe "file extension" do
it "should not be valid with '.png' extension" do
investment_with_image.image = File.new("spec/fixtures/files/logo_header.png")
investment_with_image.image = File.new("spec/fixtures/files/clippy.png")
expect(investment_with_image).to_not be_valid
expect(investment_with_image.errors[:image].size).to eq(1)
end
it "should not be valid with '.gif' extension" do
investment_with_image.image = File.new("spec/fixtures/files/logo_header.gif")
investment_with_image.image = File.new("spec/fixtures/files/clippy.gif")
expect(investment_with_image).to_not be_valid
expect(investment_with_image.errors[:image].size).to eq(1)
end
it "should be valid with '.jpg' extension" do
investment_with_image.image = File.new("spec/fixtures/files/logo_header.jpg")
investment_with_image.image = File.new("spec/fixtures/files/clippy.jpg")
expect(investment_with_image).to be_valid
end
end
describe "image dimmessions" do
it "should be valid when image dimmessions are 475X475 at least" do
expect(investment_with_image).to be_valid
end
it "should be valid when image dimmessions are 475X475 at least" do
investment_with_image.image = File.new("spec/fixtures/files/logo_header.jpg")
expect(investment_with_image).not_to be_valid
end
end
describe "description" do
it "should be valid when image and image_title are not defined" do