Permalink
Browse files

Add :conditions to options on near method for ActiveRecord

  • Loading branch information...
1 parent 2642ddb commit 0d855d732b4565886a2b3123a28a09b7f858af78 @chucre committed Feb 24, 2012
Showing with 19 additions and 2 deletions.
  1. +19 −2 lib/geocoder/stores/active_record.rb
@@ -141,11 +141,13 @@ def full_near_scope_options(latitude, longitude, radius, options)
distance = full_distance_from_sql(latitude, longitude, options)
conditions = ["#{distance} <= ?", radius]
+ conditions = add_exclude_condition(conditions, options[:exclude])
+ conditions = add_condition(conditions, options[:conditions])
default_near_scope_options(latitude, longitude, radius, options).merge(
:select => "#{options[:select] || "#{table_name}.*"}, " +
"#{distance} AS distance" +
(bearing ? ", #{bearing} AS bearing" : ""),
- :conditions => add_exclude_condition(conditions, options[:exclude])
+ :conditions => conditions
)
end
@@ -210,11 +212,13 @@ def approx_near_scope_options(latitude, longitude, radius, options)
"#{lat_attr} BETWEEN ? AND ? AND #{lon_attr} BETWEEN ? AND ?"] +
[b[0], b[2], b[1], b[3]
]
+ conditions = add_exclude_condition(conditions, options[:exclude])
+ conditions = add_condition(conditions, options[:conditions])
default_near_scope_options(latitude, longitude, radius, options).merge(
:select => "#{options[:select] || "#{table_name}.*"}, " +
"#{distance} AS distance" +
(bearing ? ", #{bearing} AS bearing" : ""),
- :conditions => add_exclude_condition(conditions, options[:exclude])
+ :conditions => conditions
)
end
@@ -240,6 +244,19 @@ def add_exclude_condition(conditions, exclude)
end
conditions
end
+
+ ##
+ # Add conditions indicated by user
+ # The given conditions MUST be an array.
+ #
+ def add_conditions(conditions, new_conditions)
+ if new_conditions
+ conditions[0] << (" AND "+new_conditions[0])
+ new_conditions.shift
+ conditions += new_conditions
+ end
+ conditions
+ end
end
##

0 comments on commit 0d855d7

Please sign in to comment.