class Post < ApplicationRecord
include UrlTitleFetchable
-
+
validates :post_type, presence: true, inclusion: { in: %w[dispatch bookmark] }
validates :title, presence: true
validates :slug, presence: true, uniqueness: true, if: :dispatch?
else
all
end
-
+
posts.order(published_at: :desc).page(page).per(per_page)
end
-
+
def rss_time
published_at.strftime("%a, %d %b %Y %H:%M:%S %Z") if published_at
end
def bookmark?
post_type == 'bookmark'
end
-
+
def short_dispatch?
- dispatch? &&
- content.present? &&
- content.split.size <= 150 &&
+ dispatch? &&
+ content.present? &&
+ content.split.size <= 150 &&
!content.strip.start_with?("Dear friends,")
end
def format_tags
return "" if tags.blank?
-
+
tags.split(/\s*(?:,|\s+and)\s*/).map { |tag| "<code>#{tag.strip}</code>" }.join(', ') + '.'
end
- def generate_excerpt(max_length = 210)
+ def generate_excerpt(max_length = 1040, take = 10)
return "" if content.blank?
-
+
stripped_content = ActionController::Base.helpers.strip_tags(content)
- excerpt = stripped_content.split('.').first || stripped_content[0...max_length]
+ excerpt = stripped_content.split('.') if stripped_content.split('.')
+ excerpt = excerpt.take(take)
+ excerpt = excerpt.join(',')
excerpt = excerpt[0...max_length] if excerpt.length > max_length
excerpt.gsub!("Dear friends,", "")
- excerpt.gsub!(/\s+/, ' ')
excerpt.strip
end
def set_slug
self.slug = title.present? ? title.parameterize : nil
end
-
+
def set_published_at
self.published_at ||= Time.current
end
-
+
private
-
+
def set_title_from_url
return unless bookmark? && url.present? && title.blank?
-
+
Rails.logger.debug("Attempting to fetch title for URL: #{url}")
fetched_title = fetch_title_from_url(url)
-
+
if fetched_title.present?
Rails.logger.debug("Successfully fetched title: #{fetched_title}")
self.title = fetched_title
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :lockable, :two_factor_authenticatable, :two_factor_backupable,
- otp_secret_encryption_key: ENV.fetch('ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY') do
- Rails.application.credentials.dig(:active_record_encryption, :primary_key) || 'test_key_1_'*4
- end
+ otp_secret_encryption_key: Rails.application.credentials.dig(:active_record_encryption, :primary_key)
encrypts :otp_secret
attr_accessor :otp_plain_secret