groups_controller.rb 1.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
# frozen_string_literal: true

module Admin
    class GroupsController < BaseController
        before_action :set_group, except: [:index]
        before_action :set_filter_params

        def index
            authorize :group, :index?
            @groups = filtered_groups.page(params[:page])
        end

        def destroy
            authorize @group, :destroy?
            @group.destroy!
            log_action :destroy, @group
            flash[:notice] = I18n.t('admin.groups.destroyed_msg')
            redirect_to admin_groups_path(page: params[:page], **@filter_params)
        end

        def enable_featured
            authorize @group, :update?
            @group.is_featured = true
            @group.save!
            log_action :update, @group
            flash[:notice] = I18n.t('admin.groups.updated_msg')
            redirect_to admin_groups_path(page: params[:page], **@filter_params)
        end

        def disable_featured
            authorize @group, :update?
            @group.is_featured = false
            @group.save!
            log_action :update, @group
            flash[:notice] = I18n.t('admin.groups.updated_msg')
            redirect_to admin_groups_path(page: params[:page], **@filter_params)
        end

        private

        def set_group
            @group = Group.find(params[:id])
        end

        def set_filter_params
            @filter_params = filter_params.to_hash.symbolize_keys
        end

        def resource_params
            params.require(:group).permit(:is_featured, :is_nsfw)
        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
    end
end