Adapts management::budget_investments to new budgets

This commit is contained in:
kikito
2016-12-06 18:05:58 +01:00
parent 34071a0f9f
commit e793a0d8e1
3 changed files with 42 additions and 66 deletions

View File

@@ -1,63 +1,49 @@
class Management::Budgets::InvestmentsController < Management::BaseController 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 :only_verified_users, except: :print
before_action :set_budget_investment, only: [:vote, :show]
before_action :load_accepting_headings, only: [:new, :create]
def index 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) @investment_ids = @investments.pluck(:id)
set_investment_ballots(@investments)
set_budget_investment_votes(@investments)
end end
def new def new
@investment = Budget::Investment.new
end end
def create def create
@investment = Budget::Investment.new(budget_investment_params)
@investment.terms_of_service = "1" @investment.terms_of_service = "1"
@investment.author = managed_user @investment.author = managed_user
if @investment.save 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 else
render :new render :new
end end
end end
def show def show
set_investment_ballots(@investment)
set_budget_investment_votes(@investment)
end end
def vote def vote
@investment.register_vote(managed_user, 'yes') @investment.register_vote(managed_user, 'yes')
set_investment_ballots(@investment)
set_budget_investment_votes(@investment)
end end
def print def print
params[:geozone] ||= 'all' params[:geozone] ||= 'all'
@investments = apply_filters_and_search(Budget::Investment).order(cached_votes_up: :desc).for_render.limit(15) @investments = apply_filters_and_search(@investments).order(cached_votes_up: :desc).for_render.limit(15)
set_budget_investment_votes(@investments)
end end
private private
def set_investment_ballots(investments) def load_budget
@investment_ballots = {} @budget = Budget.find(params[:budget_id])
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
end end
def set_budget_investment def investment_params
@investment = Budget::Investment.find(params[:id])
end
def budget_investment_params
params.require(:budget_investment).permit(:title, :description, :external_url, :geozone_id, :heading_id) params.require(:budget_investment).permit(:title, :description, :external_url, :geozone_id, :heading_id)
end end
@@ -65,11 +51,6 @@ class Management::Budgets::InvestmentsController < Management::BaseController
check_verified_user t("management.budget_investments.alert.unverified_user") check_verified_user t("management.budget_investments.alert.unverified_user")
end 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 def set_geozone_name
if params[:geozone] == 'all' if params[:geozone] == 'all'
@geozone_name = t('geozones.none') @geozone_name = t('geozones.none')
@@ -78,20 +59,14 @@ class Management::Budgets::InvestmentsController < Management::BaseController
end end
end end
def apply_filters_and_search(target) def apply_filters_and_search(investments)
target = params[:unfeasible].present? ? target.unfeasible : target.not_unfeasible investments = params[:unfeasible].present? ? investments.unfeasible : investments.not_unfeasible
if params[:geozone].present? if params[:geozone].present?
target = target.by_geozone(params[:geozone]) investments = investments.by_geozone(params[:geozone])
set_geozone_name set_geozone_name
end end
target = target.search(params[:search]) if params[:search].present? investments = investments.search(params[:search]) if params[:search].present?
target investments
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)
end end
end end

View File

@@ -11,6 +11,11 @@
<%= render 'shared/errors', resource: @investment %> <%= render 'shared/errors', resource: @investment %>
<div class="row"> <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"> <div class="small-12 column">
<%= f.label :title, t("budget.investments.form.title") %> <%= 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 %> <%= 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 %> <%= f.text_field :external_url, placeholder: t("budget.investments.form.external_url"), label: false %>
</div> </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"> <div class="small-12 column">
<%= f.label :terms_of_service do %> <%= f.label :terms_of_service do %>

View File

