From e7f9f10ba0e15ab2f535fd775aa790893e56aa10 Mon Sep 17 00:00:00 2001 From: Tom Dooner Date: Tue, 25 Feb 2020 20:51:06 -0800 Subject: [PATCH] Add `largest_independent_expenditures` to totals.json There was some feedback from the frontend team that it'd be easier if it's in there. I updated the ELECTIONS constant to index the actual Election models instead of their attributes -- this will allow us to pull the calculation at the end. --- ...lection_largest_independent_expenditure.rb | 2 +- process.rb | 31 ++++++++++--------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/calculators/election_largest_independent_expenditure.rb b/calculators/election_largest_independent_expenditure.rb index 3f1e192b0..2db443192 100644 --- a/calculators/election_largest_independent_expenditure.rb +++ b/calculators/election_largest_independent_expenditure.rb @@ -23,7 +23,7 @@ def fetch top_spenders = top_spenders_by_election[election.name] next unless top_spenders - election.save_calculation(:largest_indpendent_expenditures, top_spenders) + election.save_calculation(:largest_independent_expenditures, top_spenders) end end end diff --git a/process.rb b/process.rb index 7cb59b45c..90c521410 100644 --- a/process.rb +++ b/process.rb @@ -4,10 +4,8 @@ require 'i18n' require 'open-uri' -# map of election_name => { hash including date } -ELECTIONS = ActiveRecord::Base.connection.execute(<<-SQL).index_by { |row| row['name'] }.transform_values(&:symbolize_keys) - SELECT * from elections; -SQL +# map of election_name => Election object +ELECTIONS = Election.all.index_by(&:name) def build_file(filename, &block) filename = File.expand_path('../build', __FILE__) + filename @@ -215,9 +213,9 @@ def slugify(word) end # /_referendums/oakland/2018-11-06/oakland-childrens-initiative.md - build_file("/_referendums/#{locality}/#{election[:date]}/#{title}.md") do |f| + build_file("/_referendums/#{locality}/#{election.date}/#{title}.md") do |f| f.puts(YAML.dump( - 'election' => election[:date], + 'election' => election.date, 'locality' => locality, 'number' => referendum['Measure_number'] =~ /PENDING/ ? nil : referendum['Measure_number'], 'title' => referendum['Short_Title'], @@ -228,7 +226,7 @@ def slugify(word) end # /_data/referendum_supporting/oakland/2018-11-06/oakland-childrens-initiative.json - build_file("/_data/referendum_supporting/#{locality}/#{election[:date]}/#{title}.json") do |f| + build_file("/_data/referendum_supporting/#{locality}/#{election.date}/#{title}.json") do |f| f.puts JSON.pretty_generate(referendum.as_json.merge( contributions_by_region: referendum.calculation(:supporting_locales) || [], contributions_by_type: referendum.calculation(:supporting_type) || [], @@ -238,7 +236,7 @@ def slugify(word) end # /_data/referendum_opposing/oakland/2018-11-06/oakland-childrens-initiative.json - build_file("/_data/referendum_opposing/#{locality}/#{election[:date]}/#{title}.json") do |f| + build_file("/_data/referendum_opposing/#{locality}/#{election.date}/#{title}.json") do |f| f.puts JSON.pretty_generate(referendum.as_json.merge( contributions_by_region: referendum.calculation(:opposing_locales) || [], contributions_by_type: referendum.calculation(:opposing_type) || [], @@ -249,7 +247,16 @@ def slugify(word) end build_file('/_data/totals.json') do |f| - f.puts JSON.pretty_generate(Hash[ContributionsByOrigin.sort]) + f.puts JSON.pretty_generate( + Hash[ELECTIONS.map do |election_name, election| + [ + election_name, + ContributionsByOrigin.fetch(election_name, {}).merge( + largest_independent_expenditures: election.calculation(:largest_independent_expenditures) + ) + ] + end] + ) end build_file('/_data/stats.json') do |f| @@ -259,9 +266,3 @@ def slugify(word) date_processed: date_processed.to_s ) end - -build_file('/_data/largest_indpendent_expenditures.json') do |f| - f.puts JSON.pretty_generate(Election.all.map do |e| - [e.name, e.calculation(:largest_indpendent_expenditures)] - end.to_h) -end