Sha256: a00b3a1747d23c70c3f47dd434e43f73503d89deae79810348f7038e31ed8070

Contents?: true

Size: 1.92 KB

Versions: 8

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Admin
  class AdminUsersController < ApplicationController
    before_action :set_admin, only: %i[show edit update destroy]

    attr_reader :admin

    def index
      collection = Collection.new.with_params(params).apply(Admin::User.strict_loading)

      render locals: { collection: }
    end

    def show
      render :show, locals: { admin: }
    end

    def new
      @admin = Admin::User.new

      render :new, locals: { admin: }
    end

    def edit
      render :edit, locals: { admin: }
    end

    def create
      admin = Admin::User.new(admin_user_params)

      if admin.save
        redirect_to admin_admin_user_path(admin)
      else
        render :new, locals: { admin: }, status: :unprocessable_content
      end
    end

    def update
      if admin.update(admin_user_params)
        redirect_to action: :show
      else
        render :edit, locals: { admin: }, status: :unprocessable_content
      end
    end

    def archive
      Admin::User.where(id: params[:id]).where.not(id: current_admin.id).each(&:archive!)

      redirect_back(fallback_location: admin_admin_users_path, status: :see_other)
    end

    def restore
      Admin::User.archived.where(id: params[:id]).each(&:restore!)

      redirect_back(fallback_location: admin_admin_users_path, status: :see_other)
    end

    def destroy
      admin.destroy

      redirect_to admin_admin_users_path
    end

    private

    def set_admin
      @admin = Admin::User.with_archived.find(params[:id])
    end

    def admin_user_params
      params.require(:admin).permit(:name, :email, :password, :archived)
    end

    class Collection < Admin::Collection
      config.sorting  = :name
      config.paginate = true

      attribute :name, :string
      attribute :email, :string
      attribute :status, :archivable, default: :active
      attribute :last_sign_in_at, :date
      attribute :sign_in_count, :integer
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
katalyst-koi-4.10.3 app/controllers/admin/admin_users_controller.rb
katalyst-koi-4.10.2 app/controllers/admin/admin_users_controller.rb
katalyst-koi-4.10.1 app/controllers/admin/admin_users_controller.rb
katalyst-koi-4.10.0 app/controllers/admin/admin_users_controller.rb
katalyst-koi-4.9.5 app/controllers/admin/admin_users_controller.rb
katalyst-koi-4.9.4 app/controllers/admin/admin_users_controller.rb
katalyst-koi-4.9.3 app/controllers/admin/admin_users_controller.rb
katalyst-koi-4.9.2 app/controllers/admin/admin_users_controller.rb