From fd2635509f0ec2b3985e4ee2a4b20a56328422d3 Mon Sep 17 00:00:00 2001 From: Espartaco Palma Date: Thu, 23 Jan 2020 21:20:10 -0800 Subject: [PATCH] Introducing delegation for city and country name (Conference - Venue Using ActiveSupport delegate macro. --- app/models/conference.rb | 3 +++ spec/models/conference_spec.rb | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/app/models/conference.rb b/app/models/conference.rb index be3c36ad..8d0b786d 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -25,6 +25,9 @@ class Conference < ApplicationRecord has_one :email_settings, dependent: :destroy has_one :program, dependent: :destroy has_one :venue, dependent: :destroy + delegate :city, :country_name, to: :venue, allow_nil: true + delegate :name, :street, to: :venue, prefix: true, allow_nil: true + has_many :physical_tickets, through: :ticket_purchases has_many :ticket_purchases, dependent: :destroy has_many :payments, dependent: :destroy diff --git a/spec/models/conference_spec.rb b/spec/models/conference_spec.rb index 64cc1aea..b04869c6 100755 --- a/spec/models/conference_spec.rb +++ b/spec/models/conference_spec.rb @@ -7,6 +7,48 @@ describe Conference do let(:subject) { create(:conference, start_date: Date.new(2014, 06, 30), end_date: Date.new(2014, 06, 30)) } + context 'Delegation' do + context 'Venue' do + context 'when venue has not been set' do + it do + expect(subject.city).to eq(nil) + end + it do + expect(subject.country_name).to eq(nil) + end + it do + expect(subject.venue_name).to eq(nil) + end + it do + expect(subject.venue_street).to eq(nil) + end + end + + context 'when venue has been set' do + before(:each) do + subject.update(venue: venue) + end + + let(:venue) do + FactoryBot.create(:venue) + end + + it do + expect(subject.city).to eq(venue.city) + end + it do + expect(subject.country_name).to eq(venue.country_name) + end + it do + expect(subject.venue_name).to eq(venue.name) + end + it do + expect(subject.venue_street).to eq(venue.street) + end + end + end + end + describe '#write_event_distribution_to_db' do it 'updates pending conferences' do