solve code reviews, resolve rebase conflicts
This commit is contained in:
parent
9b704e483e
commit
ae44abc7e6
10 changed files with 32 additions and 45 deletions
|
|
@ -29,8 +29,9 @@ class ConferenceRegistrationsController < ApplicationController
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@total_price = Ticket.total_price(@conference, current_user, paid: true)
|
@total_price = Ticket.total_price(@conference, current_user, paid: true)
|
||||||
@tickets = current_user.ticket_purchases.where(conference_id: @conference.id, paid: true)
|
@tickets = current_user.ticket_purchases.by_conference(@conference).paid
|
||||||
@ticket_payments = @tickets.group_by(&:payment_id)
|
@ticket_payments = @tickets.group_by(&:ticket_id)
|
||||||
|
@total_quantity = @tickets.group(:ticket_id).sum(:quantity)
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit; end
|
def edit; end
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,9 @@ class PaymentsController < ApplicationController
|
||||||
@payment = Payment.new(payment_params)
|
@payment = Payment.new(payment_params)
|
||||||
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
||||||
|
|
||||||
if @payment.valid? && @payment.purchase(current_user, @conference, price_in_cents) && @payment.save
|
if @payment.purchase(current_user, @conference, price_in_cents) && @payment.save
|
||||||
update_paid_ticket_purchases(@conference, current_user, @payment)
|
update_purchased_ticket_purchases(@conference, current_user, @payment)
|
||||||
redirect_to conference_conference_registrations_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
|
redirect_to conference_conference_registration_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
|
||||||
else
|
else
|
||||||
render 'new'
|
render 'new'
|
||||||
end
|
end
|
||||||
|
|
@ -30,10 +30,8 @@ class PaymentsController < ApplicationController
|
||||||
(@payment.amount * 100).round
|
(@payment.amount * 100).round
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_paid_ticket_purchases(conference, user, payment)
|
def update_purchased_ticket_purchases(conference, user, payment)
|
||||||
paid_ticket_purchases = TicketPurchase.where(conference_id: conference.id,
|
paid_ticket_purchases = current_user.ticket_purchases.by_conference(conference).unpaid
|
||||||
user_id: user.id,
|
|
||||||
paid: false)
|
|
||||||
paid_ticket_purchases.each do |ticket|
|
paid_ticket_purchases.each do |ticket|
|
||||||
ticket.paid = true
|
ticket.paid = true
|
||||||
ticket.payment_id = payment.id
|
ticket.payment_id = payment.id
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ class TicketPurchasesController < ApplicationController
|
||||||
TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: false)
|
TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: false)
|
||||||
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
|
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
|
||||||
if message.blank?
|
if message.blank?
|
||||||
if current_user.ticket_purchases.unpaid.any?
|
if current_user.ticket_purchases.by_conference(@conference).unpaid.any?
|
||||||
redirect_to new_conference_payment_path, notice: 'Please pay here to purchase tickets.'
|
redirect_to new_conference_payment_path, notice: 'Please pay here to purchase tickets.'
|
||||||
else
|
else
|
||||||
redirect_to conference_conference_registration_path(@conference.short_title)
|
redirect_to conference_conference_registration_path(@conference.short_title)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,6 @@ module PaymentsHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def card_types
|
def card_types
|
||||||
[[['Visa', 'visa'], ['MasterCard', 'master'], ['Discover', 'discover'], ['American Express', 'american_express']]]
|
[['Visa', 'visa'], ['MasterCard', 'master'], ['Discover', 'discover'], ['American Express', 'american_express']]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -36,30 +36,25 @@ class Payment < ActiveRecord::Base
|
||||||
|
|
||||||
def purchase(user, conference, price_in_cents)
|
def purchase(user, conference, price_in_cents)
|
||||||
begin
|
begin
|
||||||
response = GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency)
|
recieve = GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency)
|
||||||
rescue
|
rescue
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
unless response
|
unless recieve
|
||||||
errors.add(:base, 'Unable to recieve any response')
|
errors.add(:base, 'Unable to recieve any response')
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
unless response.success?
|
unless recieve.success?
|
||||||
errors.add(:base, response.message)
|
errors.add(:base, recieve.message)
|
||||||
self.status = 'failure'
|
self.status = 'failure'
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
self.user_id = user.id
|
self.user_id = user.id
|
||||||
self.conference_id = conference.id
|
self.conference_id = conference.id
|
||||||
self.last4 = credit_card.display_number
|
self.last4 = credit_card.display_number
|
||||||
self.authorization_code = response.authorization
|
self.authorization_code = recieve.authorization
|
||||||
self.status = 'success'
|
self.status = 'success'
|
||||||
response.success?
|
recieve.success?
|
||||||
end
|
|
||||||
|
|
||||||
# method to test `purchase` method
|
|
||||||
def self.make_payment(user, conference, price_in_cents, payment)
|
|
||||||
payment.purchase(user, conference, price_in_cents)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -18,18 +18,12 @@ class Ticket < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def paid?(user)
|
def paid?(user)
|
||||||
ticket_purchases.find_by(user: user, paid: true).present?
|
ticket_purchases.paid.by_user(user).present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def quantity_bought_by(user, paid: false)
|
def quantity_bought_by(user, paid: false)
|
||||||
purchased_tickets = ticket_purchases.where(user_id: user.id, paid: paid)
|
purchased_tickets = ticket_purchases.paid.by_user(user)
|
||||||
quantity = 0
|
quantity = purchased_tickets.sum(:quantity)
|
||||||
if purchased_tickets
|
|
||||||
purchased_tickets.each do |ticket|
|
|
||||||
quantity += ticket.quantity
|
|
||||||
end
|
|
||||||
end
|
|
||||||
quantity
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def unpaid?(user)
|
def unpaid?(user)
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,10 @@ class TicketPurchase < ActiveRecord::Base
|
||||||
delegate :price_cents, to: :ticket
|
delegate :price_cents, to: :ticket
|
||||||
delegate :price_currency, to: :ticket
|
delegate :price_currency, to: :ticket
|
||||||
|
|
||||||
|
scope :paid, -> { where(paid: true) }
|
||||||
scope :unpaid, -> { where(paid: false) }
|
scope :unpaid, -> { where(paid: false) }
|
||||||
|
scope :by_conference, -> (conference) { where(conference_id: conference.id) }
|
||||||
|
scope :by_user, -> (user) { where(user_id: user.id) }
|
||||||
|
|
||||||
def self.purchase(conference, user, purchases)
|
def self.purchase(conference, user, purchases)
|
||||||
errors = []
|
errors = []
|
||||||
|
|
|
||||||
|
|
@ -94,18 +94,14 @@
|
||||||
= "(#{@tickets.first.price.symbol}#{humanized_money @total_price})"
|
= "(#{@tickets.first.price.symbol}#{humanized_money @total_price})"
|
||||||
%ul
|
%ul
|
||||||
.col.md-4
|
.col.md-4
|
||||||
- @ticket_payments.each_pair do |payment, tickets|
|
- @ticket_payments.each_pair do |ticket_id, tickets|
|
||||||
%strong
|
%li
|
||||||
Purchased
|
= @total_quantity[ticket_id]
|
||||||
- tickets.each do |ticket|
|
= tickets.first.title
|
||||||
%li
|
= word_pluralize(@total_quantity[ticket_id], 'Ticket')
|
||||||
= ticket.quantity
|
for
|
||||||
= ticket.title
|
= tickets.first.price.symbol
|
||||||
= word_pluralize(ticket.quantity, 'Ticket')
|
= humanized_money tickets.first.price
|
||||||
for
|
|
||||||
= ticket.price.symbol
|
|
||||||
= humanized_money ticket.price
|
|
||||||
%hr
|
|
||||||
- if @tickets.any?
|
- if @tickets.any?
|
||||||
= link_to 'Buy more tickets', conference_tickets_path(@conference.short_title), class: "btn btn-default"
|
= link_to 'Buy more tickets', conference_tickets_path(@conference.short_title), class: "btn btn-default"
|
||||||
- else
|
- else
|
||||||
|
|
|
||||||
|
|
@ -31,4 +31,4 @@
|
||||||
= number_to_currency @total_amount_to_pay
|
= number_to_currency @total_amount_to_pay
|
||||||
.form-group
|
.form-group
|
||||||
= f.submit "Charge Card", class: "btn btn-primary"
|
= f.submit "Charge Card", class: "btn btn-primary"
|
||||||
= link_to "Cancel", conference_conference_registrations_path, class: "btn btn-danger"
|
= link_to "Cancel", conference_conference_registration_path, class: "btn btn-danger"
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@
|
||||||
= button_tag(type: 'submit', class: 'btn btn-success btn-lg') do
|
= button_tag(type: 'submit', class: 'btn btn-success btn-lg') do
|
||||||
Continue
|
Continue
|
||||||
%i.fa.fa-shopping-cart
|
%i.fa.fa-shopping-cart
|
||||||
= link_to 'Cancel registration', conference_conference_registrations_path(@conference.short_title), method: :delete, class: 'btn btn-danger btn-sm'
|
= link_to 'Cancel registration', conference_conference_registration_path(@conference.short_title), method: :delete, class: 'btn btn-danger btn-sm'
|
||||||
.row
|
.row
|
||||||
.col-md-13
|
.col-md-13
|
||||||
%p.text-muted.text-center
|
%p.text-muted.text-center
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue