Merge pull request #2322 from consul/current_budget

Change concept of current budget to account for multiple budgets
This commit is contained in:
Raimond Garcia
2018-01-16 16:53:10 +01:00
committed by GitHub
12 changed files with 110 additions and 50 deletions

View File

@@ -2,7 +2,7 @@ class Admin::BudgetsController < Admin::BaseController
include FeatureFlags
feature_flag :budgets
has_filters %w{current finished}, only: :index
has_filters %w{open finished}, only: :index
load_and_authorize_resource

View File

@@ -25,6 +25,7 @@ class ApplicationController < ActionController::Base
layout :set_layout
respond_to :html
helper_method :current_budget
private
@@ -120,4 +121,8 @@ class ApplicationController < ActionController::Base
params[:filter] ||= "selected"
end
end
def current_budget
Budget.current
end
end

View File

@@ -19,7 +19,7 @@ class Management::BudgetsController < Management::BaseController
end
def print_investments
@budgets = Budget.current.order(created_at: :desc).page(params[:page])
@budget = Budget.current
end
private

View File

@@ -5,10 +5,10 @@ class Valuation::BudgetsController < Valuation::BaseController
load_and_authorize_resource
def index
@budgets = @budgets.current.order(created_at: :desc).page(params[:page])
@budget = Budget.current
if @budget.present?
@investments_with_valuation_open = {}
@budgets.each do |b|
@investments_with_valuation_open[b.id] = b.investments
@investments_with_valuation_open = @budget.investments
.by_valuator(current_user.valuator.try(:id))
.valuation_open
.count

View File

@@ -30,8 +30,11 @@ class Budget < ActiveRecord::Base
scope :balloting, -> { where(phase: "balloting") }
scope :reviewing_ballots, -> { where(phase: "reviewing_ballots") }
scope :finished, -> { where(phase: "finished") }
scope :open, -> { where.not(phase: "finished") }
scope :current, -> { where.not(phase: "finished") }
def self.current
where.not(phase: "drafting").last
end
def description
send("description_#{phase}").try(:html_safe)
@@ -93,10 +96,6 @@ class Budget < ActiveRecord::Base
balloting_process? || finished?
end
def current?
!finished?
end
def heading_price(heading)
heading_ids.include?(heading.id) ? heading.price : -1
end

View File

@@ -1,12 +1,10 @@
<table>
<% @budgets.each do |budget| %>
<tr id="<%= dom_id(budget) %>">
<td><%= budget.name %></td>
<td><%= budget.translated_phase %></td>
<tr id="<%= dom_id(@budget) %>">
<td><%= @budget.name %></td>
<td><%= @budget.translated_phase %></td>
<td align="right">
<%= link_to t("management.budgets.print_investments"),
print_management_budget_investments_path(budget) %>
print_management_budget_investments_path(@budget) %>
</td>
</tr>
<% end %>
</table>

View File

@@ -1,7 +1,5 @@
<h2 class="inline-block"><%= t("valuation.budgets.index.title") %></h2>
<h3><%= page_entries_info @budgets %></h3>
<table>
<thead>
<tr>
@@ -12,25 +10,21 @@
</tr>
</thead>
<tbody>
<% @budgets.each do |budget| %>
<tr id="<%= dom_id(budget) %>" class="budget">
<tr id="<%= dom_id(@budget) %>" class="budget">
<td>
<%= budget.name %>
<%= @budget.name %>
</td>
<td>
<%= t("budgets.phase.#{budget.phase}") %>
<%= t("budgets.phase.#{@budget.phase}") %>
</td>
<td>
<%= @investments_with_valuation_open[budget.id] %>
<%= @investments_with_valuation_open %>
</td>
<td>
<%= link_to t("valuation.budgets.index.evaluate"),
valuation_budget_budget_investments_path(budget_id: budget.id),
valuation_budget_budget_investments_path(budget_id: @budget.id),
class: "button hollow expanded" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= paginate @budgets %>

View File

@@ -0,0 +1,19 @@
require 'rails_helper'
describe ApplicationController do
describe "#current_budget" do
it "returns the last budget that is not in draft phase" do
old_budget = create(:budget, phase: "finished", created_at: 2.years.ago)
previous_budget = create(:budget, phase: "accepting", created_at: 1.year.ago)
current_budget = create(:budget, phase: "accepting", created_at: 1.month.ago)
next_budget = create(:budget, phase: "drafting", created_at: 1.week.ago)
budget = subject.instance_eval{ current_budget }
expect(budget).to eq(current_budget)
end
end
end

