use short-form presence validator.
use two-argument form. add unpaid scope. repair tests.
This commit is contained in:
parent
893f517ab0
commit
888c7a8524
6 changed files with 24 additions and 36 deletions
|
|
@ -28,7 +28,7 @@ class ConferenceRegistrationsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@total_price = Ticket.total_price(@conference, current_user, :paid => true)
|
@total_price = Ticket.total_price(@conference, current_user, paid: true)
|
||||||
@tickets = current_user.ticket_purchases.where(conference_id: @conference.id, paid: true)
|
@tickets = current_user.ticket_purchases.where(conference_id: @conference.id, paid: true)
|
||||||
@ticket_payments = @tickets.group_by(&:payment_id)
|
@ticket_payments = @tickets.group_by(&:payment_id)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ class TicketPurchasesController < ApplicationController
|
||||||
TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: false)
|
TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: false)
|
||||||
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
|
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
|
||||||
if message.blank?
|
if message.blank?
|
||||||
if current_user.ticket_purchases.where(paid: false).any?
|
if current_user.ticket_purchases.unpaid.any?
|
||||||
redirect_to new_conference_payment_path, notice: 'Please pay here to purchase tickets.'
|
redirect_to new_conference_payment_path, notice: 'Please pay here to purchase tickets.'
|
||||||
else
|
else
|
||||||
redirect_to conference_conference_registration_path(@conference.short_title)
|
redirect_to conference_conference_registration_path(@conference.short_title)
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,8 @@ class Ticket < ActiveRecord::Base
|
||||||
ticket_purchases.find_by(user: user, paid: true).present?
|
ticket_purchases.find_by(user: user, paid: true).present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def quantity_bought_by(user, hashed_paid)
|
def quantity_bought_by(user, paid: false)
|
||||||
purchased_tickets = ticket_purchases.where(user_id: user.id, paid: hashed_paid[:paid])
|
purchased_tickets = ticket_purchases.where(user_id: user.id, paid: paid)
|
||||||
quantity = 0
|
quantity = 0
|
||||||
if purchased_tickets
|
if purchased_tickets
|
||||||
purchased_tickets.each do |ticket|
|
purchased_tickets.each do |ticket|
|
||||||
|
|
@ -36,16 +36,16 @@ class Ticket < ActiveRecord::Base
|
||||||
ticket_purchases.find_by(user: user, paid: false).present?
|
ticket_purchases.find_by(user: user, paid: false).present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def total_price(user, hashed_paid)
|
def total_price(user, paid: false)
|
||||||
quantity_bought_by(user, hashed_paid) * price
|
quantity_bought_by(user, paid: paid) * price
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.total_price(conference, user, hashed_paid)
|
def self.total_price(conference, user, paid: false)
|
||||||
tickets = Ticket.where(conference_id: conference.id)
|
tickets = Ticket.where(conference_id: conference.id)
|
||||||
result = nil
|
result = nil
|
||||||
begin
|
begin
|
||||||
tickets.each do |ticket|
|
tickets.each do |ticket|
|
||||||
price = ticket.total_price(user, hashed_paid)
|
price = ticket.total_price(user, paid: paid)
|
||||||
if result
|
if result
|
||||||
result += price unless price.zero?
|
result += price unless price.zero?
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ class TicketPurchase < ActiveRecord::Base
|
||||||
delegate :price_cents, to: :ticket
|
delegate :price_cents, to: :ticket
|
||||||
delegate :price_currency, to: :ticket
|
delegate :price_currency, to: :ticket
|
||||||
|
|
||||||
|
scope :unpaid, -> { where(paid: false) }
|
||||||
|
|
||||||
def self.purchase(conference, user, purchases)
|
def self.purchase(conference, user, purchases)
|
||||||
errors = []
|
errors = []
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
|
|
|
||||||
|
|
@ -7,33 +7,19 @@ describe Payment do
|
||||||
expect(build(:payment)).to be_valid
|
expect(build(:payment)).to be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'is not valid without a first_name' do
|
it { is_expected.to validate_presence_of(:first_name) }
|
||||||
should validate_presence_of(:first_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is not valid without a last_name' do
|
it { is_expected.to validate_presence_of(:last_name) }
|
||||||
should validate_presence_of(:last_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is not valid without a credit_card_number' do
|
it { is_expected.to validate_presence_of(:credit_card_number) }
|
||||||
should validate_presence_of(:credit_card_number)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is not valid without a card_verification_value' do
|
it { is_expected.to validate_presence_of(:card_verification_value) }
|
||||||
should validate_presence_of(:card_verification_value)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is not valid without a expiration_month' do
|
it { is_expected.to validate_presence_of(:expiration_month) }
|
||||||
should validate_presence_of(:expiration_month)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is not valid without a expiration_year' do
|
it { is_expected.to validate_presence_of(:expiration_year) }
|
||||||
should validate_presence_of(:expiration_year)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is not valid without a amount' do
|
it { is_expected.to validate_presence_of(:amount) }
|
||||||
should validate_presence_of(:amount)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is not valid with a amount equals zero' do
|
it 'is not valid with a amount equals zero' do
|
||||||
should_not allow_value(0).for(:amount)
|
should_not allow_value(0).for(:amount)
|
||||||
|
|
|
||||||
|
|
@ -108,11 +108,11 @@ describe Ticket do
|
||||||
user: user,
|
user: user,
|
||||||
ticket: ticket,
|
ticket: ticket,
|
||||||
quantity: 20)
|
quantity: 20)
|
||||||
expect(ticket.quantity_bought_by(user, :paid => false)).to eq(20)
|
expect(ticket.quantity_bought_by(user, paid: false)).to eq(20)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns zero if the user has not bought this ticket' do
|
it 'returns zero if the user has not bought this ticket' do
|
||||||
expect(ticket.quantity_bought_by(user, :paid => false)).to eq(0)
|
expect(ticket.quantity_bought_by(user, paid: false)).to eq(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -122,11 +122,11 @@ describe Ticket do
|
||||||
user: user,
|
user: user,
|
||||||
ticket: ticket,
|
ticket: ticket,
|
||||||
quantity: 20)
|
quantity: 20)
|
||||||
expect(ticket.total_price(user, :paid => false)).to eq(Money.new(100000, 'USD'))
|
expect(ticket.total_price(user, paid: false)).to eq(Money.new(100000, 'USD'))
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns zero if the user has not bought this ticket' do
|
it 'returns zero if the user has not bought this ticket' do
|
||||||
expect(ticket.total_price(user, :paid => false)).to eq(Money.new(0, 'USD'))
|
expect(ticket.total_price(user, paid: false)).to eq(Money.new(0, 'USD'))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -136,7 +136,7 @@ describe Ticket do
|
||||||
describe 'user has bought' do
|
describe 'user has bought' do
|
||||||
context 'no tickets' do
|
context 'no tickets' do
|
||||||
it 'returns zero' do
|
it 'returns zero' do
|
||||||
expect(Ticket.total_price(conference, user, :paid => false)).to eq(Money.new(0, 'USD'))
|
expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(0, 'USD'))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -146,7 +146,7 @@ describe Ticket do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns the correct total price' do
|
it 'returns the correct total price' do
|
||||||
expect(Ticket.total_price(conference, user, :paid => false)).to eq(Money.new(100000, 'USD'))
|
expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(100000, 'USD'))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -158,7 +158,7 @@ describe Ticket do
|
||||||
|
|
||||||
it 'returns the correct total price' do
|
it 'returns the correct total price' do
|
||||||
total_price = Money.new(200000, 'USD')
|
total_price = Money.new(200000, 'USD')
|
||||||
expect(Ticket.total_price(conference, user, :paid => false)).to eq(total_price)
|
expect(Ticket.total_price(conference, user, paid: false)).to eq(total_price)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue