From f202c6b93b6b5f35d80bd5fc8184b0d38138b75a Mon Sep 17 00:00:00 2001 From: Rishabh Saxena Date: Sun, 26 Jun 2016 08:07:00 +0200 Subject: [PATCH] repairing code reviews --- app/controllers/payments_controller.rb | 22 ++++++++++++------- .../ticket_purchases_controller.rb | 1 + app/models/ability.rb | 2 +- app/models/payment.rb | 19 ++-------------- app/models/ticket_purchase.rb | 4 +++- 5 files changed, 21 insertions(+), 27 deletions(-) diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index bd633ed2..a60437a8 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -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) - @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 + 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) + 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 - render 'new' 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 diff --git a/app/controllers/ticket_purchases_controller.rb b/app/controllers/ticket_purchases_controller.rb index 571139e4..a921839e 100644 --- a/app/controllers/ticket_purchases_controller.rb +++ b/app/controllers/ticket_purchases_controller.rb @@ -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? diff --git a/app/models/ability.rb b/app/models/ability.rb index 047812a6..8bdcd6d7 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -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 diff --git a/app/models/payment.rb b/app/models/payment.rb index 75e4a180..5694303d 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -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 diff --git a/app/models/ticket_purchase.rb b/app/models/ticket_purchase.rb index 0ca5ae49..8fbc9c49 100644 --- a/app/models/ticket_purchase.rb +++ b/app/models/ticket_purchase.rb @@ -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