diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb
index 6ca15f1b6..22e6f0b52 100644
--- a/app/controllers/budgets/investments_controller.rb
+++ b/app/controllers/budgets/investments_controller.rb
@@ -92,6 +92,14 @@ module Budgets
end
end
+ def remove_image
+ @investment.image = nil
+ @investment.image_title = nil
+ @investment.save
+ redirect_to budget_investment_path(@investment.budget, @investment),
+ notice: t("flash.actions.remove_image.budget_investment")
+ end
+
private
def resource_model
diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb
index b1f37e00d..40eae0d49 100644
--- a/app/models/abilities/administrator.rb
+++ b/app/models/abilities/administrator.rb
@@ -48,7 +48,7 @@ module Abilities
can [:index, :read, :new, :create, :update, :destroy, :calculate_winners], Budget
can [:read, :create, :update, :destroy], Budget::Group
can [:read, :create, :update, :destroy], Budget::Heading
- can [:hide, :update, :toggle_selection, :edit_image, :update_image], Budget::Investment
+ can [:hide, :update, :toggle_selection, :edit_image, :update_image, :remove_image], Budget::Investment
can :valuate, Budget::Investment
can :create, Budget::ValuatorAssignment
diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb
index d4dc6a655..5dd68f045 100644
--- a/app/models/abilities/common.rb
+++ b/app/models/abilities/common.rb
@@ -57,6 +57,7 @@ module Abilities
can :vote, Budget::Investment, budget: { phase: "selecting" }
can :edit_image, Budget::Investment, author_id: user.id
can :update_image, Budget::Investment, author_id: user.id
+ can :remove_image, Budget::Investment, author_id: user.id
can [:show, :create], Budget::Ballot, budget: { phase: "balloting" }
can [:create, :destroy], Budget::Ballot::Line, budget: { phase: "balloting" }
diff --git a/app/views/budgets/investments/_image_form.html.erb b/app/views/budgets/investments/_image_form.html.erb
index 0d009b661..db73cfff0 100644
--- a/app/views/budgets/investments/_image_form.html.erb
+++ b/app/views/budgets/investments/_image_form.html.erb
@@ -17,7 +17,7 @@
<%= f.file_field :image, accept: 'image/jpeg', label: false, class:'show-for-sr' %>
<%= f.label :image, t("images.upload_image"), class:'button' %>
-
<%= @investment.image.url.split('/').last.split("?")[0] %>
+ <%= @investment.image.url.split('/').last.split("?")[0] if @investment.image.exists?%>
@@ -26,6 +26,13 @@
<%= f.submit(class: "button", value: t("budgets.investments.edit_image.form.submit_button")) %>
+ <% if @investment.image.exists? %>
+ <%= link_to t("budgets.investments.edit_image.form.remove_button"),
+ remove_image_budget_investment_path(@investment.budget, @investment),
+ class: "button hollow alert",
+ method: :delete,
+ data: { confirm: "¿Está seguro que desea eliminar la imagen del proyecto?" } %>
+ <% end %>
<% end %>
diff --git a/config/locales/en/budgets.yml b/config/locales/en/budgets.yml
index a1413eb80..9768a9299 100644
--- a/config/locales/en/budgets.yml
+++ b/config/locales/en/budgets.yml
@@ -117,6 +117,7 @@ en:
form:
image_title: Image title
submit_button: Save image
+ remove_button: Remove image
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.
diff --git a/config/locales/en/responders.yml b/config/locales/en/responders.yml
index 5a7b9a4aa..503d474ad 100644
--- a/config/locales/en/responders.yml
+++ b/config/locales/en/responders.yml
@@ -27,6 +27,8 @@ en:
topic: "Topic updated successfully."
update_image:
budget_investment: "Investment project image updated succesfully."
+ remove_image:
+ budget_investment: "Investment project image removed succesfully."
destroy:
spending_proposal: "Spending proposal deleted succesfully."
budget_investment: "Investment project deleted succesfully."
diff --git a/config/locales/es/budgets.yml b/config/locales/es/budgets.yml
index be281e35c..d7b79ed6d 100644
--- a/config/locales/es/budgets.yml
+++ b/config/locales/es/budgets.yml
@@ -117,6 +117,7 @@ es:
form:
image_title: 'Título de la imagen'
submit_button: Guardar imagen
+ remove_button: Eliminar imagen
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.
diff --git a/config/locales/es/responders.yml b/config/locales/es/responders.yml
index 1dc6519a0..0e86ce34d 100644
--- a/config/locales/es/responders.yml
+++ b/config/locales/es/responders.yml
@@ -27,6 +27,8 @@ es:
topic: "Tema actualizado correctamente."
update_image:
budget_investment: "La imagen de la propuesta de inversión se ha actualizado correctamente."
+ remove_image:
+ budget_investment: "La imagen de la propuesta de inversión se ha eliminado correctamente."
destroy:
spending_proposal: "Propuesta de inversión eliminada."
budget_investment: "Propuesta de inversión eliminada."
diff --git a/config/routes.rb b/config/routes.rb
index b2e91b5a8..386a3fbf5 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -82,6 +82,7 @@ Rails.application.routes.draw do
post :vote
get :edit_image
put :update_image
+ delete :remove_image
end
collection { get :suggest }
end
diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb
index 621fdc604..7a125909c 100644
--- a/spec/features/budgets/investments_spec.rb
+++ b/spec/features/budgets/investments_spec.rb
@@ -389,7 +389,7 @@ feature 'Budget Investments' do
context "Show investment image button" do
- scenario "should show add text when investment has not image" do
+ scenario "Should show add text when investment has not image" do
investment = create(:budget_investment, heading: heading, author: author)
login_as(author)
visit budget_investment_path(budget, investment)
@@ -397,7 +397,7 @@ feature 'Budget Investments' do
expect(page).to have_link "Add image"
end
- scenario "should show edit text when investment has already an image" do
+ scenario "Should show edit text when investment has already an image" do
investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author)
login_as(author)
visit budget_investment_path(budget, investment)
@@ -405,21 +405,21 @@ feature 'Budget Investments' do
expect(page).to have_link "Edit image"
end
- scenario "should not be shown for anonymous users" do
+ scenario "Should not be shown for anonymous users" do
investment = create(:budget_investment, heading: heading)
visit budget_investment_path(budget, investment)
expect(page).not_to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment)
end
- scenario "should not be shown when current user is not investment author" do
+ scenario "Should not be shown when current user is not investment author" do
investment = create(:budget_investment, heading: heading)
visit budget_investment_path(budget, investment)
expect(page).not_to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment)
end
- scenario "should be shown when current user is investment author" do
+ scenario "Should be shown when current user is investment author" do
investment = create(:budget_investment, heading: heading, author: author)
login_as(author)
visit budget_investment_path(budget, investment)
@@ -427,7 +427,7 @@ feature 'Budget Investments' do
expect(page).to have_link "Add image", href: edit_image_budget_investment_path(budget, investment)
end
- scenario "should be shown when current user is administrator" do
+ scenario "Should be shown when current user is administrator" do
administrator = create(:administrator).user
investment = create(:budget_investment, heading: heading, author: author)
login_as(administrator)
@@ -470,6 +470,51 @@ feature 'Budget Investments' do
expect(page).to have_content "Change your project image"
end
+ scenario "Remove image button should not be present when investment image does not exists" do
+ investment = create(:budget_investment, heading: heading, author: author)
+ login_as(author)
+ visit edit_image_budget_investment_path(budget, investment)
+
+ expect(page).not_to have_link "Remove image"
+ end
+
+ scenario "Remove image button should be present when investment has an image defined" do
+ investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author)
+ login_as(author)
+ visit edit_image_budget_investment_path(budget, investment)
+
+ expect(page).to have_link "Remove image"
+ end
+
+ scenario "Remove image button should be possible for administrators" do
+ administrator = create(:administrator).user
+ investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author)
+ login_as(administrator)
+ visit edit_image_budget_investment_path(budget, investment)
+
+ click_link "Remove image"
+
+ expect(page).to have_content "Investment project image removed succesfully."
+ end
+
+ scenario "Remove image should be possible for investment author" do
+ investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author)
+ login_as(author)
+ visit edit_image_budget_investment_path(budget, investment)
+
+ click_link "Remove image"
+
+ expect(page).to have_content "Investment project image removed succesfully."
+ end
+
+ scenario "Remove image should not be possible for any other logged users (except administrators and author)" do
+ investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author)
+ login_as(create(:user))
+ visit edit_image_budget_investment_path(budget, investment)
+
+ expect(page).to have_content "You do not have permission to carry out the action 'edit_image' on budget/investment."
+ end
+
scenario "Update image should not be possible if logged user is not the author" do
other_author = create(:user, :level_two, username: 'Manuel')
investment = create(:budget_investment, heading: heading, author: author)
diff --git a/spec/models/abilities/common_spec.rb b/spec/models/abilities/common_spec.rb
index 532aa50b9..51145a568 100644
--- a/spec/models/abilities/common_spec.rb
+++ b/spec/models/abilities/common_spec.rb
@@ -236,6 +236,7 @@ describe "Abilities::Common" do
it { should_not be_able_to(:create, ballot_in_selecting_budget) }
it { should be_able_to(:create, ballot_in_balloting_budget) }
+
it { should be_able_to(:edit_image, own_investment_in_accepting_budget) }
it { should be_able_to(:edit_image, own_investment_in_reviewing_budget) }
it { should be_able_to(:edit_image, own_investment_in_selecting_budget) }
@@ -244,6 +245,10 @@ describe "Abilities::Common" do
it { should be_able_to(:update_image, own_investment_in_reviewing_budget) }
it { should be_able_to(:update_image, own_investment_in_selecting_budget) }
it { should be_able_to(:update_image, own_investment_in_balloting_budget) }
+ it { should be_able_to(:remove_image, own_investment_in_accepting_budget) }
+ it { should be_able_to(:remove_image, own_investment_in_reviewing_budget) }
+ it { should be_able_to(:remove_image, own_investment_in_selecting_budget) }
+ it { should be_able_to(:remove_image, own_investment_in_balloting_budget) }
it { should_not be_able_to(:edit_image, investment_in_accepting_budget) }
it { should_not be_able_to(:edit_image, investment_in_reviewing_budget) }
@@ -253,6 +258,10 @@ describe "Abilities::Common" do
it { should_not be_able_to(:update_image, investment_in_reviewing_budget) }
it { should_not be_able_to(:update_image, investment_in_selecting_budget) }
it { should_not be_able_to(:update_image, investment_in_balloting_budget) }
+ it { should_not be_able_to(:remove_image, investment_in_accepting_budget) }
+ it { should_not be_able_to(:remove_image, investment_in_reviewing_budget) }
+ it { should_not be_able_to(:remove_image, investment_in_selecting_budget) }
+ it { should_not be_able_to(:remove_image, investment_in_balloting_budget) }
end
end