]> git.openstreetmap.org Git - rails.git/blob - app/models/diary_entry.rb
Add frozen_string_literal comments to ruby files
[rails.git] / app / models / diary_entry.rb
1 # frozen_string_literal: true
2
3 # == Schema Information
4 #
5 # Table name: diary_entries
6 #
7 #  id            :bigint           not null, primary key
8 #  user_id       :bigint           not null
9 #  title         :string           not null
10 #  body          :text             not null
11 #  created_at    :datetime         not null
12 #  updated_at    :datetime         not null
13 #  latitude      :float
14 #  longitude     :float
15 #  language_code :string           default("en"), not null
16 #  visible       :boolean          default(TRUE), not null
17 #  body_format   :enum             default("markdown"), not null
18 #
19 # Indexes
20 #
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)
24 #
25 # Foreign Keys
26 #
27 #  diary_entries_language_code_fkey  (language_code => languages.code)
28 #  diary_entries_user_id_fkey        (user_id => users.id)
29 #
30
31 class DiaryEntry < ApplicationRecord
32   belongs_to :user, :counter_cache => true
33   belongs_to :language, :foreign_key => "language_code", :inverse_of => :diary_entries
34
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
39
40   scope :visible, -> { where(:visible => true) }
41
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
51
52   after_save :spam_check
53
54   def body
55     @body ||= RichText.new(self[:body_format], self[:body])
56   end
57
58   private
59
60   def spam_check
61     user.spam_check
62   end
63 end