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