|
28 | 28 | class Note < ApplicationRecord
|
29 | 29 | include GeoRecord
|
30 | 30 |
|
| 31 | + belongs_to :author, :class_name => "User", :foreign_key => "user_id", :optional => true |
| 32 | + |
31 | 33 | has_many :comments, -> { left_joins(:author).where(:visible => true, :users => { :status => [nil, "active", "confirmed"] }).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id
|
32 | 34 | has_many :all_comments, -> { left_joins(:author).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id, :inverse_of => :note
|
33 | 35 | has_many :subscriptions, :class_name => "NoteSubscription"
|
@@ -89,24 +91,27 @@ def freshly_closed_until
|
89 | 91 | closed_at + DEFAULT_FRESHLY_CLOSED_LIMIT
|
90 | 92 | end
|
91 | 93 |
|
92 |
| - # Return the note's description, derived from the first comment |
| 94 | + # Return the note's description, unless record is unavailable and |
| 95 | + # it will be derived from the first comment. If note's author is |
| 96 | + # deleted, "deleted" will be returned |
93 | 97 | def description
|
94 |
| - comments.first.body |
| 98 | + if !author.nil? && author.status == "deleted" |
| 99 | + RichText.new("text", I18n.t("notes.show.description_when_author_is_deleted")) |
| 100 | + elsif user_ip.nil? && user_id.nil? |
| 101 | + comments.first.body |
| 102 | + else |
| 103 | + RichText.new("text", self[:description]) |
| 104 | + end |
95 | 105 | end
|
96 | 106 |
|
97 |
| - # Return the note's author object, derived from the first comment |
| 107 | + # Return the note's author object, unless record is unavailable and |
| 108 | + # it will be derived from the first comment |
98 | 109 | def author
|
99 |
| - comments.first.author |
100 |
| - end |
101 |
| - |
102 |
| - # Return the note's author ID, derived from the first comment |
103 |
| - def author_id |
104 |
| - comments.first.author_id |
105 |
| - end |
106 |
| - |
107 |
| - # Return the note's author IP address, derived from the first comment |
108 |
| - def author_ip |
109 |
| - comments.first.author_ip |
| 110 | + if user_ip.nil? && user_id.nil? |
| 111 | + all_comments.first.author |
| 112 | + else |
| 113 | + self[:author] |
| 114 | + end |
110 | 115 | end
|
111 | 116 |
|
112 | 117 | private
|
|
0 commit comments