class Annonce < ActiveRecord::Base has_many :annonce_account_favs has_many :annonce_account_likes, :through => :annonce_account_favs belongs_to :devise belongs_to :annonce_account belongs_to :annonce_cat belongs_to :city belongs_to :specific_annonce, :polymorphic => true belongs_to :annonce_auto, foreign_key: 'specific_annonce_id' belongs_to :annonce_carav, foreign_key: 'specific_annonce_id' belongs_to :annonce_high, foreign_key: 'specific_annonce_id' belongs_to :annonce_inf, foreign_key: 'specific_annonce_id' belongs_to :annonce_moto, foreign_key: 'specific_annonce_id' belongs_to :annonce_pdv, foreign_key: 'specific_annonce_id' belongs_to :annonce_play, foreign_key: 'specific_annonce_id' belongs_to :annonce_tel, foreign_key: 'specific_annonce_id' belongs_to :annonce_util, foreign_key: 'specific_annonce_id' belongs_to :annonce_camping, foreign_key: 'specific_annonce_id' belongs_to :annonce_gite, foreign_key: 'specific_annonce_id' belongs_to :annonce_hab, foreign_key: 'specific_annonce_id' belongs_to :annonce_hotel, foreign_key: 'specific_annonce_id' belongs_to :annonce_naut, foreign_key: 'specific_annonce_id' belongs_to :annonce_num, foreign_key: 'specific_annonce_id' belongs_to :annonce_office, foreign_key: 'specific_annonce_id' belongs_to :annonce_pet, foreign_key: 'specific_annonce_id' belongs_to :annonce_draw, foreign_key: 'specific_annonce_id' belongs_to :annonce_antiquity, foreign_key: 'specific_annonce_id' belongs_to :annonce_graphic, foreign_key: 'specific_annonce_id' belongs_to :annonce_sculpture, foreign_key: 'specific_annonce_id' has_many :annonce_tag_cats has_many :tag_cats, :through => :annonce_tag_cats accepts_nested_attributes_for :specific_annonce has_many :annonce_photos belongs_to :default_photo, :class_name => "AnnoncePhoto" validates :title, :presence => true validates :g_text, :presence => true, :if => :validate_localisation before_validation do if validate_localisation if !(self.localisation_complete? and self.g_text?) errors.add(:g_text, "Vous devez entrer le nom de votre ville et sélectionner un résultat") end else self.g_postal_code = nil self.g_city = nil self.g_sub_state = nil self.g_sub_state_code = nil self.g_state = nil self.g_state_code = nil self.g_country = nil self.g_country_code = nil self.g_text = nil self.g_latitude = nil self.g_longitude = nil end end def localisation_complete? true if self.g_city? and self.g_country? and self.g_country_code? and self.g_latitude? and self.g_longitude? # self.g_postal_code? and self.g_sub_state? and self.g_sub_state_code? #and self.g_state? and self.g_state_code? end def validate_localisation true #if !self.internet end validates :annonce_cat_id, :presence => true validates :annonce_account_id, :presence => true before_create do self.nbr_photos = 5 self.list_head = Time.now self.enabled = true end def nbr_photo_restantes self.nbr_photos.to_i - self.annonce_photos.visibles.count end scope :with_img, lambda { where("default_photo_id is not null") } scope :not_published, lambda { where("published != 1 or published is null and enabled = 1") } scope :valid_to_show, lambda { not_blocked.where(:enabled => true, :published => true).where("published_date <= ? and refused is null",Date.today ) } scope :not_blocked, lambda { joins(:annonce_account).where("annonce_accounts.deleted_at is null" ).where("annonce_accounts.blocked = 0 or annonce_accounts.blocked is NULL") } scope :refused, lambda { where(:refused => true) } scope :desactived, lambda { where(:enabled => false) } scope :moderated, lambda { where(:moderated => true) } scope :not_moderated, lambda { where("moderated != 1 or moderated is null") } reverse_geocoded_by :g_latitude, :g_longitude end