Adapts management::budget_investments to new budgets
This commit is contained in:
@@ -1,63 +1,49 @@
|
||||
class Management::Budgets::InvestmentsController < Management::BaseController
|
||||
|
||||
load_and_authorize_resource :budget
|
||||
load_and_authorize_resource :investment, through: :budget, class: 'Budget::Investment'
|
||||
|
||||
before_action :only_verified_users, except: :print
|
||||
before_action :set_budget_investment, only: [:vote, :show]
|
||||
before_action :load_accepting_headings, only: [:new, :create]
|
||||
|
||||
def index
|
||||
@investments = apply_filters_and_search(Budget::Investment).order(cached_votes_up: :desc).page(params[:page]).for_render
|
||||
@investments = apply_filters_and_search(@investments).order(cached_votes_up: :desc).page(params[:page]).for_render
|
||||
@investment_ids = @investments.pluck(:id)
|
||||
set_investment_ballots(@investments)
|
||||
set_budget_investment_votes(@investments)
|
||||
end
|
||||
|
||||
def new
|
||||
@investment = Budget::Investment.new
|
||||
end
|
||||
|
||||
def create
|
||||
@investment = Budget::Investment.new(budget_investment_params)
|
||||
@investment.terms_of_service = "1"
|
||||
@investment.author = managed_user
|
||||
|
||||
if @investment.save
|
||||
redirect_to management_budgets_investment_path(@investment), notice: t('flash.actions.create.notice', resource_name: Budget::Investment.model_name.human, count: 1)
|
||||
notice= t('flash.actions.create.notice', resource_name: Budget::Investment.model_name.human, count: 1)
|
||||
redirect_to management_budget_investment_path(@budget, @investment), notice: notice
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
set_investment_ballots(@investment)
|
||||
set_budget_investment_votes(@investment)
|
||||
end
|
||||
|
||||
def vote
|
||||
@investment.register_vote(managed_user, 'yes')
|
||||
set_investment_ballots(@investment)
|
||||
set_budget_investment_votes(@investment)
|
||||
end
|
||||
|
||||
def print
|
||||
params[:geozone] ||= 'all'
|
||||
@investments = apply_filters_and_search(Budget::Investment).order(cached_votes_up: :desc).for_render.limit(15)
|
||||
set_budget_investment_votes(@investments)
|
||||
@investments = apply_filters_and_search(@investments).order(cached_votes_up: :desc).for_render.limit(15)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_investment_ballots(investments)
|
||||
@investment_ballots = {}
|
||||
Budget.where(id: Array.wrap(investments).map(&:budget_id).uniq).each do |budget|
|
||||
@investment_ballots[budget] = Budget::Ballot.where(user: current_user, budget: budget).first_or_create
|
||||
end
|
||||
def load_budget
|
||||
@budget = Budget.find(params[:budget_id])
|
||||
end
|
||||
|
||||
def set_budget_investment
|
||||
@investment = Budget::Investment.find(params[:id])
|
||||
end
|
||||
|
||||
def budget_investment_params
|
||||
def investment_params
|
||||
params.require(:budget_investment).permit(:title, :description, :external_url, :geozone_id, :heading_id)
|
||||
end
|
||||
|
||||
@@ -65,11 +51,6 @@ class Management::Budgets::InvestmentsController < Management::BaseController
|
||||
check_verified_user t("management.budget_investments.alert.unverified_user")
|
||||
end
|
||||
|
||||
# This should not be necessary. Maybe we could create a specific show view for managers.
|
||||
def set_budget_investment_votes(budget_investments)
|
||||
@investment_votes = managed_user ? managed_user.budget_investment_votes(budget_investments) : {}
|
||||
end
|
||||
|
||||
def set_geozone_name
|
||||
if params[:geozone] == 'all'
|
||||
@geozone_name = t('geozones.none')
|
||||
@@ -78,20 +59,14 @@ class Management::Budgets::InvestmentsController < Management::BaseController
|
||||
end
|
||||
end
|
||||
|
||||
def apply_filters_and_search(target)
|
||||
target = params[:unfeasible].present? ? target.unfeasible : target.not_unfeasible
|
||||
def apply_filters_and_search(investments)
|
||||
investments = params[:unfeasible].present? ? investments.unfeasible : investments.not_unfeasible
|
||||
if params[:geozone].present?
|
||||
target = target.by_geozone(params[:geozone])
|
||||
investments = investments.by_geozone(params[:geozone])
|
||||
set_geozone_name
|
||||
end
|
||||
target = target.search(params[:search]) if params[:search].present?
|
||||
target
|
||||
end
|
||||
|
||||
def load_accepting_headings
|
||||
accepting_budget_ids = Budget.accepting.pluck(:id)
|
||||
accepting_budget_group_ids = Budget::Group.where(budget_id: accepting_budget_ids).pluck(:id)
|
||||
@headings = Budget::Heading.where(group_id: accepting_budget_group_ids).order(:group_id, :name).includes(:group => :budget)
|
||||
investments = investments.search(params[:search]) if params[:search].present?
|
||||
investments
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -11,6 +11,11 @@
|
||||
<%= render 'shared/errors', resource: @investment %>
|
||||
|
||||
<div class="row">
|
||||
<div class="small-12 column">
|
||||
<%= f.label :heading_id, t("budget.investments.form.heading") %>
|
||||
<%= f.select :heading_id, budget_heading_select_options(@budget), {include_blank: t("budget.headings.none"), label: false} %>
|
||||
</div>
|
||||
|
||||
<div class="small-12 column">
|
||||
<%= f.label :title, t("budget.investments.form.title") %>
|
||||
<%= f.text_field :title, maxlength: SpendingProposal.title_max_length, placeholder: t("budget.investments.form.title"), label: false %>
|
||||
@@ -28,10 +33,7 @@
|
||||
<%= f.text_field :external_url, placeholder: t("budget.investments.form.external_url"), label: false %>
|
||||
</div>
|
||||
|
||||
<div class="small-12 column">
|
||||
<%= f.label :heading_id, t("budget.investments.form.heading") %>
|
||||
<%= f.select :heading_id, multiple_budgets_heading_select_options(@headings), {include_blank: t("budget.headings.none"), label: false} %>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="small-12 column">
|
||||
<%= f.label :terms_of_service do %>
|
||||
|
||||
Reference in New Issue
Block a user