Allow administrators to edit and update images of any budget investment.

This commit is contained in:
Senén Rodero Rodríguez
2017-06-21 18:04:46 +02:00
parent aa471974bd
commit ce0c0f7024
4 changed files with 57 additions and 16 deletions

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], Budget::Investment can [:hide, :update, :toggle_selection, :edit_image, :update_image], Budget::Investment
can :valuate, Budget::Investment can :valuate, Budget::Investment
can :create, Budget::ValuatorAssignment can :create, Budget::ValuatorAssignment

View File

@@ -5,12 +5,12 @@
<% if investment.image.exists? %> <% if investment.image.exists? %>
<picture> <picture>
<source srcset="<%= investment.image.url(:medium) %>" <source srcset="<%= investment.image.url(:medium) %>"
alt="<%= investment.image_description %>" alt="<%= investment.image_title %>"
title= "<%= investment.image_description %>" title= "<%= investment.image_title %>"
media="(max-width: 640px)"> media="(max-width: 640px)">
<img srcset="<%= investment.image.url(:thumb) %>" <img srcset="<%= investment.image.url(:thumb) %>"
alt="<%= investment.image_description %>" alt="<%= investment.image_title %>"
title="<%= investment.image_description %>"> title="<%= investment.image_title %>">
</picture> </picture>
<% else %> <% else %>

View File

@@ -10,7 +10,7 @@
class: 'button hollow float-right' %> class: 'button hollow float-right' %>
<% end %> <% end %>
<% if author_of?(investment, current_user) %> <% if author_of?(investment, current_user) || (current_user && current_user.administrator?) %>
<%= link_to t("budgets.investments.show.edit_image"), <%= link_to t("budgets.investments.show.edit_image"),
edit_image_budget_investment_path(investment.budget, investment), edit_image_budget_investment_path(investment.budget, investment),
class: 'button hollow float-right' %> class: 'button hollow float-right' %>

View File

@@ -35,11 +35,11 @@ feature 'Budget Investments' do
visit budget_investments_path(budget, heading_id: heading.id) visit budget_investments_path(budget, heading_id: heading.id)
within("#budget_investment_#{investment.id}") do within("#budget_investment_#{investment.id}") do
expect(page).not_to have_css("img.th") expect(page).not_to have_css("picture")
end end
within("#budget_investment_#{investment_with_image.id}") do within("#budget_investment_#{investment_with_image.id}") do
expect(page).to have_css("img.th[alt='#{investment_with_image.image_title}'][title='#{investment_with_image.image_title}']") expect(page).to have_css("picture img[alt='#{investment_with_image.image_title}'][title='#{investment_with_image.image_title}']")
end end
end end
@@ -387,7 +387,7 @@ feature 'Budget Investments' do
expect(page).to have_link "Go back", href: budget_investments_path(budget, heading_id: investment.heading) expect(page).to have_link "Go back", href: budget_investments_path(budget, heading_id: investment.heading)
end end
context "Edit image button" do context "Show edit image button" do
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)
@@ -409,16 +409,25 @@ feature 'Budget Investments' do
expect(page).to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment) expect(page).to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment)
end end
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)
visit budget_investment_path(budget, investment)
expect(page).to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment)
end
end end
scenario "edit page should not be accesible when there is no logged user" do scenario "Edit image page should not be accesible when there is no logged user" do
investment = create(:budget_investment, heading: heading, author: author) investment = create(:budget_investment, heading: heading, author: author)
visit edit_image_budget_investment_path(budget, investment) visit edit_image_budget_investment_path(budget, investment)
expect(page).to have_content "You must sign in or register to continue" expect(page).to have_content "You must sign in or register to continue"
end end
scenario "edit page should redirect to investment show page if logged user is not the author" do scenario "Edit image page should redirect to investment show page 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)
login_as(other_author) login_as(other_author)
@@ -427,7 +436,24 @@ feature 'Budget Investments' do
expect(page).to have_content "You do not have permission to carry out the action 'edit_image' on budget/investment." expect(page).to have_content "You do not have permission to carry out the action 'edit_image' on budget/investment."
end end
scenario "Update image should not be posible if logged user is not the author" do scenario "Edit image page should be accesible when author is currently logged" do
investment = create(:budget_investment, heading: heading, author: author)
login_as(author)
visit edit_image_budget_investment_path(budget, investment)
expect(page).to have_content "Change your project image"
end
scenario "Edit image page should be accesible when there is logged any administrator" do
administrator = create(:administrator).user
investment = create(:budget_investment, heading: heading, author: author)
login_as(administrator)
visit edit_image_budget_investment_path(budget, investment)
expect(page).to have_content "Change your project image"
end
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)
login_as(other_author) login_as(other_author)
@@ -437,7 +463,7 @@ feature 'Budget Investments' do
expect(page).to have_content 'You do not have permission' expect(page).to have_content 'You do not have permission'
end end
scenario "Update image should be posible for authors" do scenario "Update image should be possible for authors" do
investment = create(:budget_investment, heading: heading, author: author) investment = create(:budget_investment, heading: heading, author: author)
login_as(author) login_as(author)
@@ -451,6 +477,21 @@ feature 'Budget Investments' do
expect(page).to have_content 'Investment project image updated succesfully. ' expect(page).to have_content 'Investment project image updated succesfully. '
end end
scenario "Update image should be possible for authors" do
administrator = create(:administrator).user
investment = create(:budget_investment, heading: heading, author: author)
login_as(administrator)
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"
within ".budget-investment-show" do
expect(page).to have_css("img[src*='logo_header.jpg']")
end
expect(page).to have_content 'Investment project image updated succesfully. '
end
context "Show (feasible budget investment)" do context "Show (feasible budget investment)" do
let(:investment) do let(:investment) do
create(:budget_investment, create(:budget_investment,
@@ -551,12 +592,12 @@ feature 'Budget Investments' do
context "Destroy" do context "Destroy" do
scenario "Admin cannot destroy budget investments" do scenario "Admin cannot destroy budget investments" do
admin = create(:administrator) administrator = create(:administrator).user
user = create(:user, :level_two) user = create(:user, :level_two)
investment = create(:budget_investment, heading: heading, author: user) investment = create(:budget_investment, heading: heading, author: user)
login_as(admin.user) login_as(administrator)
visit user_path(user) visit user_path(administrator)
within("#budget_investment_#{investment.id}") do within("#budget_investment_#{investment.id}") do
expect(page).to_not have_link "Delete" expect(page).to_not have_link "Delete"