]> git.openstreetmap.org Git - rails.git/blob - app/models/diary_entry.rb
Merge branch 'master' into moderation
[rails.git] / app / models / diary_entry.rb
1 class DiaryEntry < ActiveRecord::Base
2   belongs_to :user, :counter_cache => true
3   belongs_to :language, :foreign_key => "language_code"
4
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"
7   has_many :subscriptions, :class_name => "DiaryEntrySubscription"
8   has_many :subscribers, :through => :subscriptions, :source => :user
9
10   scope :visible, -> { where(:visible => true) }
11
12   validates :title, :body, :presence => true
13   validates :title, :length => 1..255
14   validates :latitude, :allow_nil => true,
15                        :numericality => { :greater_than_or_equal_to => -90,
16                                           :less_than_or_equal_to => 90 }
17   validates :longitude, :allow_nil => true,
18                         :numericality => { :greater_than_or_equal_to => -180,
19                                            :less_than_or_equal_to => 180 }
20   validates :language, :user, :associated => true
21
22   after_save :spam_check
23
24   def body
25     RichText.new(self[:body_format], self[:body])
26   end
27
28   private
29
30   def spam_check
31     user.spam_check
32   end
33 end