Allow to remove budget investment image from edit image page to authors and administrators.

This commit is contained in:
Senén Rodero Rodríguez
2017-06-23 14:23:58 +02:00
parent 943ef06ec8
commit 4469aa9b32
11 changed files with 85 additions and 8 deletions

View File

@@ -92,6 +92,14 @@ module Budgets
end end
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 private
def resource_model def resource_model

View File

@@ -48,7 +48,7 @@ module Abilities
can [:index, :read, :new, :create, :update, :destroy, :calculate_winners], Budget can [:index, :read, :new, :create, :update, :destroy, :calculate_winners], Budget
can [:read, :create, :update, :destroy], Budget::Group can [:read, :create, :update, :destroy], Budget::Group
can [:read, :create, :update, :destroy], Budget::Heading 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 :valuate, Budget::Investment
can :create, Budget::ValuatorAssignment can :create, Budget::ValuatorAssignment

View File

@@ -57,6 +57,7 @@ module Abilities
can :vote, Budget::Investment, budget: { phase: "selecting" } can :vote, Budget::Investment, budget: { phase: "selecting" }
can :edit_image, Budget::Investment, author_id: user.id can :edit_image, Budget::Investment, author_id: user.id
can :update_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 [:show, :create], Budget::Ballot, budget: { phase: "balloting" }
can [:create, :destroy], Budget::Ballot::Line, budget: { phase: "balloting" } can [:create, :destroy], Budget::Ballot::Line, budget: { phase: "balloting" }

View File

@@ -17,7 +17,7 @@
<%= f.file_field :image, accept: 'image/jpeg', label: false, class:'show-for-sr' %> <%= f.file_field :image, accept: 'image/jpeg', label: false, class:'show-for-sr' %>
<br> <br>
<%= f.label :image, t("images.upload_image"), class:'button' %> <%= f.label :image, t("images.upload_image"), class:'button' %>
<p><%= @investment.image.url.split('/').last.split("?")[0] %></p> <p><%= @investment.image.url.split('/').last.split("?")[0] if @investment.image.exists?%></p>
</div> </div>
</div> </div>
<div class="small-12 column"> <div class="small-12 column">
@@ -26,6 +26,13 @@
</div> </div>
<div class="actions small-12 column"> <div class="actions small-12 column">
<%= f.submit(class: "button", value: t("budgets.investments.edit_image.form.submit_button")) %> <%= 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 %>
</div> </div>
</div> </div>
<% end %> <% end %>

View File

@@ -117,6 +117,7 @@ en:
form: form:
image_title: Image title image_title: Image title
submit_button: Save image submit_button: Save image
remove_button: Remove image
recommendation_title: Image recomendations 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_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. recommendation_two: Give a descriptive title to the image.

View File

@@ -27,6 +27,8 @@ en:
topic: "Topic updated successfully." topic: "Topic updated successfully."
update_image: update_image:
budget_investment: "Investment project image updated succesfully." budget_investment: "Investment project image updated succesfully."
remove_image:
budget_investment: "Investment project image removed succesfully."
destroy: destroy:
spending_proposal: "Spending proposal deleted succesfully." spending_proposal: "Spending proposal deleted succesfully."
budget_investment: "Investment project deleted succesfully." budget_investment: "Investment project deleted succesfully."

View File

@@ -117,6 +117,7 @@ es:
form: form:
image_title: 'Título de la imagen' image_title: 'Título de la imagen'
submit_button: Guardar imagen submit_button: Guardar imagen
remove_button: Eliminar imagen
recommendation_title: Recomendaciones para cambiar la 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_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. recommendation_two: Dale un título descriptivo a la imagen.

View File

@@ -27,6 +27,8 @@ es:
topic: "Tema actualizado correctamente." topic: "Tema actualizado correctamente."
update_image: update_image:
budget_investment: "La imagen de la propuesta de inversión se ha actualizado correctamente." 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: destroy:
spending_proposal: "Propuesta de inversión eliminada." spending_proposal: "Propuesta de inversión eliminada."
budget_investment: "Propuesta de inversión eliminada." budget_investment: "Propuesta de inversión eliminada."

View File

@@ -82,6 +82,7 @@ Rails.application.routes.draw do
post :vote post :vote
get :edit_image get :edit_image
put :update_image put :update_image
delete :remove_image
end end
collection { get :suggest } collection { get :suggest }
end end

View File

@@ -389,7 +389,7 @@ feature 'Budget Investments' do
context "Show investment image button" 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) investment = create(:budget_investment, heading: heading, author: author)
login_as(author) login_as(author)
visit budget_investment_path(budget, investment) visit budget_investment_path(budget, investment)
@@ -397,7 +397,7 @@ feature 'Budget Investments' do
expect(page).to have_link "Add image" expect(page).to have_link "Add image"
end 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) investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author)
login_as(author) login_as(author)
visit budget_investment_path(budget, investment) visit budget_investment_path(budget, investment)
@@ -405,21 +405,21 @@ feature 'Budget Investments' do
expect(page).to have_link "Edit image" expect(page).to have_link "Edit image"
end 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) investment = create(:budget_investment, heading: heading)
visit budget_investment_path(budget, investment) visit budget_investment_path(budget, investment)
expect(page).not_to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment) expect(page).not_to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment)
end 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) investment = create(:budget_investment, heading: heading)
visit budget_investment_path(budget, investment) visit budget_investment_path(budget, investment)
expect(page).not_to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment) expect(page).not_to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment)
end 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) investment = create(:budget_investment, heading: heading, author: author)
login_as(author) login_as(author)
visit budget_investment_path(budget, investment) 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) expect(page).to have_link "Add image", href: edit_image_budget_investment_path(budget, investment)
end 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 administrator = create(:administrator).user
investment = create(:budget_investment, heading: heading, author: author) investment = create(:budget_investment, heading: heading, author: author)
login_as(administrator) login_as(administrator)
@@ -470,6 +470,51 @@ feature 'Budget Investments' do
expect(page).to have_content "Change your project image" expect(page).to have_content "Change your project image"
end 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 scenario "Update image should not be possible if logged user is not the author" do
other_author = create(:user, :level_two, username: 'Manuel') other_author = create(:user, :level_two, username: 'Manuel')
investment = create(:budget_investment, heading: heading, author: author) investment = create(:budget_investment, heading: heading, author: author)

View File

@@ -236,6 +236,7 @@ describe "Abilities::Common" do
it { should_not be_able_to(:create, ballot_in_selecting_budget) } 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(: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_accepting_budget) }
it { should be_able_to(:edit_image, own_investment_in_reviewing_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) } 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_reviewing_budget) }
it { should be_able_to(:update_image, own_investment_in_selecting_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(: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_accepting_budget) }
it { should_not be_able_to(:edit_image, investment_in_reviewing_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_reviewing_budget) }
it { should_not be_able_to(:update_image, investment_in_selecting_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(: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
end end