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