From b8b35ab05f70e6edd524397d2fb0dca3630fa840 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 12 Mar 2011 18:09:46 +0000 Subject: [PATCH] Moved wiki page updated_on eager load to a scope and fixed timestamp titles on wiki page index (#7818). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5098 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/wiki_controller.rb | 29 +++++++++++-------------- app/helpers/application_helper.rb | 4 ++-- app/models/wiki_page.rb | 18 +++++++++++++++ test/functional/wiki_controller_test.rb | 5 +++-- test/unit/wiki_page_test.rb | 11 ++++++++-- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index e7e389306..f3a9b90ad 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -1,5 +1,5 @@ -# redMine - project management software -# Copyright (C) 2006-2007 Jean-Philippe Lang +# Redmine - project management software +# Copyright (C) 2006-2011 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -44,7 +44,14 @@ class WikiController < ApplicationController # List of pages, sorted alphabetically and by parent (hierarchy) def index - load_pages_grouped_by_date_without_content + load_pages_for_index + @pages_by_parent_id = @pages.group_by(&:parent_id) + end + + # List of page, by last update + def date_index + load_pages_for_index + @pages_by_date = @pages.group_by {|p| p.updated_on.to_date} end # display a page (in editing mode if it doesn't exist) @@ -215,10 +222,6 @@ class WikiController < ApplicationController redirect_to :action => 'show', :project_id => @project, :id => nil end end - - def date_index - load_pages_grouped_by_date_without_content - end def preview page = @wiki.find_page(params[:id]) @@ -266,14 +269,8 @@ private extend helper unless self.instance_of?(helper) helper.instance_method(:initial_page_content).bind(self).call(page) end - - # eager load information about last updates, without loading text - def load_pages_grouped_by_date_without_content - @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on", - :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id", - :order => 'title' - @pages_by_date = @pages.group_by {|p| p.updated_on.to_date} - @pages_by_parent_id = @pages.group_by(&:parent_id) - end + def load_pages_for_index + @pages = @wiki.pages.with_updated_on.all(:order => 'title', :include => {:wiki => :project}) + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index be0ebaec1..03f40e2bf 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,5 +1,5 @@ # Redmine - project management software -# Copyright (C) 2006-2010 Jean-Philippe Lang +# Copyright (C) 2006-2011 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -194,7 +194,7 @@ module ApplicationHelper pages[node].each do |page| content << "
  • " content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title}, - :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil)) + :title => (page.updated_on ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil)) content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id] content << "
  • \n" end diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 8cd836a31..923f381b7 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -41,6 +41,12 @@ class WikiPage < ActiveRecord::Base validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false validates_associated :content + # eager load information about last updates, without loading text + named_scope :with_updated_on, { + :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on", + :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id" + } + # Wiki pages that are protected by default DEFAULT_PROTECTED_PAGES = %w(sidebar) @@ -121,6 +127,18 @@ class WikiPage < ActiveRecord::Base content.text if content end + def updated_on + unless @updated_on + if time = read_attribute(:updated_on) + # content updated_on was eager loaded with the page + @updated_on = Time.parse(time) rescue nil + else + @updated_on = content && content.updated_on + end + end + @updated_on + end + # Returns true if usr is allowed to edit the page, otherwise false def editable_by?(usr) !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project) diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index c40b2f392..80ed17b61 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -1,5 +1,5 @@ -# redMine - project management software -# Copyright (C) 2006-2007 Jean-Philippe Lang +# Redmine - project management software +# Copyright (C) 2006-2011 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -339,6 +339,7 @@ class WikiControllerTest < ActionController::TestCase pages = assigns(:pages) assert_not_nil pages assert_equal Project.find(1).wiki.pages.size, pages.size + assert_equal pages.first.content.updated_on, pages.first.updated_on assert_tag :ul, :attributes => { :class => 'pages-hierarchy' }, :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' }, diff --git a/test/unit/wiki_page_test.rb b/test/unit/wiki_page_test.rb index 20c6d383e..14c7ee6a8 100644 --- a/test/unit/wiki_page_test.rb +++ b/test/unit/wiki_page_test.rb @@ -1,5 +1,5 @@ -# redMine - project management software -# Copyright (C) 2006-2007 Jean-Philippe Lang +# Redmine - project management software +# Copyright (C) 2006-2011 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -121,4 +121,11 @@ class WikiPageTest < ActiveSupport::TestCase assert_nil child.parent_id end end + + def test_updated_on_eager_load + page = WikiPage.with_updated_on.first + assert page.is_a?(WikiPage) + assert_not_nil page.read_attribute(:updated_on) + assert_equal page.content.updated_on, page.updated_on + end end