@@ -4,8 +4,8 @@ feature 'Budget Investments' do
background do background do
login_as_manager login_as_manager
budget = create(:budget, phase: 'accepting', name: "2016") @budget = create(:budget, phase: 'accepting', name: "2016")
group = create(:budget_group, budget: budget, name: 'Whole city') group = create(:budget_group, budget: @budget, name: 'Whole city')
@heading = create(:budget_heading, group: group, name: "Health") @heading = create(:budget_heading, group: group, name: "Health")
end end
@@ -17,15 +17,16 @@ feature 'Budget Investments' do
login_managed_user(user) login_managed_user(user)
click_link "Create budget investment" click_link "Create budget investment"
click_link "Create New Investment"
within(".account-info") do within(".account-info") do
expect(page).to have_content "Identified as" expect(page).to have_content "Identified as"
expect(page).to have_content "#{user.username}" expect(page).to have_content user.username
expect(page).to have_content "#{user.email}" expect(page).to have_content user.email
expect(page).to have_content "#{user.document_number}" expect(page).to have_content user.document_number
end end
select "2016 - Whole city - Health", from: 'budget_investment_heading_id' select "Whole city: Health", from: 'budget_investment_heading_id'
fill_in 'budget_investment_title', with: 'Build a park in my neighborhood' fill_in 'budget_investment_title', with: 'Build a park in my neighborhood'
fill_in 'budget_investment_description', with: 'There is no parks here...' fill_in 'budget_investment_description', with: 'There is no parks here...'
fill_in 'budget_investment_external_url', with: 'http://moarparks.com' fill_in 'budget_investment_external_url', with: 'http://moarparks.com'
@@ -42,9 +43,7 @@ feature 'Budget Investments' do
expect(page).to have_content 'There is no parks here...' expect(page).to have_content 'There is no parks here...'
expect(page).to have_content 'http://moarparks.com' expect(page).to have_content 'http://moarparks.com'
expect(page).to have_content user.name expect(page).to have_content user.name
expect(page).to have_content I18n.l(Budget::Investment.last.created_at.to_date) expect(page).to have_content I18n.l(@budget.created_at.to_date)
expect(current_path).to eq(management_budgets_investment_path(Budget::Investment.last))
end end
scenario "Should not allow unverified users to create budget investments" do scenario "Should not allow unverified users to create budget investments" do
@@ -70,14 +69,14 @@ feature 'Budget Investments' do
fill_in "search", with: "what you got" fill_in "search", with: "what you got"
click_button "Search" click_button "Search"
expect(current_path).to eq(management_budgets_investments_path) expect(current_path).to eq(management_budget_investments_path)
within("#budget-investments") do within("#budget-investments") do
expect(page).to have_css('.budget-investment', count: 1) expect(page).to have_css('.budget-investment', count: 1)
expect(page).to have_content(budget_investment1.title) expect(page).to have_content(budget_investment1.title)
expect(page).to_not have_content(budget_investment2.title) expect(page).to_not have_content(budget_investment2.title)
expect(page).to have_css("a[href='#{management_budgets_investment_path(budget_investment1)}']", text: budget_investment1.title) expect(page).to have_css("a[href='#{management_budget_investment_path(budget_investment1)}']", text: budget_investment1.title)
expect(page).to have_css("a[href='#{management_budgets_investment_path(budget_investment1)}']", text: budget_investment1.description) expect(page).to have_css("a[href='#{management_budget_investment_path(budget_investment1)}']", text: budget_investment1.description)
end end
end end
@@ -93,14 +92,14 @@ feature 'Budget Investments' do
fill_in "search", with: "Area 52" fill_in "search", with: "Area 52"
click_button "Search" click_button "Search"
expect(current_path).to eq(management_budgets_investments_path) expect(current_path).to eq(management_budget_investments_path)
within("#budget-investments") do within("#budget-investments") do
expect(page).to have_css('.budget-investment', count: 1) expect(page).to have_css('.budget-investment', count: 1)
expect(page).to_not have_content(budget_investment1.title) expect(page).to_not have_content(budget_investment1.title)
expect(page).to have_content(budget_investment2.title) expect(page).to have_content(budget_investment2.title)
expect(page).to have_css("a[href='#{management_budgets_investment_path(budget_investment2)}']", text: budget_investment2.title) expect(page).to have_css("a[href='#{management_budget_investment_path(budget_investment2)}']", text: budget_investment2.title)
expect(page).to have_css("a[href='#{management_budgets_investment_path(budget_investment2)}']", text: budget_investment2.description) expect(page).to have_css("a[href='#{management_budget_investment_path(budget_investment2)}']", text: budget_investment2.description)
end end
end end
end end
@@ -114,7 +113,7 @@ feature 'Budget Investments' do
click_link "Support Budget Investments" click_link "Support Budget Investments"
expect(current_path).to eq(management_budgets_investments_path) expect(current_path).to eq(management_budget_investments_path)
within(".account-info") do within(".account-info") do
expect(page).to have_content "Identified as" expect(page).to have_content "Identified as"
@@ -125,10 +124,10 @@ feature 'Budget Investments' do
within("#budget-investments") do within("#budget-investments") do
expect(page).to have_css('.budget-investment', count: 2) expect(page).to have_css('.budget-investment', count: 2)
expect(page).to have_css("a[href='#{management_budgets_investment_path(budget_investment1)}']", text: budget_investment1.title) expect(page).to have_css("a[href='#{management_budget_investment_path(budget_investment1)}']", text: budget_investment1.title)
expect(page).to have_css("a[href='#{management_budgets_investment_path(budget_investment1)}']", text: budget_investment1.description) expect(page).to have_css("a[href='#{management_budget_investment_path(budget_investment1)}']", text: budget_investment1.description)
expect(page).to have_css("a[href='#{management_budgets_investment_path(budget_investment2)}']", text: budget_investment2.title) expect(page).to have_css("a[href='#{management_budget_investment_path(budget_investment2)}']", text: budget_investment2.title)
expect(page).to have_css("a[href='#{management_budgets_investment_path(budget_investment2)}']", text: budget_investment2.description) expect(page).to have_css("a[href='#{management_budget_investment_path(budget_investment2)}']", text: budget_investment2.description)
end end
end end
@@ -148,7 +147,7 @@ feature 'Budget Investments' do
expect(page).to have_content "1 support" expect(page).to have_content "1 support"
expect(page).to have_content "You have already supported this. Share it!" expect(page).to have_content "You have already supported this. Share it!"
end end
expect(current_path).to eq(management_budgets_investments_path) expect(current_path).to eq(management_budget_investments_path)
end end
scenario 'Voting budget investments on behalf of someone in show view', :js do scenario 'Voting budget investments on behalf of someone in show view', :js do
@@ -166,7 +165,7 @@ feature 'Budget Investments' do
find('.js-in-favor a').click find('.js-in-favor a').click
expect(page).to have_content "1 support" expect(page).to have_content "1 support"
expect(page).to have_content "You have already supported this. Share it!" expect(page).to have_content "You have already supported this. Share it!"
expect(current_path).to eq(management_budgets_investment_path(budget_investment)) expect(current_path).to eq(management_budget_investment_path(budget_investment))
end end
scenario "Should not allow unverified users to vote proposals" do scenario "Should not allow unverified users to vote proposals" do