diff --git a/app/models/podcast_episode.rb b/app/models/podcast_episode.rb index e4248a01c..1cf747ba6 100644 --- a/app/models/podcast_episode.rb +++ b/app/models/podcast_episode.rb @@ -30,12 +30,13 @@ class PodcastEpisode < ApplicationRecord validates :media_url, presence: true, uniqueness: true validates :guid, presence: true, uniqueness: true + # 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_create :purge_all after_destroy :purge, :purge_all - after_save :bust_cache + after_save :bust_cache - after_commit :index_to_elasticsearch, on: %i[create update] + after_commit :index_to_elasticsearch, on: %i[update] after_commit :remove_from_elasticsearch, on: [:destroy] before_validation :process_html_and_prefix_all_images diff --git a/app/services/podcasts/create_episode.rb b/app/services/podcasts/create_episode.rb index 1c90d10d2..80f3ee56f 100644 --- a/app/services/podcasts/create_episode.rb +++ b/app/services/podcasts/create_episode.rb @@ -29,7 +29,9 @@ module Podcasts conflict_target: %i[media_url], columns: %i[title slug subtitle summary website_url published_at reachable media_url https body] } - PodcastEpisode.find(import_result.ids.first) + episode = PodcastEpisode.find(import_result.ids.first) + finalize(episode) + episode end private @@ -42,5 +44,10 @@ module Podcasts episode.media_url = result.url episode.https = result.https end + + def finalize(episode) + episode.purge_all + episode.index_to_elasticsearch + end end end diff --git a/spec/services/podcasts/create_episode_spec.rb b/spec/services/podcasts/create_episode_spec.rb index 3967208dd..fb0e976fd 100644 --- a/spec/services/podcasts/create_episode_spec.rb +++ b/spec/services/podcasts/create_episode_spec.rb @@ -19,6 +19,11 @@ RSpec.describe Podcasts::CreateEpisode, type: :service do end.to change(PodcastEpisode, :count).by(1) end + it "indexes the episode" do + sidekiq_perform_enqueued_jobs { described_class.call(podcast.id, item) } + expect { podcast.podcast_episodes.each(&:elasticsearch_doc) }.not_to raise_error + end + it "creates an episode with correct data" do episode = described_class.call(podcast.id, item) expect(episode.title).to eq("Individual Contributor Career Growth w/ Matt Klein (part 1)")