Sha256: 26e2340eee6767398bff07b67896f90417832d90bb3f95930cc4146a872054cf

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

module Admin
  # AdminController
  class AdminController < ::ApplicationController
    layout 'admin/layouts/application'
    before_action :authenticate_user!
    before_action :paginator_params
    before_action :set_setting
    before_action :can_multiple_destroy, only: [:destroy_multiple]
    before_action :tables_name

    def root
      if current_user
        redirect_to dashboard_path
      else
        redirect_to new_user_session_path
      end
    end

    def paginator_params
      @search_field = model.search_field if listing?
      @query = params[:search] unless params[:search].blank?
      @current_page = params[:page] unless params[:page].blank?
    end

    def set_setting
      @setting = Setting.first
    end

    # def close_index_show
    # end

    private

    def tables_name
      @models = ApplicationRecord.connection.tables.map do |model|
        model.capitalize.singularize.camelize
      end
    end

    # Get submit key to redirect, only [:create, :update]
    def redirect(object, commit)
      if commit.key?('_save')
        redirect_to([:admin, object], notice: actions_messages(object))
      elsif commit.key?('_add_other')
        redirect_to(
          send("new_admin_#{underscore(object)}_path"),
          notice: actions_messages(object)
        )
      end
    end

    def redefine_ids(ids)
      is_admin = controller_path.include?('admin')
      klass =  is_admin ? controller_name : controller_path

      ids.delete('[]').split(',').select do |id|
        id if klass.classify.constantize.exists? id
      end
    end

    # Check whether the user has permission to delete
    # each of the selected objects
    def can_multiple_destroy
      is_admin = controller_path.include?('admin')
      klass = is_admin ? controller_name : controller_path

      redefine_ids(params[:multiple_ids]).each do |id|
        authorize! :destroy, klass.classify.constantize.find(id)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
keppler-2.0.2 installer/core/app/controllers/admin/admin_controller.rb
keppler-2.0.1 installer/core/app/controllers/admin/admin_controller.rb
keppler-2.0.0 installer/core/app/controllers/admin/admin_controller.rb