Module: DynamicMenus

Defined in:
lib/dynamic_menus.rb,
lib/dynamic_menus/version.rb

Constant Summary collapse

RECURSIVE_WALKTHROUGH_ALLOWED_PARAMS =
[:all, :for_menu, :level, :pass_args]
CONNECT_MODES =
[:validate_show, :check_callback]
CONNECT_ALLOWED_ARGS =
[:mode, :idstr]
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.connect(args, &blk) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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

.connectsObject



142
143
144
# File 'lib/dynamic_menus.rb', line 142

def self.connects
  return @connects
end

.create_tablesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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_tablesObject



35
36
37
38
39
40
# 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

.initObject



42
43
44
45
46
47
# 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) ⇒ Object



9
10
11
# File 'lib/dynamic_menus.rb', line 9

def self.on_validate_write_access &blk
  @on_validate_write_access = blk
end

.recursive_walkthrough(args = {}, &blk) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# 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?Boolean

Returns:

  • (Boolean)


13
14
15
16
# 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