add ability to create and purchase free tickets

This commit is contained in:
Rishabh Saxena 2016-08-02 01:37:55 +05:30
parent c4938166f5
commit 4dcfc55418
No known key found for this signature in database
GPG key ID: FA3BDC38A9CB6807
2 changed files with 16 additions and 6 deletions

View file

@ -13,7 +13,7 @@ class Ticket < ActiveRecord::Base
validates :price_cents, :price_currency, :title, presence: true
validates_numericality_of :price_cents, greater_than: 0
validates_numericality_of :price_cents, greater_than_or_equal_to: 0
def bought?(user)
buyers.include?(user)

View file

@ -23,7 +23,7 @@ class TicketPurchase < ActiveRecord::Base
ActiveRecord::Base.transaction do
conference.tickets.each do |ticket|
quantity = purchases[ticket.id.to_s].to_i
# if the user bought the ticket, just update the quantity
# if the user bought the ticket and is still unpaid, just update the quantity
if ticket.bought?(user) && ticket.unpaid?(user)
purchase = update_quantity(conference, quantity, ticket, user)
else
@ -39,10 +39,20 @@ class TicketPurchase < ActiveRecord::Base
end
def self.purchase_ticket(conference, quantity, ticket, user)
purchase = new(ticket_id: ticket.id,
conference_id: conference.id,
user_id: user.id,
quantity: quantity) if quantity > 0
if quantity > 0
if ticket.price_cents.zero?
purchase = new(ticket_id: ticket.id,
conference_id: conference.id,
user_id: user.id,
quantity: quantity,
paid: true)
else
purchase = new(ticket_id: ticket.id,
conference_id: conference.id,
user_id: user.id,
quantity: quantity)
end
end
purchase
end