From 668df627b1c02f4307729d4dabd866a4292608e5 Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Fri, 7 Sep 2012 17:54:46 -0400 Subject: [PATCH] Make select_addon method more general. Also rename :ignore to :geo_only (more descriptive/specific--we might introduce other options in the future and :ignore is kind of vague). --- lib/geocoder/stores/active_record.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/geocoder/stores/active_record.rb b/lib/geocoder/stores/active_record.rb index 96b11e07..426622e8 100644 --- a/lib/geocoder/stores/active_record.rb +++ b/lib/geocoder/stores/active_record.rb @@ -148,9 +148,7 @@ module Geocoder::Store distance = full_distance_from_sql(latitude, longitude, options) conditions = ["#{distance} <= ?", radius] default_near_scope_options(latitude, longitude, radius, options).merge( - :select => select_addon(options) + - "#{distance} AS distance" + - (bearing ? ", #{bearing} AS bearing" : ""), + :select => select_clause(options[:select], distance, bearing), :conditions => add_exclude_condition(conditions, options[:exclude]) ) end @@ -222,18 +220,22 @@ module Geocoder::Store [b[0], b[2], b[1], b[3] ] default_near_scope_options(latitude, longitude, radius, options).merge( - :select => select_addon(options) + - "#{distance} AS distance" + - (bearing ? ", #{bearing} AS bearing" : ""), + :select => select_clause(options[:select], distance, bearing), :conditions => add_exclude_condition(conditions, options[:exclude]) ) end ## - # Select string to add + # Generate the SELECT clause. # - def select_addon(options) - options[:select] == :ignore ? "" : "#{options[:select] || full_column_name("*")}, " + def select_clause(columns, distance, bearing) + if columns == :geo_only + clause = "" + else + clause = (columns || full_column_name("*")) + ", " + end + clause + "#{distance} AS distance" + + (bearing ? ", #{bearing} AS bearing" : "") end ## -- GitLab