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, sections, quantities = {}) ⇒ Sale

Returns a new instance of Sale.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/sale.rb', line 9

def initialize(show, sections, quantities = {})
  @show       = show
  @sections   = sections
  
  #When coming from a browser, all keys and values in @quantities are STRINGS
  @quantities = quantities
  @cart       = BoxOffice::Cart.new
  @tickets     = []
  
  #This is irritating, it means you can't add tickets to a sale later
  load_tickets      
  cart.tickets << 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

#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

#sectionsObject

Returns the value of attribute sections.



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

def sections
  @sections
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)


55
56
57
58
59
60
61
# File 'app/models/sale.rb', line 55

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



44
45
46
47
48
49
50
51
52
53
# File 'app/models/sale.rb', line 44

def load_tickets
  sections.each do |section|
    tickets_available_in_section = Ticket.available({:section_id => section.id, :show_id => @show.id}, @quantities[section.id.to_s])
    if tickets_available_in_section.length != @quantities[section.id.to_s].to_i
      errors.add(:base, "There aren't enough tickets available in that section")
    else
      @tickets = @tickets + tickets_available_in_section
    end
  end
end

#non_zero_quantities?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'app/models/sale.rb', line 37

def non_zero_quantities?
  @quantities.each do |k,v|
    return true if (v.to_i > 0)
  end
  false
end

#sell(payment) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/sale.rb', line 23

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