1 class DiaryEntry < ActiveRecord::Base
2 belongs_to :user, :counter_cache => true
3 belongs_to :language, :foreign_key => 'language_code'
5 has_many :comments, -> { order(:id).preload(:user) }, :class_name => "DiaryComment"
6 has_many :visible_comments, -> { joins(:user).where(:visible => true, :users => { :status => %w(active confirmed) }).order(:id) }, :class_name => "DiaryComment"
8 scope :visible, -> { where(:visible => true) }
10 validates_presence_of :title, :body
11 validates_length_of :title, :within => 1..255
12 # validates_length_of :language, :within => 2..5, :allow_nil => false
13 validates_numericality_of :latitude, :allow_nil => true,
14 :greater_than_or_equal_to => -90, :less_than_or_equal_to => 90
15 validates_numericality_of :longitude, :allow_nil => true,
16 :greater_than_or_equal_to => -180, :less_than_or_equal_to => 180
17 validates_associated :language
19 after_save :spam_check
22 RichText.new(read_attribute(:body_format), read_attribute(:body))