Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add advanced filters to admin budget investments #1956

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/assets/stylesheets/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,11 @@ code {
.filter {
display: inline-block;
margin: 0 $line-height / 2;

label {
font-weight: normal;
margin: 0;
}
}

.button {
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/admin/budget_investments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController

feature_flag :budgets

has_orders %w{oldest}, only: [:show, :edit]
has_filters(%w{all without_admin without_valuator under_valuation
valuation_finished winners},
only: [:index, :toggle_selection])
has_orders %w[oldest], only: [:show, :edit]
has_filters %w[all], only: [:index, :toggle_selection]

before_action :load_budget
before_action :load_investment, only: [:show, :edit, :update, :toggle_selection]
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/admin/budgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def edit
def calculate_winners
return unless @budget.balloting_process?
@budget.headings.each { |heading| Budget::Result.new(@budget, heading).delay.calculate_winners }
redirect_to admin_budget_budget_investments_path(budget_id: @budget.id, filter: "winners"),
redirect_to admin_budget_budget_investments_path(
budget_id: @budget.id,
advanced_filters: ["winners"]),
notice: I18n.t("admin.budgets.winners.calculated")
end

Expand Down
8 changes: 7 additions & 1 deletion app/helpers/admin_budget_investments_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
module AdminBudgetInvestmentsHelper

def advanced_menu_visibility
(params[:advanced_filters].empty? && params["min_total_supports"].blank?) ? "hide" : ""
if params[:advanced_filters].empty? &&
params["min_total_supports"].blank? &&
params["max_total_supports"].blank?
"hide"
else
""
end
end

def init_advanced_menu
Expand Down
13 changes: 11 additions & 2 deletions app/models/budget/investment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def self.scoped_filter(params, current_filter)
results = Investment.by_budget(budget)

results = results.where("cached_votes_up + physical_votes >= ?",
params[:min_total_supports]) if params[:min_total_supports].present?
params[:min_total_supports]) if params[:min_total_supports].present?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [114/100] (https://github.com/bbatsov/ruby-style-guide#80-character-limits)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [114/110] (https://github.com/bbatsov/ruby-style-guide#80-character-limits)

results = results.where("cached_votes_up + physical_votes <= ?",
params[:max_total_supports]) if params[:max_total_supports].present?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [114/100] (https://github.com/bbatsov/ruby-style-guide#80-character-limits)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [114/110] (https://github.com/bbatsov/ruby-style-guide#80-character-limits)

results = results.where(group_id: params[:group_id]) if params[:group_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?
Expand All @@ -137,12 +139,19 @@ def self.scoped_filter(params, current_filter)
end

def self.advanced_filters(params, results)
results = results.without_admin if params[:advanced_filters].include?("without_admin")
results = results.without_valuator if params[:advanced_filters].include?("without_valuator")
results = results.under_valuation if params[:advanced_filters].include?("under_valuation")
results = results.valuation_finished if params[:advanced_filters].include?("valuation_finished")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [102/100] (https://github.com/bbatsov/ruby-style-guide#80-character-limits)

results = results.winners if params[:advanced_filters].include?("winners")

ids = []
ids += results.valuation_finished_feasible.pluck(:id) if params[:advanced_filters].include?("feasible")
ids += results.where(selected: true).pluck(:id) if params[:advanced_filters].include?("selected")
ids += results.undecided.pluck(:id) if params[:advanced_filters].include?("undecided")
ids += results.unfeasible.pluck(:id) if params[:advanced_filters].include?("unfeasible")
results.where("budget_investments.id IN (?)", ids)
results = results.where("budget_investments.id IN (?)", ids) if ids.any?
results
end

def self.order_filter(params)
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/budget_investments/_investments.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
admin_budget_budget_investments_path(csv_params),
class: "float-right small clear" %>

<% if params[:filter] == "winners" %>
<% if params[:advanced_filters].include?("winners") %>
<% if display_calculate_winners_button?(@budget) %>
<%= link_to calculate_winner_button_text(@budget),
calculate_winners_admin_budget_path(@budget),
Expand Down
18 changes: 13 additions & 5 deletions app/views/admin/budget_investments/_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@
<div id="advanced_filters" class="<%= advanced_menu_visibility %>" data-toggler=".hide">
<div class="small-12 column">
<div class="advanced-filters-content">
<% ["feasible", "selected", "undecided", "unfeasible"].each do |option| %>
<% %w[feasible selected undecided unfeasible without_admin without_valuator under_valuation
valuation_finished winners].each do |filter| %>
<div class="filter">
<%= check_box_tag "advanced_filters[]", option, params[:advanced_filters].index(option), id: "advanced_filters_#{option}" %>
<%= t("admin.budget_investments.index.filters.#{option}") %>
<%= check_box_tag "advanced_filters[]", filter, params[:advanced_filters].index(filter), id: "advanced_filters_#{filter}" %>
<%= label_tag "advanced_filters[#{filter}]", t("admin.budget_investments.index.filters.#{filter}") %>
</div>
<% end %>
<div class="filter">
<%= text_field_tag :min_total_supports, params["min_total_supports"], placeholder: t("admin.budget_investments.index.filters.min_total_supports") %>
<div>
<div class="filter">
<%= label_tag :min_total_supports, t("admin.budget_investments.index.filters.min_total_supports") %>
<%= text_field_tag :min_total_supports, params["min_total_supports"] %>
</div>
<div class="filter">
<%= label_tag :max_total_supports, t("admin.budget_investments.index.filters.max_total_supports") %>
<%= text_field_tag :max_total_supports, params["max_total_supports"] %>
</div>
</div>
</div>
</div>
Expand Down
27 changes: 14 additions & 13 deletions app/views/admin/budget_investments/_select_investment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,26 @@
filter: params[:filter],
sort_by: params[:sort_by],
min_total_supports: params[:min_total_supports],
max_total_supports: params[:max_total_supports],
advanced_filters: params[:advanced_filters],
page: params[:page]),
method: :patch,
remote: true,
class: "button small expanded" %>
<% elsif investment.feasible? && investment.valuation_finished? %>
<% unless investment.budget.finished? %>
<%= link_to t("admin.budget_investments.index.select"),
toggle_selection_admin_budget_budget_investment_path(@budget,
investment,
filter: params[:filter],
sort_by: params[:sort_by],
min_total_supports: params[:min_total_supports],
advanced_filters: params[:advanced_filters],
page: params[:page]),
method: :patch,
remote: true,
class: "button small hollow expanded" %>
<% end %>
<%= link_to_unless investment.budget.finished?,
t("admin.budget_investments.index.select"),
toggle_selection_admin_budget_budget_investment_path(@budget,
investment,
filter: params[:filter],
sort_by: params[:sort_by],
min_total_supports: params[:min_total_supports],
max_total_supports: params[:max_total_supports],
advanced_filters: params[:advanced_filters],
page: params[:page]),
method: :patch,
remote: true,
class: "button small hollow expanded" %>
<% end %>
</td>
<% if params[:filter] == "selected" %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ en:
undecided: Undecided
unfeasible: Unfeasible
min_total_supports: Minimum supports
max_total_supports: Maximum supports
winners: Winners
valuation_open: Open
one_filter_html: "Current applied filters: <b><em>%{filter}</em></b>"
Expand Down
1 change: 1 addition & 0 deletions config/locales/es/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ es:
undecided: Sin decidir
unfeasible: Inviables
min_total_supports: Apoyos mínimos
max_total_supports: Apoyos máximos
winners: Ganadores
one_filter_html: "Filtros en uso: <b><em>%{filter}</em></b>"
two_filters_html: "Filtros en uso: <b><em>%{filter}, %{advanced_filters}</em></b>"
Expand Down
16 changes: 10 additions & 6 deletions spec/factories/budgets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,17 @@
valuation_finished true
end

trait :hidden do
hidden_at { Time.current }
end
trait :hidden do
hidden_at { Time.current }
end

trait :with_ignored_flag do
ignored_flag_at { Time.current }
end
trait :with_ignored_flag do
ignored_flag_at { Time.current }
end

trait :with_administrator do
administrator
end

trait :flagged do
after :create do |investment|
Expand Down
Loading