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

Commit

Permalink
Memoizing calls to route.stop_codes and route.stop_area codes, and us…
Browse files Browse the repository at this point in the history
…ing .length for route_operator check rather than .size in order to allow find_all_by_number_and_common_stop to be called with a different data generation scope for finding routes from the previous data generation that match a given route.
  • Loading branch information
crowbot committed Apr 4, 2012
1 parent ce6a529 commit 06c85da
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions app/models/route.rb
Expand Up @@ -83,11 +83,13 @@ def region_name
def stop_codes
stops.map{ |stop| stop.atco_code or stop.other_code }.uniq
end
memoize :stop_codes

def stop_area_codes
stop_areas = stops.map{ |stop| stop.stop_areas }.flatten
stop_areas.map{ |stop_area| stop_area.code }.uniq
end
memoize :stop_area_codes

def transport_mode_name
transport_mode.name
Expand Down Expand Up @@ -415,13 +417,20 @@ def self.full_find(id, scope)
# Return routes with this number and transport mode that have a stop or stop area in common with
# the route given
def self.find_all_by_number_and_common_stop(new_route, options={})
# If this is the first call to these methods on the new_route model (they are memoized), note
# that is will be executed in the current scope of the models concerned. i.e. whatever the data
# generation conditions for the current scope on the models are, will be applied.
stop_codes = new_route.stop_codes
stop_area_codes = new_route.stop_area_codes
# do we think we know the operator for this route? If so, return any route with the same operator that
# meets our other criteria. If we don't know the operator, or we pass the :use_operator_codes option
# only return routes with the same operator code (optionally only from the same admin area)
if ! options[:skip_operator_comparison]

if new_route.route_operators.size == 1 && !options[:use_operator_codes]
# Using length, not size on the route passed in to this method. Size requeries the database,
# length just looks at the length of the loaded association. When using changing scopes,
# as in the data_generations code, size will apply the current scope in database queries
# so may produce unexpected results.
if new_route.route_operators.length == 1 && !options[:use_operator_codes]
operator_clause = "AND route_operators.operator_id = ? "
operator_params = [new_route.route_operators.first.operator_id]
else
Expand Down Expand Up @@ -461,7 +470,7 @@ def self.find_all_by_number_and_common_stop(new_route, options={})
id_clause = " AND routes.id != ?"
id_params = [new_route.id]
end
stop_area_codes = new_route.stop_area_codes

condition_string = "number = ? AND transport_mode_id = ? #{operator_clause} #{id_clause}"
conditions = [condition_string, new_route.number, new_route.transport_mode.id]
conditions += operator_params
Expand Down

0 comments on commit 06c85da

Please sign in to comment.