From 4fac78f1b446e30a5a36128d6549222ecf5a5035 Mon Sep 17 00:00:00 2001
From: Manasa <43354059+Manasa2850@users.noreply.github.com>
Date: Thu, 17 Dec 2020 23:25:16 +0530
Subject: [PATCH] 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
---
app/controllers/admin/podcasts_controller.rb | 26 ++++++----------
app/views/admin/podcasts/edit.html.erb | 23 ++++++++-------
app/views/admin/podcasts/index.html.erb | 6 ++--
config/routes.rb | 3 +-
spec/requests/admin/podcasts_spec.rb | 31 +++++---------------
5 files changed, 32 insertions(+), 57 deletions(-)
diff --git a/app/controllers/admin/podcasts_controller.rb b/app/controllers/admin/podcasts_controller.rb
index c5d3354c6..eb420fd18 100644
--- a/app/controllers/admin/podcasts_controller.rb
+++ b/app/controllers/admin/podcasts_controller.rb
@@ -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
diff --git a/app/views/admin/podcasts/edit.html.erb b/app/views/admin/podcasts/edit.html.erb
index 1f485f36d..b714e57ae 100644
--- a/app/views/admin/podcasts/edit.html.erb
+++ b/app/views/admin/podcasts/edit.html.erb
@@ -23,33 +23,34 @@
<% end %>
-
Manage Admins
- <% if @podcast.admins.present? %>
+
Manage Owners
+
+ <% if @podcast.owners.present? %>
- <% @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| %>
-
- <%= 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" %>
<% end %>
<% end %>
<% else %>
-
There are no admins for this podcast.
+
There are no owners for this podcast.
<% end %>
<% if @podcast.creator.present? %>
Created by <%= link_to "@#{@podcast.creator.username}", "/#{@podcast.creator.username}" %>
<% 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| %>
- <%= 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" %>
- <%= f.submit "Add Admin", class: "btn btn-primary" %>
+ <%= f.submit "Add Owner", class: "btn btn-primary" %>
<% end %>
diff --git a/app/views/admin/podcasts/index.html.erb b/app/views/admin/podcasts/index.html.erb
index edaa507f4..533c5a13f 100644
--- a/app/views/admin/podcasts/index.html.erb
+++ b/app/views/admin/podcasts/index.html.erb
@@ -17,7 +17,7 @@
Reach |
Pub |
Status Notice |
- Admin |
+ Owners |
@@ -31,8 +31,8 @@
<%= podcast.published %> |
<%= podcast.status_notice %> |
- <% podcast.admins.pluck(:username).each do |username| %>
- <%= link_to "@#{username}", "/#{username}" %>
+ <% podcast.owners.select(:username).each do |user| %>
++ <%= link_to "@#{user.username}", user_url(user), target: "_blank", rel: :noopener %>
<% end %>
|
diff --git a/config/routes.rb b/config/routes.rb
index 1eb6b9a6a..cd68928b5 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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
diff --git a/spec/requests/admin/podcasts_spec.rb b/spec/requests/admin/podcasts_spec.rb
index 6a727d76c..4581dcc21 100644
--- a/spec/requests/admin/podcasts_spec.rb
+++ b/spec/requests/admin/podcasts_spec.rb
@@ -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