diff options
Diffstat (limited to 'app/models/blog.rb')
| -rw-r--r-- | app/models/blog.rb | 55 |
1 files changed, 52 insertions, 3 deletions
| diff --git a/app/models/blog.rb b/app/models/blog.rb index 322a808..03c0619 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb | |||
| @@ -1,19 +1,62 @@ | |||
| 1 | class Blog < ApplicationRecord | 1 | class Blog < ApplicationRecord |
| 2 | has_many :records, as: :recordable, inverse_of: :recordable | 2 | include Recordable |
| 3 | include Votable | ||
| 4 | |||
| 5 | acts_as_taggable | ||
| 6 | |||
| 7 | has_many :comments | ||
| 8 | belongs_to :user | ||
| 9 | |||
| 10 | has_many_attached :images do |attachable| | ||
| 11 | attachable.variant :thumb, resize_to_limit: [300, 300] | ||
| 12 | end | ||
| 3 | 13 | ||
| 4 | validates :title, presence: true | 14 | validates :title, presence: true |
| 5 | validates :body, presence: true, if: :published | 15 | validates :body, presence: true, if: :published |
| 6 | validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published | 16 | validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published |
| 7 | 17 | validates :user, presence: true | |
| 8 | accepts_nested_attributes_for :records, allow_destroy: true | 18 | validates :images, content_type: ['image/png', 'image/jpeg', 'video/mp4', 'image/gif'] |
| 9 | 19 | ||
| 10 | before_validation :set_draft_title | 20 | before_validation :set_draft_title |
| 11 | before_save :set_published_at | 21 | before_save :set_published_at |
| 22 | after_save :send_webmentions | ||
| 12 | 23 | ||
| 13 | def path | 24 | def path |
| 14 | "/says/#{slug}" | 25 | "/says/#{slug}" |
| 15 | end | 26 | end |
| 16 | 27 | ||
| 28 | def taggable | ||
| 29 | self | ||
| 30 | end | ||
| 31 | |||
| 32 | def has_read_more | ||
| 33 | body.include?("<!--MORE-->") | ||
| 34 | end | ||
| 35 | |||
| 36 | def short_body | ||
| 37 | body[0..(body.index("<!--MORE-->")-1)] | ||
| 38 | end | ||
| 39 | |||
| 40 | def to_param | ||
| 41 | slug | ||
| 42 | end | ||
| 43 | |||
| 44 | def visible_date | ||
| 45 | if published | ||
| 46 | published_at | ||
| 47 | else | ||
| 48 | updated_at | ||
| 49 | end | ||
| 50 | end | ||
| 51 | |||
| 52 | def prev | ||
| 53 | Blog.where(published: true).where("published_at < ?", published_at).order(published_at: :desc).first | ||
| 54 | end | ||
| 55 | |||
| 56 | def next | ||
| 57 | Blog.where(published: true).where("published_at > ?", published_at).order(published_at: :asc).first | ||
| 58 | end | ||
| 59 | |||
| 17 | private | 60 | private |
| 18 | def set_draft_title | 61 | def set_draft_title |
| 19 | if self.title.blank? and not self.published | 62 | if self.title.blank? and not self.published |
| @@ -30,4 +73,10 @@ class Blog < ApplicationRecord | |||
| 30 | self.published_at = nil | 73 | self.published_at = nil |
| 31 | end | 74 | end |
| 32 | end | 75 | end |
| 76 | |||
| 77 | def send_webmentions | ||
| 78 | return unless self.published | ||
| 79 | |||
| 80 | SendWebmentionsJob.perform_later self | ||
| 81 | end | ||
| 33 | end | 82 | end |
