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'
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)
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
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
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