Skip to content

Instantly share code, notes, and snippets.

@Verkalets
Created October 29, 2014 12:55
Show Gist options
  • Save Verkalets/b7e5891752966f407c2f to your computer and use it in GitHub Desktop.
Save Verkalets/b7e5891752966f407c2f to your computer and use it in GitHub Desktop.
class City
include Mongoid::Document
include Mongoid::Timestamps
include Geocoder::Model::Mongoid
field :name, type: String
field :hotel_id, type: String
field :description, type: String
field :user_email, type: String
field :min_price, type: String
field :total, type: String
field :density, type: String
field :ethnicities, type: String
field :religions, type: String
field :_id, type: String, default: ->{ name }
field :cityimg, type: String
mount_uploader :cityimg
#Google Maps Fields
field :address, type: String
field :town, type: String
field :state, type: String
field :country, type: String
field :lat, type: Float
field :lng, type: Float
field :coordinates, :type => Array
geocoded_by :full_address
reverse_geocoded_by :coordinates
after_validation :geocode
after_validation :look_up_address, :if => :coordinates
def full_address
"#{self.name}"
end
def look_up_address
# self.coordinates = [self.longitude, self.latitude]
self.lng = self.coordinates[0]
self.lat = self.coordinates[1]
reverse_geocode
end
belongs_to :user, class_name: 'User', inverse_of: :cities
has_many :hotels, class_name: 'Hotel', inverse_of: :city, :dependent => :destroy
has_many :places, class_name: 'Place', inverse_of: :city, :dependent => :destroy
has_many :blogs, class_name: 'Blog', inverse_of: :city, :dependent => :destroy
after_validation :add_low_price
def add_low_price
if self.hotels.count >= 1
@hotels = self.hotels
@hotel = @hotels.order_by([:private_room_price, :asc])
@hotel_min = @hotel.first
self.min_price = @hotel_min.private_room_price
else
self.min_price = 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment