module DynamicMenus

Constants

CONNECT_ALLOWED_ARGS
CONNECT_MODES
RECURSIVE_WALKTHROUGH_ALLOWED_PARAMS
VERSION

Public Class Methods

connect(args, &blk) click to toggle source
# File lib/dynamic_menus.rb, line 122
def self.connect(args, &blk)
  args.each do |key, val|
    raise "Invalid key: '#{key}'." if !CONNECT_ALLOWED_ARGS.include?(key)
  end
  
  mode = args[:mode]
  raise "No ':modes' given." if !mode
  raise "Invalid mode: '#{mode}'." if !CONNECT_MODES.include?(mode)
  
  idstr = args[:idstr]
  raise "Invalid ':idstr' given: '#{idstr}' - #{idstr.class.name}." if !idstr.is_a?(Symbol)
  
  @connects = {} if !@connects
  @connects[mode] = {} if !@connects.key?(mode)
  @connects[mode][idstr] = [] if !@connects[mode].key?(idstr)
  @connects[mode][idstr] << {blk: blk}
  
  true
end
connects() click to toggle source
# File lib/dynamic_menus.rb, line 142
def self.connects
  return @connects
end
create_tables() click to toggle source
# File lib/dynamic_menus.rb, line 18
def self.create_tables
  ActiveRecord::Schema.define do
    if !table_exists? :dynamic_menus
      create_table :dynamic_menus, force: true do |t|
        t.integer :dynamic_menu_id
        t.string :idstr
        t.string :url
        t.string :check_callbacks
        t.integer :sort
      end
    end
    
    DynamicMenus.init
    DynamicMenu.create_translation_table! title: :string if !table_exists? :dynamic_menu_translations
  end
end
drop_tables() click to toggle source
# File lib/dynamic_menus.rb, line 35
def self.drop_tables
  ActiveRecord::Schema.define do
    drop_table :dynamic_menus if table_exists? :dynamic_menus
    drop_table :dynamic_menu_translations if table_exists? :dynamic_menu_translations
  end
end
init() click to toggle source
# File lib/dynamic_menus.rb, line 42
def self.init
  return nil if @initialized
  @initialized = true
  require "#{File.dirname(__FILE__)}/../app/models/dynamic_menu.rb"
  require "#{File.dirname(__FILE__)}/../app/controllers/dynamic_menus_controller.rb"
end
on_validate_write_access(&blk) click to toggle source
# File lib/dynamic_menus.rb, line 9
def self.on_validate_write_access &blk
  @on_validate_write_access = blk
end
recursive_walkthrough(args = {}, &blk) click to toggle source
# File lib/dynamic_menus.rb, line 64
def self.recursive_walkthrough(args = {}, &blk)
  args.each do |key, val|
    raise "Invalid key: '#{key}'." if !RECURSIVE_WALKTHROUGH_ALLOWED_PARAMS.include?(key)
  end
  
  raise "No block was given." if !blk
  
  if args[:for_menu]
    menus = args[:for_menu].dynamic_menus.order("sort")
  else
    menus = DynamicMenu.where(dynamic_menu_id: 0).order("sort")
  end
  
  level = args[:level]
  level = 0 if level == nil
  
  menus.each do |menu|
    show = true
    
    if !menu.idstr.to_s.empty?
      idstr = menu.idstr.to_sym
      
      if !args[:all] and @connects and @connects.key?(:validate_show) and @connects[:validate_show].key?(idstr)
        @connects[:validate_show][idstr].each do |data|
          res = data[:blk].call(menu: menu, args: args)
          if !res
            show = false
            break
          end
        end
      end
    end
    
    if !args[:all] and show and @connects and menu.check_callbacks?
      menu.check_callbacks_enum.each do |callback_id|
        if @connects.key?(:check_callback) and @connects[:check_callback].key?(callback_id)
          @connects[:check_callback][callback_id].each do |data|
            res = data[:blk].call(menu: menu, args: args)
            if !res
              show = false
              break
            end
          end
        else
          warn "DynamicMenus: No such check_callback has been connected: '#{callback_id}'."
        end
      end
    end
    
    if show
      blk.call(menu: menu, level: level)
      DynamicMenus.recursive_walkthrough(for_menu: menu, level: level + 1, &blk)
    end
  end
end
setup(data) click to toggle source
# File lib/dynamic_menus.rb, line 49
def self.setup(data)
  routes = data[:routes]
  self.init
  routes.resources :dynamic_menus
  
  #routes.match "/dynamic_menus/:action", to: proc{ |env|
  #  DynamicMenus.init
  #  action = env["action_dispatch.request.path_parameters"][:action]
  #  DynamicMenusController.action(action).call(env)
  #}
  
  #routes.match "/dynamic_menus" => routes.redirect("/dynamic_menus/index")
end
write_access?() click to toggle source
# File lib/dynamic_menus.rb, line 13
def self.write_access?
  return true if !@on_validate_write_access
  return @on_validate_write_access.call
end