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
- .connect(args, &blk) ⇒ Object
- .connects ⇒ Object
- .create_tables ⇒ Object
- .drop_tables ⇒ Object
- .init ⇒ Object
- .on_validate_write_access(&blk) ⇒ Object
- .recursive_walkthrough(args = {}, &blk) ⇒ Object
- .setup(data) ⇒ Object
- .write_access? ⇒ Boolean
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 |
.connects ⇒ Object
142 143 144 |
# File 'lib/dynamic_menus.rb', line 142 def self.connects return @connects end |
.create_tables ⇒ Object
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_tables ⇒ Object
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 |
.init ⇒ Object
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] = args[:for_menu]..order("sort") else = DynamicMenu.where(dynamic_menu_id: 0).order("sort") end level = args[:level] level = 0 if level == nil .each do || show = true if !.idstr.to_s.empty? idstr = .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: , args: args) if !res show = false break end end end end if !args[:all] and show and @connects and .check_callbacks? .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: , 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: , level: level) DynamicMenus.recursive_walkthrough(for_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
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 |