docbrown/app/controllers/admin/podcasts_controller.rb
Ben Halpern 09fa66a03f
Fix and clean up podcast pages (#15004)
* Fix and clean up podcast pages

* Adjust tests

* Fix a test

* Fix missing i18n

* Fix tests

* Fix tests

* Fiddle with test

* Sure up css and tests

* Add featured as allowed param

* Fix a couple tests

* xit out test

* Update app/views/podcast_episodes/index.html.erb

* Update app/views/podcast_episodes/index.html.erb

Co-authored-by: ludwiczakpawel <ludwiczakpawel@gmail.com>

Co-authored-by: ludwiczakpawel <ludwiczakpawel@gmail.com>
2021-10-15 17:38:57 -04:00

80 lines
2.2 KiB
Ruby

module Admin
class PodcastsController < Admin::ApplicationController
layout "admin"
before_action :find_podcast, only: %i[edit update fetch add_owner]
PODCAST_ALLOWED_PARAMS = %i[
title
feed_url
description
itunes_url
overcast_url
android_url
soundcloud_url
website_url
twitter_username
pattern_image
main_color_hex
slug
image
featured
reachable
published
].freeze
def index
@podcasts = Podcast.left_outer_joins(:podcast_episodes)
.select("podcasts.*, count(podcast_episodes) as episodes_count")
.group("podcasts.id").order("podcasts.created_at" => :desc)
.page(params[:page]).per(50)
return if params[:search].blank?
@podcasts = @podcasts.where("podcasts.title ILIKE :search", search: "%#{params[:search]}%")
end
def edit
@podcast_ownership = Podcast.find(params[:id])
end
def update
if @podcast.update(podcast_params)
redirect_to admin_podcasts_path, notice: "Podcast updated"
else
render :edit
end
end
def fetch
limit = params[:limit].to_i.zero? ? nil : params[:limit].to_i
force = params[:force].to_i == 1
Podcasts::GetEpisodesWorker.perform_async(podcast_id: @podcast.id, limit: limit, force: force)
flash[:notice] = "Podcast's episodes fetching was scheduled (#{@podcast.title}, ##{@podcast.id})"
redirect_to admin_podcasts_path
end
def add_owner
@podcast_ownership = @podcast.podcast_ownerships.build(user_id: params["podcast"]["user_id"])
if @podcast_ownership.save
redirect_to admin_podcasts_path, notice: "New owner added!"
else
redirect_to edit_admin_podcast_path(@podcast), notice: @podcast_ownership.errors_as_sentence
end
end
private
def find_podcast
@podcast = Podcast.find(params[:id])
end
def find_user
@user = User.find_by(id: params[:podcast][:user_id])
redirect_to edit_admin_podcast_path(@podcast), notice: "No such user" unless @user
end
def podcast_params
params.require(:podcast).permit(PODCAST_ALLOWED_PARAMS)
end
end
end