From 33d32b9294432d77ff9932bf591cdadfad7a0412 Mon Sep 17 00:00:00 2001 From: ViditChitkara Date: Fri, 22 Dec 2017 23:05:13 +0530 Subject: [PATCH] currency of a single ticket could be updated now closes #1913 fixed travis faliures minor changes removed empty file added tests regarding currency updation --- app/models/ticket.rb | 4 +++- spec/models/ticket_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 80e7f3e0..6ae4a91d 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -76,7 +76,9 @@ class Ticket < ApplicationRecord private def tickets_of_conference_have_same_currency - unless Ticket.where(conference_id: conference_id).all?{|t| t.price_currency == price_currency } + tickets = Ticket.where(conference_id: conference_id) + return if tickets.count.zero? || (tickets.count == 1 && self == tickets.first) + unless tickets.all?{|t| t.price_currency == price_currency } errors.add(:price_currency, 'is different from the existing tickets of this conference.') end end diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index 1ffbcd2f..d54dc5cb 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -6,6 +6,7 @@ describe Ticket do let(:user) { create(:user) } describe 'validation' do + it 'has a valid factory' do expect(build(:ticket)).to be_valid end @@ -177,4 +178,25 @@ describe Ticket do end end end + + describe 'currency updation' do + context 'when more than one ticket exist for a conference' do + it 'should not allow currency update' do + ticket.update(price_currency: 'INR') + expected_error_message = 'Price currency is different from the existing tickets of this conference.' + expect(ticket.errors.full_messages).to eq([expected_error_message]) + end + end + + context 'when a single ticket exists for a conference' do + before do + ticket.destroy + end + + it 'should allow currency update' do + free_ticket = Ticket.first + expect { free_ticket.update_attributes(price_currency: 'INR') }.to change { free_ticket.reload.price_currency }.from('USD').to('INR') + end + end + end end