docbrown/app/controllers/internal/podcasts_controller.rb
Anna Buianova 1dfa3d6183
Fix podcasts internal page (#6205)
* Fix podcasts internal page

* Add a test to check what is displayed in /internal/podcasts
2020-02-20 11:51:37 -05:00

60 lines
1.6 KiB
Ruby

class Internal::PodcastsController < Internal::ApplicationController
layout "internal"
before_action :find_podcast, only: %i[edit update 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)
@podcasts = @podcasts.where("podcasts.title ILIKE :search", search: "%#{params[:search]}%") if params[:search].present?
end
def edit; end
def update
if @podcast.update(podcast_params)
redirect_to internal_podcasts_path, notice: "Podcast updated"
else
render :edit
end
end
def remove_admin
removed_roles = @user.remove_role(:podcast_admin, @podcast)
if removed_roles.empty?
redirect_to edit_internal_podcast_path(@podcast), notice: "Error"
else
redirect_to internal_podcasts_path, notice: "Removed admin"
end
end
def add_admin
role = @user.add_role(:podcast_admin, @podcast)
if role.persisted?
redirect_to internal_podcasts_path, notice: "Added admin"
else
redirect_to edit_internal_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_internal_podcast_path(@podcast), notice: "No such user" unless @user
end
def podcast_params
allowed_params = %i[
title feed_url
]
params.require(:podcast).permit(allowed_params)
end
end