Class: Sale

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/sale.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#buyerObject (readonly)

Returns the value of attribute buyer.



5
6
7
# File 'app/models/sale.rb', line 5

def buyer
  @buyer
end

#cartObject

Returns the value of attribute cart.



4
5
6
# File 'app/models/sale.rb', line 4

def cart
  @cart
end

#errorObject

Returns the value of attribute error.



4
5
6
# File 'app/models/sale.rb', line 4

def error
  @error
end

#messageObject

Returns the value of attribute message.



4
5
6
# File 'app/models/sale.rb', line 4

def message
  @message
end

#notesObject

Returns the value of attribute notes.



4
5
6
# File 'app/models/sale.rb', line 4

def notes
  @notes
end

#orderObject

Returns the value of attribute order.



4
5
6
# File 'app/models/sale.rb', line 4

def order
  @order
end

#quantitiesObject

Returns the value of attribute quantities.



4
5
6
# File 'app/models/sale.rb', line 4

def quantities
  @quantities
end

#sale_madeObject

Returns the value of attribute sale_made.



4
5
6
# File 'app/models/sale.rb', line 4

def sale_made
  @sale_made
end

#ticket_typesObject

Returns the value of attribute ticket_types.



4
5
6
# File 'app/models/sale.rb', line 4

def ticket_types
  @ticket_types
end

#ticketsObject

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

Returns:

  • (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_ticketsObject



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

Returns:

  • (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