Skip to content

Commit

Permalink
Merge pull request #969 from transitland/update_computed_attrs-bug-fix
Browse files Browse the repository at this point in the history
Update computed attrs bug fix
  • Loading branch information
doublestranded committed Feb 14, 2017
2 parents 8e5258c + 44178a2 commit cb2f7aa
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/route_stop_patterns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def index
@rsps = @rsps.with_trips(params[:trips])
end
if params[:stops_visited].present?
@rsps = @rsps.with_stops(params[:stops_visited])
@rsps = @rsps.with_all_stops(params[:stops_visited])
end

# Includes
Expand Down
2 changes: 1 addition & 1 deletion app/models/changeset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def update_computed_attributes
self.stops_created_or_updated.each do |stop|
operators_to_update_convex_hull.merge(OperatorServingStop.where(stop: stop).map(&:operator))
end
rsps_to_update_distances.merge(RouteStopPattern.with_stops(self.stops_created_or_updated.map(&:onestop_id).join(',')))
rsps_to_update_distances.merge(RouteStopPattern.with_any_stops(self.stops_created_or_updated.map(&:onestop_id).join(',')))

operators_to_update_convex_hull.each { |operator|
operator.geometry = operator.recompute_convex_hull_around_stops
Expand Down
3 changes: 2 additions & 1 deletion app/models/route_stop_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ def tl_geometry(stop_points, issues)
end

scope :with_trips, -> (search_string) { where{trips.within(search_string)} }
scope :with_stops, -> (search_string) { where{stop_pattern.within(search_string)} }
scope :with_all_stops, -> (search_string) { where{stop_pattern.within(search_string)} }
scope :with_any_stops, -> (stop_onestop_ids) { where( "stop_pattern && ARRAY[?]::varchar[]", stop_onestop_ids ) }

def ordered_ssp_trip_chunks(&block)
if block
Expand Down
2 changes: 1 addition & 1 deletion app/models/stop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def csv_row_values

def update_stop_pattern_onestop_ids(old_onestop_ids, changeset)
old_onestop_ids = Array.wrap(old_onestop_ids)
RouteStopPattern.with_stops(old_onestop_ids.join(',')).each do |rsp|
RouteStopPattern.with_any_stops(old_onestop_ids.join(',')).each do |rsp|
rsp.stop_pattern.map! { |stop_onestop_id| old_onestop_ids.include?(stop_onestop_id) ? self.onestop_id : stop_onestop_id }
rsp.update_making_history(changeset: changeset)
end
Expand Down
23 changes: 18 additions & 5 deletions spec/models/route_stop_pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,25 @@
# end
# end

it 'can be found by stops' do
it '.with_all_stops' do
rsp = create(:route_stop_pattern, stop_pattern: @sp, geometry: @geom, onestop_id: @onestop_id)
expect(RouteStopPattern.with_stops('s-9q8yw8y448-bayshorecaltrainstation')).to match_array([rsp])
expect(RouteStopPattern.with_stops('s-9q8yw8y448-bayshorecaltrainstation,s-9q8yyugptw-sanfranciscocaltrainstation')).to match_array([rsp])
expect(RouteStopPattern.with_stops('s-9q9k659e3r-sanjosecaltrainstation')).to match_array([])
expect(RouteStopPattern.with_stops('s-9q8yw8y448-bayshorecaltrainstation,s-9q9k659e3r-sanjosecaltrainstation')).to match_array([])
expect(RouteStopPattern.with_all_stops('s-9q8yw8y448-bayshorecaltrainstation')).to match_array([rsp])
expect(RouteStopPattern.with_all_stops('s-9q8yw8y448-bayshorecaltrainstation,s-9q8yyugptw-sanfranciscocaltrainstation')).to match_array([rsp])
expect(RouteStopPattern.with_all_stops('s-9q9k659e3r-sanjosecaltrainstation')).to match_array([])
# Stop 's-9q9k659e3r-sanjosecaltrainstation' does not exist
expect(RouteStopPattern.with_all_stops('s-9q8yw8y448-bayshorecaltrainstation,s-9q9k659e3r-sanjosecaltrainstation')).to match_array([])
end

it '.with_any_stops' do
rsp = create(:route_stop_pattern, stop_pattern: @sp, geometry: @geom, onestop_id: @onestop_id)
expect(RouteStopPattern.with_any_stops(['s-9q8yw8y448-bayshorecaltrainstation'])).to match_array([rsp])
expect(RouteStopPattern.with_any_stops(['s-9q9k659e3r-sanjosecaltrainstation'])).to match_array([])
# Stop 's-9q9k659e3r-sanjosecaltrainstation' does not exist yet
expect(RouteStopPattern.with_any_stops(['s-9q8yw8y448-bayshorecaltrainstation','s-9q9k659e3r-sanjosecaltrainstation'])).to match_array([rsp])
# create Stop 's-9q9k659e3r-sanjosecaltrainstation'
stop = create(:stop, onestop_id: 's-9q9k659e3r-sanjosecaltrainstation')
rsp2 = create(:route_stop_pattern, stop_pattern: [@sp[0],stop.onestop_id], geometry: @geom)
expect(RouteStopPattern.with_any_stops(['s-9q8yw8y448-bayshorecaltrainstation','s-9q9k659e3r-sanjosecaltrainstation'])).to match_array([rsp, rsp2])
end

it 'can be found by trips' do
Expand Down

0 comments on commit cb2f7aa

Please sign in to comment.