Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b835ce4

Browse files
committedJun 30, 2014
Rewrite location add function and add tests
1 parent f263b60 commit b835ce4

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
 

‎app/models/opportunity.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,13 @@ def set_location_city
316316
def add_opportunity_locations_to_team
317317
geocoded_all = true
318318
self.location.split('|').each do |location_string|
319-
geocoded_all &&= self.team.team_locations.where(conditions: ['address LIKE ?', "%#{location_string}%"]).exists? or anywhere?(location_string) ? false : self.team.team_locations.build(address: location_string, name: location_string).geocode
319+
# skip if location is anywhere or already exists
320+
if anywhere?(location_string) || self.team.team_locations.where(address: /.*#{location_string}.*/).count > 0
321+
geocoded_all = false
322+
next
323+
end
324+
325+
geocoded_all &&= self.team.team_locations.build(address: location_string, name: location_string).geocode
320326
end
321327
geocoded_all || nil
322328
end

‎spec/models/opportunity_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,37 @@
101101
job.has_application_from?(user).should be_true
102102
end
103103
end
104+
105+
describe "changing job location" do
106+
it "should set location_city" do
107+
job = Fabricate(:job)
108+
job.location = "Amsterdam|San Francisco"
109+
job.save
110+
(job.location_city.split("|") - ["Amsterdam", "San Francisco"]).should == []
111+
end
112+
113+
it "should not add anywhere to location_city" do
114+
job = Fabricate(:job)
115+
job.location = "Amsterdam|San Francisco|anywhere"
116+
job.save
117+
(job.location_city.split("|") - ["Amsterdam", "San Francisco"]).should == []
118+
end
119+
120+
it "should update location_city with changes" do
121+
job = Fabricate(:job)
122+
job.location = "Amsterdam|San Francisco"
123+
job.save
124+
(job.location_city.split("|") - ["Amsterdam", "San Francisco"]).should == []
125+
job.location = "Amsterdam"
126+
job.save
127+
job.location_city.should == "Amsterdam"
128+
end
129+
130+
it "should not add existing locations to the team" do
131+
job = Fabricate(:job)
132+
job.location = "San Francisco"
133+
job.save
134+
job.team.team_locations.count.should === 1
135+
end
136+
end
104137
end

0 commit comments

Comments
 (0)
Please sign in to comment.