gab-social/app/controllers/admin/groups_controller.rb

82 lines
1.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Admin
2020-12-16 00:31:30 +00:00
class GroupsController < BaseController
before_action :set_group, except: [:index]
before_action :set_accounts, only: [:show]
2020-12-16 00:31:30 +00:00
before_action :set_filter_params
def index
authorize :group, :index?
@groups = filtered_groups.page(params[:page])
end
2020-12-16 00:31:30 +00:00
def show
authorize :group, :index?
end
2020-12-16 00:31:30 +00:00
def update
if @group.update(resource_params)
redirect_to admin_group_path(@group.id), notice: I18n.t('generic.changes_saved_msg')
else
render action: :edit
end
2020-12-16 00:31:30 +00:00
end
def destroy
authorize @group, :destroy?
@group.destroy!
log_action :destroy, @group
flash[:notice] = 'Group destroyed'
redirect_to admin_groups_path
2020-12-16 00:31:30 +00:00
end
def make_me_admin
GroupAccount.create(group: @group, account: current_account, role: 'admin')
redirect_to admin_group_path(@group.id), notice: 'You are now an admin of this group'
2020-12-16 00:31:30 +00:00
end
private
def set_group
@group = Group.find(params[:id])
end
def set_accounts
@admins = GroupAccount.where(group: @group, role: 'admin')
@mods = GroupAccount.where(group: @group, role: 'moderator')
end
2020-12-16 00:31:30 +00:00
def set_filter_params
@filter_params = filter_params.to_hash.symbolize_keys
end
def resource_params
params.require(:group).permit(
:title,
:description,
# :slug,
:tags,
:is_private,
:is_archived,
:is_visible,
:is_featured,
:is_nsfw
)
2020-12-16 00:31:30 +00:00
end
def filtered_groups
query = Group.order('is_featured DESC, member_count DESC')
if params[:title]
query = query.where("LOWER(title) LIKE LOWER(?)", "%#{params[:title]}%")
end
return query
end
def filter_params
params.permit(:sort,)
end
2020-12-16 00:31:30 +00:00
end
end
2020-12-16 00:31:30 +00:00