View File

@@ -259,9 +259,10 @@ feature 'Budget Investments' do
context "Printing" do
scenario 'Printing budget investments' do
16.times { create(:budget_investment, budget: @budget) }
16.times { create(:budget_investment, budget: @budget, heading: @heading) }
click_link "Print Budget Investments"
expect(page).to have_content(@budget.name)
within "#budget_#{@budget.id}" do
click_link "Print Budget Investments"
@@ -273,15 +274,17 @@ feature 'Budget Investments' do
scenario "Filtering budget investments by heading to be printed", :js do
district_9 = create(:budget_heading, group: @group, name: "District Nine")
another_heading = create(:budget_heading, group: @group)
low_investment = create(:budget_investment, budget: @budget, title: 'Nuke district 9', heading: district_9, cached_votes_up: 1)
mid_investment = create(:budget_investment, budget: @budget, title: 'Change district 9', heading: district_9, cached_votes_up: 10)
top_investment = create(:budget_investment, budget: @budget, title: 'Destroy district 9', heading: district_9, cached_votes_up: 100)
unvoted_investment = create(:budget_investment, budget: @budget, title: 'Add new districts to the city')
unvoted_investment = create(:budget_investment, budget: @budget, heading: another_heading, title: 'Add new districts to the city')
user = create(:user, :level_two)
login_managed_user(user)
click_link "Print Budget Investments"
expect(page).to have_content(@budget.name)
within "#budget_#{@budget.id}" do
click_link "Print Budget Investments"

View File

@@ -24,18 +24,15 @@ feature 'Valuation budgets' do
end
scenario 'Filters by phase' do
budget1 = create(:budget)
budget2 = create(:budget, :accepting)
budget3 = create(:budget, :selecting)
budget4 = create(:budget, :balloting)
budget5 = create(:budget, :finished)
budget1 = create(:budget, :finished)
budget2 = create(:budget, :finished)
budget3 = create(:budget, :accepting)
visit valuation_budgets_path
expect(page).to have_content(budget1.name)
expect(page).to have_content(budget2.name)
expect(page).to_not have_content(budget1.name)
expect(page).to_not have_content(budget2.name)
expect(page).to have_content(budget3.name)
expect(page).to have_content(budget4.name)
expect(page).not_to have_content(budget5.name)
end
end

View File

@@ -66,6 +66,8 @@ feature 'Valuation' do
scenario 'Access as a valuator is authorized' do
create(:valuator, user: user)
create(:budget)
login_as(user)
visit root_path
@@ -78,6 +80,8 @@ feature 'Valuation' do
scenario 'Access as an administrator is authorized' do
create(:administrator, user: user)
create(:budget)
login_as(user)
visit root_path
@@ -90,6 +94,8 @@ feature 'Valuation' do
scenario "Valuation access links" do
create(:valuator, user: user)
create(:budget)
login_as(user)
visit root_path
@@ -100,6 +106,8 @@ feature 'Valuation' do
scenario 'Valuation dashboard' do
create(:valuator, user: user)
create(:budget)
login_as(user)
visit root_path

View File

@@ -98,6 +98,43 @@ describe Budget do
end
end
describe "#current" do
it "returns nil if there is only one budget and it is still in drafting phase" do
budget = create(:budget, phase: "drafting")
expect(Budget.current).to eq(nil)
end
it "returns the budget if there is only one and not in drafting phase" do
budget = create(:budget, phase: "accepting")
expect(Budget.current).to eq(budget)
end
it "returns the last budget created that is not in drafting phase" do
old_budget = create(:budget, phase: "finished", created_at: 2.years.ago)
previous_budget = create(:budget, phase: "accepting", created_at: 1.year.ago)
current_budget = create(:budget, phase: "accepting", created_at: 1.month.ago)
next_budget = create(:budget, phase: "drafting", created_at: 1.week.ago)
expect(Budget.current).to eq(current_budget)
end
end
describe "#open" do
it "returns all budgets that are not in the finished phase" do
phases = Budget::PHASES - ["finished"]
phases.each do |phase|
budget = create(:budget, phase: phase)
expect(Budget.open).to include(budget)
end
end
end
describe "heading_price" do
let(:budget) { create(:budget) }
let(:group) { create(:budget_group, budget: budget) }