1 # == Schema Information
3 # Table name: diary_entries
5 # id :bigint(8) not null, primary key
6 # user_id :bigint(8) not null
7 # title :string not null
9 # created_at :datetime not null
10 # updated_at :datetime not null
13 # language_code :string default("en"), not null
14 # visible :boolean default(TRUE), not null
15 # body_format :enum default("markdown"), not null
19 # diary_entry_created_at_index (created_at)
20 # diary_entry_language_code_created_at_index (language_code,created_at)
21 # diary_entry_user_id_created_at_index (user_id,created_at)
25 # diary_entries_language_code_fkey (language_code => languages.code)
26 # diary_entries_user_id_fkey (user_id => users.id)
29 class DiaryEntry < ApplicationRecord
30 belongs_to :user, :counter_cache => true
31 belongs_to :language, :foreign_key => "language_code", :inverse_of => :diary_entries
33 has_many :comments, -> { order(:id).preload(:user) }, :class_name => "DiaryComment", :inverse_of => :diary_entry
34 has_many :visible_comments, -> { joins(:user).where(:visible => true, :users => { :status => %w[active confirmed] }).order(:id) }, :class_name => "DiaryComment"
35 has_many :subscriptions, :class_name => "DiaryEntrySubscription"
36 has_many :subscribers, :through => :subscriptions, :source => :user
38 scope :visible, -> { where(:visible => true) }
40 validates :title, :presence => true, :length => 1..255, :characters => true
41 validates :body, :presence => true, :characters => true
42 validates :latitude, :allow_nil => true,
43 :numericality => { :greater_than_or_equal_to => -90,
44 :less_than_or_equal_to => 90 }
45 validates :longitude, :allow_nil => true,
46 :numericality => { :greater_than_or_equal_to => -180,
47 :less_than_or_equal_to => 180 }
48 validates :language, :user, :associated => true
50 after_save :spam_check
53 @body ||= RichText.new(self[:body_format], self[:body])