Makes all tests pass in bi management

This commit is contained in:
kikito
2016-12-07 19:16:37 +01:00
parent 1e3a44b380
commit 748fd8becf
7 changed files with 63 additions and 51 deletions

View File

@@ -1,7 +1,7 @@
class Management::Budgets::InvestmentsController < Management::BaseController
load_and_authorize_resource :budget
load_and_authorize_resource :investment, through: :budget, class: 'Budget::Investment'
load_resource :budget
load_resource :investment, through: :budget, class: 'Budget::Investment'
before_action :only_verified_users, except: :print
@@ -35,8 +35,8 @@ class Management::Budgets::InvestmentsController < Management::BaseController
end
def print
params[:geozone] ||= 'all'
@investments = apply_filters_and_search(@investments).order(cached_votes_up: :desc).for_render.limit(15)
set_investment_votes(@investments)
end
private
@@ -45,31 +45,19 @@ class Management::Budgets::InvestmentsController < Management::BaseController
@investment_votes = managed_user ? managed_user.budget_investment_votes(investments) : {}
end
def load_budget
@budget = Budget.find(params[:budget_id])
end
def investment_params
params.require(:budget_investment).permit(:title, :description, :external_url, :geozone_id, :heading_id)
params.require(:budget_investment).permit(:title, :description, :external_url, :heading_id)
end
def only_verified_users
check_verified_user t("management.budget_investments.alert.unverified_user")
end
def set_geozone_name
if params[:geozone] == 'all'
@geozone_name = t('geozones.none')
else
@geozone_name = Geozone.find(params[:geozone]).name
end
end
def apply_filters_and_search(investments)
investments = params[:unfeasible].present? ? investments.unfeasible : investments.not_unfeasible
if params[:geozone].present?
investments = investments.by_geozone(params[:geozone])
set_geozone_name
if params[:heading_id].present?
investments = investments.by_heading(params[:heading_id])
@heading = Budget::Heading.find(params[:heading_id])
end
investments = investments.search(params[:search]) if params[:search].present?
investments

View File

@@ -1,8 +1,8 @@
module BudgetHeadingsHelper
def budget_heading_select_options(budget)
budget.headings.includes(:group).order('budget_groups.name', 'budget_headings.name').map do |heading|
["#{heading.group.name}: #{heading.name}", heading.id]
budget.headings.order_by_group_name.map do |heading|
[heading.name_scoped_by_group, heading.id]
end
end

View File

@@ -9,6 +9,8 @@ class Budget
validates :name, presence: true
validates :price, presence: true
scope :order_by_group_name, -> { includes(:group).order('budget_groups.name', 'budget_headings.name') }
def budget
group.budget
end
@@ -17,5 +19,9 @@ class Budget
group.budget = resource
end
def name_scoped_by_group
"#{group.name}: #{name}"
end
end
end

View File

@@ -1,8 +1,23 @@
<%= form_for(Budget::Investment.new, url: url, as: :budget_investment, method: :get) do |f| %>
<div class="row">
<div class="small-12 medium-6 column">
<%= text_field_tag :search, "", placeholder: t("admin.shared.budget_investment_search.placeholder") %>
<%= text_field_tag :search, "" %>
</div>
<div class="small-12 medium-6 column">
<%= select_tag :heading_id,
options_for_select(budget_heading_select_options(@budget),
params[:heading_id]),
include_blank: true
%>
</div>
</div>
<div class="row">
<div class="small-12 medium-6 column">
<%= check_box_tag :unfeasible, "1", params[:unfeasible].present? %>
</div>
</div>
<div class="row">
<div class="form-inline small-12 medium-3 column end">
<%= f.submit t("shared.search"), class: "button" %>
</div>

View File

