Skip to content

Instantly share code, notes, and snippets.

@dsauerbrun
Last active May 9, 2020 00:50
Show Gist options
  • Save dsauerbrun/5dfc7d122182f23872cd2865de3b31a3 to your computer and use it in GitHub Desktop.
Save dsauerbrun/5dfc7d122182f23872cd2865de3b31a3 to your computer and use it in GitHub Desktop.
class Location < ActiveRecord::Base
has_paper_trail
acts_as_mappable :lat_column_name => :latitude,:lng_column_name => :longitude
belongs_to :grade
validates_presence_of :slug
has_and_belongs_to_many :grades
has_and_belongs_to_many :transportations
has_and_belongs_to_many :climbing_types
has_and_belongs_to_many :seasons
#FIX ME REMOVE accommodations
has_and_belongs_to_many :accommodations
has_many :info_sections
has_many :accommodation_location_details
has_many :food_option_location_details
has_one :primary_transportation
belongs_to :user
has_attached_file :home_thumb, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :home_thumb, :content_type => /\Aimage\/.*\Z/
def continent_enum
['Asia','Australia','North America', 'South America','Africa','Europe','Antarctica']
end
def to_param
slug
end
def get_locations_within_miles(miles)
begin
locations = Location.all
close_locations = []
locations.each do |location|
if self.distance_to(location) < miles and !self.eql?(location)
close_locations << location
end
end
return close_locations
rescue
return []
end
end
def date_range(seasons = nil)
if seasons.nil?
months = self.seasons
else
months = seasons
end
range_string = ''
month_array = {}
months.each do |month|
month_array[month.numerical_value] = month.name
end
month_array = Hash[month_array.sort]
previous_month = 0
ranges = []
counter = 0
#this variable will be set to the month to stop at in case we have an edge case of december - january
wrapper_break_month = -1
if month_array.length == 12
return 'Jan - Dec'
end
month_array.each do |numerical,month|
counter += 1
#first month of the range
if previous_month == 0
#corner case for if january and december are on
if month_array.has_key? 12 and numerical == 1
#get latest month
latest_month = 13
month_array.clone.to_a.reverse.each do |month_num, month_str|
if latest_month - 1 == month_num
latest_month = month_num
end
end
ranges.push(month_array[latest_month])
range_string << month_array[latest_month][0...3]
wrapper_break_month = latest_month
else
ranges.push(month)
range_string << month[0...3]
end
end
if counter == month_array.length and previous_month != 0
if !ranges.include?(month_array[previous_month])
if wrapper_break_month == numerical
ranges.push(month_array[previous_month])
range_string << ' - ' << month_array[previous_month][0...3]
else
ranges.push(month[previous_month])
range_string << ' - ' << month[0...3]
end
else
ranges.push(month)
range_string << ', ' << month[0...3]
end
elsif (previous_month !=0 and previous_month+1 != numerical)
#if the previous month already exists in the array dont push it again(will happen if there is a one month window for a location)
if !ranges.include?(month_array[previous_month])
#corner case for if we had a wrapping date range(IE. December-january, we want to print the month without a comma then kill the loop
if wrapper_break_month == numerical
ranges.push(month_array[previous_month])
range_string << ' - ' << month_array[previous_month][0...3]
break
else
ranges.push(month_array[previous_month])
range_string << ' - ' << month_array[previous_month][0...3] << ', '
end
else
if wrapper_break_month == numerical
if !ranges.include?(month_array[previous_month])
ranges.push(month_array[previous_month])
range_string << ' - ' << month_array[previous_month][0...3]
end
break
else
range_string << ', '
end
end
ranges.push(month)
range_string << month[0...3]
end
previous_month = numerical
end
return range_string
end
def get_accommodations
accommodations = []
self.accommodation_location_details.each do |accommodation|
accommObj = {}
accommObj['url'] = accommodation.accommodation.icon.url
accommObj['name'] = accommodation.accommodation.name
accommObj['cost'] = accommodation.cost
accommObj['id'] = accommodation.accommodation.id
accommodations.push(accommObj)
end
return accommodations
end
def get_limited_location_json
json_return = self
json_return[:home_thumb] = self.home_thumb.url
json_return[:climbing_types] = self.get_climbing_types
json_return[:grades] = self.grades.collect { |grade| grade.html_attributes }
json_return[:date_range] = self.date_range
json_return[:accommodations] = self.get_accommodations
return json_return
end
end
class Location < ActiveRecord::Base
has_paper_trail
acts_as_mappable :lat_column_name => :latitude,:lng_column_name => :longitude
belongs_to :grade
validates_presence_of :slug
has_and_belongs_to_many :grades
has_and_belongs_to_many :transportations
has_and_belongs_to_many :climbing_types
has_and_belongs_to_many :seasons
#FIX ME REMOVE accommodations
has_and_belongs_to_many :accommodations
has_many :info_sections
has_many :accommodation_location_details
has_many :food_option_location_details
has_one :primary_transportation
belongs_to :user
has_attached_file :home_thumb, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :home_thumb, :content_type => /\Aimage\/.*\Z/
def continent_enum
['Asia','Australia','North America', 'South America','Africa','Europe','Antarctica']
end
def to_param
slug
end
def get_locations_within_miles(miles)
begin
locations = Location.all
close_locations = []
locations.each do |location|
if self.distance_to(location) < miles and !self.eql?(location)
close_locations << location
end
end
return close_locations
rescue
return []
end
end
def date_range(seasons = nil)
if seasons.nil?
months = self.seasons
else
months = seasons
end
range_string = ''
month_array = {}
months.each do |month|
month_array[month.numerical_value] = month.name
end
month_array = Hash[month_array.sort]
previous_month = 0
ranges = []
counter = 0
#this variable will be set to the month to stop at in case we have an edge case of december - january
wrapper_break_month = -1
if month_array.length == 12
return 'Jan - Dec'
end
month_array.each do |numerical,month|
counter += 1
#first month of the range
if previous_month == 0
#corner case for if january and december are on
if month_array.has_key? 12 and numerical == 1
#get latest month
latest_month = 13
month_array.clone.to_a.reverse.each do |month_num, month_str|
if latest_month - 1 == month_num
latest_month = month_num
end
end
ranges.push(month_array[latest_month])
range_string << month_array[latest_month][0...3]
wrapper_break_month = latest_month
else
ranges.push(month)
range_string << month[0...3]
end
end
if counter == month_array.length and previous_month != 0
if !ranges.include?(month_array[previous_month])
if wrapper_break_month == numerical
ranges.push(month_array[previous_month])
range_string << ' - ' << month_array[previous_month][0...3]
else
ranges.push(month[previous_month])
range_string << ' - ' << month[0...3]
end
else
ranges.push(month)
range_string << ', ' << month[0...3]
end
elsif (previous_month !=0 and previous_month+1 != numerical)
#if the previous month already exists in the array dont push it again(will happen if there is a one month window for a location)
if !ranges.include?(month_array[previous_month])
#corner case for if we had a wrapping date range(IE. December-january, we want to print the month without a comma then kill the loop
if wrapper_break_month == numerical
ranges.push(month_array[previous_month])
range_string << ' - ' << month_array[previous_month][0...3]
break
else
ranges.push(month_array[previous_month])
range_string << ' - ' << month_array[previous_month][0...3] << ', '
end
else
if wrapper_break_month == numerical
if !ranges.include?(month_array[previous_month])
ranges.push(month_array[previous_month])
range_string << ' - ' << month_array[previous_month][0...3]
end
break
else
range_string << ', '
end
end
ranges.push(month)
range_string << month[0...3]
end
previous_month = numerical
end
return range_string
end
def get_accommodations
accommodations = []
self.accommodation_location_details.each do |accommodation|
accommObj = {}
accommObj['url'] = accommodation.accommodation.icon.url
accommObj['name'] = accommodation.accommodation.name
accommObj['cost'] = accommodation.cost
accommObj['id'] = accommodation.accommodation.id
accommodations.push(accommObj)
end
return accommodations
end
def get_limited_location_json
json_return = {}
json_return[:location] = self
json_return[:walking_distance] = self.walking_distance
json_return[:closest_accommodation] = self.closest_accommodation
json_return[:latitude] = self.latitude
json_return[:longitude] = self.longitude
json_return[:slug] = self.slug
json_return[:name] = self.name
json_return[:country] = self.country
json_return[:price_range_floor_cents] = self.price_range_floor_cents
json_return[:price_range_ceiling_cents] = self.price_range_ceiling_cents
json_return[:home_thumb] = self.home_thumb.url
json_return[:climbing_types] = self.get_climbing_types
json_return[:grades] = self.grades.collect { |grade| grade.html_attributes }
json_return[:airport_code] = self.airport_code
json_return[:date_range] = self.date_range
json_return[:rating] = self.rating
json_return[:solo_friendly] = self.solo_friendly
json_return[:id] = self.id
#new stuff
json_return[:accommodations] = self.get_accommodations
return json_return
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment