changes made at the request of voodoorai2000

This commit is contained in:
Raúl Fuentes
2018-01-09 11:44:44 +01:00
parent b3461b2051
commit 29018e3062
5 changed files with 84 additions and 54 deletions

View File

@@ -1,7 +1,5 @@
class Admin::BudgetInvestmentsController < Admin::BaseController
require 'csv'
include FeatureFlags
feature_flag :budgets
@@ -18,7 +16,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
respond_to do |format|
format.html
format.csv do
send_data set_csv, filename: 'budget_investments.csv'
send_data Budget::Investment.to_csv(@investments, {headers: true}),
filename: 'budget_investments.csv'
end
end
end
@@ -52,46 +51,10 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
private
def set_csv
attributes = [t("admin.budget_investments.index.table_id"),
t("admin.budget_investments.index.table_title"),
t("admin.budget_investments.index.table_supports"),
t("admin.budget_investments.index.table_admin"),
t("admin.budget_investments.index.table_valuator"),
t("admin.budget_investments.index.table_geozone"),
t("admin.budget_investments.index.table_feasibility"),
t("admin.budget_investments.index.table_valuation_finished"),
t("admin.budget_investments.index.table_selection")]
csv_string = CSV.generate(headers: true) do |csv|
csv << attributes
@investments.each do |investment|
id = investment.id.to_s
title = investment.title
total_votes = investment.total_votes.to_s
administrator = if investment.administrator.present?
investment.administrator.name
else
t("admin.budget_investments.index.no_admin_assigned")
end
valuators = if investment.valuators.empty?
t("admin.budget_investments.index.no_valuators_assigned")
else
investment.valuators.collect(&:description_or_name).join(', ')
end
heading_name = investment.heading.name
feasibility_string = "admin.budget_investments.index.feasibility.#{investment.feasibility}"
price = t(feasibility_string, price: investment.formatted_price)
valuation_finished = investment.valuation_finished? ? t('shared.yes') : t('shared.no')
csv << [id, title, total_votes, administrator, valuators, heading_name, price, valuation_finished]
end
end
csv_string
end
def load_investments
@investments = Budget::Investment.scoped_filter(params, @current_filter)
.order(cached_votes_up: :desc, created_at: :desc)
.page(params[:page])
@investments = @investments.page(params[:page]) unless request.format.csv?
end
def budget_investment_params

View File

@@ -1,11 +1,19 @@
module BudgetsHelper
def csv_params
csv_params = params.clone.merge(format: :csv)
csv_params = params.clone.merge(format: :csv).symbolize_keys
csv_params.delete(:page)
csv_params
end
def investment_selected_link(investment)
options = investment_selected_link_options(investment)
path = toggle_selection_admin_budget_budget_investment_path(@budget,
investment, filter: params[:filter], page: params[:page])
link_options = {method: :patch, remote: true, class: options[:link_class]}
link_to options[:text], path, link_options
end
def investment_selected_link_options(investment)
if investment.selected?
{link_class: "button small expanded",

View File

@@ -1,5 +1,6 @@
class Budget
class Investment < ActiveRecord::Base
require 'csv'
include Measurable
include Sanitizable
include Taggable
@@ -262,6 +263,46 @@ class Budget
investments
end
def self.to_csv(investments, options = {})
attrs = [I18n.t("admin.budget_investments.index.table_id"),
I18n.t("admin.budget_investments.index.table_title"),
I18n.t("admin.budget_investments.index.table_supports"),
I18n.t("admin.budget_investments.index.table_admin"),
I18n.t("admin.budget_investments.index.table_valuator"),
I18n.t("admin.budget_investments.index.table_geozone"),
I18n.t("admin.budget_investments.index.table_feasibility"),
I18n.t("admin.budget_investments.index.table_valuation_finished"),
I18n.t("admin.budget_investments.index.table_selection")]
csv_string = CSV.generate(options) do |csv|
csv << attrs
investments.each do |investment|
id = investment.id.to_s
title = investment.title
total_votes = investment.total_votes.to_s
admin = if investment.administrator.present?
investment.administrator.name
else
I18n.t("admin.budget_investments.index.no_admin_assigned")
end
vals = if investment.valuators.empty?
I18n.t("admin.budget_investments.index.no_valuators_assigned")
else
investment.valuators.collect(&:description_or_name).join(', ')
end
heading_name = investment.heading.name
price_string = "admin.budget_investments.index.feasibility"\
".#{investment.feasibility}"
price = I18n.t(price_string, price: investment.formatted_price)
valuation_finished = investment.valuation_finished? ?
I18n.t('shared.yes') :
I18n.t('shared.no')
csv << [id, title, total_votes, admin, vals, heading_name, price,
valuation_finished]
end
end
csv_string
end
private
def set_denormalized_ids

View File

@@ -58,10 +58,7 @@
<%= investment.valuation_finished? ? t('shared.yes'): t('shared.no') %>
</td>
<td class="small">
<% options = investment_selected_link_options(investment) %>
<%= link_to toggle_selection_admin_budget_budget_investment_path(@budget, investment, filter: params[:filter], page: params[:page]), method: :patch, remote: true, class: options[:link_class] do %>
<%= options[:text] %>
<% end %>
<%= investment_selected_link(investment) %>
</td>
<% if params[:filter] == 'selected' %>
<td class="small text-center">

View File

@@ -630,23 +630,44 @@ feature 'Admin budget investments' do
context "Selecting csv" do
scenario "Downloading CSV file" do
investment1 = create(:budget_investment, :unfeasible, budget: @budget)
investment2 = create(:budget_investment, :feasible, budget: @budget)
investment = create(:budget_investment, :feasible, budget: @budget,
price: 100)
valuator = create(:valuator, user: create(:user, username: 'Rachel',
email: 'rachel@val.org'))
investment.valuators << valuator
admin = create(:administrator, user: create(:user, username: 'Gema'))
investment.update(administrator_id: admin.id)
visit admin_budget_budget_investments_path(@budget, format: :csv)
header = page.response_headers['Content-Disposition']
header.should match(/^attachment/)
header.should match(/filename="budget_investments.csv"$/)
expect(header).to match(/^attachment/)
expect(header).to match(/filename="budget_investments.csv"$/)
expect(page).to have_content investment2.title
expect(page).to have_content investment1.title
valuators = investment.valuators.collect(&:description_or_name).join(', ')
feasibility_string = "admin.budget_investments.index"\
".feasibility.#{investment.feasibility}"
price = I18n.t(feasibility_string, price: investment.formatted_price)
expect(page).to have_content investment.title
expect(page).to have_content investment.total_votes.to_s
expect(page).to have_content investment.id.to_s
expect(page).to have_content investment.heading.name
expect(page).to have_content investment.administrator.name
expect(page).to have_content valuators
expect(page).to have_content price
expect(page).to have_content I18n.t('shared.no')
end
scenario "Downloading CSV file with applied filter" do
investment1 = create(:budget_investment, :unfeasible, budget: @budget, title: 'compatible')
investment2 = create(:budget_investment, :finished, budget: @budget, title: 'valuation_finished')
visit admin_budget_budget_investments_path(@budget, format: :csv, filter: :valuation_finished)
investment1 = create(:budget_investment, :unfeasible, budget: @budget,
title: 'compatible')
investment2 = create(:budget_investment, :finished, budget: @budget,
title: 'finished')
visit admin_budget_budget_investments_path(@budget, format: :csv,
filter: :valuation_finished)
header = page.response_headers['Content-Disposition']
header.should match(/^attachment/)