Allow admin to add users as podcast owners (MLH Fellowship) (#11728) [deploy]
* added podcast_owner role * changed view files * added owner functions * changed function name in view file * removed owner role * removed changes in view file * controller changes * added permitted attributes * fixed the failing tests * deleted the remove_owner feature for now * changed controller * fixed the build error * added route to add_owner * added another test * made suggested changes
This commit is contained in:
parent
1079c4e402
commit
4fac78f1b4
5 changed files with 32 additions and 57 deletions
|
|
@ -2,8 +2,7 @@ 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]
|
||||
before_action :find_podcast, only: %i[edit update fetch add_owner]
|
||||
|
||||
def index
|
||||
@podcasts = Podcast.left_outer_joins(:podcast_episodes)
|
||||
|
|
@ -16,7 +15,9 @@ module Admin
|
|||
@podcasts = @podcasts.where("podcasts.title ILIKE :search", search: "%#{params[:search]}%")
|
||||
end
|
||||
|
||||
def edit; end
|
||||
def edit
|
||||
@podcast_ownership = Podcast.find(params[:id])
|
||||
end
|
||||
|
||||
def update
|
||||
if @podcast.update(podcast_params)
|
||||
|
|
@ -34,21 +35,12 @@ module Admin
|
|||
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"
|
||||
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 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"
|
||||
redirect_to edit_admin_podcast_path(@podcast), notice: @podcast_ownership.errors_as_sentence
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -23,33 +23,34 @@
|
|||
<% end %>
|
||||
</div>
|
||||
<div class="crayons-card p-6">
|
||||
<h4 class="mb-4">Manage Admins</h4>
|
||||
<% if @podcast.admins.present? %>
|
||||
<h4 class="mb-4">Manage Owners</h4>
|
||||
|
||||
<% if @podcast.owners.present? %>
|
||||
<ul class="list-group list-group-flush mb-3">
|
||||
<% @podcast.admins.each do |admin| %>
|
||||
<%= form_for @podcast, url: remove_admin_admin_podcast_path(@podcast.id), html: { method: :delete, class: "form-inline" } do |f| %>
|
||||
<% @podcast.owners.select(:id, :username).each do |owner| %>
|
||||
<%= form_for @podcast, url: "#", html: { method: :delete, class: "form-inline" } do |f| %>
|
||||
<li class="list-group-item w-100">
|
||||
<%= link_to "@#{admin.username}", "/#{admin.username}" %>
|
||||
<%= f.hidden_field :user_id, value: admin.id %>
|
||||
<%= f.submit "Remove", class: "btn btn-danger btn-sm float-right" %>
|
||||
<%= link_to "@#{owner.username}", user_url(owner), target: "_blank", rel: :noopener %>
|
||||
<%# f.hidden_field :user_id, value: owner.id %>
|
||||
<%# f.submit "Remove", class: "btn btn-danger btn-sm float-right" %>
|
||||
</li>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% else %>
|
||||
<p>There are no admins for this podcast.</p>
|
||||
<p>There are no owners for this podcast.</p>
|
||||
<% end %>
|
||||
|
||||
<% if @podcast.creator.present? %>
|
||||
<p>Created by <%= link_to "@#{@podcast.creator.username}", "/#{@podcast.creator.username}" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= form_for @podcast, url: add_admin_admin_podcast_path(@podcast.id), html: { method: :post } do |f| %>
|
||||
<%= form_for @podcast, url: add_owner_admin_podcast_path(@podcast.id), html: { method: :post } do |f| %>
|
||||
<div class="form-group">
|
||||
<%= f.label "Add Admin (by user_id)", for: "podcast_user_id" %>
|
||||
<%= f.label "Add Owner (by user_id)", for: "podcast_user_id" %>
|
||||
<%= f.text_field :user_id, value: "", class: "form-control" %>
|
||||
</div>
|
||||
<%= f.submit "Add Admin", class: "btn btn-primary" %>
|
||||
<%= f.submit "Add Owner", class: "btn btn-primary" %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
<th scope="col">Reach</th>
|
||||
<th scope="col">Pub</th>
|
||||
<th scope="col">Status Notice</th>
|
||||
<th scope="col">Admin</th>
|
||||
<th scope="col">Owners</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="crayons-card">
|
||||
|
|
@ -31,8 +31,8 @@
|
|||
<td><%= podcast.published %></td>
|
||||
<td><%= podcast.status_notice %></td>
|
||||
<td>
|
||||
<% podcast.admins.pluck(:username).each do |username| %>
|
||||
<%= link_to "@#{username}", "/#{username}" %>
|
||||
<% podcast.owners.select(:username).each do |user| %>
|
||||
+ <p><%= link_to "@#{user.username}", user_url(user), target: "_blank", rel: :noopener %></p>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
|
|
|
|||
|
|
@ -73,8 +73,7 @@ Rails.application.routes.draw do
|
|||
resources :podcasts, only: %i[index edit update destroy] do
|
||||
member do
|
||||
post :fetch
|
||||
post :add_admin
|
||||
delete :remove_admin
|
||||
post :add_owner
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ RSpec.describe "/admin/podcasts", type: :request do
|
|||
let(:admin) { create(:user, :super_admin) }
|
||||
let(:podcast) { create(:podcast, published: false) }
|
||||
let(:user) { create(:user) }
|
||||
let(:podcast_ownership) { create(:podcast_ownership) }
|
||||
|
||||
before do
|
||||
sign_in admin
|
||||
|
|
@ -14,7 +15,6 @@ RSpec.describe "/admin/podcasts", type: :request do
|
|||
|
||||
before do
|
||||
create(:podcast_episode, podcast: podcast)
|
||||
user.add_role(:podcast_admin, Podcast.order(Arel.sql("RANDOM()")).first)
|
||||
end
|
||||
|
||||
it "renders success" do
|
||||
|
|
@ -29,32 +29,15 @@ RSpec.describe "/admin/podcasts", type: :request do
|
|||
end
|
||||
end
|
||||
|
||||
describe "Adding admin" do
|
||||
it "adds an admin" do
|
||||
describe "Adding owner" do
|
||||
it "adds an owner" do
|
||||
expect do
|
||||
post add_admin_admin_podcast_path(podcast.id), params: { podcast: { user_id: user.id } }
|
||||
end.to change(Role, :count).by(1)
|
||||
user.reload
|
||||
expect(user.has_role?(:podcast_admin, podcast)).to be true
|
||||
post add_owner_admin_podcast_path(podcast.id), params: { podcast: { user_id: user.id } }
|
||||
end.to change(PodcastOwnership, :count).by(1)
|
||||
end
|
||||
|
||||
it "does nothing when adding an admin for non-existent user" do
|
||||
post add_admin_admin_podcast_path(podcast.id), params: { podcast: { user_id: user.id + 1 } }
|
||||
expect(response).to redirect_to(edit_admin_podcast_path(podcast))
|
||||
end
|
||||
end
|
||||
|
||||
describe "Removing admin" do
|
||||
it "removes an admin" do
|
||||
user.add_role(:podcast_admin, podcast)
|
||||
expect do
|
||||
delete remove_admin_admin_podcast_path(podcast.id), params: { podcast: { user_id: user.id } }
|
||||
end.to change(Role, :count).by(-1)
|
||||
expect(user.has_role?(:podcast_admin, podcast)).to be false
|
||||
end
|
||||
|
||||
it "does nothing when removing an admin for non-existent user" do
|
||||
delete remove_admin_admin_podcast_path(podcast.id), params: { podcast: { user_id: user.id + 1 } }
|
||||
it "does nothing when adding a non-existent user as owner" do
|
||||
post add_owner_admin_podcast_path(podcast.id), params: { podcast: { user_id: user.id + 1 } }
|
||||
expect(response).to redirect_to(edit_admin_podcast_path(podcast))
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue