docbrown/app/controllers/admin/chat_channels_controller.rb
Ridhwana c5bcb4c021
RFC 50: fix some more hardcoded paths (#13473)
* fix: remove hardcoded paths

* fix: use path helpers

* swap href for link_to

* feat: use path helpers

* feat: use a data path to send through the path that we will be calling

* feat: pass route through to js

* dus index path

* chore: change to use path_helpers

* feat: update DUS controller

* chore: oops remove these paths
2021-04-26 17:49:54 +02:00

58 lines
1.7 KiB
Ruby

module Admin
class ChatChannelsController < Admin::ApplicationController
layout "admin"
def index
@q = ChatChannel.where(channel_type: "invite_only").includes(:users).ransack(params[:q])
@group_chat_channels = @q.result.page(params[:page]).per(50)
end
def create
ChatChannels::CreateWithUsers.call(
users: users_by_param,
channel_type: "invite_only",
contrived_name: chat_channel_params[:channel_name],
membership_role: "mod",
)
redirect_back(fallback_location: admin_chat_channels_path)
end
def update
@chat_channel = ChatChannel.find(params[:id])
@chat_channel.invite_users(users: users_by_param)
redirect_back(fallback_location: admin_chat_channels_path)
end
def remove_user
@chat_channel = ChatChannel.find(params[:id])
@chat_channel.remove_user(user_by_param)
redirect_back(fallback_location: admin_chat_channels_path)
end
def destroy
@chat_channel = ChatChannel.find(params[:id])
if @chat_channel.users.count.zero?
@chat_channel.destroy
flash[:success] = "Channel was successfully deleted."
else
flash[:alert] = "Channel NOT deleted, because it still has users."
end
redirect_back(fallback_location: admin_chat_channels_path)
end
private
def users_by_param
User.where(username: chat_channel_params[:usernames_string].downcase.delete(" ").split(","))
end
def user_by_param
User.find_by(username: chat_channel_params[:username_string])
end
def chat_channel_params
allowed_params = %i[usernames_string channel_name username_string]
params.require(:chat_channel).permit(allowed_params)
end
end
end