Class: RadCoreRails::ZipCode

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rad_core_rails/zip_code.rb

Class Method Summary collapse

Class Method Details

.distance_args(filter) ⇒ Object

Query arguments for the ‘distance_query` based on passed filter params.



52
53
54
55
56
57
# File 'lib/rad_core_rails/zip_code.rb', line 52

def distance_args(filter)
  zip_value = filter[:values].try(:first) || ''
  distance_miles = filter[:option] || 0

  [zip_value, zip_value, zip_value, distance_miles]
end

.distance_queryObject

Query for returning an array of zip codes around the zip-code sent to this function with a given radius in miles.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rad_core_rails/zip_code.rb', line 29

def distance_query
  <<-SQL
   WITH t AS (SELECT
   zip, (
     3959 * ACOS (
       LEAST(
         GREATEST(
           (
				COS(RADIANS((#{lat_sub_query})))
           	* COS(RADIANS(zip_codes.latitude))
           	* COS(RADIANS(zip_codes.longitude) - RADIANS((#{lng_sub_query})))
           	+ SIN(RADIANS((#{lat_sub_query})))
           	* SIN(RADIANS(zip_codes.latitude))
			), -1
		), 1
	)
     )
   ) AS distance
   FROM zip_codes) SELECT t.zip FROM t WHERE t.distance < ?
  SQL
end

.lat_sub_queryObject

Query for returning a one latitude for given zip-code



9
10
11
12
13
14
15
16
# File 'lib/rad_core_rails/zip_code.rb', line 9

def lat_sub_query
  <<-SQL
  SELECT zip_codes.latitude
  FROM zip_codes
  WHERE zip = ?
  LIMIT 1
  SQL
end

.lng_sub_queryObject

Query for returning a one longitude value for given zip-code



19
20
21
22
23
24
25
26
# File 'lib/rad_core_rails/zip_code.rb', line 19

def lng_sub_query
  <<-SQL
  SELECT zip_codes.longitude
  FROM zip_codes
  WHERE zip = ?
  LIMIT 1
  SQL
end