Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version at #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ post.destroy
# undelete post
post.undo! user

# post version at
post.version_at(yyyy, mm, dd, hh, min, sec)

# disable tracking for comments within a block
Comment.disable_tracking do
comment.update_attributes(:title => "Test 3")
Expand Down
10 changes: 10 additions & 0 deletions lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ def undo!(modifier = nil, options_or_version = nil)
save!
end

def version_at(year, month = nil, day = nil, hour = nil, minute = nil, second = nil)
at = Time.new(year, month, day, hour, minute, second)
duplicate = self.dup
history_tracks.where(:created_at.gt => at, action: "update").desc(:version).map do |h|
undo_attr = h.undo_attr(modifier)
duplicate.attributes.merge!(undo_attr)
end
duplicate
end

def redo!(modifier = nil, options_or_version = nil)
versions = get_versions_criteria(options_or_version).to_a
versions.sort! { |v1, v2| v1.version <=> v2.version }
Expand Down
23 changes: 23 additions & 0 deletions spec/integration/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,29 @@ class Foo < Comment
end
end

describe "version_at" do
before :each do
[Time.new(2014,01,01), Time.new(2014,01,02), Time.new(2014,01,03), Time.new(2014,01,04) ].each_with_index do |time, index|
post.history_tracks.create(created_at: time,
association_chain: [{"name"=>"Post", "id"=> post.id}],
modified: {"title"=>"Test #{index + 1}"},
original: {"title"=>"Test #{index}"},
version: index+1,
action: "update",
scope: "post",
modifier_id: user.id)
end
end

it "should get version_at" do
post.version_at(2013,12,31).title.should == "Test 0"
post.version_at(2014,01,01,12).title.should == "Test 1"
post.version_at(2014,01,02).title.should == "Test 2"
post.version_at(2014,01,03,23,59,59).title.should == "Test 3"
end

end

describe "trackables" do
before :each do
comment.update_attributes(title: "Test2") # version == 2
Expand Down