fixes #1897
This commit is contained in:
@@ -155,6 +155,14 @@ class Note < ActiveRecord::Base
|
|||||||
CurrentUser.user.increment!(:note_update_count)
|
CurrentUser.user.increment!(:note_update_count)
|
||||||
update_column(:version, version.to_i + 1)
|
update_column(:version, version.to_i + 1)
|
||||||
|
|
||||||
|
if merge_version?
|
||||||
|
merge_version
|
||||||
|
else
|
||||||
|
create_new_version
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_new_version
|
||||||
versions.create(
|
versions.create(
|
||||||
:updater_id => updater_id,
|
:updater_id => updater_id,
|
||||||
:updater_ip_addr => updater_ip_addr,
|
:updater_ip_addr => updater_ip_addr,
|
||||||
@@ -169,6 +177,24 @@ class Note < ActiveRecord::Base
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def merge_version
|
||||||
|
prev = versions.last
|
||||||
|
prev.update_attributes(
|
||||||
|
:x => x,
|
||||||
|
:y => y,
|
||||||
|
:width => width,
|
||||||
|
:height => height,
|
||||||
|
:is_active => is_active,
|
||||||
|
:body => body,
|
||||||
|
:version => version
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def merge_version?
|
||||||
|
prev = versions.last
|
||||||
|
prev && prev.updater_id == CurrentUser.user.id && prev.updated_at > 1.hour.ago
|
||||||
|
end
|
||||||
|
|
||||||
def revert_to(version)
|
def revert_to(version)
|
||||||
self.x = version.x
|
self.x = version.x
|
||||||
self.y = version.y
|
self.y = version.y
|
||||||
|
|||||||
@@ -57,7 +57,9 @@ class NoteTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
should "create a version" do
|
should "create a version" do
|
||||||
assert_difference("NoteVersion.count", 1) do
|
assert_difference("NoteVersion.count", 1) do
|
||||||
@note = FactoryGirl.create(:note, :post => @post)
|
Timecop.travel(1.day.from_now) do
|
||||||
|
@note = FactoryGirl.create(:note, :post => @post)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal(1, @note.versions.count)
|
assert_equal(1, @note.versions.count)
|
||||||
@@ -109,7 +111,9 @@ class NoteTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
should "create a version" do
|
should "create a version" do
|
||||||
assert_difference("NoteVersion.count", 1) do
|
assert_difference("NoteVersion.count", 1) do
|
||||||
@note.update_attributes(:body => "fafafa")
|
Timecop.travel(1.day.from_now) do
|
||||||
|
@note.update_attributes(:body => "fafafa")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
assert_equal(2, @note.versions.count)
|
assert_equal(2, @note.versions.count)
|
||||||
assert_equal(2, @note.versions.last.version)
|
assert_equal(2, @note.versions.last.version)
|
||||||
|
|||||||
Reference in New Issue
Block a user