Add advanced filters for Admin::Budget::Investment

This commit is contained in:
Angel Perez
2018-01-18 20:50:16 -04:00
parent 7fef4190fa
commit e5c73d8f02
7 changed files with 82 additions and 18 deletions

View File

@@ -15,6 +15,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
def index
respond_to do |format|
format.html
format.js { render layout: false }
format.csv do
send_data Budget::Investment.to_csv(@investments, headers: true),
filename: 'budget_investments.csv'

View File

@@ -1,6 +1,7 @@
require 'csv'
class Budget
class Investment < ActiveRecord::Base
require 'csv'
include Rails.application.routes.url_helpers
include Measurable
include Sanitizable
@@ -53,7 +54,7 @@ class Budget
scope :valuation_open, -> { where(valuation_finished: false) }
scope :without_admin, -> { valuation_open.where(administrator_id: nil) }
scope :managed, -> { valuation_open.where(valuator_assignments_count: 0).where("administrator_id IS NOT ?", nil) }
scope :valuating, -> { valuation_open.where("valuator_assignments_count > 0 AND valuation_finished = ?", false) }
scope :valuating, -> { valuation_open.where("valuator_assignments_count > 0") }
scope :valuation_finished, -> { where(valuation_finished: true) }
scope :valuation_finished_feasible, -> { where(valuation_finished: true, feasibility: "feasible") }
scope :feasible, -> { where(feasibility: "feasible") }
@@ -86,20 +87,42 @@ class Budget
end
def self.filter_params(params)
params.select{|x, _| %w{heading_id group_id administrator_id tag_name valuator_id}.include? x.to_s }
params.select{ |x, _| %w{heading_id group_id administrator_id tag_name valuator_id}.include?(x.to_s) }
end
def self.scoped_filter(params, current_filter)
results = Investment.where(budget_id: params[:budget_id])
budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id])
results = Investment.where(budget_id: budget.id)
results = limit_results(budget, params, results) if params[:max_per_heading].present?
results = results.where(group_id: params[:group_id]) if params[:group_id].present?
results = results.by_heading(params[:heading_id]) if params[:heading_id].present?
results = results.by_admin(params[:administrator_id]) if params[:administrator_id].present?
results = results.by_tag(params[:tag_name]) if params[:tag_name].present?
results = results.by_heading(params[:heading_id]) if params[:heading_id].present?
results = results.by_valuator(params[:valuator_id]) if params[:valuator_id].present?
results = results.by_admin(params[:administrator_id]) if params[:administrator_id].present?
# Advanced filters
results = results.valuation_finished_feasible if params[:second_filter] == 'feasible'
results = results.where(selected: true) if params[:second_filter] == 'selected'
results = results.undecided if params[:second_filter] == 'undecided'
results = results.unfeasible if params[:second_filter] == 'unfeasible'
results = results.send(current_filter) if current_filter.present?
results.includes(:heading, :group, :budget, administrator: :user, valuators: :user)
end
def self.limit_results(budget, params, results)
max_per_heading = params[:max_per_heading].to_i
return results if max_per_heading <= 0
ids = []
budget.headings.pluck(:id).each do |hid|
ids += Investment.where(heading_id: hid).order(confidence_score: :desc).limit(max_per_heading).pluck(:id)
end
results.where("budget_investments.id IN (?)", ids)
end
def searchable_values
{ title => 'A',
author.username => 'B',

View File

@@ -0,0 +1,27 @@
<div id="advanced-filters" class="callout primary">
<%= form_tag(admin_budget_budget_investments_path(budget: @budget,
filter: params[:filter],
second_filter: params[:second_filter],
max_per_heading: params[:max_per_heading],
page: 1), method: :get, remote: true, enforce_utf8: false) do %>
<%= check_box_tag :second_filter, "feasible" %>
<%= t("#{i18n_namespace}.filters.feasible") %>
<%= check_box_tag :second_filter, "selected" %>
<%= t("#{i18n_namespace}.filters.selected") %>
<%= check_box_tag :second_filter, "undecided" %>
<%= t("#{i18n_namespace}.filters.undecided") %>
<%= check_box_tag :second_filter, "unfeasible" %>
<%= t("#{i18n_namespace}.filters.unfeasible") %>
<div class="large-1">
<%= text_field_tag :max_per_heading %>
<%= t("#{i18n_namespace}.filters.max_per_heading") %>
</div>
<%= submit_tag t("#{i18n_namespace}.filters.button"), class: "button small float-right" %>
<% end %>
</div>

View File

@@ -1,21 +1,21 @@
<h2 class="inline-block"><%= @budget.name %> - <%= t("admin.budget_investments.index.title") %></h2>
<div class="row margin">
<%= form_tag admin_budget_budget_investments_path(budget: @budget), method: :get, enforce_utf8: false do %>
<%= form_tag(admin_budget_budget_investments_path(budget: @budget), method: :get, enforce_utf8: false) do %>
<div class="small-12 medium-3 column">
<%= select_tag :administrator_id,
options_for_select(admin_select_options, params[:administrator_id]),
{ prompt: t("admin.budget_investments.index.administrator_filter_all"),
label: false,
class: "js-submit-on-change" } %>
options_for_select(admin_select_options, params[:administrator_id]),
{ prompt: t("admin.budget_investments.index.administrator_filter_all"),
label: false,
class: "js-submit-on-change" } %>
</div>
<div class="small-12 medium-3 column">
<%= select_tag :valuator_id,
options_for_select(valuator_select_options, params[:valuator_id]),
{ prompt: t("admin.budget_investments.index.valuator_filter_all"),
label: false,
class: "js-submit-on-change" } %>
options_for_select(valuator_select_options, params[:valuator_id]),
{ prompt: t("admin.budget_investments.index.valuator_filter_all"),
label: false,
class: "js-submit-on-change" } %>
</div>
<div class="small-12 medium-3 column">
@@ -36,8 +36,10 @@
<% end %>
</div>
<%= render '/shared/filter_subnav', i18n_namespace: "admin.budget_investments.index" %>
<%= render "advanced_filters", i18n_namespace: "admin.budget_investments.index" %>
<%= render "/shared/filter_subnav", i18n_namespace: "admin.budget_investments.index" %>
<div id="investments">
<%= render '/admin/budget_investments/investments' %>
<%= render "investments" %>
</div>

View File

@@ -0,0 +1 @@
$("#investments").html('<%= j render("admin/budget_investments/investments") %>');

View File

@@ -144,9 +144,14 @@ en:
valuating: Under valuation
valuation_finished: Valuation finished
valuation_finished_feasible: Val. fin. Feasible
feasible: Feasible
selected: Selected
undecided: Undecided
unfeasible: Unfeasible
max_per_heading: Max. supports per heading
winners: Winners
all: All
button: Filter
download_current_selection: "Download current selection"
no_budget_investments: "There are no investment projects."
title: Investment projects

View File

@@ -144,9 +144,14 @@ es:
valuating: En evaluación
valuation_finished: Evaluación finalizada
valuation_finished_feasible: Viables
selected: Seleccionadas
feasible: Viables
selected: Seleccionados
undecided: Sin decidir
unfeasible: Inviables
max_per_heading: Corte por partida
winners: Ganadoras
all: Todas
button: Filtrar
download_current_selection: "Descargar selección actual"
no_budget_investments: "No hay proyectos de gasto."
title: Proyectos de gasto