Class: PriceQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/unit4/checkout/price_query.rb

Overview

used for querying the database for the non-discounted product price

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ PriceQuery

Returns a new instance of PriceQuery.



5
6
7
8
# File 'lib/unit4/checkout/price_query.rb', line 5

def initialize(item)
  @sanitized_query = prepare_sql_statement(item)
  @item = item
end

Instance Method Details

#find_price(saved_prices) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/unit4/checkout/price_query.rb', line 15

def find_price(saved_prices)
  # grab the price from the hash if available
  return saved_prices[@item] if saved_prices[@item]

  results = ActiveRecord::Base.connection.exec_query(@sanitized_query)
  results.rows.first.first if results.present?
end

#prepare_sql_statement(item) ⇒ Object



10
11
12
13
# File 'lib/unit4/checkout/price_query.rb', line 10

def prepare_sql_statement(item)
  # Future work: facilitate DB table name different from products, i.e. ask gem user for db name
  ActiveRecord::Base.sanitize_sql_array(["SELECT 'products'.'price' FROM 'products' WHERE 'products'.'id' = ?", item])
end