Commit 3f106db

mo khan <mo@mokhan.ca>
2016-05-21 16:04:28
skip import if gyms in city already exist.
1 parent a435221
Changed files (4)
app/jobs/import_gyms_job.rb
@@ -2,6 +2,8 @@ class ImportGymsJob < ActiveJob::Base
   queue_as :default
 
   def perform(location)
-    Gym.import(location.city) if location.present?
+    if location.present? && !Gym.closest_to(location).exists?
+      Gym.import(location.city)
+    end
   end
 end
app/models/gym.rb
@@ -61,6 +61,7 @@ class Gym < ActiveRecord::Base
   end
 
   def self.import(city, pages: 5)
+    return if Rails.env.test? || city.blank?
     (1..pages).each do |page|
       Gym.search_yelp(q: 'gym', city: city, page: page).each(&:save!)
     end
spec/jobs/import_gyms_job_spec.rb
@@ -15,4 +15,13 @@ describe ImportGymsJob do
     subject.perform(nil)
     expect(Gym).to_not have_received(:import)
   end
+
+  it 'skips the import of gyms in the city are already present' do
+    allow(Gym).to receive(:import)
+    create(:gym, location: location)
+
+    subject.perform(location)
+
+    expect(Gym).to_not have_received(:import)
+  end
 end
spec/models/gym_spec.rb
@@ -124,15 +124,14 @@ describe Gym do
     it "returns true when a dup is found" do
       subject.location = create(:portland)
       subject.save!
-      other = create(:gym, location: create(:portland))
+      create(:gym, location: create(:portland))
 
       expect(subject.duplicate?).to be_truthy
     end
 
     it 'returns true when another gym has the same yelp id' do
       subject.yelp_id = "hello-world"
-      subject.save!
-      other = create(:gym, yelp_id: subject.yelp_id)
+      create(:gym, yelp_id: subject.yelp_id)
 
       expect(subject.duplicate?).to be_truthy
     end