Sha256: af78a7cf07fc78c20b7be34973f60537468fb556cc9e6a9d2360b14f3cb8d45a

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

#encoding: utf-8
class AdminStructure

  include Rails.application.routes.url_helpers

  Section = Struct.new(:name, :path, :icon, :description, :counter, :show_menu_counter, :creation_path) do

    def show_menu_counter?
      show_menu_counter.present?
    end

  end

  Separator = Struct.new(:title)

  CONFIG_FILE_NAME = 'structure.rb'

  attr_accessor :sections

  def initialize
    @sections = []
    load_sections_from_config
  end

  def menu_items
    dashboard_item = Section.new('Сводка', admin_dashboard_path, 'glyphicon glyphicon-align-left')
    [dashboard_item] + @sections
  end

  def sections
    @sections.find_all { |section| section.is_a?(Section) }
  end

  private

  def load_sections_from_config
    config_path = Rails.root.join('config', CONFIG_FILE_NAME).to_s
    if File.exist?(config_path)
      instance_eval(File.read(config_path), config_path)
    end
  end

  # Options:
  # creation_path - false or path string
  def section(section_reference, options = {})
    path = options.delete(:path)
    icon = options.delete(:icon)
    description = options.delete(:description)
    counter = options.delete(:counter)
    show_menu_counter = options.delete(:show_menu_counter)
    creation_path = options.delete(:creation_path)
    if section_reference.is_a?(Class)
      section_name = section_reference.model_name.human
      path = send("admin_#{section_reference.model_name.route_key}_path") unless path
      counter = ->() { section_reference.count } unless counter
      creation_path = send("new_admin_#{section_reference.model_name.singular}_path") if creation_path.nil?
    else
      section_name = section_reference
    end
    @sections << Section.new(section_name, path, icon, description, counter, show_menu_counter, creation_path)
  end

  def separator(title)
    @sections << Separator.new(title)
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
coalla-cms-0.4.2.0 app/utils/admin_structure.rb
coalla-cms-0.4.4.3 app/utils/admin_structure.rb