added csv download link to budget_investments (issue 1625)

This commit is contained in:
Raúl Fuentes
2017-11-21 13:21:56 +01:00
parent 27ce8c9b1b
commit 95c1aa43e8
3 changed files with 69 additions and 19 deletions

View File

@@ -1,4 +1,7 @@
class Admin::BudgetInvestmentsController < Admin::BaseController
require 'csv'
include FeatureFlags
feature_flag :budgets
@@ -12,6 +15,12 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
before_action :load_investments, only: [:index, :toggle_selection]
def index
respond_to do |format|
format.html
format.csv do
send_data set_csv, filename: 'budget_investments.csv'
end
end
end
def show
@@ -43,6 +52,41 @@ 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
if investment.administrator.present?
administrator = investment.administrator.name
else
administrator = t("admin.budget_investments.index.no_admin_assigned")
end
if investment.valuators.empty?
valuators = t("admin.budget_investments.index.no_valuators_assigned")
else
valuators = investment.valuators.collect(&:description_or_name).join(', ')
end
heading_name = investment.heading.name
price = t("admin.budget_investments.index.feasibility.#{investment.feasibility}", 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)