Class: Country

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/country.rb

Instance Method Summary collapse

Instance Method Details

#all_country_years_and_ranked_geography(once = false, scoped = true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/country.rb', line 16

def all_country_years_and_ranked_geography(once = false, scoped = true)
  geog_unit_codes = ['NAT', 'HFLAD', 'HSLAD', 'FLAD', 'SLAD']
  geog_units = GeogUnit.all.map{ |gu| {gu.code => gu} }.reduce Hash.new, :merge
  if scoped
    tps = terrapop_samples.where.not(year: nil).order(year: :desc).to_a
  else
    tps = TerrapopSample.unscoped.load.where(country_id: id).where().not(year: nil).order(year: :desc).to_a
  end
  results = []
  tps.each do |terrapop_sample|
    temp_geog_unit_codes = geog_unit_codes.clone
    year = terrapop_sample.year
    countryyear = short_name.upcase + year.to_s
    until temp_geog_unit_codes.empty?
      geog_unit_code = temp_geog_unit_codes.shift
      geog_unit_code.nil? && next
      map = terrapop_sample.maps.joins("INNER JOIN country_levels cl ON maps.country_level_id = cl.id").where(["cl.geog_unit_id = ?", geog_units[geog_unit_code].id]).first
      map.nil? && next
      sample_geog_level = terrapop_sample.sample_geog_level_for_country_level(map.country_level)
      if !sample_geog_level.nil?
        once && (return {countryyear: countryyear, sample_geog_level: sample_geog_level})
        results << {countryyear: countryyear, sample_geog_level: sample_geog_level}
      end
    end
  end
  results
end

#country_year_and_highest_geographyObject



11
12
13
# File 'app/models/country.rb', line 11

def country_year_and_highest_geography
  all_country_years_and_ranked_geography(true)
end