about summary refs log tree commit diff stats
path: root/app/models/comment.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/comment.rb')
-rw-r--r--app/models/comment.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..b85f3b6 --- /dev/null +++ b/app/models/comment.rb
@@ -0,0 +1,39 @@
1class Comment < ApplicationRecord
2 extend Enumerize
3
4 belongs_to :blog
5
6 has_many :replies, class_name: "Comment", foreign_key: "reply_to_id"
7 belongs_to :reply_to, class_name: "Comment", optional: true
8
9 validates :body, presence: true
10 validates :username, presence: true
11 validates :email, presence: true, format: URI::MailTo::EMAIL_REGEXP
12
13 scope :published_and_ordered, -> { where(status: :published).order(published_at: :asc) }
14
15 enumerize :status,
16 in: [:published, :pending, :rejected],
17 default: :published,
18 predicates: true
19
20 before_save :set_published_at
21
22 def gravatar_url
23 hash = Digest::MD5.hexdigest(email)
24 "https://www.gravatar.com/avatar/#{hash}?size=40&default=identicon&rating=g"
25 end
26
27 private
28
29 def set_published_at
30 if self.published?
31 if self.published_at.blank?
32 self.published_at = DateTime.now
33 end
34 else
35 self.published_at = nil
36 end
37 end
38
39end