diff --git a/app/controllers/admin/budgets_controller.rb b/app/controllers/admin/budgets_controller.rb index d2f8706c0..7dbd66d7b 100644 --- a/app/controllers/admin/budgets_controller.rb +++ b/app/controllers/admin/budgets_controller.rb @@ -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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 495a25314..f03a05239 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/management/budgets_controller.rb b/app/controllers/management/budgets_controller.rb index dd7259c99..470d1f9fd 100644 --- a/app/controllers/management/budgets_controller.rb +++ b/app/controllers/management/budgets_controller.rb @@ -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 diff --git a/app/controllers/valuation/budgets_controller.rb b/app/controllers/valuation/budgets_controller.rb index 744b0d4bf..cf99f95e3 100644 --- a/app/controllers/valuation/budgets_controller.rb +++ b/app/controllers/valuation/budgets_controller.rb @@ -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]) - @investments_with_valuation_open = {} - @budgets.each do |b| - @investments_with_valuation_open[b.id] = b.investments + @budget = Budget.current + if @budget.present? + @investments_with_valuation_open = {} + @investments_with_valuation_open = @budget.investments .by_valuator(current_user.valuator.try(:id)) .valuation_open .count diff --git a/app/models/budget.rb b/app/models/budget.rb index af1149d66..608599217 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -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 diff --git a/app/views/management/budgets/print_investments.html.erb b/app/views/management/budgets/print_investments.html.erb index bc115ea3d..d5be7c1ff 100644 --- a/app/views/management/budgets/print_investments.html.erb +++ b/app/views/management/budgets/print_investments.html.erb @@ -1,12 +1,10 @@ -<% @budgets.each do |budget| %> - - - + + + -<% end %>
<%= budget.name %><%= budget.translated_phase %>
<%= @budget.name %><%= @budget.translated_phase %> <%= link_to t("management.budgets.print_investments"), - print_management_budget_investments_path(budget) %> + print_management_budget_investments_path(@budget) %>
diff --git a/app/views/valuation/budgets/index.html.erb b/app/views/valuation/budgets/index.html.erb index 6d65f9ee6..f1fd302b6 100644 --- a/app/views/valuation/budgets/index.html.erb +++ b/app/views/valuation/budgets/index.html.erb @@ -1,7 +1,5 @@

<%= t("valuation.budgets.index.title") %>

-

<%= page_entries_info @budgets %>

- @@ -12,25 +10,21 @@ - <% @budgets.each do |budget| %> - - - - - - - <% end %> + + + + + +
- <%= budget.name %> - - <%= t("budgets.phase.#{budget.phase}") %> - - <%= @investments_with_valuation_open[budget.id] %> - - <%= link_to t("valuation.budgets.index.evaluate"), - valuation_budget_budget_investments_path(budget_id: budget.id), - class: "button hollow expanded" %> -
+ <%= @budget.name %> + + <%= t("budgets.phase.#{@budget.phase}") %> + + <%= @investments_with_valuation_open %> + + <%= link_to t("valuation.budgets.index.evaluate"), + valuation_budget_budget_investments_path(budget_id: @budget.id), + class: "button hollow expanded" %> +
- -<%= paginate @budgets %> diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb new file mode 100644 index 000000000..f11068b65 --- /dev/null +++ b/spec/controllers/application_controller_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/features/management/budget_investments_spec.rb b/spec/features/management/budget_investments_spec.rb index f1ced7334..6132c40cd 100644 --- a/spec/features/management/budget_investments_spec.rb +++ b/spec/features/management/budget_investments_spec.rb @@ -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" diff --git a/spec/features/valuation/budgets_spec.rb b/spec/features/valuation/budgets_spec.rb index ec3b780ab..4fd821c7e 100644 --- a/spec/features/valuation/budgets_spec.rb +++ b/spec/features/valuation/budgets_spec.rb @@ -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 diff --git a/spec/features/valuation_spec.rb b/spec/features/valuation_spec.rb index c004f0773..1cb95d23d 100644 --- a/spec/features/valuation_spec.rb +++ b/spec/features/valuation_spec.rb @@ -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 diff --git a/spec/models/budget_spec.rb b/spec/models/budget_spec.rb index 70c292962..f8ec2d5a5 100644 --- a/spec/models/budget_spec.rb +++ b/spec/models/budget_spec.rb @@ -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) }