Extract Budget Investment to_csv to its own class

The csv generation doesn't seem like a Model concern, at least not taking
into account the amount of lines of the method (36+). Just a simple ruby
class that encapsulates the logic makes it easier to read and maintain as
we increase the columns exported.. also customize in case other forks need
different values.
This commit is contained in:
Bertocq
2018-04-11 17:32:13 +02:00
parent 83f4f4f65c
commit aacac71362
3 changed files with 60 additions and 46 deletions

View File

@@ -20,7 +20,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
format.html format.html
format.js format.js
format.csv do format.csv do
send_data Budget::Investment.to_csv(@investments), send_data Budget::Investment::Exporter.new(@investments).to_csv,
filename: 'budget_investments.csv' filename: 'budget_investments.csv'
end end
end end

View File

@@ -1,5 +1,3 @@
require 'csv'
class Budget class Budget
class Investment < ActiveRecord::Base class Investment < ActiveRecord::Base
SORTING_OPTIONS = %w(id title supports).freeze SORTING_OPTIONS = %w(id title supports).freeze
@@ -328,49 +326,6 @@ class Budget
self.valuator_groups.collect(&:name).compact.join(', ').presence self.valuator_groups.collect(&:name).compact.join(', ').presence
end 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 private
def set_denormalized_ids def set_denormalized_ids

View File

@@ -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