refactors investment filters

This commit is contained in:
rgarcia
2017-05-06 03:37:29 +02:00
parent fa825185f5
commit c0488c3b3c
12 changed files with 85 additions and 59 deletions

View File

@@ -114,4 +114,10 @@ class ApplicationController < ActionController::Base
store_location_for(:user, request.path) store_location_for(:user, request.path)
end end
end end
def set_default_budget_filter
if @budget.try(:balloting?)
params[:filter] ||= "selected"
end
end
end end

View File

@@ -3,6 +3,9 @@ module Budgets
load_and_authorize_resource :budget load_and_authorize_resource :budget
load_and_authorize_resource :group, class: "Budget::Group" load_and_authorize_resource :group, class: "Budget::Group"
before_action :set_default_budget_filter, only: :show
has_filters %w{not_unfeasible feasible unfeasible unselected selected}, only: [:show]
def show def show
end end

View File

@@ -14,18 +14,20 @@ module Budgets
before_action :load_heading, only: [:index, :show] before_action :load_heading, only: [:index, :show]
before_action :set_random_seed, only: :index before_action :set_random_seed, only: :index
before_action :load_categories, only: [:index, :new, :create] before_action :load_categories, only: [:index, :new, :create]
before_action :set_default_budget_filter, only: :index
feature_flag :budgets feature_flag :budgets
has_orders %w{most_voted newest oldest}, only: :show has_orders %w{most_voted newest oldest}, only: :show
has_orders ->(c) { c.instance_variable_get(:@budget).investments_orders }, only: :index has_orders ->(c) { c.instance_variable_get(:@budget).investments_orders }, only: :index
has_filters %w{not_unfeasible feasible unfeasible unselected selected}, only: [:index, :show]
invisible_captcha only: [:create, :update], honeypot: :subtitle, scope: :budget_investment invisible_captcha only: [:create, :update], honeypot: :subtitle, scope: :budget_investment
respond_to :html, :js respond_to :html, :js
def index def index
@investments = @investments.apply_filters_and_search(@budget, params).send("sort_by_#{@current_order}").page(params[:page]).per(10).for_render @investments = @investments.apply_filters_and_search(@budget, params, @current_filter).send("sort_by_#{@current_order}").page(params[:page]).per(10).for_render
@investment_ids = @investments.pluck(:id) @investment_ids = @investments.pluck(:id)
load_investment_votes(@investments) load_investment_votes(@investments)
@tag_cloud = tag_cloud @tag_cloud = tag_cloud
@@ -107,6 +109,7 @@ module Budgets
def tag_cloud def tag_cloud
TagCloud.new(Budget::Investment, params[:search]) TagCloud.new(Budget::Investment, params[:search])
end end
end end
end end

View File

@@ -3,6 +3,9 @@ class BudgetsController < ApplicationController
feature_flag :budgets feature_flag :budgets
load_and_authorize_resource load_and_authorize_resource
before_action :set_default_budget_filter, only: :show
has_filters %w{not_unfeasible feasible unfeasible unselected selected}, only: :show
respond_to :html, :js respond_to :html, :js
def show def show

View File