@@ -1,29 +1,35 @@
<main>
<div class="row">
<span class="not-print">
<%= render 'admin/shared/budget_investment_search', url: print_management_budget_investments_path(@budget) %>
</span>
<div class="wrap row">
<div id="budget-investments" class="budget-investments-list small-12 column">
<div class="not-print">
<%= form_tag print_management_budget_investments_path, method: :get, enforce_utf8: false do %>
<div class="small-12 medium-4 column float-left">
<%= select_tag :geozone,
options_for_select(geozone_select_options.unshift([t("geozones.none"), "all"]), params[:geozone]),
{ label: false,
class: "js-submit-on-change" } %>
</div>
<% end %>
<a id="print_link" href="javascript:window.print();" class="button warning float-right">
<%= t('management.budget_investments.print.print_button') %>
</a>
</div>
<div class="small-12 search-results">
<%= content_tag(:h2, t("management.budget_investments.filters.unfeasible"), class: "inline-block") if params[:unfeasible].present? %>
<%= content_tag(:h2, t("management.budget_investments.filters.by_geozone", geozone: @geozone_name), class: "inline-block") if @geozone_name.present? %>
<%= content_tag(:h2, t("management.budget_investments.search_results", count: @budget_investments.size, search_term: params[:search]), class: "inline-block") if params[:search].present? %>
<% if params[:unfeasible].present? %>
<h2 class="inline-block"><%= t("management.budget_investments.filters.unfeasible") %></h2>
<% end %>
<% if @heading.present? %>
<h2 class="inline-block"><%= t("management.budget_investments.filters.heading", heading_name: @heading.name) %></h2>
<% end %>
<h2 class="inline-block"><%= t("management.budget_investments.search_results", count: @investments.count, search_term: params[:search]) %></h2>
</div>
<%= render @budget_investments %>
<% @investments.each do |investment| %>
<%= render partial: '/budgets/investments/investment', locals: {
investment: investment,
investment_ids: @investment_ids,
investment_votes: @investment_votes,
investment_ballots: @investment_ballots
} %>
<% end %>
<div class="for-print-only">
<p><strong><%= t("management.print.budget_investments_info") %></strong><br>

View File

@@ -435,6 +435,7 @@ es:
flag: Denunciar como inapropiado
print:
print_button: Imprimir esta información
search: Buscar
show: Mostrar
suggest:
debate:

View File

@@ -202,45 +202,41 @@ feature 'Budget Investments' do
scenario 'Printing budget investments' do
16.times { create(:budget_investment, budget: @budget) }
click_link "Print budget investments"
click_link "Print Budget Investments"
expect(page).to have_content(@budget.name)
within "#budget_#{@budget.id}" do
click_link "Support Budget Investments"
click_link "Print Budget Investments"
end
expect(page).to have_css('.budget-investment', count: 15)
expect(page).to have_css("a[href='javascript:window.print();']", text: 'Print')
end
scenario "Filtering budget investments by geozone to be printed", :js do
scenario "Filtering budget investments by heading to be printed", :js do
district_9 = create(:budget_heading, group: @group, name: "District Nine")
create(:budget_investment, budget: @budget, title: 'Change district 9', heading: district_9, cached_votes_up: 10)
create(:budget_investment, budget: @budget, title: 'Destroy district 9', heading: district_9, cached_votes_up: 100)
create(:budget_investment, budget: @budget, title: 'Nuke district 9', heading: district_9, cached_votes_up: 1)
create(:budget_investment, budget: @budget, title: 'Add new districts to the city', heading: @heading)
create(:budget_investment, budget: @budget, title: 'Add new districts to the city')
user = create(:user, :level_two)
login_managed_user(user)
click_link "Print budget investments"
click_link "Print Budget Investments"
expect(page).to have_content(@budget.name)
within "#budget_#{@budget.id}" do
click_link "Support Budget Investments"
click_link "Print Budget Investments"
end
expect(page).to have_content "Budget investments with scope: All city"
within '#budget-investments' do
expect(page).to have_content('Add new districts to the city')
expect(page).to_not have_content('Change district 9')
expect(page).to_not have_content('Destroy district 9')
expect(page).to_not have_content('Nuke district 9')
expect(page).to have_content('Change district 9')
expect(page).to have_content('Destroy district 9')
expect(page).to have_content('Nuke district 9')
end
select 'District Nine', from: 'geozone'
expect(page).to have_content "Investment projects with scope: District Nine"
expect(current_url).to include("geozone=#{district_9.id}")
select 'Whole city: District Nine', from: 'heading_id'
click_button("Search")
within '#budget-investments' do
expect(page).to_not have_content('Add new districts to the city')