Add logic to handle budget investments with an execution process

This commit is contained in:
María Checa
2018-07-24 17:53:08 +02:00
committed by Javi Martín
parent 406bbb4cdb
commit d089cc14a5
5 changed files with 51 additions and 48 deletions

View File

@@ -7,14 +7,19 @@ module Budgets
def show
authorize! :read_executions, @budget
@statuses = ::Budget::Investment::Status.all
@headings = @budget.headings
.includes(investments: :milestones)
.joins(investments: :milestones)
.distinct
.order(name: :asc)
if params[:status].present?
@headings = @headings.where(filter_investment_by_latest_milestone, params[:status])
@investments_by_heading = @budget.investments.winners
.joins(:milestones).includes(:milestones)
.select { |i| i.milestones.published.with_status
.order_by_publication_date.last
.status_id == params[:status].to_i }
.uniq
.group_by(&:heading)
else
@investments_by_heading = @budget.investments.winners
.joins(:milestones).includes(:milestones)
.distinct.group_by(&:heading)
end
@headings = reorder_alphabetically_with_city_heading_first(@headings)
@@ -25,24 +30,5 @@ module Budgets
def load_budget
@budget = Budget.find_by(slug: params[:id]) || Budget.find_by(id: params[:id])
end
def filter_investment_by_latest_milestone
<<-SQL
(SELECT status_id FROM budget_investment_milestones
WHERE investment_id = budget_investments.id ORDER BY publication_date DESC LIMIT 1) = ?
SQL
end
def reorder_alphabetically_with_city_heading_first(headings)
headings.sort do |a, b|
if a.name == 'Toda la ciudad'
-1
elsif b.name == 'Toda la ciudad'
1
else
a.name <=> b.name
end
end
end
end
end

View File

@@ -18,6 +18,8 @@ class Budget
validate :description_or_status_present?
scope :order_by_publication_date, -> { order(publication_date: :asc) }
scope :published, -> { where("publication_date <= ?", Date.today) }
scope :with_status, -> { where("status_id IS NOT NULL") }
def self.title_max_length
80

View File

@@ -1,9 +1,9 @@
<% @headings.each do |heading| %>
<% @investments_by_heading.each do |heading, investments| %>
<h4 id="<%= heading.name.parameterize %>">
<%= heading.name %>
<%= heading.name %> (<%= investments.count %>)
</h4>
<div class="row" data-equalizer-on="medium" data-equalizer>
<% heading.investments.each do |investment| %>
<% investments.each do |investment| %>
<div class="small-12 medium-6 large-4 column end margin-bottom">
<div class="budget-execution">
<%= link_to budget_investment_path(@budget, investment, anchor: "tab-milestones"), data: { 'equalizer-watch': true } do %>

View File

@@ -44,7 +44,7 @@
<%= t("budgets.executions.heading_selection_title") %>
</h3>
<ul class="menu vertical no-margin-top no-padding-top">
<% @headings.each do |heading| %>
<% @investments_by_heading.each_pair do |heading, investments| %>
<li>
<%= link_to heading.name, "#" + heading.name.parameterize %>
</li>
@@ -56,13 +56,14 @@
<%= form_tag(budget_executions_path(@budget), method: :get) do %>
<div class="small-12 medium-3 column">
<%= label_tag t("budgets.executions.filters.label") %>
<%= select_tag :status, options_from_collection_for_select(
@statuses, :id, :name, params[:status]
), class: "js-submit-on-change", prompt: t("budgets.executions.filters.all") %>
<%= select_tag :status,
options_from_collection_for_select(@statuses, :id, :name, params[:status]),
class: "js-submit-on-change",
prompt: t("budgets.executions.filters.all") %>
</div>
<% end %>
<% if @headings.any? %>
<% if @investments_by_heading.any? %>
<%= render 'budgets/executions/investments' %>
<% else %>
<div class="callout primary clear">

View File

@@ -4,12 +4,12 @@ feature 'Executions' do
let(:budget) { create(:budget, phase: 'finished') }
let(:group) { create(:budget_group, budget: budget) }
let(:heading) { create(:budget_heading, group: group, price: 1000) }
let(:heading) { create(:budget_heading, group: group) }
let!(:investment1) { create(:budget_investment, :winner, heading: heading, price: 200, ballot_lines_count: 900) }
let!(:investment2) { create(:budget_investment, :winner, heading: heading, price: 300, ballot_lines_count: 800) }
let!(:investment3) { create(:budget_investment, :incompatible, heading: heading, price: 500, ballot_lines_count: 700) }
let!(:investment4) { create(:budget_investment, :winner, heading: heading, price: 600, ballot_lines_count: 600) }
let!(:investment1) { create(:budget_investment, :winner, heading: heading) }
let!(:investment2) { create(:budget_investment, :winner, heading: heading) }
let!(:investment4) { create(:budget_investment, :winner, heading: heading) }
let!(:investment3) { create(:budget_investment, :incompatible, heading: heading) }
scenario 'only displays investments with milestones' do
create(:budget_investment_milestone, investment: investment1)
@@ -101,10 +101,10 @@ feature 'Executions' do
scenario "renders last milestone's image if investment has multiple milestones with images associated" do
milestone1 = create(:budget_investment_milestone, investment: investment1,
publication_date: Date.yesterday)
publication_date: 2.weeks.ago)
milestone2 = create(:budget_investment_milestone, investment: investment1,
publication_date: Date.tomorrow)
publication_date: Date.yesterday)
create(:image, imageable: milestone1, title: 'First milestone image')
create(:image, imageable: milestone2, title: 'Second milestone image')
@@ -155,6 +155,26 @@ feature 'Executions' do
end
scenario 'are based on latest milestone status', :js do
create(:budget_investment_milestone, investment: investment1,
publication_date: 1.month.ago,
status: status1)
create(:budget_investment_milestone, investment: investment1,
publication_date: Date.yesterday,
status: status2)
visit budget_path(budget)
click_link 'See results'
click_link 'Milestones'
select 'Studying the project', from: 'status'
expect(page).not_to have_content(investment1.title)
select 'Bidding', from: 'status'
expect(page).to have_content(investment1.title)
end
scenario 'milestones with future dates are not shown', :js do
create(:budget_investment_milestone, investment: investment1,
publication_date: Date.yesterday,
status: status1)
@@ -164,20 +184,14 @@ feature 'Executions' do
status: status2)
visit budget_path(budget)
click_link 'See results'
click_link 'Milestones'
expect(page).to have_content(investment1.title)
select 'Studying the project', from: 'status'
expect(page).not_to have_content(investment1.title)
expect(page).to have_content(investment1.title)
select 'Bidding', from: 'status'
expect(page).to have_content(investment1.title)
expect(page).not_to have_content('No winner investments for this heading')
expect(page).not_to have_content(investment1.title)
end
end