Skip to content

Commit

Permalink
Edit Budget Investment only in accepting phase (consuldemocracy#3716)
Browse files Browse the repository at this point in the history
This way users who made a typo can fix it before the investment is reviewed.
  • Loading branch information
denialtorres authored and javierm committed Oct 18, 2019
1 parent 59c77c1 commit 1f1a85f
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/controllers/admin/budget_investments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def edit
end

def update
authorize! :admin_update, @investment
if @investment.update(budget_investment_params)
redirect_to admin_budget_budget_investment_path(@budget,
@investment,
Expand Down
11 changes: 10 additions & 1 deletion app/controllers/budgets/investments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InvestmentsController < ApplicationController
before_action :load_ballot, only: [:index, :show]
before_action :load_heading, only: [:index, :show]
before_action :set_random_seed, only: :index
before_action :load_categories, only: [:index, :new, :create]
before_action :load_categories, only: [:index, :new, :create, :edit, :update]
before_action :set_default_budget_filter, only: :index
before_action :set_view, only: :index
before_action :load_content_blocks, only: :index
Expand Down Expand Up @@ -76,6 +76,15 @@ def create
end
end

def update
if @investment.update(investment_params)
redirect_to budget_investment_path(@budget, @investment),
notice: t("flash.actions.update.budget_investment")
else
render "edit"
end
end

def destroy
@investment.destroy
redirect_to user_path(current_user, filter: "budget_investments"), notice: t("flash.actions.destroy.budget_investment")
Expand Down
2 changes: 1 addition & 1 deletion app/models/abilities/administrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def initialize(user)
can [:index, :read, :new, :create, :update, :destroy, :calculate_winners, :assigned_users_translation], Budget
can [:read, :create, :update, :destroy], Budget::Group
can [:read, :create, :update, :destroy], Budget::Heading
can [:hide, :update, :toggle_selection], Budget::Investment
can [:hide, :admin_update, :toggle_selection], Budget::Investment
can [:valuate, :comment_valuation], Budget::Investment
can :create, Budget::ValuatorAssignment
can [:edit_dossier], Budget::Investment
Expand Down
2 changes: 2 additions & 0 deletions app/models/abilities/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def initialize(user)
can :create, Legislation::Answer

can :create, Budget::Investment, budget: { phase: "accepting" }
can :edit, Budget::Investment, budget: { phase: "accepting" }, author_id: user.id
can :update, Budget::Investment, budget: { phase: "accepting" }, author_id: user.id
can :suggest, Budget::Investment, budget: { phase: "accepting" }
can :destroy, Budget::Investment, budget: { phase: ["accepting", "reviewing"] }, author_id: user.id
can :vote, Budget::Investment, budget: { phase: "selecting" }
Expand Down
2 changes: 1 addition & 1 deletion app/views/budgets/investments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= translatable_form_for(@investment, url: form_url, method: :post, html: { multipart: true }) do |f| %>
<%= translatable_form_for(@investment, url: form_url, html: { multipart: true }) do |f| %>
<%= render "shared/errors", resource: @investment %>

Expand Down
6 changes: 6 additions & 0 deletions app/views/budgets/investments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="budget-investment-new row">
<div class="small-12 medium-9 column">
<h1><%= t("management.budget_investments.edit") %></h1>
<%= render "/budgets/investments/form", form_url: budget_investment_path(@budget, @investment) %>
</div>
</div>
4 changes: 4 additions & 0 deletions app/views/users/_budget_investment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
method: :delete, class: "button hollow alert expanded",
data: { confirm: "#{t("users.show.delete_alert")}" } %>
<% end %>
<% if can? :update, budget_investment %>
<%= link_to t("shared.edit"), edit_budget_investment_path(budget_investment.budget, budget_investment),
class: "button hollow expanded" %>
<% end %>
</td>
</tr>
1 change: 1 addition & 0 deletions config/locales/en/management.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ en:
alert:
unverified_user: User is not verified
create: Create a budget investment
edit: Edit budget investment
filters:
heading: Concept
unfeasible: Unfeasible investment
Expand Down
1 change: 1 addition & 0 deletions config/locales/es/management.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ es:
alert:
unverified_user: Usuario no verificado
create: Crear nuevo proyecto
edit: Editar proyecto de gasto
filters:
heading: Concepto
unfeasible: Proyectos no factibles
Expand Down
2 changes: 1 addition & 1 deletion config/routes/budget.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resources :budgets, only: [:show, :index] do
resources :groups, controller: "budgets/groups", only: [:show]
resources :investments, controller: "budgets/investments", only: [:index, :new, :create, :show, :destroy] do
resources :investments, controller: "budgets/investments" do
member do
post :vote
put :flag
Expand Down
48 changes: 48 additions & 0 deletions spec/features/budgets/investments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,54 @@ def investments_order
expect(page).to have_content "Build a skyscraper"
end

scenario "Edit", :js do
daniel = create(:user, :level_two)

create(:budget_investment, heading: heading,title: "Get Schwifty", author: daniel, created_at: 1.day.ago)

login_as(daniel)

visit user_path(daniel, filter: "budget_investments")

click_link("Edit", match: :first)
fill_in "Title", with: "Park improvements"
check "budget_investment_terms_of_service"

click_button "Update Investment"

expect(page).to have_content "Investment project updated succesfully"
expect(page).to have_content "Park improvements"
end

scenario "Trigger validation errors in edit view" do
daniel = create(:user, :level_two)
message_error = "is too short (minimum is 4 characters), can't be blank"
create(:budget_investment, heading: heading,title: "Get SH", author: daniel, created_at: 1.day.ago)

login_as(daniel)

visit user_path(daniel, filter: "budget_investments")
click_link("Edit", match: :first)
fill_in "Title", with: ""
check "budget_investment_terms_of_service"

click_button "Update Investment"

expect(page).to have_content message_error
end

scenario "Another User can't edit budget investment" do
message_error = "You do not have permission to carry out the action 'edit' on budget/investment"
admin = create(:administrator)
daniel = create(:user, :level_two)
investment = create(:budget_investment, heading: heading, author: daniel)

login_as(admin.user)
visit edit_budget_investment_path(budget, investment)

expect(page).to have_content message_error
end

scenario "Errors on create" do
login_as(author)

Expand Down
2 changes: 1 addition & 1 deletion spec/models/abilities/administrator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

it { should be_able_to(:create, Budget::ValuatorAssignment) }

it { should be_able_to(:update, Budget::Investment) }
it { should be_able_to(:admin_update, Budget::Investment) }
it { should be_able_to(:hide, Budget::Investment) }

it { should be_able_to(:valuate, create(:budget_investment, budget: create(:budget, :valuating))) }
Expand Down
5 changes: 5 additions & 0 deletions spec/models/abilities/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@
it { should_not be_able_to(:destroy, own_investment_in_selecting_budget) }
it { should_not be_able_to(:destroy, own_investment_in_balloting_budget) }

it { should be_able_to(:edit, own_investment_in_accepting_budget) }
it { should_not be_able_to(:edit, own_investment_in_reviewing_budget) }
it { should_not be_able_to(:edit, own_investment_in_selecting_budget) }
it { should_not be_able_to(:edit, own_investment_in_balloting_budget) }

it { should be_able_to(:create, ballot_in_balloting_budget) }
it { should_not be_able_to(:create, ballot_in_accepting_budget) }
it { should_not be_able_to(:create, ballot_in_selecting_budget) }
Expand Down

0 comments on commit 1f1a85f

Please sign in to comment.