Class: Pont::CompAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/pont/comp_attributes.rb

Constant Summary collapse

BASE =
[
  :channel_code,
  :contract_type_code,
  :enrollment_type,
  :in_gas_eligible_market,
  :market_code,
  :office_code,
  :order_type,
  :plan_code,
  :plan_comp_filter,
  :product_type_code,
  :sourced_by_source_code,
  :valid_email
].freeze
PIVOT =
[
  :account_confirmed_start_dt,
  :account_offer_reason,
  :account_status,
  :avg_annual_usage,
  :campaign_code,
  :comp_ineligible_dt,
  :comp_type_code,
  :confirmed_start_dt,
  :failed_validation,
  :installation_required,
  :is_cancelled,
  :is_duplicate,
  :is_partial,
  :is_rejected,
  :is_rescinded,
  :is_winback,
  :payable_status_reason,
  :revenue_class_code,
  :sale_dt,
  :state_code,
  :total_residuals
].concat(BASE).sort.freeze

Class Method Summary collapse

Class Method Details

.create_pivotObject

TODO: Move this to after party Run this in garcon after adding new attributes heroku run padrino runner “Pont::CompAttributes.create_pivot” –remote production



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pont/comp_attributes.rb', line 47

def self.create_pivot
quoted_attributes = PIVOT.map { |attribute| "$$#{attribute}$$" }.join(', ')
typed_attributes = PIVOT.map { |attribute| "#{attribute} text" }.join(', ')

# The Rails versions isn't currently working because of an issue with array[?] adding quotes
# We only run this in garcon for now but will need to be fixed before moving to soleil
if defined?(Rails)
  ApplicationRecord.transaction do
    ActiveRecord::Base.connection.execute('DROP VIEW IF EXISTS "comp_attribute_pivot"')
    query = %(
      CREATE VIEW \"comp_attribute_pivot\" AS
      select * from crosstab('select comp_id, comp_attribute_key, comp_attribute_value from comp_attributes
        order by 1, 2'::text,
        'select unnest(array[?])'::text) comp_attributes(comp_id integer, ?)
        order by comp_id
      )
      sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, [query, quoted_attributes, typed_attributes])
      ActiveRecord::Base.connection.execute(sanitized_query)
    end
  else
    Sequel::Model.db.transaction do
      Sequel::Model.db.drop_view(:comp_attribute_pivot, if_exists: true)
      Sequel::Model.db.create_view(:comp_attribute_pivot, "select * from crosstab('select comp_id, comp_attribute_key, comp_attribute_value from comp_attributes order by 1, 2'::text, 'select unnest(array[#{quoted_attributes}])'::text) comp_attributes(comp_id integer, #{typed_attributes}) order by comp_id")
    end
  end
end