diff --git a/app/controllers/conference_registrations_controller.rb b/app/controllers/conference_registrations_controller.rb index c2037318..e8809b28 100644 --- a/app/controllers/conference_registrations_controller.rb +++ b/app/controllers/conference_registrations_controller.rb @@ -27,8 +27,9 @@ class ConferenceRegistrationsController < ApplicationController end def show - @total_price = Ticket.total_price(@conference, current_user, paid: true) + @total_price = Ticket.total_price_user(@conference, current_user, paid: true) @tickets = current_user.ticket_purchases.by_conference(@conference).paid + @total_price_per_ticket = @tickets.group(:ticket_id).sum('amount_paid * quantity') @ticket_payments = @tickets.group_by(&:ticket_id) @total_quantity = @tickets.group(:ticket_id).sum(:quantity) end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index ce527472..ac1d1927 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -55,6 +55,11 @@ class Ticket < ActiveRecord::Base result ? result : Money.new(0, 'USD') end + def self.total_price_user(conference, user, paid: false) + tickets = TicketPurchase.where(conference: conference, user: user, paid: paid) + tickets.inject(0){ |sum, ticket| sum + (ticket.amount_paid * ticket.quantity) } + end + def tickets_sold ticket_purchases.paid.sum(:quantity) end diff --git a/app/views/conference_registrations/show.html.haml b/app/views/conference_registrations/show.html.haml index 83d6d89c..a50cf198 100644 --- a/app/views/conference_registrations/show.html.haml +++ b/app/views/conference_registrations/show.html.haml @@ -101,7 +101,7 @@ = word_pluralize(@total_quantity[ticket_id], 'Ticket') for = tickets.first.price.symbol - = humanized_money tickets.first.price + = humanized_money @total_price_per_ticket[ticket_id] %br - if @tickets.any? = link_to 'Get more tickets', conference_tickets_path(@conference.short_title), class: "btn btn-default" diff --git a/spec/controllers/conference_registration_controller_spec.rb b/spec/controllers/conference_registration_controller_spec.rb index 9afaaed8..a6f1503c 100644 --- a/spec/controllers/conference_registration_controller_spec.rb +++ b/spec/controllers/conference_registration_controller_spec.rb @@ -252,7 +252,7 @@ describe ConferenceRegistrationsController, type: :controller do end it 'does not assign price of purchased tickets to total_price and purchased tickets to tickets without payment' do - expect(assigns(:total_price)).to eq Money.new(0, 'USD') + expect(assigns(:total_price)).to eq 0 end end @@ -262,7 +262,7 @@ describe ConferenceRegistrationsController, type: :controller do end it 'assigns 0 dollars to total_price and empty array to tickets variables' do - expect(assigns(:total_price)).to eq Money.new(0, 'USD') + expect(assigns(:total_price)).to eq 0 expect(assigns(:tickets)).to match_array [] end end