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
This commit is contained in:
parent
f7127e9466
commit
b8b35ab05f
|
@ -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
|
||||
|
|
|
@ -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 << "<li>"
|
||||
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 << "</li>\n"
|
||||
end
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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' },
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue