]> git.openstreetmap.org Git - rails.git/blob - app/models/diary_entry.rb
Probit control characters in user names
[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 => ["active", "confirmed"] }).order(:id) }, :class_name => "DiaryComment"
7
8   scope :visible, -> { where(:visible => true) }
9
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
18
19   after_initialize :set_defaults
20   after_save :spam_check
21
22   def body
23     RichText.new(read_attribute(:body_format), read_attribute(:body))
24   end
25
26 private
27
28   def set_defaults
29     self.body_format = "markdown" unless self.attribute_present?(:body_format)
30   end
31
32   def spam_check
33     user.spam_check
34   end
35 end