Page#revise raises ValidationError if a revision is exactly same as the page before
This commit is contained in:
parent
7780a13fe8
commit
878a6336fb
2 changed files with 54 additions and 34 deletions
|
@ -15,13 +15,18 @@ class Page
|
||||||
end
|
end
|
||||||
|
|
||||||
def revise(content, created_at, author)
|
def revise(content, created_at, author)
|
||||||
|
if not @revisions.empty? and content == @revisions.last.content
|
||||||
|
raise Instiki::ValidationError.new(
|
||||||
|
"You have tried to save page '#{name}' without changing its content")
|
||||||
|
end
|
||||||
|
|
||||||
# A user may change a page, look at it and make some more changes - several times.
|
# A user may change a page, look at it and make some more changes - several times.
|
||||||
# Not to record every such iteration as a new revision, if the previous revision was done
|
# Not to record every such iteration as a new revision, if the previous revision was done
|
||||||
# by the same author, not more than 30 minutes ago, then update the last revision instead of
|
# by the same author, not more than 30 minutes ago, then update the last revision instead of
|
||||||
# creating a new one
|
# creating a new one
|
||||||
if !@revisions.empty? && continous_revision?(created_at, author)
|
if !@revisions.empty? && continous_revision?(created_at, author)
|
||||||
@revisions.last.created_at = created_at
|
@revisions.last.created_at = created_at
|
||||||
@revisions.last.content = content
|
@revisions.last.content = content
|
||||||
@revisions.last.clear_display_cache
|
@revisions.last.clear_display_cache
|
||||||
else
|
else
|
||||||
@revisions << Revision.new(self, @revisions.length, content, created_at, author)
|
@revisions << Revision.new(self, @revisions.length, content, created_at, author)
|
||||||
|
@ -38,11 +43,11 @@ class Page
|
||||||
def revisions?
|
def revisions?
|
||||||
revisions.length > 1
|
revisions.length > 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def revised_on
|
def revised_on
|
||||||
created_on
|
created_on
|
||||||
end
|
end
|
||||||
|
|
||||||
def in_category?(cat)
|
def in_category?(cat)
|
||||||
cat.nil? || cat.empty? || categories.include?(cat)
|
cat.nil? || cat.empty? || categories.include?(cat)
|
||||||
end
|
end
|
||||||
|
@ -50,7 +55,7 @@ class Page
|
||||||
def categories
|
def categories
|
||||||
display_content.find_chunks(Category).map { |cat| cat.list }.flatten
|
display_content.find_chunks(Category).map { |cat| cat.list }.flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
def authors
|
def authors
|
||||||
revisions.collect { |rev| rev.author }
|
revisions.collect { |rev| rev.author }
|
||||||
end
|
end
|
||||||
|
@ -67,18 +72,19 @@ class Page
|
||||||
def link(options = {})
|
def link(options = {})
|
||||||
web.make_link(name, nil, options)
|
web.make_link(name, nil, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def author_link(options = {})
|
def author_link(options = {})
|
||||||
web.make_link(author, nil, options)
|
web.make_link(author, nil, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def continous_revision?(created_at, author)
|
def continous_revision?(created_at, author)
|
||||||
@revisions.last.author == author && @revisions.last.created_at + 30.minutes > created_at
|
@revisions.last.author == author && @revisions.last.created_at + 30.minutes > created_at
|
||||||
end
|
end
|
||||||
|
|
||||||
# Forward method calls to the current revision, so the page responds to all revision calls
|
# Forward method calls to the current revision, so the page responds to all revision calls
|
||||||
def method_missing(method_symbol)
|
def method_missing(method_symbol)
|
||||||
revisions.last.send(method_symbol)
|
revisions.last.send(method_symbol)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,6 +11,7 @@ class MockWeb < Web
|
||||||
end
|
end
|
||||||
|
|
||||||
class PageTest < Test::Unit::TestCase
|
class PageTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@page = Page.new(
|
@page = Page.new(
|
||||||
MockWeb.new,
|
MockWeb.new,
|
||||||
|
@ -21,11 +22,7 @@ class PageTest < Test::Unit::TestCase
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_basics
|
def test_lock
|
||||||
assert_equal "First Page", @page.plain_name
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_locking
|
|
||||||
assert !@page.locked?(Time.local(2004, 4, 4, 16, 50))
|
assert !@page.locked?(Time.local(2004, 4, 4, 16, 50))
|
||||||
|
|
||||||
@page.lock(Time.local(2004, 4, 4, 16, 30), "DavidHeinemeierHansson")
|
@page.lock(Time.local(2004, 4, 4, 16, 30), "DavidHeinemeierHansson")
|
||||||
|
@ -38,19 +35,52 @@ class PageTest < Test::Unit::TestCase
|
||||||
assert !@page.locked?(Time.local(2004, 4, 4, 16, 50))
|
assert !@page.locked?(Time.local(2004, 4, 4, 16, 50))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_locking_duration
|
def test_lock_duration
|
||||||
@page.lock(Time.local(2004, 4, 4, 16, 30), "DavidHeinemeierHansson")
|
@page.lock(Time.local(2004, 4, 4, 16, 30), "DavidHeinemeierHansson")
|
||||||
|
|
||||||
assert_equal 15, @page.lock_duration(Time.local(2004, 4, 4, 16, 45))
|
assert_equal 15, @page.lock_duration(Time.local(2004, 4, 4, 16, 45))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_revision
|
def test_plain_name
|
||||||
@page.revise("HisWay would be MyWay in kinda lame", Time.local(2004, 4, 4, 16, 55), "MarianneSyhler")
|
assert_equal "First Page", @page.plain_name
|
||||||
assert_equal 2, @page.revisions.length, "Should have two revisions"
|
end
|
||||||
assert_equal "MarianneSyhler", @page.author, "Mary should be the author now"
|
|
||||||
assert_equal "DavidHeinemeierHansson", @page.revisions.first.author, "David was the first author"
|
def test_revise
|
||||||
|
@page.revise('HisWay would be MyWay in kinda lame', Time.local(2004, 4, 4, 16, 55), 'MarianneSyhler')
|
||||||
|
assert_equal 2, @page.revisions.length, 'Should have two revisions'
|
||||||
|
assert_equal 'MarianneSyhler', @page.author, 'Mary should be the author now'
|
||||||
|
assert_equal 'DavidHeinemeierHansson', @page.revisions.first.author, 'David was the first author'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_revise_continous_revision
|
||||||
|
@page.revise('HisWay would be MyWay in kinda lame', Time.local(2004, 4, 4, 16, 55), 'MarianneSyhler')
|
||||||
|
assert_equal 2, @page.revisions.length
|
||||||
|
|
||||||
|
@page.revise('HisWay would be MyWay in kinda update', Time.local(2004, 4, 4, 16, 57), 'MarianneSyhler')
|
||||||
|
assert_equal 2, @page.revisions.length
|
||||||
|
assert_equal 'HisWay would be MyWay in kinda update', @page.revisions.last.content
|
||||||
|
assert_equal Time.local(2004, 4, 4, 16, 57), @page.revisions.last.created_at
|
||||||
|
|
||||||
|
@page.revise('HisWay would be MyWay in the house', Time.local(2004, 4, 4, 16, 58), 'DavidHeinemeierHansson')
|
||||||
|
assert_equal 3, @page.revisions.length
|
||||||
|
assert_equal 'HisWay would be MyWay in the house', @page.revisions.last.content
|
||||||
|
|
||||||
|
@page.revise('HisWay would be MyWay in my way', Time.local(2004, 4, 4, 17, 30), 'DavidHeinemeierHansson')
|
||||||
|
assert_equal 4, @page.revisions.length
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_revise_content_unchanged
|
||||||
|
last_revision_before = @page.revisions.last
|
||||||
|
revisions_number_before = @page.revisions.size
|
||||||
|
|
||||||
|
assert_raises(Instiki::ValidationError) {
|
||||||
|
@page.revise(@page.revisions.last.content.dup, Time.now, 'AlexeyVerkhovsky')
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_same last_revision_before, @page.revisions.last
|
||||||
|
assert_equal revisions_number_before, @page.revisions.size
|
||||||
|
end
|
||||||
|
|
||||||
def test_rollback
|
def test_rollback
|
||||||
@page.revise("spot two", Time.now, "David")
|
@page.revise("spot two", Time.now, "David")
|
||||||
@page.revise("spot three", Time.now + 2000, "David")
|
@page.revise("spot three", Time.now + 2000, "David")
|
||||||
|
@ -59,20 +89,4 @@ class PageTest < Test::Unit::TestCase
|
||||||
assert_equal "spot two", @page.content
|
assert_equal "spot two", @page.content
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_continous_revision
|
|
||||||
@page.revise("HisWay would be MyWay in kinda lame", Time.local(2004, 4, 4, 16, 55), "MarianneSyhler")
|
|
||||||
assert_equal 2, @page.revisions.length
|
|
||||||
|
|
||||||
@page.revise("HisWay would be MyWay in kinda update", Time.local(2004, 4, 4, 16, 57), "MarianneSyhler")
|
|
||||||
assert_equal 2, @page.revisions.length
|
|
||||||
assert_equal "HisWay would be MyWay in kinda update", @page.revisions.last.content
|
|
||||||
assert_equal Time.local(2004, 4, 4, 16, 57), @page.revisions.last.created_at
|
|
||||||
|
|
||||||
@page.revise("HisWay would be MyWay in the house", Time.local(2004, 4, 4, 16, 58), "DavidHeinemeierHansson")
|
|
||||||
assert_equal 3, @page.revisions.length
|
|
||||||
assert_equal "HisWay would be MyWay in the house", @page.revisions.last.content
|
|
||||||
|
|
||||||
@page.revise("HisWay would be MyWay in my way", Time.local(2004, 4, 4, 17, 30), "DavidHeinemeierHansson")
|
|
||||||
assert_equal 4, @page.revisions.length
|
|
||||||
end
|
|
||||||
end
|
end
|
Loading…
Reference in a new issue