Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
fix terrible load time on CallTool::Target
Browse files Browse the repository at this point in the history
  • Loading branch information
NealJMD committed Jul 26, 2017
1 parent 657408c commit 1e0d41a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 37 deletions.
13 changes: 3 additions & 10 deletions app/models/call_tool/target.rb
Expand Up @@ -28,19 +28,12 @@ def to_hash
Hash[MAIN_ATTRS.collect { |attr| [attr, send(attr)] }].merge(fields: fields)
end

def country_name
@country_name ||= ISO3166::Country[country_code]&.name
end

def country_name=(country_name)
@country_name = country_name
self.country_code = ISO3166::Country.find_country_by_name(country_name)&.alpha2
end

def country=(country)
if ISO3166::Country[country].present?
self.country_code = country
else
self.country_name = ISO3166::Country[country].name
elsif ISO3166::Country.find_country_by_name(country)
self.country_code = ISO3166::Country.find_country_by_name(country)&.alpha2
self.country_name = country
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/plugins/call_tool.rb
Expand Up @@ -59,7 +59,7 @@ def targets
end

def empty_cols
CallTool::Target::MAIN_ATTRS.select do |field|
::CallTool::Target::MAIN_ATTRS.select do |field|
targets.map { |t| t.try(field) }.compact.empty?
end
end
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Expand Up @@ -36,7 +36,7 @@ deployment:
- BRAINTREE_TOKEN_URL=$PRODUCTION_BRAINTREE_TOKEN_URL ./bin/build.sh
- ./bin/deploy.sh $CIRCLE_SHA1 'champaign' 'env-production' 'champaign-assets-production' 'logs3.papertrailapp.com:44107' 'actions.sumofus.org'
staging:
branch: 'feature/call-tool-dynamic-target-parser'
branch: 'fix-call-edit-load'
commands:
- BRAINTREE_TOKEN_URL=$STAGING_BRAINTREE_TOKEN_URL ./bin/build.sh
- ./bin/deploy.sh $CIRCLE_SHA1 'champaign' 'env-staging' 'champaign-assets-staging' 'logs3.papertrailapp.com:34848' 'action-staging.sumofus.org'
2 changes: 1 addition & 1 deletion spec/factories/plugins_call_tools.rb
Expand Up @@ -41,7 +41,7 @@
}

trait :with_country do
country_name {
country {
ISO3166::Country.find_country_by_alpha2(Faker::Address.country_code).name
}
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/call_tool_data.csv
@@ -1,4 +1,4 @@
country name,phone number,name,title,caller id
country,phone number,name,title,caller id
Germany,+448008085429,Abe Ben,MEP for Germany,+448008085400
United Kingdom,+448000119712,Claire Do,MEP South East England,
United Kingdom,+61261885481,Emily Fred,MEP for South West England,
Expand Down
22 changes: 12 additions & 10 deletions spec/models/call_tool/target_spec.rb
@@ -1,25 +1,27 @@
# frozen_string_literal: true

require 'rails_helper'

describe CallTool::Target do
let(:target) { CallTool::Target.new }

describe '#country_name=' do
describe '#country=' do
it 'assigns the country code if code is valid' do
target.country_name = 'United states'
target.country = 'US'
expect(target.country_code).to eq 'US'
expect(target.country_name).to eq 'United States of America'
end

it 'sets country_code to nil if name is invalid' do
target.country_name = 'Magic Country'
expect(target.country_code).to be_nil
it 'assigns the country code if name is valid' do
target.country = 'United states'
expect(target.country_code).to eq 'US'
expect(target.country_name).to eq 'United states'
end
end

describe '#country_name' do
it 'returns the name of the country matching the country_code' do
target.country_code = 'AR'
expect(target.country_name).to eq('Argentina')
it 'sets country_code to nil if name is invalid' do
target.country = 'Magic Country'
expect(target.country_code).to be_nil
expect(target.country_name).to be_nil
end
end

Expand Down
16 changes: 3 additions & 13 deletions spec/services/targets_parser_spec.rb
Expand Up @@ -5,7 +5,7 @@
describe CallTool::TargetsParser do
let(:csv_string) do
<<-EOS
Country name, State, phone number, Phone Extension, NAME, title, caller id, dynamic column
Country, State, phone number, Phone Extension, NAME, title, caller id, dynamic column
united kingdom, Greater London, 4410000000, 123, Claire Do, MEP South East England, 1234567, Dynamic
united kingdom, Greater London, 4411111111,, Emily Fred, MEP for South West England, 123
united kingdom, Brighton and Hove, 442222222,, George Harris, MEP for South West England, 123
Expand Down Expand Up @@ -34,22 +34,12 @@
expect(t.fields[:dynamic_column]).to eq('Dynamic')
end

it 'detects a `country_name` field and sets the country code' do
t = targets.first
expect(t.country_code).to eq('GB')
end

it 'detects a `country` field with a name and sets the country code' do
different_csv = csv_string.gsub(/Country name/i, 'country')
expect(different_csv.include?('ountry name')).to eq false
different_targets = CallTool::TargetsParser.parse_csv(different_csv)
t = different_targets.first
expect(t.country_code).to eq('GB')
expect(targets.first.country_code).to eq('GB')
end

it 'detects a `country` field with a code and sets it to country code' do
different_csv = csv_string.gsub(/Country name/i, 'country').gsub(/united kingdom/i, 'DE')
expect(different_csv.include?('ountry name')).to eq false
different_csv = csv_string.gsub(/united kingdom/i, 'DE')
different_targets = CallTool::TargetsParser.parse_csv(different_csv)
t = different_targets.first
expect(t.country_code).to eq('DE')
Expand Down

0 comments on commit 1e0d41a

Please sign in to comment.