docbrown/app/models/podcast_episode.rb
Julianna Tetreault c87974b7ac
[deploy] Add co_author_ids to Articles (#10339)
* Replaces second_user_id and third_user_id with co_author_ids on the Articles table

* Adjusts migration that adds co_author_ids to also add array: true

* Replaces second_user_id and third_user_id with co_author_ids in all applicable files
  - Use co_author_ids in _individual_article view
  - Use co_author_ids in articles and stories controllers
  - Use co_author_ids in article and podcast_episode models
  - Use co_author_ids in article_dashboard
  - Use co_author_ids in comments_helper
  - Use co_author_ids in articles_spec

* Replaces second_user_id and third_user_id with co_author_ids in article.rb model

* Remove unused #assign_co_authors method from the Stories::Controller

* Add #co_authors and #co_author_name_and_path to article_decorator.rb

* Adds a descriptive form field for co_author_ids for Admins in /admin/articles

* Adds a conditional in article/show.html.erb to render co_author names on an Article in a human-readable way

* Replaces get_co_author_name_and_path with proper method name, co_author_name_and_path in show.html.erb

* Adds latest schema to (hopefully) resolve Travis conflicts

* Adds the safe operator to show.html.erb

* Adjusts articles_spec.rb to use an array in the first test

* Replaces unless &.empty? with if ./present? in articles/show.html.erb

* Replaces second_user_id with co_author_ids in stories_show_spec.rb test

* Replaces elsif with else in articles/show.html.erb

* Cleans up show.html.erb by removing conditional in favor of decorator method

* Refactors splitting of params in Articles::Controller and optimizes query in #co_authors

* Reverts removal of second_user_id and third_user_id and migration file

* Adds a data_update script to update co_author_ids with existing second and third user_ids

* Adds validations to co_authors and flash_messages to indicate whether an update was successful or not
  - Adds to methods to article.rb to validate the IDs of co_authors and authors
  - Adds flash messages to the Admin::Articles::Controller and a redirect to the show page
  - Removes the JS highlighting upon submit when updating an Article in Admin
  - Refactors #update action in Admin::Articles::Controller

* Adds tests to article_spec.rb around co_author_id validations in article.rb

* Adjusts #validate_co_authors_must_not_be_the_same to use .include? instead

* Uses Field::Select.with_options in article_dashboard.rb to properly display co_author_ids

* Reverts removal of assigning co_author_ids in the Stories::Controller

* Adjusts error message and adjusts return logic in article.rb (thanks, Fernando!)

* Fixes failing article_spec.rb test that checks for co_author_ids uniqueness

* Adds a default array to co_author_ids and checks if they are .blank?

* Refactor data_update script to use a single SQL statement (thank you, Michael)

* Preserve array order of co_author_ids in article_decorator.rb

* Add db file for default: []

* Add validation to fix bug related to text inputs and invalid users when adding co_authors

* Adds tests to ensure that co_author_ids are both an integer and an integer > 0

* Updates admin/articles_spec.rb to default [] instead of nil

* Adjusts validations in article.rb to be DRY-er and more consistent

* Consolidates validations further

* Refactors validations in article.rb to use procs

* Refactors data_update script to remove nil values from array
2020-10-02 11:06:11 -04:00

121 lines
3.2 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
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