]> git.openstreetmap.org Git - rails.git/blob - app/models/diary_entry.rb
f7584c6378cee90e82d3e0153d58ac0ade530ead
[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, :class_name => "DiaryComment",
6                       :include => :user,
7                       :order => "diary_comments.id"
8   has_many :visible_comments, :class_name => "DiaryComment",
9                               :include => :user,
10                               :conditions => {
11                                 :users => { :status => ["active", "confirmed" ] },
12                                 :visible => true
13                               },
14                               :order => "diary_comments.id"
15
16   scope :visible, where(:visible => true)
17
18   validates_presence_of :title, :body
19   validates_length_of :title, :within => 1..255
20   #validates_length_of :language, :within => 2..5, :allow_nil => false
21   validates_numericality_of :latitude, :allow_nil => true,
22                             :greater_than_or_equal_to => -90, :less_than_or_equal_to => 90
23   validates_numericality_of :longitude, :allow_nil => true,
24                             :greater_than_or_equal_to => -180, :less_than_or_equal_to => 180
25   validates_associated :language
26
27   after_initialize :set_defaults
28   after_save :spam_check
29
30   def body
31     RichText.new(read_attribute(:body_format), read_attribute(:body))
32   end
33
34 private
35
36   def set_defaults
37     self.body_format = "markdown" unless self.attribute_present?(:body_format)
38   end
39
40   def spam_check
41     user.spam_check
42   end
43 end