@@ -46,8 +46,8 @@ class Budget
scope :not_unfeasible, -> { where.not(feasibility: "unfeasible") } scope :not_unfeasible, -> { where.not(feasibility: "unfeasible") }
scope :undecided, -> { where(feasibility: "undecided") } scope :undecided, -> { where(feasibility: "undecided") }
scope :with_supports, -> { where('cached_votes_up > 0') } scope :with_supports, -> { where('cached_votes_up > 0') }
scope :selected, -> { where(selected: true) } scope :selected, -> { feasible.where(selected: true) }
scope :unselected, -> { where(selected: false) } scope :unselected, -> { feasible.where(selected: false) }
scope :last_week, -> { where("created_at >= ?", 7.days.ago)} scope :last_week, -> { where("created_at >= ?", 7.days.ago)}
scope :by_group, -> (group_id) { where(group_id: group_id) } scope :by_group, -> (group_id) { where(group_id: group_id) }
@@ -234,17 +234,9 @@ class Budget
budget.formatted_amount(price) budget.formatted_amount(price)
end end
def self.apply_filters_and_search(budget, params) def self.apply_filters_and_search(budget, params, current_filter=nil)
investments = all investments = all
investments = investments.send(current_filter) if current_filter.present?
if budget.balloting? && params[:unfeasible].blank? && params[:unselected].blank?
investments = investments.selected
elsif budget.balloting? && params[:unfeasible].blank? && params[:unselected].present?
investments = investments.feasible.unselected
else
investments = params[:unfeasible].present? ? investments.unfeasible : investments.not_unfeasible
end
investments = investments.by_heading(params[:heading_id]) if params[:heading_id].present? investments = investments.by_heading(params[:heading_id]) if params[:heading_id].present?
investments = investments.search(params[:search]) if params[:search].present? investments = investments.search(params[:search]) if params[:search].present?
investments investments
@@ -256,5 +248,6 @@ class Budget
self.group_id = self.heading.try(:group_id) if self.heading_id_changed? self.group_id = self.heading.try(:group_id) if self.heading_id_changed?
self.budget_id ||= self.heading.try(:group).try(:budget_id) self.budget_id ||= self.heading.try(:group).try(:budget_id)
end end
end end
end end

View File

@@ -7,13 +7,13 @@
</div> </div>
</div> </div>
<% if params[:unfeasible] %> <% if @current_filter == "unfeasible" %>
<div class="row margin-top"> <div class="row margin-top">
<div class="small-12 column"> <div class="small-12 column">
<h3><%= t("budgets.groups.show.unfeasible_title") %></h3> <h3><%= t("budgets.groups.show.unfeasible_title") %></h3>
</div> </div>
</div> </div>
<% elsif params[:unselected] %> <% elsif @current_filter == "unselected" %>
<div class="row margin-top"> <div class="row margin-top">
<div class="small-12 column"> <div class="small-12 column">
<h3><%= t("budgets.groups.show.unselected_title") %></h3> <h3><%= t("budgets.groups.show.unselected_title") %></h3>
@@ -31,8 +31,7 @@
class="<%= css_for_ballot_heading(heading) %>"> class="<%= css_for_ballot_heading(heading) %>">
<%= link_to heading.name, <%= link_to heading.name,
budget_investments_path(heading_id: heading.id, budget_investments_path(heading_id: heading.id,
unfeasible: params[:unfeasible], filter: @current_filter),
unselected: params[:unselected]),
data: { no_turbolink: true } %><br> data: { no_turbolink: true } %><br>
</span> </span>
<% end %> <% end %>
@@ -46,20 +45,24 @@
</div> </div>
</div> </div>
<% if params[:unfeasible].blank? %> <% unless @current_filter == "unfeasible" %>
<div class="row"> <div class="row">
<div class="small-12 column"> <div class="small-12 column">
<small><%= link_to t("budgets.groups.show.unfeasible"), <small>
budget_path(@budget, unfeasible: 1) %></small> <%= link_to t("budgets.groups.show.unfeasible"),
budget_path(@budget, filter: "unfeasible") %>
</small>
</div> </div>
</div> </div>
<% end %> <% end %>
<% if params[:unselected].blank? %> <% unless @current_filter == "unselected" %>
<div class="row"> <div class="row">
<div class="small-12 column"> <div class="small-12 column">
<small><%= link_to t("budgets.groups.show.unselected"), <small>
budget_path(@budget, unselected: 1) %></small> <%= link_to t("budgets.groups.show.unselected"),
budget_path(@budget, filter: "unselected") %>
</small>
</div> </div>
</div> </div>
<% end %> <% end %>

View File

