Swap and correct WikiMenuItems titles and names

Previously, the `title` attribute of a WikiMenuItem was used to identify
a page that was linked to.

With slugs, that makes no sense. We swap the name and title usage so
that:

1. Name is used for the wiki_page slug that is being linked to
2. Title is used for the displayed name
pull/4747/head
Oliver Günther 8 years ago
parent 7842fd5a39
commit 1ff88a823b
  1. 57
      db/migrate/20160803094931_wiki_menu_titles_to_slug.rb
  2. 78
      spec/features/wiki/wiki_menu_item_migration_spec.rb

@ -0,0 +1,57 @@
class WikiMenuTitlesToSlug < ActiveRecord::Migration
def up
migrate_menu_items
end
def down
rollback_menu_items
end
##
# Fix lookup of wiki pages in menu items by referencing the actual slug in the title attribute.
# As the title attribute is fixed from MenuItem, and the name was used, swap the two around
# to avoid confusing the actual title of the menu item (previously == name).
def migrate_menu_items
ActiveRecord::Base.transaction do
::MenuItems::WikiMenuItem.includes(:wiki).find_each do |item|
# Find the page
wiki_page = item.wiki.find_page(item.title)
# Set the title to the actual slug
# If the page could not be found, migrate the title to form a slug
slug = wiki_page.nil? ? item.title.to_url : wiki_page.slug
# Use the name to set the title.
# This clears up the previously irritating mixup of the two.
menu_item_title = item.name
item.update_columns(title: menu_item_title, name: slug)
end
end
end
##
#
# Restore the old title wherever possible
# This tries to remove the slug usages without guaranteeing that links
# will be valid afterwards.
def rollback_menu_items
ActiveRecord::Base.transaction do
::MenuItems::WikiMenuItem.includes(:wiki).find_each do |item|
# Find the page
wiki_page = item.wiki.find_page(item.title)
# Restore the switch of title and name
old_name = item.title
old_title =
if wiki_page.present?
wiki_page.title
else
item.name
end
item.update_columns(title: old_title, name: old_name)
end
end
end
end

@ -0,0 +1,78 @@
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
require Rails.root.join('db/migrate/20160803094931_wiki_menu_titles_to_slug.rb')
describe 'Wiki menu_items migration', type: :feature do
let(:project) { FactoryGirl.create :project }
let(:wiki_page) {
FactoryGirl.create :wiki_page_with_content,
wiki: project.wiki,
title: 'Base de donées'
}
let!(:menu_item) {
FactoryGirl.create(:wiki_menu_item,
:with_menu_item_options,
wiki: project.wiki,
name: 'My linked page',
title: wiki_page.title)
}
before do
project.wiki.pages << wiki_page
# Run the title replacement of the migration
::WikiMenuTitlesToSlug.new.migrate_menu_items
menu_item.reload
end
it 'updates the menu item' do
expect(menu_item.name).to eq(wiki_page.slug)
expect(menu_item.title).to eq('My linked page')
end
describe 'visiting the wiki' do
let(:user) { FactoryGirl.create :admin }
before do
login_as(user)
end
it 'shows the menu item' do
visit project_wiki_path(project, project.wiki)
link = page.find('#menu-sidebar a.icon-wiki', text: menu_item.name)
link.click
expect(page).to have_selector('.wiki-title', text: wiki_page.title)
expect(current_path).to eq(project_wiki_path(project, wiki_page.slug))
end
end
end
Loading…
Cancel
Save