Merge pull request #1118 from rishabhs95/free-ticket

ability to create and purchase free tickets
This commit is contained in:
Hernán Schmidt 2017-01-31 14:54:18 +01:00 committed by GitHub
commit a4ef10cf1b
9 changed files with 88 additions and 24 deletions

View file

@ -1,3 +1,4 @@
# rubocop:disable Metrics/ClassLength
##
# This class represents a conference
class Conference < ActiveRecord::Base
@ -64,6 +65,8 @@ class Conference < ActiveRecord::Base
before_create :add_color
before_create :create_email_settings
after_create :create_free_ticket
def date_range_string
startstr = 'Unknown - '
endstr = 'Unknown'
@ -647,6 +650,14 @@ class Conference < ActiveRecord::Base
create_roles
end
##
# Creates free ticket for the conference
# after the conference has been successfully created
# Will create 1 new record for 'free' ticket
def create_free_ticket
tickets.where(title: 'Free Access', price_cents: 0).first_or_create!(description: 'Get free access tickets for the conference.')
end
##
# Creates the roles of the conference
# after the conference has been successfully created
@ -1053,3 +1064,4 @@ class Conference < ActiveRecord::Base
result
end
end
# rubocop:enable Metrics/ClassLength

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,13 @@ 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
purchase = new(ticket_id: ticket.id,
conference_id: conference.id,
user_id: user.id,
quantity: quantity,
paid: ticket.price_cents.zero?)
end
purchase
end