diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index 12baf20d4..0208fccd8 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -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 diff --git a/app/helpers/budgets_helper.rb b/app/helpers/budgets_helper.rb index e6d9f16d4..a14b86538 100644 --- a/app/helpers/budgets_helper.rb +++ b/app/helpers/budgets_helper.rb @@ -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", diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index f40878702..4e2e5d0b0 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -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 diff --git a/app/views/admin/budget_investments/_investments.html.erb b/app/views/admin/budget_investments/_investments.html.erb index 8d2a5191e..75bde7002 100644 --- a/app/views/admin/budget_investments/_investments.html.erb +++ b/app/views/admin/budget_investments/_investments.html.erb @@ -58,10 +58,7 @@ <%= investment.valuation_finished? ? t('shared.yes'): t('shared.no') %>