Merge pull request #1084 from consul/spending-proposals-summary

Spending proposals admin summary
This commit is contained in:
Juanjo Bazán
2016-04-25 11:29:07 +02:00
17 changed files with 147 additions and 8 deletions

View File

@@ -354,6 +354,18 @@ body.admin {
.investment-projects-list.medium-9 {
width: 100%;
}
.investment-projects-summary {
th {
text-align: center;
width: 33%;
&:first-child {
text-align: left;
}
}
}
}
.admin-content .select-geozone {

View File

@@ -28,6 +28,10 @@ class Admin::SpendingProposalsController < Admin::BaseController
end
end
def summary
@spending_proposals = SpendingProposal.for_summary.group(:geozone).sum(:price)
end
private
def spending_proposal_params

View File

@@ -13,4 +13,13 @@ module SpendingProposalsHelper
spending_proposal_path(spending_proposal, options)
end
end
def spending_proposal_count_for_geozone(geozone)
if geozone.present?
geozone.spending_proposals.for_summary.count
else
SpendingProposal.where(geozone: nil).for_summary.count
end
end
end

View File

@@ -37,7 +37,7 @@ module Abilities
can :manage, Annotation
can [:read, :update, :destroy], SpendingProposal
can [:read, :update, :destroy, :summary], SpendingProposal
end
end
end

View File

@@ -1,7 +1,9 @@
class Geozone < ActiveRecord::Base
has_many :spending_proposals
validates :name, presence: true
def self.names
Geozone.pluck(:name)
end
end

View File

@@ -135,4 +135,8 @@ class SpendingProposal < ActiveRecord::Base
self.responsible_name = author.try(:document_number) if author.try(:document_number).present?
end
def self.for_summary
valuation_finished.feasible
end
end

View File

@@ -1,4 +1,8 @@
<h2><%= t("admin.spending_proposals.index.title") %></h2>
<%= link_to t("admin.spending_proposals.index.summary_link"),
summary_admin_spending_proposals_path,
class: "button float-right" %>
<h2 class="inline-block"><%= t("admin.spending_proposals.index.title") %></h2>
<div class="row margin">
<%= form_tag admin_spending_proposals_path, method: :get, enforce_utf8: false do %>

View File

@@ -0,0 +1,20 @@
<%= link_to admin_spending_proposals_path, class: "back" do %>
<span class="icon-angle-left"></span>
<%= t("shared.back") %>
<% end %>
<h2><%= t("admin.spending_proposals.summary.title") %></h2>
<table id="spending_proposals" class="investment-projects-summary">
<th><%= t("admin.spending_proposals.summary.geozone_name") %></th>
<th><%= t("admin.spending_proposals.summary.count_for_geozone") %></th>
<th><%= t("admin.spending_proposals.summary.cost_for_geozone") %></th>
<% @spending_proposals.each do |geozone, price| %>
<tr id="<%= geozone.present? ? dom_id(geozone) : 'geozone_all_city' %>">
<td class="name"><%= geozone.present? ? geozone.name : t("geozones.none") %></td>
<td class="proposals-count text-center"><%= spending_proposal_count_for_geozone(geozone) %></td>
<td class="total-price text-center"><%= number_to_currency(price) %></td>
</tr>
<% end %>
</table>

View File

@@ -1,8 +1,3 @@
<% # Params:
#
# i18n_namespace: for example "moderation.debates.index"
%>
<ul class="menu simple clear">
<li class="sr-only"><%= t("#{i18n_namespace}.filter") %>: </li>

View File

@@ -167,6 +167,7 @@ en:
assigned_admin: Assigned administrator
no_admin_assigned: No admin assigned
no_valuators_assigned: No valuators assigned
summary_link: "Summary"
show:
assigned_admin: Assigned administrator
assigned_valuators: Assigned valuators
@@ -190,6 +191,11 @@ en:
tags: Tags
tags_placeholder: "Write the tags you want separated by commas (,)"
undefined: Undefined
summary:
title: Summary for feasible and finished investment projects
geozone_name: Scope
count_for_geozone: Count
cost_for_geozone: Cost
stats:
show:
stats_title: Stats

View File

@@ -167,6 +167,7 @@ es:
assigned_admin: Administrador asignado
no_admin_assigned: Sin admin asignado
no_valuators_assigned: Sin evaluador
summary_link: "Resumen"
show:
assigned_admin: Administrador asignado
assigned_valuators: Evaluadores asignados
@@ -190,6 +191,11 @@ es:
tags: Etiquetas
tags_placeholder: "Escribe las etiquetas que desees separadas por comas (,)"
undefined: Sin definir
summary:
title: Resumen de propuestas viables y finalizadas
geozone_name: Ámbito de ciudad
count_for_geozone: Número de propuestas
cost_for_geozone: Coste total
stats:
show:
stats_title: Estadísticas

View File

@@ -362,6 +362,7 @@ en:
to: 'To'
author_info:
author_deleted: User deleted
back: Back
check: Select
check_all: All
check_none: None

View File

@@ -362,6 +362,7 @@ es:
to: 'Hasta'
author_info:
author_deleted: Usuario eliminado
back: Volver
check: Seleccionar
check_all: Todos
check_none: Ninguno

View File

@@ -135,6 +135,8 @@ Rails.application.routes.draw do
patch :assign_admin
patch :assign_valuators
end
get :summary, on: :collection
end
resources :comments, only: :index do

View File

@@ -266,17 +266,24 @@ puts "Creating Spending Proposals"
tags = Faker::Lorem.words(10)
(1..30).each do |i|
(1..60).each do |i|
geozone = Geozone.reorder("RANDOM()").first
author = User.reorder("RANDOM()").first
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
feasible_explanation = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
valuation_finished = [true, false].sample
feasible = [true, false].sample
spending_proposal = SpendingProposal.create!(author: author,
title: Faker::Lorem.sentence(3).truncate(60),
external_url: Faker::Internet.url,
description: description,
created_at: rand((Time.now - 1.week) .. Time.now),
geozone: [geozone, nil].sample,
feasible: feasible,
feasible_explanation: feasible_explanation,
valuation_finished: valuation_finished,
tag_list: tags.sample(3).join(','),
price: rand(1000000),
terms_of_service: "1")
puts " #{spending_proposal.title}"
end

View File

@@ -425,4 +425,47 @@ feature 'Admin spending proposals' do
end
context 'Summary' do
scenario "Diplays summary for every geozone" do
california = create(:geozone)
new_york = create(:geozone)
washington = create(:geozone)
proposal1 = create(:spending_proposal, title: "Build a highway", price: '10000000', geozone: nil, feasible: true, valuation_finished: true)
proposal1 = create(:spending_proposal, title: "Build a university", price: '5000000', geozone: nil, feasible: true, valuation_finished: true)
proposal3 = create(:spending_proposal, title: "Build a hospital", price: '1000000', geozone: california, feasible: true, valuation_finished: true)
proposal4 = create(:spending_proposal, title: "Build a school", price: '500000', geozone: california, feasible: true, valuation_finished: true)
proposal5 = create(:spending_proposal, title: "Plant more trees", price: '30000', geozone: new_york, feasible: true, valuation_finished: true)
proposal6 = create(:spending_proposal, title: "Destroy the seas", price: '999999', geozone: washington, feasible: false, valuation_finished: true)
visit admin_spending_proposals_path
click_link "Summary"
expect(page).to have_content "Summary for feasible and finished investment projects"
within("#geozone_all_city") do
expect(page).to have_css(".name", text: "All city")
expect(page).to have_css(".proposals-count", text: 2)
expect(page).to have_css(".total-price", text: "$15,000,000")
end
within("#geozone_#{california.id}") do
expect(page).to have_css(".name", text: california.name)
expect(page).to have_css(".proposals-count", text: 2)
expect(page).to have_css(".total-price", text: "$1,500,000")
end
within("#geozone_#{new_york.id}") do
expect(page).to have_css(".name", text: new_york.name)
expect(page).to have_css(".proposals-count", text: 1)
expect(page).to have_css(".total-price", text: '$30,000')
end
expect(page).to_not have_content washington.name
end
end
end

View File

@@ -344,5 +344,28 @@ describe SpendingProposal do
end
end
describe "#for_summary" do
it "returns only feasible and valuation finished proposals" do
sp1 = create(:spending_proposal, feasible: true, valuation_finished: true)
sp2 = create(:spending_proposal, feasible: true, valuation_finished: true)
sp3 = create(:spending_proposal, feasible: false, valuation_finished: false)
expect(SpendingProposal.for_summary).to include(sp1)
expect(SpendingProposal.for_summary).to include(sp2)
expect(SpendingProposal.for_summary).to_not include(sp3)
end
it "does not return unfeasible proposals" do
sp = create(:spending_proposal, feasible: false, valuation_finished: true)
expect(SpendingProposal.for_summary).to_not include(sp)
end
it "does not return proposals pending valuation" do
sp = create(:spending_proposal, feasible: true, valuation_finished: false)
expect(SpendingProposal.for_summary).to_not include(sp)
end
end
end