From 8298b2a4bf7698408bd504f42f171962eb397fd7 Mon Sep 17 00:00:00 2001 From: did Date: Tue, 18 Oct 2011 18:45:45 +0200 Subject: [PATCH] preventing recursive loop when used as engine in an app --- app/cells/admin/menu_cell.rb | 16 ++++++++++------ app/cells/admin/settings_menu_cell.rb | 12 ++++++------ .../{settings_menu => sub_menu}/show.html.haml | 0 app/cells/admin/sub_menu_cell.rb | 11 +++++++++++ 4 files changed, 27 insertions(+), 12 deletions(-) rename app/cells/admin/{settings_menu => sub_menu}/show.html.haml (100%) create mode 100644 app/cells/admin/sub_menu_cell.rb diff --git a/app/cells/admin/menu_cell.rb b/app/cells/admin/menu_cell.rb index bc54789b..1d6c920b 100644 --- a/app/cells/admin/menu_cell.rb +++ b/app/cells/admin/menu_cell.rb @@ -40,13 +40,17 @@ class Admin::MenuCell < Cell::Base method_name = "build_list_with_#{name}".to_sym previous_method_name = "build_list_without_#{name}".to_sym - self.send(:define_method, method_name) do - self.send(previous_method_name) - block.call(MenuProxy.new(self)) - end + unless self.instance_methods.include?(method_name) # prevents the method to be called twice which will raise a "stack level too deep" exception - # Note: this might cause "stack level too deep" if called twice for the same name - alias_method_chain :build_list, name.to_sym + self.send(:define_method, method_name) do + self.send(previous_method_name) + block.call(MenuProxy.new(self)) + end + + # Note: this might cause "stack level too deep" if called twice for the same name + alias_method_chain :build_list, name.to_sym + + end end protected diff --git a/app/cells/admin/settings_menu_cell.rb b/app/cells/admin/settings_menu_cell.rb index 30b6ae8d..3da2659d 100644 --- a/app/cells/admin/settings_menu_cell.rb +++ b/app/cells/admin/settings_menu_cell.rb @@ -1,4 +1,4 @@ -class Admin::SettingsMenuCell < ::Admin::MenuCell +class Admin::SettingsMenuCell < ::Admin::SubMenuCell #::Admin::MenuCell protected @@ -8,10 +8,10 @@ class Admin::SettingsMenuCell < ::Admin::MenuCell add :account, :url => edit_admin_my_account_url end - def build_item(name, attributes) - item = super - enhanced_class = "#{'on' if name.to_s == sections(:sub)} #{item[:class]}" - item.merge(:class => enhanced_class) - end + # def build_item(name, attributes) + # item = super + # enhanced_class = "#{'on' if name.to_s == sections(:sub)} #{item[:class]}" + # item.merge(:class => enhanced_class) + # end end diff --git a/app/cells/admin/settings_menu/show.html.haml b/app/cells/admin/sub_menu/show.html.haml similarity index 100% rename from app/cells/admin/settings_menu/show.html.haml rename to app/cells/admin/sub_menu/show.html.haml diff --git a/app/cells/admin/sub_menu_cell.rb b/app/cells/admin/sub_menu_cell.rb new file mode 100644 index 00000000..4884bcd7 --- /dev/null +++ b/app/cells/admin/sub_menu_cell.rb @@ -0,0 +1,11 @@ +class Admin::SubMenuCell < ::Admin::MenuCell + + protected + + def build_item(name, attributes) + item = super + enhanced_class = "#{'on' if name.to_s == sections(:sub)} #{item[:class]}" + item.merge(:class => enhanced_class) + end + +end