Browse Source

Replaces a piggy back query with an association for loading wiki pages updates.

git-svn-id: http://svn.redmine.org/redmine/trunk@14625 e93f8b46-1217-0410-a6f0-8f06a7374b81
tags/3.2.0
Jean-Philippe Lang 8 years ago
parent
commit
2d78003c87
3 changed files with 23 additions and 23 deletions
  1. 2
    0
      app/models/wiki_content.rb
  2. 14
    16
      app/models/wiki_page.rb
  3. 7
    7
      test/unit/wiki_page_test.rb

+ 2
- 0
app/models/wiki_content.rb View File

@@ -29,6 +29,8 @@ class WikiContent < ActiveRecord::Base

after_save :send_notification

scope :without_text, lambda {select(:id, :page_id, :version, :updated_on)}

def visible?(user=User.current)
page.visible?(user)
end

+ 14
- 16
app/models/wiki_page.rb View File

@@ -23,6 +23,8 @@ class WikiPage < ActiveRecord::Base

belongs_to :wiki
has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
has_one :content_without_text, lambda {without_text.readonly}, :class_name => 'WikiContent', :foreign_key => 'page_id'

acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
acts_as_tree :dependent => :nullify, :order => 'title'

@@ -52,10 +54,7 @@ class WikiPage < ActiveRecord::Base
after_save :handle_children_move

# eager load information about last updates, without loading text
scope :with_updated_on, lambda {
select("#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version").
joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id")
}
scope :with_updated_on, lambda { preload(:content_without_text) }

# Wiki pages that are protected by default
DEFAULT_PROTECTED_PAGES = %w(sidebar)
@@ -183,18 +182,11 @@ class WikiPage < ActiveRecord::Base
end

def updated_on
unless @updated_on
if time = read_attribute(:updated_on)
# content updated_on was eager loaded with the page
begin
@updated_on = (self.class.default_timezone == :utc ? Time.parse(time.to_s).utc : Time.parse(time.to_s).localtime)
rescue
end
else
@updated_on = content && content.updated_on
end
end
@updated_on
content_attribute(:updated_on)
end

def version
content_attribute(:version)
end

# Returns true if usr is allowed to edit the page, otherwise false
@@ -244,6 +236,12 @@ class WikiPage < ActiveRecord::Base
errors.add(:parent_title, :not_same_project)
end
end

private

def content_attribute(name)
(association(:content).loaded? ? content : content_without_text).try(name)
end
end

class WikiDiff < Redmine::Helpers::Diff

+ 7
- 7
test/unit/wiki_page_test.rb View File

@@ -144,13 +144,13 @@ class WikiPageTest < ActiveSupport::TestCase
end
end

def test_updated_on_eager_load
page = WikiPage.with_updated_on.order('id').first
assert page.is_a?(WikiPage)
assert_not_nil page.read_attribute(:updated_on)
assert_equal Time.gm(2007, 3, 6, 23, 10, 51), page.content.updated_on
assert_equal page.content.updated_on, page.updated_on
assert_not_nil page.read_attribute(:version)
def test_with_updated_on_scope_should_preload_updated_on_and_version
page = WikiPage.with_updated_on.where(:id => 1).first
# make the assertions fail if attributes are not preloaded
WikiContent.update_all(:updated_on => '2001-01-01 10:00:00', :version => 1)
assert_equal Time.gm(2007, 3, 6, 23, 10, 51), page.updated_on
assert_equal 3, page.version
end

def test_descendants

Loading…
Cancel
Save