Add edit_image and update_image action to budget/investments controller.

This commit is contained in:
Senén Rodero Rodríguez
2017-06-21 12:56:01 +02:00
parent 515d6d7e70
commit acc79cbe31
14 changed files with 167 additions and 3 deletions

View File

@@ -80,6 +80,18 @@ module Budgets
super super
end end
def edit_image
end
def update_image
if @investment.update(investment_params)
redirect_to budget_investment_path(@investment.budget, @investment),
notice: t("flash.actions.update_image.budget_investment")
else
render :edit_image
end
end
private private
def resource_model def resource_model

View File

@@ -55,6 +55,9 @@ module Abilities
can :suggest, Budget::Investment, budget: { phase: "accepting" } can :suggest, Budget::Investment, budget: { phase: "accepting" }
can :destroy, Budget::Investment, budget: { phase: ["accepting", "reviewing"] }, author_id: user.id can :destroy, Budget::Investment, budget: { phase: ["accepting", "reviewing"] }, author_id: user.id
can :vote, Budget::Investment, budget: { phase: "selecting" } 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 [: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

@@ -0,0 +1,29 @@
<%= form_for([@investment.budget, @investment], url: update_image_budget_investment_path(@investment.budget, @investment), multipart: true, method: :put) do |f| %>
<%= render 'shared/errors', resource: @investment %>
<div class="row">
<div class="small-12 column">
<span class="note-marked">
<%= t("budgets.investments.edit_image.note", title: @investment.title) %></p>
</span>
</div>
<div class="small-12 column">
<%= image_tag @investment.image.url(:large) %>
</div>
<div class="small-12 column">
<%= f.file_field :image, accept: 'image/jpeg' %>
</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 %>
</div>
<div class="actions small-12 column">
<%= f.submit(class: "button", value: t("budgets.investments.edit_image.form.submit_button")) %>
</div>
</div>
<% end %>

View File

@@ -10,6 +10,12 @@
class: 'button hollow float-right' %> class: 'button hollow float-right' %>
<% end %> <% end %>
<% if author_of?(investment, current_user) %>
<%= link_to t("budgets.investments.show.edit_image"),
edit_image_budget_investment_path(investment.budget, investment),
class: 'button hollow float-right' %>
<% end %>
<h1><%= investment.title %></h1> <h1><%= investment.title %></h1>
<div class="budget-investment-info"> <div class="budget-investment-info">

View File

@@ -0,0 +1,10 @@
<div class="budget-investment-image-edit row">
<div class="small-12 column">
<%= render "shared/back_link" %>
<h1><%= t("budgets.investments.edit_image.title") %></h1>
<%= render "image_form" %>
</div>
</div>

View File

@@ -105,7 +105,7 @@ en:
location: "Location" location: "Location"
organization_name: "If you are proposing in the name of a collective/organization, write its name" organization_name: "If you are proposing in the name of a collective/organization, write its name"
image: "Proposal descriptive image" image: "Proposal descriptive image"
image_title: "Image description" image_title: "Image title"
budget/investment/milestone: budget/investment/milestone:
title: "Title" title: "Title"
description: "Description" description: "Description"

View File

@@ -108,6 +108,13 @@ en:
milestones_tab: Milestones milestones_tab: Milestones
no_milestones: Don't have defined milestones no_milestones: Don't have defined milestones
milestone_publish_date: "Published %{publish_date}" milestone_publish_date: "Published %{publish_date}"
edit_image: Edit image
edit_image:
title: Change your investment project image
note: 'You are changing investment project image of: %{title}'
form:
image_title: Image title
submit_button: Save image
wrong_price_format: Only integer numbers wrong_price_format: Only integer numbers
investment: investment:
add: Vote add: Vote

View File

@@ -25,6 +25,8 @@ en:
spending_proposal: "Investment project updated succesfully." spending_proposal: "Investment project updated succesfully."
budget_investment: "Investment project updated succesfully." budget_investment: "Investment project updated succesfully."
topic: "Topic updated successfully." topic: "Topic updated successfully."
update_image:
budget_investment: "Investment project image updated 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

@@ -100,7 +100,7 @@ es:
location: "Ubicación" location: "Ubicación"
organization_name: "Si estás proponiendo en nombre de una organización o colectivo, escribe su nombre" organization_name: "Si estás proponiendo en nombre de una organización o colectivo, escribe su nombre"
image: "Imagen descriptiva de la propueta" image: "Imagen descriptiva de la propueta"
image_title: "Texto descriptivo de la imagen" image_title: "Título de la imagen"
comment: comment:
body: "Comentario" body: "Comentario"
user: "Usuario" user: "Usuario"

View File

@@ -108,6 +108,13 @@ es:
milestones_tab: Seguimiento milestones_tab: Seguimiento
no_milestones: No hay hitos definidos no_milestones: No hay hitos definidos
milestone_publish_date: "Publicado el %{publish_date}" milestone_publish_date: "Publicado el %{publish_date}"
edit_image: Editar imagen
edit_image:
title: Cambia la imagen de tu proyecto de inversión
note: 'Estas cambiando la imagen del proyecto de inversión: %{title}'
form:
image_title: 'Título de la imagen'
submit_button: Guardar imagen
wrong_price_format: Solo puede incluir caracteres numéricos wrong_price_format: Solo puede incluir caracteres numéricos
investment: investment:
add: Votar add: Votar

View File

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

@@ -78,7 +78,11 @@ Rails.application.routes.draw do
resources :budgets, only: [:show, :index] do resources :budgets, only: [:show, :index] do
resources :groups, controller: "budgets/groups", only: [:show] resources :groups, controller: "budgets/groups", only: [:show]
resources :investments, controller: "budgets/investments", only: [:index, :new, :create, :show, :destroy] do resources :investments, controller: "budgets/investments", only: [:index, :new, :create, :show, :destroy] do
member { post :vote } member do
post :vote
get :edit_image
put :update_image
end
collection { get :suggest } collection { get :suggest }
end end
resource :ballot, only: :show, controller: "budgets/ballots" do resource :ballot, only: :show, controller: "budgets/ballots" do

View File

@@ -387,6 +387,70 @@ 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
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
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
investment = create(:budget_investment, heading: heading, author: author)
login_as(author)
visit budget_investment_path(budget, investment)
expect(page).to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment)
end
end
scenario "edit page should not be accesible when there is no logged user" do
investment = create(:budget_investment, heading: heading, author: author)
visit edit_image_budget_investment_path(budget, investment)
expect(page).to have_content "You must sign in or register to continue"
end
scenario "edit page should redirect to investment show page if logged user is not the author" do
other_author = create(:user, :level_two, username: 'Manuel')
investment = create(:budget_investment, heading: heading, author: author)
login_as(other_author)
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 posible if logged user is not the author" do
other_author = create(:user, :level_two, username: 'Manuel')
investment = create(:budget_investment, heading: heading, author: author)
login_as(other_author)
visit edit_image_budget_investment_path(investment.budget, investment)
expect(current_path).not_to eq(edit_image_budget_investment_path(investment.budget, investment))
expect(page).to have_content 'You do not have permission'
end
scenario "Update image should be posible for authors" 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"
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,

View File

@@ -235,6 +235,24 @@ describe "Abilities::Common" do
it { should_not be_able_to(:create, ballot_in_accepting_budget) } it { should_not be_able_to(:create, ballot_in_accepting_budget) }
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_reviewing_budget) }
it { should be_able_to(:edit_image, own_investment_in_selecting_budget) }
it { should be_able_to(:edit_image, own_investment_in_balloting_budget) }
it { should be_able_to(:update_image, own_investment_in_accepting_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_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) }
it { should_not be_able_to(:edit_image, investment_in_selecting_budget) }
it { should_not be_able_to(:edit_image, investment_in_balloting_budget) }
it { should_not be_able_to(:update_image, investment_in_accepting_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_balloting_budget) }
end end
end end