diff --git a/app/helpers/investments_helper.rb b/app/helpers/investments_helper.rb index e6767c4c5..ab3a2ec28 100644 --- a/app/helpers/investments_helper.rb +++ b/app/helpers/investments_helper.rb @@ -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 \ No newline at end of file diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 90f718dfa..b8bf690bc 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -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 diff --git a/app/views/budgets/investments/_image_form.html.erb b/app/views/budgets/investments/_image_form.html.erb index 89b2a5bcd..de820f7c7 100644 --- a/app/views/budgets/investments/_image_form.html.erb +++ b/app/views/budgets/investments/_image_form.html.erb @@ -19,6 +19,11 @@

<%= investment_image_file_name(@investment) %>

+
+
+

<%= errors_on_image(@investment)%>

+
+
<%= 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 %> diff --git a/config/locales/en/budgets.yml b/config/locales/en/budgets.yml index 9768a9299..c8412cf64 100644 --- a/config/locales/en/budgets.yml +++ b/config/locales/en/budgets.yml @@ -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 diff --git a/config/locales/es/budgets.yml b/config/locales/es/budgets.yml index d7b79ed6d..0e408123e 100644 --- a/config/locales/es/budgets.yml +++ b/config/locales/es/budgets.yml @@ -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 diff --git a/spec/factories.rb b/spec/factories.rb index 5cbbd26aa..4f44ccb32 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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 diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index fb3061733..3894063a2 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -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, diff --git a/spec/fixtures/files/clippy.gif b/spec/fixtures/files/clippy.gif new file mode 100644 index 000000000..c2c2278af Binary files /dev/null and b/spec/fixtures/files/clippy.gif differ diff --git a/spec/fixtures/files/clippy.jpg b/spec/fixtures/files/clippy.jpg new file mode 100644 index 000000000..1edb6659b Binary files /dev/null and b/spec/fixtures/files/clippy.jpg differ diff --git a/spec/fixtures/files/clippy.png b/spec/fixtures/files/clippy.png new file mode 100644 index 000000000..44e09b4e3 Binary files /dev/null and b/spec/fixtures/files/clippy.png differ diff --git a/spec/models/budget/investment_spec.rb b/spec/models/budget/investment_spec.rb index 04acef461..1da20786b 100644 --- a/spec/models/budget/investment_spec.rb +++ b/spec/models/budget/investment_spec.rb @@ -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