Class: PriceQuery
- Inherits:
-
Object
- Object
- PriceQuery
- Defined in:
- lib/unit4/checkout/price_query.rb
Overview
used for querying the database for the non-discounted product price
Instance Method Summary collapse
- #find_price(saved_prices) ⇒ Object
-
#initialize(item) ⇒ PriceQuery
constructor
A new instance of PriceQuery.
- #prepare_sql_statement(item) ⇒ Object
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 |