Skip to content

Commit

Permalink
Added rest of the live/dead fields to the exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerldixon committed Sep 1, 2016
1 parent b8482bc commit 657bb52
Showing 1 changed file with 73 additions and 41 deletions.
114 changes: 73 additions & 41 deletions lib/modules/csv_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,83 @@ def self.build(reports)
CSV.open(filepath, "wb") do |csv|
csv << column_names
reports.each do |report|
# For each ape, make a row
report_data = [
report.id,
report.state,
report&.user&.full_name,
report&.user&.agency&.name,
report.data['answers']['own_organisation']['selected'],
report.created_at,
report.data['answers']['country_of_discovery']['selected'],
report.data['answers']['region_of_discovery']['selected'],
report.answer_to("date_of_discovery")&.strftime("%d/%m/%Y"),
report.data['answers']['location_coords']['selected'],
report.data['answers']['type_of_location']['selected'],
"Ape Status TBC",
"Ape Genus TBC",
"Species/Subsoecies TBC",
"Intended Use TBC",
"Photo TBC",
"Age TBC",
"Gender TBC",
"Last Location TBC",
"Country of Origin TBC",
"Condition",
"Identifiers",
"Name",
"Bone Qty",
"Foot/Hand Qty",
"Genitalia Qty",
"Hair Qty",
"Meat Kg",
"Skin Qty",
"Skull Qty",
report.data['answers']['confiscated']['selected'],
report.data['answers']['arrests_made']['selected'],
report.data['answers']['prosecution']['selected'],
report.data['answers']['prosecution_successful']['selected'],
report.data['answers']['punishment']['selected'],
report.data['answers']['illegal_activities']['selected'],
report.data['answers']['proximity']['selected']
]
# Make the first row for the report, no apes
row = make_report_row(report)
csv << row

csv << report_data
# For each ape in live, dead and parts, make a row
statuses = ['live', 'dead', 'parts']
statuses.each do |status|
apes['answers'][status].each do |ape|
row = make_report_row(report, ape, status.to_sym)
csv << row
end
end
end
end

filepath
end

def make_report_row(report, ape=nil, status=nil)
# If an ape is passed in, it create a row with the details of this ape,
# if not, it leaves these details blank and just creates a report row

report_data_1 = [
report.id,
report.state,
report&.user&.full_name,
report&.user&.agency&.name,
report.data.dig('answers', 'own_organisation', 'selected'),
report.created_at,
report.dig('answers', 'country_of_discovery', 'selected'),
report.data('answers', 'region_of_discovery', 'selected'),
report.answer_to("date_of_discovery")&.strftime("%d/%m/%Y"),
report.data.dig('answers', 'location_coords', 'selected'),
report.data.dig('answers', 'type_of_location', 'selected')
]

if ape.present? && (status == :live or status == :dead)
ape_data = [
status.titleize,
ape.dig("genus_#{status}", 'selected'),
ape.dig("species_subspecies_#{status}", 'selected'),
ape.dig("intended_use_#{status}", 'selected'),
"Photo TBC",
ape.dig("age_#{status}", 'selected'),
ape.dig("gender_#{status}", 'selected'),
ape.dig("last_known_location_#{status}", 'selected'),
ape.dig("alleged_origin_country_#{status}", 'selected'),
ape.dig("condition_#{status}", 'selected'),
ape.dig("unique_identifiers_#{status}", 'selected'),
ape.dig("individual_name_#{status}", 'selected')
]

# Add empty fields for body parts
ape_data += Array.new(7, "")
end
else
#"Bone Qty",
#"Foot/Hand Qty",
#"Genitalia Qty",
#"Hair Qty",
#"Meat Kg",
#"Skin Qty",
#"Skull Qty"
ape_data = Array.new(19, "")
end

report_data_2 = [
report.data.dig('answers', 'confiscated', 'selected'),
report.data.dig('answers', 'arrests_made', 'selected'),
report.data.dig('answers', 'prosecution', 'selected'),
report.data.dig('answers', 'prosecution_successful', 'selected'),
report.data.dig('answers', 'punishment', 'selected'),
report.data.dig('answers', 'illegal_activities', 'selected'),
report.data.dig('answers', 'proximity', 'selected')
]

# Join the segments to form the full row
report_data_1 + ape_data + report_data_2
end
end

0 comments on commit 657bb52

Please sign in to comment.