pass paid value as hash, improve code reviews

This commit is contained in:
Rishabh Saxena 2016-07-08 20:24:56 +05:30
parent 8b03f1be74
commit 893f517ab0
7 changed files with 32 additions and 34 deletions

View file

@ -28,8 +28,8 @@ class ConferenceRegistrationsController < ApplicationController
end
def show
@total_price = Ticket.total_price(@conference, current_user, true)
@tickets = current_user.ticket_purchases.where(conference_id: @conference.id)
@total_price = Ticket.total_price(@conference, current_user, :paid => true)
@tickets = current_user.ticket_purchases.where(conference_id: @conference.id, paid: true)
@ticket_payments = @tickets.group_by(&:payment_id)
end

View file

@ -9,20 +9,18 @@ class PaymentsController < ApplicationController
end
def new
@total_amount_to_pay = Ticket.total_price(@conference, current_user, false)
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
end
def create
@payment = Payment.new(payment_params)
@total_amount_to_pay = Ticket.total_price(@conference, current_user, false)
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
if @payment.valid? && @payment.purchase(current_user, @conference, price_in_cents)
if @payment.save
update_paid_ticket_purchases(@conference, current_user, @payment)
redirect_to conference_conference_registrations_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
else
render 'new'
end
if @payment.valid? && @payment.purchase(current_user, @conference, price_in_cents) && @payment.save
update_paid_ticket_purchases(@conference, current_user, @payment)
redirect_to conference_conference_registrations_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
else
render 'new'
end
end
@ -36,12 +34,10 @@ class PaymentsController < ApplicationController
paid_ticket_purchases = TicketPurchase.where(conference_id: conference.id,
user_id: user.id,
paid: false)
begin
paid_ticket_purchases.each do |ticket|
ticket.paid = true
ticket.payment_id = payment.id
ticket.save
end
paid_ticket_purchases.each do |ticket|
ticket.paid = true
ticket.payment_id = payment.id
ticket.save
end
end

View file

@ -21,11 +21,11 @@ class Ticket < ActiveRecord::Base
ticket_purchases.find_by(user: user, paid: true).present?
end
def quantity_bought_by(user, paid)
result = ticket_purchases.where(user_id: user.id, paid: paid)
def quantity_bought_by(user, hashed_paid)
purchased_tickets = ticket_purchases.where(user_id: user.id, paid: hashed_paid[:paid])
quantity = 0
if result
result.each do |ticket|
if purchased_tickets
purchased_tickets.each do |ticket|
quantity += ticket.quantity
end
end
@ -36,16 +36,16 @@ class Ticket < ActiveRecord::Base
ticket_purchases.find_by(user: user, paid: false).present?
end
def total_price(user, paid)
quantity_bought_by(user, paid) * price
def total_price(user, hashed_paid)
quantity_bought_by(user, hashed_paid) * price
end
def self.total_price(conference, user, paid)
def self.total_price(conference, user, hashed_paid)
tickets = Ticket.where(conference_id: conference.id)
result = nil
begin
tickets.each do |ticket|
price = ticket.total_price(user, paid)
price = ticket.total_price(user, hashed_paid)
if result
result += price unless price.zero?
else

View file

@ -5,7 +5,7 @@
%h1
Payment Attempts for
= @conference.title
- if !flash[:notice].blank?
- if flash[:notice].present?
.alert.alert-success
= flash[:notice]
%table.table.table-bordered.table-striped