From d9748071340bc106570c29650df4a3b3a2333c74 Mon Sep 17 00:00:00 2001 From: shlok007 Date: Mon, 23 Jan 2017 09:19:15 +0530 Subject: [PATCH] Display paid and unpaid tickets in admin/tickets#show --- app/models/ticket.rb | 6 ++++-- app/views/admin/tickets/show.html.haml | 2 +- spec/models/ticket_spec.rb | 29 ++++++++++---------------- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index ec9c3436..3d127dbb 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -19,8 +19,10 @@ class Ticket < ActiveRecord::Base buyers.include?(user) end - def paid?(user) - ticket_purchases.paid.by_user(user).present? + def tickets_paid(user) + paid_tickets = quantity_bought_by(user, paid: true) + unpaid_tickets = quantity_bought_by(user, paid: false) + "#{paid_tickets}/#{paid_tickets+unpaid_tickets}" end def quantity_bought_by(user, paid: false) diff --git a/app/views/admin/tickets/show.html.haml b/app/views/admin/tickets/show.html.haml index 9f86a4a7..11882144 100644 --- a/app/views/admin/tickets/show.html.haml +++ b/app/views/admin/tickets/show.html.haml @@ -33,5 +33,5 @@ %td = buyer.affiliation %td - = @ticket.paid?(buyer) + = @ticket.tickets_paid(buyer) diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index 631ca59a..1fa8c38c 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -64,24 +64,6 @@ describe Ticket do end end - describe '#paid?' do - let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket) } - - context 'user has paid' do - before { ticket_purchase.update_attributes(paid: true) } - - it 'returns true' do - expect(ticket.paid?(user)).to eq(true) - end - end - - context 'user has not paid' do - it 'returns false' do - expect(ticket.paid?(user)).to eq(false) - end - end - end - describe '#unpaid?' do let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket) } @@ -101,6 +83,17 @@ describe Ticket do end end + describe '#tickets_paid' do + before do + create(:ticket_purchase, user: user, ticket: ticket) + create(:ticket_purchase, user: user, ticket: ticket, paid: true) + end + + it 'returns correct number of paid/total tickets' do + expect(ticket.tickets_paid(user)).to eq('10/20') + end + end + describe '#quantity_bought_by' do context 'user has not paid' do it 'returns the correct value if the user has bought this ticket' do