diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index dbaa91f99..7a57c8814 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -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' diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index da99db004..21cdab879 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -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', diff --git a/app/views/admin/budget_investments/_advanced_filters.html.erb b/app/views/admin/budget_investments/_advanced_filters.html.erb new file mode 100644 index 000000000..e9b68efa8 --- /dev/null +++ b/app/views/admin/budget_investments/_advanced_filters.html.erb @@ -0,0 +1,27 @@ +
+ <%= 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") %> + +
+ <%= text_field_tag :max_per_heading %> + <%= t("#{i18n_namespace}.filters.max_per_heading") %> +
+ + <%= submit_tag t("#{i18n_namespace}.filters.button"), class: "button small float-right" %> + <% end %> +
diff --git a/app/views/admin/budget_investments/index.html.erb b/app/views/admin/budget_investments/index.html.erb index e86402fd5..6deafed19 100644 --- a/app/views/admin/budget_investments/index.html.erb +++ b/app/views/admin/budget_investments/index.html.erb @@ -1,21 +1,21 @@

<%= @budget.name %> - <%= t("admin.budget_investments.index.title") %>

- <%= 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 %>
<%= 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" } %>
<%= 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" } %>
@@ -36,8 +36,10 @@ <% end %>
-<%= 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" %>
- <%= render '/admin/budget_investments/investments' %> + <%= render "investments" %>
diff --git a/app/views/admin/budget_investments/index.js.erb b/app/views/admin/budget_investments/index.js.erb new file mode 100644 index 000000000..dc3a8d67a --- /dev/null +++ b/app/views/admin/budget_investments/index.js.erb @@ -0,0 +1 @@ +$("#investments").html('<%= j render("admin/budget_investments/investments") %>'); diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 16f599c8b..b6918bc8d 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -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 diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 07fe9340a..c3bc13a0d 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -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