diff --git a/app/controllers/wiki_menu_items_controller.rb b/app/controllers/wiki_menu_items_controller.rb index 76dd045173..119b65ee80 100644 --- a/app/controllers/wiki_menu_items_controller.rb +++ b/app/controllers/wiki_menu_items_controller.rb @@ -96,10 +96,11 @@ class WikiMenuItemsController < ApplicationController possible_parent_menu_items = WikiMenuItem.main_items(@page.wiki.id) - [@wiki_menu_item] @parent_menu_item_options = possible_parent_menu_items.map {|item| [item.name, item.id]} - @selected_parent_menu_item = if @wiki_menu_item.parent + + @selected_parent_menu_item_id = if @wiki_menu_item.parent @wiki_menu_item.parent.id - elsif @page.parent && menu_item_of_parent_page = possible_parent_menu_items.detect {|item| item.title == @page.parent.title} - menu_item_of_parent_page.id + else + @page.nearest_parent_menu_item(:is_main_item => true).try :id end end end diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index e8b436be03..050817087c 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -209,10 +209,20 @@ class WikiPage < ActiveRecord::Base def nearest_menu_item if self.menu_item self.menu_item - elsif self.parent - self.parent.nearest_menu_item else - nil + nearest_parent_menu_item + end + end + + def nearest_parent_menu_item(options={}) + return nil unless self.parent + + options = options.with_indifferent_access + + if (parent_menu_item = self.parent.menu_item) && (!options[:is_main_item] || parent_menu_item.is_main_item?) + parent_menu_item + else + self.parent.nearest_parent_menu_item end end diff --git a/app/views/wiki_menu_items/edit.html.erb b/app/views/wiki_menu_items/edit.html.erb index 576391c894..facbcf11d1 100644 --- a/app/views/wiki_menu_items/edit.html.erb +++ b/app/views/wiki_menu_items/edit.html.erb @@ -65,7 +65,7 @@ See doc/COPYRIGHT.rdoc for more details. <% disabled = @parent_menu_item_options.empty? %> <%= form.radio_button 'setting', :sub_item, :disabled => disabled %> <%= form.label 'setting_sub_item', l(:label_wiki_show_submenu_item), {:id => 'with-select'} %> - <%= select_tag "parent_wiki_menu_item", options_for_select(@parent_menu_item_options, @selected_parent_menu_item), :disabled => disabled %> + <%= select_tag "parent_wiki_menu_item", options_for_select(@parent_menu_item_options, @selected_parent_menu_item_id), :disabled => disabled %>