From fd2635509f0ec2b3985e4ee2a4b20a56328422d3 Mon Sep 17 00:00:00 2001 From: Espartaco Palma Date: Thu, 23 Jan 2020 21:20:10 -0800 Subject: [PATCH 1/3] 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 From f5986f4160908c7d2435a5d49cdb1db8b3469620 Mon Sep 17 00:00:00 2001 From: Espartaco Palma Date: Thu, 23 Jan 2020 21:43:44 -0800 Subject: [PATCH 2/3] Using new accessor for city, country_name, venue name & street --- app/pdfs/ticket_pdf.rb | 6 +++--- app/views/conference_registrations/show.html.haml | 6 +++--- app/views/conferences/_conference_details.html.haml | 2 +- app/views/physical_tickets/show.html.haml | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/pdfs/ticket_pdf.rb b/app/pdfs/ticket_pdf.rb index 306d5870..c4408e7c 100644 --- a/app/pdfs/ticket_pdf.rb +++ b/app/pdfs/ticket_pdf.rb @@ -69,9 +69,9 @@ class TicketPdf < Prawn::Document draw_text @conference.title.to_s, at: [@mid_horizontal + 30, cursor - 30], size: 12 draw_text @conference.organization.name.to_s, at: [@mid_horizontal + 30, cursor - 50], size: 12 if @conference.venue - draw_text @conference.venue.name, at: [@mid_horizontal + 30, cursor - 70] - draw_text @conference.venue.street, at: [@mid_horizontal + 30, cursor - 90] - draw_text @conference.venue.city, at: [@mid_horizontal + 30, cursor - 110] + draw_text @conference.venue_name, at: [@mid_horizontal + 30, cursor - 70] + draw_text @conference.venue_street, at: [@mid_horizontal + 30, cursor - 90] + draw_text @conference.city, at: [@mid_horizontal + 30, cursor - 110] end move_up 130 move_down @mid_vertical diff --git a/app/views/conference_registrations/show.html.haml b/app/views/conference_registrations/show.html.haml index d060e412..359b9de7 100644 --- a/app/views/conference_registrations/show.html.haml +++ b/app/views/conference_registrations/show.html.haml @@ -9,9 +9,9 @@ -if @conference.venue at %strong - = "#{@conference.venue.name}," - = "#{@conference.venue.street}," - = "#{@conference.venue.city} / #{@conference.venue.country_name}." + = "#{@conference.venue_name}," + = "#{@conference.venue_street}," + = "#{@conference.city} / #{@conference.country_name}." %small = date_string(@conference.start_date, @conference.end_date) - unless @conference.code_of_conduct.blank? diff --git a/app/views/conferences/_conference_details.html.haml b/app/views/conferences/_conference_details.html.haml index 08b73023..ea33887e 100644 --- a/app/views/conferences/_conference_details.html.haml +++ b/app/views/conferences/_conference_details.html.haml @@ -12,7 +12,7 @@ = date_string(conference.start_date, conference.end_date) - if conference.venue %p - = "#{conference.venue.city}/#{conference.venue.country_name}" + = "#{conference.city}/#{conference.country_name}" - unless conference.description.blank? %p = markdown(conference.description) diff --git a/app/views/physical_tickets/show.html.haml b/app/views/physical_tickets/show.html.haml index 7dbc44da..ec8e7379 100644 --- a/app/views/physical_tickets/show.html.haml +++ b/app/views/physical_tickets/show.html.haml @@ -9,9 +9,9 @@ - if @conference.venue at %strong - #{@conference.venue.name}, - #{@conference.venue.street}, - #{@conference.venue.city} / #{@conference.venue.country_name}. + #{@conference.venue_name}, + #{@conference.venue_street}, + #{@conference.city} / #{@conference.country_name}. %small = date_string(@conference.start_date, @conference.end_date) .row From 5d79049e5970c01752002da53a58e0add5e2d131 Mon Sep 17 00:00:00 2001 From: Espartaco Palma Date: Thu, 23 Jan 2020 23:31:35 -0800 Subject: [PATCH 3/3] Linters gonna lint I was not aware the linting was also applied to specs --- .rubocop_todo.yml | 2 +- spec/models/conference_spec.rb | 64 +++++++++++++--------------------- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a7514f19..c968a091 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -434,7 +434,7 @@ Metrics/BlockNesting: # Offense count: 13 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 650 + Max: 652 # Offense count: 33 Metrics/CyclomaticComplexity: diff --git a/spec/models/conference_spec.rb b/spec/models/conference_spec.rb index b04869c6..1c3ecb3b 100755 --- a/spec/models/conference_spec.rb +++ b/spec/models/conference_spec.rb @@ -3,51 +3,37 @@ # frozen_string_literal: true require 'spec_helper' -describe Conference do +context 'Delegation' do + subject do + FactoryBot.create(:conference, start_date: 1.month.from_now, end_date: 2.month.from_now) + end - 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 + context 'Venue' do + context 'when venue has not been set' do + it 'the accessors should be nil' do + expect(subject.city).to eq(nil) + expect(subject.country_name).to eq(nil) + expect(subject.venue_name).to eq(nil) + 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 + context 'when venue has been set' do + it 'should delegate to venue' do + venue = FactoryBot.create(:venue) + subject.update(venue: venue) + expect(subject.city).to eq(venue.city) + expect(subject.country_name).to eq(venue.country_name) + expect(subject.venue_name).to eq(venue.name) + expect(subject.venue_street).to eq(venue.street) end end end +end + +describe Conference do + + let(:subject) { create(:conference, start_date: Date.new(2014, 06, 30), end_date: Date.new(2014, 06, 30)) } describe '#write_event_distribution_to_db' do