about summary refs log tree commit diff stats
path: root/app/models
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2023-10-12 17:10:34 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2023-10-12 17:10:34 -0400
commit835af696703484208882a70cc5dd47c5838ecf58 (patch)
tree8174d5b8c6f2486da56569bad361dd10aa1183f8 /app/models
parentb24e6f58a525fc576e23ef9d498745e64c16cd6c (diff)
downloadthoughts-835af696703484208882a70cc5dd47c5838ecf58.tar.gz
thoughts-835af696703484208882a70cc5dd47c5838ecf58.tar.bz2
thoughts-835af696703484208882a70cc5dd47c5838ecf58.zip
Added blog post commenting
Diffstat (limited to 'app/models')
-rw-r--r--app/models/blog.rb2
-rw-r--r--app/models/comment.rb36
2 files changed, 38 insertions, 0 deletions
diff --git a/app/models/blog.rb b/app/models/blog.rb index 415167c..b677e2b 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb
@@ -3,6 +3,8 @@ class Blog < ApplicationRecord
3 3
4 acts_as_taggable 4 acts_as_taggable
5 5
6 has_many :comments
7
6 validates :title, presence: true 8 validates :title, presence: true
7 validates :body, presence: true, if: :published 9 validates :body, presence: true, if: :published
8 validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published 10 validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published
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 @@
1class 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
36end