docbrown/app/controllers/admin/podcasts_controller.rb
Wouter van Marrum 9946ac221a
Added form fields for the admin podcasts dashboard. (#10517)
* Added form fields for the admin podcasts dashboard.

* Updated podcasts_controller.rb; Added parameters to the allowed params.
Updated edit.html.erb; Removed duplicated field.
Updated podcasts_spec.rb; Updated test.

* Fixed a typo.

* Refactored test which hopefully fixes the build

* Added missing comma.

* Added file fixture uploads.

* Fixed a typo.

* Update spec/requests/admin/podcasts_spec.rb

Co-authored-by: Fernando Valverde <fernando@visualcosita.com>

* Again fixed another typo.

* And again fixed another typo.

* Fixed merge conflicts.

Co-authored-by: Fernando Valverde <fernando@visualcosita.com>
Co-authored-by: rhymes <rhymes@hey.com>
2020-11-02 17:19:39 +01:00

87 lines
2.4 KiB
Ruby

module Admin
class PodcastsController < Admin::ApplicationController
layout "admin"
before_action :find_podcast, only: %i[edit update fetch remove_admin add_admin]
before_action :find_user, only: %i[remove_admin add_admin]
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; 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 remove_admin
removed_roles = @user.remove_role(:podcast_admin, @podcast)
if removed_roles.empty?
redirect_to edit_admin_podcast_path(@podcast), notice: "Error"
else
redirect_to admin_podcasts_path, notice: "Removed admin"
end
end
def add_admin
role = @user.add_role(:podcast_admin, @podcast)
if role.persisted?
redirect_to admin_podcasts_path, notice: "Added admin"
else
redirect_to edit_admin_podcast_path(@podcast), notice: "Error"
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
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
reachable
published
]
params.require(:podcast).permit(allowed_params)
end
end
end