From: Jean-Philippe Lang Date: Mon, 28 Sep 2015 18:39:07 +0000 (+0000) Subject: Replaces a piggy back query with an association for loading wiki pages updates. X-Git-Tag: 3.2.0~222 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2d78003c87099fb6a832025027701e9456727727;p=redmine.git 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 --- diff --git a/app/models/wiki_content.rb b/app/models/wiki_content.rb index 223b6ca47..992e4902f 100644 --- a/app/models/wiki_content.rb +++ b/app/models/wiki_content.rb @@ -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 diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index ff8a12a99..988819a77 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -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 diff --git a/test/unit/wiki_page_test.rb b/test/unit/wiki_page_test.rb index 68e77a2e5..5fb184175 100644 --- a/test/unit/wiki_page_test.rb +++ b/test/unit/wiki_page_test.rb @@ -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