Extract controllers to support investments

Since we're going to add an action to remove supports, having a separate
controller makes things easier.

Note there was a strange piece of code which assumed users were not
verified if they couldn't vote investments. Now the code is also
strange, since it assumes users are not verified if they can't create
votes. We might need to revisit these conditions if our logic changes in
the future.
This commit is contained in:
Javi Martín
2021-06-13 20:38:42 +02:00
parent 071da81be6
commit 758cdaf8d7
12 changed files with 52 additions and 35 deletions

View File

@@ -10,9 +10,9 @@ class Budgets::Investments::VotesComponent < ApplicationComponent
def vote_path
case namespace
when "management"
vote_management_budget_investment_path(investment.budget, investment, value: "yes")
management_budget_investment_votes_path(investment.budget, investment)
else
vote_budget_investment_path(investment.budget, investment, value: "yes")
budget_investment_votes_path(investment.budget, investment)
end
end

View File

@@ -0,0 +1,21 @@
module Budgets
module Investments
class VotesController < ApplicationController
load_and_authorize_resource :budget
load_and_authorize_resource :investment, through: :budget, class: "Budget::Investment"
def create
@investment.register_selection(current_user)
respond_to do |format|
format.html do
redirect_to budget_investments_path(heading_id: @investment.heading.id),
notice: t("flash.actions.create.support")
end
format.js
end
end
end
end
end

View File

@@ -90,19 +90,6 @@ module Budgets
redirect_to user_path(current_user, filter: "budget_investments"), notice: t("flash.actions.destroy.budget_investment")
end
def vote
@investment.register_selection(current_user)
respond_to do |format|
format.html do
redirect_to budget_investments_path(heading_id: @investment.heading.id),
notice: t("flash.actions.create.support")
end
format.js
end
end
def suggest
@resource_path_method = :namespaced_budget_investment_path
@resource_relation = resource_model.where(budget: @budget).apply_filters_and_search(@budget, params, @current_filter)

View File

@@ -0,0 +1,17 @@
class Management::Budgets::Investments::VotesController < Management::BaseController
load_resource :budget, find_by: :slug_or_id
load_resource :investment, through: :budget, class: "Budget::Investment"
def create
@investment.register_selection(managed_user)
respond_to do |format|
format.html do
redirect_to management_budget_investments_path(heading_id: @investment.heading.id),
notice: t("flash.actions.create.support")
end
format.js
end
end
end

View File

@@ -38,19 +38,6 @@ class Management::Budgets::InvestmentsController < Management::BaseController
def show
end
def vote
@investment.register_selection(managed_user)
respond_to do |format|
format.html do
redirect_to management_budget_investments_path(heading_id: @investment.heading.id),
notice: t("flash.actions.create.support")
end
format.js
end
end
def print
@investments = @investments.apply_filters_and_search(@budget, params).order(cached_votes_up: :desc).for_render.limit(15)
end

View File

@@ -99,7 +99,10 @@ module Abilities
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" }
can :create, ActsAsVotable::Vote,
voter_id: user.id,
votable_type: "Budget::Investment",
votable: { budget: { phase: "selecting" }}
can [:show, :create], Budget::Ballot, budget: { phase: "balloting" }
can [:create, :destroy], Budget::Ballot::Line, budget: { phase: "balloting" }

View File

@@ -277,7 +277,7 @@ class Budget
def permission_problem(user)
return :not_logged_in unless user
return :organization if user.organization?
return :not_verified unless user.can?(:vote, Budget::Investment)
return :not_verified unless user.can?(:create, ActsAsVotable::Vote)
nil
end

View File

@@ -2,12 +2,13 @@ resources :budgets, only: [:show, :index] do
resources :groups, controller: "budgets/groups", only: [:show]
resources :investments, controller: "budgets/investments" do
member do
post :vote
put :flag
put :unflag
end
collection { get :suggest }
resources :votes, controller: "budgets/investments/votes", only: :create
end
resource :ballot, only: :show, controller: "budgets/ballots" do

View File

@@ -39,8 +39,9 @@ namespace :management do
end
resources :investments, only: [:index, :new, :create, :show, :destroy], controller: "budgets/investments" do
post :vote, on: :member
get :print, on: :collection
resources :votes, controller: "budgets/investments/votes", only: :create
end
end
end

View File

@@ -248,9 +248,9 @@ describe Abilities::Common do
it { should_not be_able_to(:create, investment_in_selecting_budget) }
it { should_not be_able_to(:create, investment_in_balloting_budget) }
it { should be_able_to(:vote, investment_in_selecting_budget) }
it { should_not be_able_to(:vote, investment_in_accepting_budget) }
it { should_not be_able_to(:vote, investment_in_balloting_budget) }
it { should be_able_to(:create, user.votes.build(votable: investment_in_selecting_budget)) }
it { should_not be_able_to(:create, user.votes.build(votable: investment_in_accepting_budget)) }
it { should_not be_able_to(:create, user.votes.build(votable: investment_in_balloting_budget)) }
it { should_not be_able_to(:destroy, investment_in_accepting_budget) }
it { should_not be_able_to(:destroy, investment_in_reviewing_budget) }