Class: Sale
- Inherits:
-
Object
- Object
- Sale
- Includes:
- ActiveModel::Validations
- Defined in:
- app/models/sale.rb
Instance Attribute Summary collapse
-
#buyer ⇒ Object
readonly
Returns the value of attribute buyer.
-
#cart ⇒ Object
Returns the value of attribute cart.
-
#error ⇒ Object
Returns the value of attribute error.
-
#message ⇒ Object
Returns the value of attribute message.
-
#notes ⇒ Object
Returns the value of attribute notes.
-
#order ⇒ Object
Returns the value of attribute order.
-
#quantities ⇒ Object
Returns the value of attribute quantities.
-
#sale_made ⇒ Object
Returns the value of attribute sale_made.
-
#ticket_types ⇒ Object
Returns the value of attribute ticket_types.
-
#tickets ⇒ Object
Returns the value of attribute tickets.
Instance Method Summary collapse
- #has_tickets? ⇒ Boolean
-
#initialize(show, ticket_types, cart, quantities = {}, notes = nil) ⇒ Sale
constructor
A new instance of Sale.
- #load_tickets ⇒ Object
- #non_zero_quantities? ⇒ Boolean
- #sell(payment) ⇒ Object
Constructor Details
#initialize(show, ticket_types, cart, quantities = {}, notes = nil) ⇒ Sale
Returns a new instance of Sale.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/models/sale.rb', line 9 def initialize(show, ticket_types, cart, quantities = {}, notes=nil) @show = show @ticket_types = ticket_types @notes = notes #When coming from a browser, all keys and values in @quantities are STRINGS @quantities = quantities @cart = cart @tickets = [] load_tickets end |
Instance Attribute Details
#buyer ⇒ Object (readonly)
Returns the value of attribute buyer.
5 6 7 |
# File 'app/models/sale.rb', line 5 def buyer @buyer end |
#cart ⇒ Object
Returns the value of attribute cart.
4 5 6 |
# File 'app/models/sale.rb', line 4 def cart @cart end |
#error ⇒ Object
Returns the value of attribute error.
4 5 6 |
# File 'app/models/sale.rb', line 4 def error @error end |
#message ⇒ Object
Returns the value of attribute message.
4 5 6 |
# File 'app/models/sale.rb', line 4 def @message end |
#notes ⇒ Object
Returns the value of attribute notes.
4 5 6 |
# File 'app/models/sale.rb', line 4 def notes @notes end |
#order ⇒ Object
Returns the value of attribute order.
4 5 6 |
# File 'app/models/sale.rb', line 4 def order @order end |
#quantities ⇒ Object
Returns the value of attribute quantities.
4 5 6 |
# File 'app/models/sale.rb', line 4 def quantities @quantities end |
#sale_made ⇒ Object
Returns the value of attribute sale_made.
4 5 6 |
# File 'app/models/sale.rb', line 4 def sale_made @sale_made end |
#ticket_types ⇒ Object
Returns the value of attribute ticket_types.
4 5 6 |
# File 'app/models/sale.rb', line 4 def ticket_types @ticket_types end |
#tickets ⇒ Object
Returns the value of attribute tickets.
4 5 6 |
# File 'app/models/sale.rb', line 4 def tickets @tickets end |
Instance Method Details
#has_tickets? ⇒ Boolean
59 60 61 62 63 64 65 |
# File 'app/models/sale.rb', line 59 def has_tickets? unless non_zero_quantities? errors.add(:base, "Please select a number of tickets to purchase") and return false end errors.add(:base, "no tickets were added") unless @tickets.size > 0 @tickets.size > 0 end |
#load_tickets ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/sale.rb', line 43 def load_tickets @quantities.keys.each do |ticket_type_id| amount_requested = @quantities[ticket_type_id].to_i if amount_requested > 0 ticket_type = TicketType.find(ticket_type_id) tickets_available_in_ticket_type = ticket_type.available_tickets(amount_requested) if tickets_available_in_ticket_type.length != amount_requested errors.add(:base, "There aren't enough tickets available for that ticket type") else Ticket.lock(tickets_available_in_ticket_type, ticket_type, @cart) @tickets = @tickets + tickets_available_in_ticket_type end end end end |
#non_zero_quantities? ⇒ Boolean
36 37 38 39 40 41 |
# File 'app/models/sale.rb', line 36 def non_zero_quantities? @quantities.each do |k,v| return true if (v.to_i > 0) end false end |
#sell(payment) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/models/sale.rb', line 22 def sell(payment) if valid? case payment when CompPayment @sale_made = comp_tickets(payment) else @sale_made = sell_tickets(payment) end else @sale_made = false end @sale_made end |