Sha256: 9c81e2c8dbe85c69f1ee6f4a63d41d65687b060c84d1736f4d27e89daf68c32f

Contents?: true

Size: 1.42 KB

Versions: 22

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Renalware
  class Admin::UsersController < BaseController
    include Renalware::Concerns::Pageable

    def index
      query = params.fetch(:q, {})
      query[:s] ||= "family_name"
      search = User
        .includes(:roles)
        .where.not(username: :systemuser)
        .ransack(query)
      users = search.result(distinct: true).page(page).per(per_page)
      authorize users
      render locals: { users: users, user_search: search }
    end

    def edit
      render locals: { user: find_and_authorize_user }
    end

    def update
      user = find_and_authorize_user
      if System::UpdateUser.new(user).call(update_params)
        redirect_to admin_users_path,
                    notice: t(".success", model_name: "user")
      else
        flash.now[:error] = t(".failed", model_name: "user")
        render :edit, locals: { user: user }
      end
    end

    private

    def find_and_authorize_user
      User.find(params[:id]).tap { |user_| authorize user_ }
    end

    def update_params
      roles = Array(Role.fetch(role_ids))
      user_params.merge(roles: roles)
    end

    def user_params
      params
        .require(:user)
        .permit(
          :approved,
          :unexpire,
          :telephone,
          :consultant,
          :hidden,
          role_ids: []
        )
    end

    def role_ids
      (user_params[:role_ids] || []).reject(&:blank?)
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
renalware-core-2.0.132 app/controllers/renalware/admin/users_controller.rb
renalware-core-2.0.131 app/controllers/renalware/admin/users_controller.rb