diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/blog.rb | 2 | ||||
-rw-r--r-- | app/models/comment.rb | 36 |
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 @@ | |||
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 | ||