Skip to content

Commit 1eb203e

Browse files
committed
Merged @wesley PR for WIP #31
2 parents 44bbcc1 + b835ce4 commit 1eb203e

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

app/models/opportunity.rb

+13-10
Original file line numberDiff line numberDiff line change
@@ -305,21 +305,24 @@ def assign_random_id
305305

306306
protected
307307
def set_location_city
308-
self.location_city = begin
309-
locations = []
310-
begin
311-
locations = self.team.cities.compact.select { |city| self.location.include?(city) }
312-
end while locations.blank? && add_opportunity_locations_to_team && set_location_city
313-
self.team.cities.join("|")
314-
end unless self.location.nil?
315-
errors.add(:location, "is not valid, please specify one or more cities separated by | (e.g. Miami, FL | San Francisco). put 'anywhere' if location doesn't matter") if !self.location.nil? and !valid_location_city
316-
self.location_city
308+
add_opportunity_locations_to_team
309+
locations = self.team.cities.compact.select { |city| self.location.include?(city) }
310+
311+
return if locations.blank? && anywhere?(self.location)
312+
313+
self.location_city = locations.join("|")
317314
end
318315

319316
def add_opportunity_locations_to_team
320317
geocoded_all = true
321318
self.location.split('|').each do |location_string|
322-
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
323326
end
324327
geocoded_all || nil
325328
end

app/views/opportunities/_form.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
-if @team.team_locations.any?
2020
=j.label :location do
2121
== Select one or more locations where the candidate must be located or #{link_to('add/manage team locations', edit_team_locations_path(@team))}
22-
=j.select(:location, @team.cities+["anywhere"], {:selected => @job.locations}, {:multiple => true})
22+
=j.select(:location, @team.cities+["anywhere"], {:selected => (@job.location.blank? ? [] : @job.location.split("|"))}, {:multiple => true})
2323
-else
2424
=j.label :location, 'Specify the city/location where the candidate must be located'
2525
=j.text_field :location

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)