/app/controllers/admin/chat_channels_controller.rb

https://github.com/thepracticaldev/dev.to · Ruby · 58 lines · 49 code · 9 blank · 0 comment · 1 complexity · ed5f2ff37dd20637799d44c6429be8e5 MD5 · raw file

  1. module Admin
  2. class ChatChannelsController < Admin::ApplicationController
  3. layout "admin"
  4. def index
  5. @q = ChatChannel.where(channel_type: "invite_only").includes(:users).ransack(params[:q])
  6. @group_chat_channels = @q.result.page(params[:page]).per(50)
  7. end
  8. def create
  9. ChatChannels::CreateWithUsers.call(
  10. users: users_by_param,
  11. channel_type: "invite_only",
  12. contrived_name: chat_channel_params[:channel_name],
  13. membership_role: "mod",
  14. )
  15. redirect_back(fallback_location: "/admin/chat_channels")
  16. end
  17. def update
  18. @chat_channel = ChatChannel.find(params[:id])
  19. @chat_channel.invite_users(users: users_by_param)
  20. redirect_back(fallback_location: "/admin/chat_channels")
  21. end
  22. def remove_user
  23. @chat_channel = ChatChannel.find(params[:id])
  24. @chat_channel.remove_user(user_by_param)
  25. redirect_back(fallback_location: "/admin/chat_channels")
  26. end
  27. def destroy
  28. @chat_channel = ChatChannel.find(params[:id])
  29. if @chat_channel.users.count.zero?
  30. @chat_channel.destroy
  31. flash[:success] = "Channel was successfully deleted."
  32. else
  33. flash[:alert] = "Channel NOT deleted, because it still has users."
  34. end
  35. redirect_back(fallback_location: "/admin/chat_channels")
  36. end
  37. private
  38. def users_by_param
  39. User.where(username: chat_channel_params[:usernames_string].downcase.delete(" ").split(","))
  40. end
  41. def user_by_param
  42. User.find_by(username: chat_channel_params[:username_string])
  43. end
  44. def chat_channel_params
  45. allowed_params = %i[usernames_string channel_name username_string]
  46. params.require(:chat_channel).permit(allowed_params)
  47. end
  48. end
  49. end