docbrown/app/models/podcast_episode.rb
Nenad Pantelić ad04158b90
Add podcast_appearances joined table (forem#82) (#11354)
* Add podcast_appearances joined table (forem#82)

* Add podcast appearances keys unique constraint and creator property (forem#82)

* Add podcast_appearance role validation forem(#82)

* Add spec test for podcast_appearances model (forem#82)

* Small refactoring - place podcast appearance association by alphabetical order

* Adapt m2m association to pass rspec test (forem#82)

* Rename podcast appearance model to podcast episode appearance and remove fk indexing

* Rename podcast appearance model to podcast episode appearance and remove fk indexing

* Rename podcast episode appearance models and spec models. Update podcast episode and user spec tests (forem#82)

* Rename podcast episode appearances composite index to follow Rails naming pattern (forem#82)

* Remove ddl_transaction disabling and conurrent index adding from appearance migration

* Add role validation in model spec (forem#82)
2020-11-30 17:12:52 +01:00

123 lines
3.3 KiB
Ruby

class PodcastEpisode < ApplicationRecord
self.ignored_columns = %w[
duration_in_seconds
]
include Searchable
SEARCH_SERIALIZER = Search::PodcastEpisodeSerializer
SEARCH_CLASS = Search::FeedContent
acts_as_taggable
delegate :slug, to: :podcast, prefix: true
delegate :image_url, to: :podcast, prefix: true
delegate :title, to: :podcast, prefix: true
delegate :published, to: :podcast
belongs_to :podcast
has_many :comments, as: :commentable, inverse_of: :commentable, dependent: :nullify
has_many :podcast_episode_appearances, dependent: :destroy
has_many :users, through: :podcast_episode_appearances
mount_uploader :image, ProfileImageUploader
mount_uploader :social_image, ProfileImageUploader
validates :comments_count, presence: true
validates :guid, presence: true, uniqueness: true
validates :media_url, presence: true, uniqueness: true
validates :reactions_count, presence: true
validates :slug, presence: true
validates :title, presence: true
before_validation :process_html_and_prefix_all_images
# NOTE: Any create callbacks will not be run since we use activerecord-import to create episodes
# https://github.com/zdennis/activerecord-import#callbacks
after_update :purge
after_destroy :purge, :purge_all
after_save :bust_cache
after_commit :index_to_elasticsearch, on: %i[update]
after_commit :remove_from_elasticsearch, on: [:destroy]
scope :reachable, -> { where(reachable: true) }
scope :published, -> { joins(:podcast).where(podcasts: { published: true }) }
scope :available, -> { reachable.published }
scope :for_user, lambda { |user|
joins(:podcast).where(podcasts: { creator_id: user.id })
}
scope :eager_load_serialized_data, -> {}
def search_id
"podcast_episode_#{id}"
end
def comments_blob
comments.pluck(:body_markdown).join(" ")
end
def path
return unless podcast&.slug
"/#{podcast.slug}/#{slug}"
end
def description
ActionView::Base.full_sanitizer.sanitize(body)
end
def profile_image_url
image_url || "http://41orchard.com/wp-content/uploads/2011/12/Robot-Chalkboard-Decal.gif"
end
def body_text
ActionView::Base.full_sanitizer.sanitize(processed_html)
end
def zero_method
0
end
alias hotness_score zero_method
alias search_score zero_method
alias public_reactions_count zero_method
def class_name
self.class.name
end
def tag_keywords_for_search
tags.pluck(:keywords_for_search).join
end
## Useless stubs
def nil_method
nil
end
alias user_id nil_method
alias co_author_ids nil_method
private
def bust_cache
PodcastEpisodes::BustCacheWorker.perform_async(id, path, podcast_slug)
end
def process_html_and_prefix_all_images
return if body.blank?
self.processed_html = body
.gsub("\r\n<p>&nbsp;</p>\r\n", "").gsub("\r\n<p>&nbsp;</p>\r\n", "")
.gsub("\r\n<h3>&nbsp;</h3>\r\n", "").gsub("\r\n<h3>&nbsp;</h3>\r\n", "")
self.processed_html = "<p>#{processed_html}</p>" unless processed_html.include?("<p>")
doc = Nokogiri::HTML(processed_html)
doc.css("img").each do |img|
img_src = img.attr("src")
next unless img_src
cloudinary_img_src = Images::Optimizer.call(img_src, width: 725)
self.processed_html = processed_html.gsub(img_src, cloudinary_img_src)
end
end
end