Sha256: 7490404ec64a7726a8e0c741752a5ff57ad01b347e8a1af1187223a8fb30245f

Contents?: true

Size: 1.56 KB

Versions: 26

Compression:

Stored size: 1.56 KB

Contents

module Tramway
  module Admin
    class ApplicationController < ActionController::Base
      include AuthManagment
      include RecordRoutesHelper
      before_action :authenticate_admin!
      before_action :check_available!
      before_action :collections_counts, if: :model_given?
      before_action :check_available_scope!, if: :model_given?, only: :index
      before_action :application

      protect_from_forgery with: :exception

      def check_available!
        render '/404' unless model_given?
      end

      def check_available_scope!
        if params[:scope].present?
          render '/404' unless available_scope_given?
        end
      end

      def collections_counts
        @counts = "#{params[:model]}Decorator".constantize.collections.reduce({}) do |hash, collection|
          hash.merge! collection => params[:model].constantize.active.send(collection).count
        end
      end

      def model_class
        params[:model].constantize
      end

      def decorator_class
        "#{model_class}Decorator".constantize
      end

      def form_class
        "#{model_class}Form".constantize
      end

      def application
        @application = ::Tramway::Core.application.model_class.first
      end

      private

      def model_given?
        params[:model].in?(::Tramway::Admin.available_models.map(&:to_s)) ||
          params[:model].in?(::Tramway::Admin.singleton_models.map(&:to_s))
      end

      def available_scope_given?
        params[:scope].present? && params[:scope].in?(decorator_class.collections.map(&:to_s))
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
tramway-admin-1.1.4.1 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.4 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.3.4 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.3.3 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.3.2 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.3.1 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.15 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.14 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.13 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.12 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.11 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.10 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.9 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.8 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.7 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.6 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.5 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.4 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.3 app/controllers/tramway/admin/application_controller.rb
tramway-admin-1.1.2.2 app/controllers/tramway/admin/application_controller.rb