BathHacked/energy-sparks

View on GitHub
app/models/comparison/annual_energy_use.rb

Summary

Maintainability
A
35 mins
Test Coverage
# == Schema Information
#
# Table name: annual_energy_uses
#
#  alert_generation_run_id        :bigint(8)
#  electricity_last_year_co2      :float
#  electricity_last_year_gbp      :float
#  electricity_last_year_kwh      :float
#  electricity_tariff_has_changed :boolean
#  floor_area                     :float
#  gas_last_year_co2              :float
#  gas_last_year_gbp              :float
#  gas_last_year_kwh              :float
#  gas_tariff_has_changed         :boolean
#  id                             :bigint(8)
#  pupils                         :float
#  school_id                      :bigint(8)
#  school_type_name               :text
#  storage_heaters_last_year_co2  :float
#  storage_heaters_last_year_gbp  :float
#  storage_heaters_last_year_kwh  :float
#
class Comparison::AnnualEnergyUse < Comparison::View
  self.table_name = 'annual_energy_uses'

  scope :with_data, -> { where('electricity_last_year_kwh IS NOT NULL OR gas_last_year_kwh IS NOT NULL OR storage_heaters_last_year_kwh IS NOT NULL')}
  scope :sort_default, -> { by_total([:electricity_last_year_kwh, :gas_last_year_kwh, :storage_heaters_last_year_kwh], 'DESC NULLS LAST') }

  def any_tariff_changed?
    electricity_tariff_has_changed || gas_tariff_has_changed
  end

  # For CSV export
  def fuel_type_names
    codes = []
    codes << I18n.t('common.electricity') if electricity_previous_period_kwh
    codes << I18n.t('common.gas') if gas_previous_period_kwh
    codes << I18n.t('common.storage_heaters') if storage_heater_previous_period_kwh
    codes << I18n.t('common.solar_pv') if solar_type.present?
    codes.join(';')
  end
end