require "rubygems" require "active_record" require "globalize3" require "action_controller" require "haml" require "haml-rails" module DynamicMenus def self.on_validate_write_access &blk @on_validate_write_access = blk end def self.write_access? return true if !@on_validate_write_access return @on_validate_write_access.call end 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 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 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 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 RECURSIVE_WALKTHROUGH_ALLOWED_PARAMS = [:all, :for_menu, :level, :pass_args] 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 CONNECT_MODES = [:validate_show, :check_callback] CONNECT_ALLOWED_ARGS = [:mode, :idstr] 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 def self.connects return @connects end end def _(str, args = {}) return I18n.t(str, *args, args.merge(:default => str)) end