repairing code reviews

This commit is contained in:
Rishabh Saxena 2016-06-26 08:07:00 +02:00
parent ee71456060
commit f202c6b93b
5 changed files with 21 additions and 27 deletions

View file

@ -5,7 +5,7 @@ class PaymentsController < ApplicationController
authorize_resource :conference_registrations, class: Registration
def index
@payments = Payment.where(user_id: current_user.id)
@payments = current_user.payments
end
def new
@ -16,18 +16,24 @@ class PaymentsController < ApplicationController
@payment = Payment.new(payment_params)
@total_amount_to_pay = Ticket.total_price(@conference, current_user, 'f')
if @payment.valid?
if @payment.purchase(current_user, @conference)
if @payment.valid? && @payment.purchase(current_user, @conference, price_in_cents)
@payment.save
@update_ticket_purchases = TicketPurchase.update_paid_ticket_purchases(@conference, current_user, @payment)
return redirect_to conference_conference_registrations_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
end
end
if @payment.save
redirect_to conference_conference_registrations_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
else
render 'new'
end
end
private
def price_in_cents
(@payment.amount * 100).round
end
def payment_params
params.require(:payment).permit(:first_name, :last_name, :credit_card_number, :expiration_month, :expiration_year, :card_verification_value, :amount)
end

View file

@ -4,6 +4,7 @@ class TicketPurchasesController < ApplicationController
authorize_resource :conference_registrations, class: Registration
def create
TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: 'f')
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
if message.blank?
if current_user.ticket_purchases.where(paid: 'f').any?

View file

@ -81,7 +81,7 @@ class Ability
can :index, Ticket
can :manage, TicketPurchase, user_id: user.id
can [:index, :new, :create], Payment
can :manage, Payment, user_id: user.id
can [:create, :destroy], Subscription, user_id: user.id

View file

@ -17,8 +17,6 @@ class Payment < ActiveRecord::Base
validates :expiration_year, presence: true
validates :amount, presence: true, numericality: { greater_than: 0 }
validate :validate_card, on: create
enum status: {
unpaid: 0,
success: 1,
@ -36,20 +34,7 @@ class Payment < ActiveRecord::Base
)
end
def validate_card
unless credit_card.validate.empty?
credit_card.errors.full_messages.each do |message|
errors.add(:base, message)
end
end
credit_card.validate.empty?
end
def price_in_cents
(amount * 100).round
end
def purchase(user, conference)
def purchase(user, conference, price_in_cents)
begin
response = GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency)
rescue
@ -69,7 +54,7 @@ class Payment < ActiveRecord::Base
self.conference_id = conference.id
self.last4 = credit_card.display_number
self.authorization_code = response.authorization
self.status = 1
self.status = 'success'
response.success?
end
end

View file

@ -57,7 +57,9 @@ class TicketPurchase < ActiveRecord::Base
paid: 'f')
begin
paid_ticket_purchases.each do |ticket|
ticket.update_columns(paid: 't', payment_id: payment.id)
ticket.paid = 't'
ticket.payment_id = payment.id
ticket.save
end
end
end