diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index c85cbd98f..7eee73a5d 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -20,7 +20,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController format.html format.js format.csv do - send_data Budget::Investment.to_csv(@investments), + send_data Budget::Investment::Exporter.new(@investments).to_csv, filename: 'budget_investments.csv' end end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 24d038d14..4f2775826 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -1,5 +1,3 @@ -require 'csv' - class Budget class Investment < ActiveRecord::Base SORTING_OPTIONS = %w(id title supports).freeze @@ -328,49 +326,6 @@ class Budget self.valuator_groups.collect(&:name).compact.join(', ').presence end - def self.to_csv(investments) - 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_valuation_group"), - 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(headers: true) 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 - assigned_valuators = investment.assigned_valuators || '-' - assigned_valuation_groups = investment.assigned_valuation_groups || '-' - 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, assigned_valuators, assigned_valuation_groups, - heading_name, price, valuation_finished] - end - end - csv_string - end - private def set_denormalized_ids diff --git a/app/models/budget/investment/exporter.rb b/app/models/budget/investment/exporter.rb new file mode 100644 index 000000000..03900fd7e --- /dev/null +++ b/app/models/budget/investment/exporter.rb @@ -0,0 +1,59 @@ +class Budget::Investment::Exporter + require 'csv' + + def initialize(investments) + @investments = investments + end + + def to_csv + CSV.generate(headers: true) do |csv| + csv << headers + @investments.each { |investment| csv << csv_values(investment) } + end + end + + private + + def headers + [ + 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_valuation_group"), + 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") + ] + end + + def csv_values(investment) + [ + investment.id.to_s, + investment.title, + investment.total_votes.to_s, + admin(investment), + investment.assigned_valuators || '-', + investment.assigned_valuation_groups || '-', + investment.heading.name, + price(investment), + investment.valuation_finished? ? I18n.t('shared.yes') : I18n.t('shared.no'), + investment.selected? ? I18n.t('shared.yes') : I18n.t('shared.no') + ] + end + + def admin(investment) + if investment.administrator.present? + investment.administrator.name + else + I18n.t("admin.budget_investments.index.no_admin_assigned") + end + end + + def price(investment) + price_string = "admin.budget_investments.index.feasibility.#{investment.feasibility}" + I18n.t(price_string, price: investment.formatted_price) + end +end