@@ -16,7 +16,7 @@
<div class="small-12 search-results margin-bottom"> <div class="small-12 search-results margin-bottom">
<% if params[:unfeasible].present? %> <% if @current_filter == "unfeasible" %>
<h2><%= t("budgets.investments.index.unfeasible") %>: <%= @heading.name %></h2> <h2><%= t("budgets.investments.index.unfeasible") %>: <%= @heading.name %></h2>
<div class="callout primary margin"> <div class="callout primary margin">
<%= t("budgets.investments.index.unfeasible_text", <%= t("budgets.investments.index.unfeasible_text",
@@ -34,7 +34,7 @@
<% end %> <% end %>
</div> </div>
<%= render('shared/order_links', i18n_namespace: "budgets.investments.index") unless params[:unfeasible].present? %> <%= render('shared/order_links', i18n_namespace: "budgets.investments.index") unless @current_filter == "unfeasible" %>
<% @investments.each do |investment| %> <% @investments.each do |investment| %>
<%= render partial: '/budgets/investments/investment', locals: { <%= render partial: '/budgets/investments/investment', locals: {

View File

@@ -37,9 +37,9 @@
<div class="row margin"> <div class="row margin">
<div class="small-12 medium-9 column"> <div class="small-12 medium-9 column">
<% if params[:unfeasible] %> <% if @current_filter == "unfeasible" %>
<h3 class="margin-bottom"><%= t("budgets.show.unfeasible_title") %></h3> <h3 class="margin-bottom"><%= t("budgets.show.unfeasible_title") %></h3>
<% elsif params[:unselected] %> <% elsif @current_filter == "unselected" %>
<h3 class="margin-bottom"><%= t("budgets.show.unselected_title") %></h3> <h3 class="margin-bottom"><%= t("budgets.show.unselected_title") %></h3>
<% end %> <% end %>
<table class="table-fixed"> <table class="table-fixed">
@@ -54,14 +54,12 @@
<%= link_to group.name, <%= link_to group.name,
budget_investments_path(@budget, budget_investments_path(@budget,
heading_id: group.headings.first.id, heading_id: group.headings.first.id,
unfeasible: params[:unfeasible], filter: @current_filter),
unselected: params[:unselected]),
data: { no_turbolink: true } %> data: { no_turbolink: true } %>
<% else %> <% else %>
<%= link_to group.name, <%= link_to group.name,
budget_group_path(@budget, group, budget_group_path(@budget, group,
unfeasible: params[:unfeasible], filter: @current_filter) %>
unselected: params[:unselected]) %>
<% end %> <% end %>
<br> <br>
</td> </td>
@@ -72,20 +70,24 @@
</div> </div>
</div> </div>
<% unless params[:unfeasible] %> <% unless @current_filter == "unfeasible" %>
<div class="row"> <div class="row">
<div class="small-12 column"> <div class="small-12 column">
<small><%= link_to t("budgets.show.unfeasible"), <small>
budget_path(@budget, unfeasible: 1) %></small> <%= link_to t("budgets.show.unfeasible"),
budget_path(@budget, filter: "unfeasible") %>
</small>
</div> </div>
</div> </div>
<% end %> <% end %>
<% unless params[:unselected] %> <% unless @current_filter == "unselected" %>
<div class="row"> <div class="row">
<div class="small-12 column"> <div class="small-12 column">
<small><%= link_to t("budgets.show.unselected"), <small>
budget_path(@budget, unselected: 1) %></small> <%= link_to t("budgets.show.unselected"),
budget_path(@budget, filter: "unselected") %>
</small>
</div> </div>
</div> </div>
<% end %> <% end %>

View File

@@ -284,6 +284,12 @@ FactoryGirl.define do
feasibility "feasible" feasibility "feasible"
valuation_finished true valuation_finished true
end end
trait :unselected do
selected false
feasibility "feasible"
valuation_finished true
end
end end
factory :budget_ballot, class: 'Budget::Ballot' do factory :budget_ballot, class: 'Budget::Ballot' do

View File

@@ -447,10 +447,12 @@ feature 'Ballots' do
end end
scenario 'Unselected investments' do scenario 'Unselected investments' do
investment = create(:budget_investment, heading: new_york) investment = create(:budget_investment, heading: new_york, title: "WTF asdfasfd")
login_as(user) login_as(user)
visit budget_investments_path(budget, heading_id: new_york.id, unfeasible: 1) visit budget_path(budget)
click_link states.name
click_link new_york.name
expect(page).to_not have_css("#budget_investment_#{investment.id}") expect(page).to_not have_css("#budget_investment_#{investment.id}")
end end
@@ -459,7 +461,9 @@ feature 'Ballots' do
investment = create(:budget_investment, feasibility: "undecided", heading: new_york) investment = create(:budget_investment, feasibility: "undecided", heading: new_york)
login_as(user) login_as(user)
visit budget_investments_path(budget, heading_id: new_york.id) visit budget_path(budget)
click_link states.name
click_link new_york.name
within("#budget-investments") do within("#budget-investments") do
expect(page).to_not have_css("div.ballot") expect(page).to_not have_css("div.ballot")

View File

@@ -8,10 +8,14 @@ feature 'Budget Investments' do
let!(:heading) { create(:budget_heading, name: "More hospitals", group: group) } let!(:heading) { create(:budget_heading, name: "More hospitals", group: group) }
scenario 'Index' do scenario 'Index' do
investments = [create(:budget_investment, heading: heading), create(:budget_investment, heading: heading), create(:budget_investment, :feasible, heading: heading)] investments = [create(:budget_investment, heading: heading),
create(:budget_investment, heading: heading),
create(:budget_investment, :feasible, heading: heading)]
unfeasible_investment = create(:budget_investment, :unfeasible, heading: heading) unfeasible_investment = create(:budget_investment, :unfeasible, heading: heading)
visit budget_investments_path(budget, heading_id: heading.id) visit budget_path(budget)
click_link "Health"
expect(page).to have_selector('#budget-investments .budget-investment', count: 3) expect(page).to have_selector('#budget-investments .budget-investment', count: 3)
investments.each do |investment| investments.each do |investment|
@@ -56,7 +60,7 @@ feature 'Budget Investments' do
investment3 = create(:budget_investment, heading: heading) investment3 = create(:budget_investment, heading: heading)
investment4 = create(:budget_investment, :feasible, heading: heading) investment4 = create(:budget_investment, :feasible, heading: heading)
visit budget_investments_path(budget_id: budget.id, heading_id: heading.id, unfeasible: 1) visit budget_investments_path(budget_id: budget.id, heading_id: heading.id, filter: "unfeasible")
within("#budget-investments") do within("#budget-investments") do
expect(page).to have_css('.budget-investment', count: 1) expect(page).to have_css('.budget-investment', count: 1)
@@ -77,7 +81,7 @@ feature 'Budget Investments' do
click_link "All City" click_link "All City"
expected_path = budget_investments_path(budget, heading_id: heading.id, unfeasible: 1) expected_path = budget_investments_path(budget, heading_id: heading.id, filter: "unfeasible")
expect(page).to have_current_path(expected_path) expect(page).to have_current_path(expected_path)
end end
@@ -93,7 +97,7 @@ feature 'Budget Investments' do
click_link 'Districts' click_link 'Districts'
click_link 'Carabanchel' click_link 'Carabanchel'
expected_path = budget_investments_path(budget, heading_id: heading1.id, unfeasible: 1) expected_path = budget_investments_path(budget, heading_id: heading1.id, filter: "unfeasible")
expect(page).to have_current_path(expected_path) expect(page).to have_current_path(expected_path)
end end
end end
@@ -516,8 +520,6 @@ feature 'Budget Investments' do
expect(page).to have_content "€10,000" expect(page).to have_content "€10,000"
end end
scenario "Sidebar in show should display vote text" do scenario "Sidebar in show should display vote text" do
investment = create(:budget_investment, :selected, budget: budget) investment = create(:budget_investment, :selected, budget: budget)
visit budget_investment_path(budget, investment) visit budget_investment_path(budget, investment)
@@ -537,7 +539,6 @@ feature 'Budget Investments' do
carabanchel_heading = create(:budget_heading, group: group, name: "Carabanchel") carabanchel_heading = create(:budget_heading, group: group, name: "Carabanchel")
new_york_heading = create(:budget_heading, group: group, name: "New York") new_york_heading = create(:budget_heading, group: group, name: "New York")
sp1 = create(:budget_investment, :selected, price: 1, heading: global_heading) sp1 = create(:budget_investment, :selected, price: 1, heading: global_heading)
sp2 = create(:budget_investment, :selected, price: 10, heading: global_heading) sp2 = create(:budget_investment, :selected, price: 10, heading: global_heading)
sp3 = create(:budget_investment, :selected, price: 100, heading: global_heading) sp3 = create(:budget_investment, :selected, price: 100, heading: global_heading)
@@ -603,12 +604,12 @@ feature 'Budget Investments' do
end end
scenario 'Show unselected budget investments' do scenario 'Show unselected budget investments' do
investment1 = create(:budget_investment, :feasible, heading: heading, valuation_finished: true) investment1 = create(:budget_investment, :unselected, :feasible, heading: heading, valuation_finished: true)
investment2 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true) investment2 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true)
investment3 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true) investment3 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true)
investment4 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true) investment4 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true)
visit budget_investments_path(budget_id: budget.id, heading_id: heading.id, unselected: 1) visit budget_investments_path(budget_id: budget.id, heading_id: heading.id, filter: "unselected")
within("#budget-investments") do within("#budget-investments") do
expect(page).to have_css('.budget-investment', count: 1) expect(page).to have_css('.budget-investment', count: 1)
@@ -629,7 +630,7 @@ feature 'Budget Investments' do
click_link "All City" click_link "All City"
expected_path = budget_investments_path(budget, heading_id: heading.id, unselected: 1) expected_path = budget_investments_path(budget, heading_id: heading.id, filter: "unselected")
expect(page).to have_current_path(expected_path) expect(page).to have_current_path(expected_path)
end end
@@ -645,7 +646,7 @@ feature 'Budget Investments' do
click_link 'Districts' click_link 'Districts'
click_link 'Carabanchel' click_link 'Carabanchel'
expected_path = budget_investments_path(budget, heading_id: heading1.id, unselected: 1) expected_path = budget_investments_path(budget, heading_id: heading1.id, filter: "unselected")
expect(page).to have_current_path(expected_path) expect(page).to have_current_path(expected_path)
end end

View File

@@ -199,11 +199,12 @@ feature 'Tags' do
if budget.balloting? if budget.balloting?
[investment1, investment2, investment3].each do |investment| [investment1, investment2, investment3].each do |investment|
investment.update(selected: true) investment.update(selected: true, feasibility: "feasible")
end end
end end
visit budget_investments_path(budget, heading_id: heading.id) visit budget_path(budget)
click_link group.name
within "#tag-cloud" do within "#tag-cloud" do
click_link "Medio Ambiente" click_link "Medio Ambiente"
@@ -246,11 +247,12 @@ feature 'Tags' do
if budget.balloting? if budget.balloting?
[investment1, investment2, investment3].each do |investment| [investment1, investment2, investment3].each do |investment|
investment.update(selected: true) investment.update(selected: true, feasibility: "feasible")
end end
end end
visit budget_investments_path(budget, heading_id: heading.id, search: 'Economía') visit budget_path(budget)
click_link group.name
within "#categories" do within "#categories" do
click_link "Medio Ambiente" click_link "Medio Ambiente"