Created
November 29, 2012 15:27
-
-
Save tdelam/4169793 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Profile < ActiveRecord::Base | |
belongs_to :user | |
acts_as_ferret | |
def city_locate | |
[city, province_state].join(", ") | |
end | |
def fullname | |
[firstname, lastname].join(" ") | |
end | |
def user_name | |
user.username | |
end | |
def age | |
return if birthdate.nil? | |
today = Date.today | |
if today.month >= birthdate.month and today.day >= birthdate.day | |
today.year - birthdate.year | |
else | |
today.year - birthdate.year - 1 | |
end | |
end | |
def self.find_by_asl(params) | |
where = [] | |
unless params[:min_age].blank? | |
where << "ADDDATE(birthdate, INTERVAL :min_age YEAR) < CURDATE()" | |
end | |
unless params[:max_age].blank? | |
where << "ADDDATE(birthdate, INTERVAL :max_age+1 YEAR) > CURDATE()" | |
end | |
where << "gender = :gender" unless params[:gender].blank? | |
zip_code = params[:zip_code] | |
unless zip_code.blank? and params[:miles].blank? | |
location = GeoDatum.find_by_zip_code(zip_code) | |
distance = sql_distance_away(location) | |
where << "#{distance} <= :miles" | |
end | |
if where.empty? | |
[] | |
else | |
find(:all, | |
:joins => "left join geo_data on geo_data.zip_code = profiles.postal_code", | |
:conditions => [where.join(" AND "), params], | |
:order => "lastname, firstname") | |
end | |
end | |
def location | |
if not postal_code.blank? and (city.blank? or province_state.blank?) | |
lookup = GeoDatum.find_by_zip_code(postal_code) | |
if lookup | |
self.city = lookup.city.capitalize_each if city.blank? | |
self.state = lookup.state if province_state.blank? | |
end | |
end | |
[city, province_state, postal_code].join(" ") | |
end | |
def avatar | |
Avatar.new(self) | |
end | |
private | |
def self.sql_distance_away(point) | |
h = "POWER(SIN((RADIANS(latitude - #{point.latitude}))/2.0),2) + " + | |
"COS(RADIANS(#{point.latitude})) * COS(RADIANS(latitude)) * " + | |
"POWER(SIN((RADIANS(longitude - #{point.longitude}))/2.0),2)" | |
r = 3956 | |
"2 * #{r} * ASIN(SQRT(#{h}))" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment