genki-dm-has-versions 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +1 -1
- data/lib/dm-has-versions/has/versions.rb +26 -3
- data/spec/dm-has-versions_spec.rb +34 -0
- metadata +1 -1
data/Rakefile
CHANGED
@@ -2,7 +2,9 @@ module DataMapper
|
|
2
2
|
module Has
|
3
3
|
module Versions
|
4
4
|
def has_versions(options = {})
|
5
|
-
|
5
|
+
ignores = [options[:ignore]].flatten.compact.map do |ignore|
|
6
|
+
properties[ignore.to_s.intern]
|
7
|
+
end
|
6
8
|
|
7
9
|
class << self; self end.class_eval do
|
8
10
|
define_method :const_missing do |name|
|
@@ -36,7 +38,8 @@ module DataMapper
|
|
36
38
|
end
|
37
39
|
|
38
40
|
self.after :update do |result|
|
39
|
-
if result && dirty_attributes.except(*
|
41
|
+
if result && dirty_attributes.except(*ignores).present?
|
42
|
+
return result if pending_version_attributes.empty?
|
40
43
|
attributes = self.attributes.merge(pending_version_attributes)
|
41
44
|
original_key = "#{self.class.storage_name.singular}_id"
|
42
45
|
attributes[original_key.intern] = self.id
|
@@ -72,7 +75,27 @@ module DataMapper
|
|
72
75
|
def versions
|
73
76
|
version = self.class.const_get("Version")
|
74
77
|
original_key = "#{self.class.storage_name.singular}_id".intern
|
75
|
-
version.all(original_key => self.id)
|
78
|
+
version.all(original_key => self.id, :order => [:id.asc])
|
79
|
+
end
|
80
|
+
|
81
|
+
def version
|
82
|
+
versions.size
|
83
|
+
end
|
84
|
+
|
85
|
+
def revert_to(version)
|
86
|
+
if target = versions.first(:offset => version)
|
87
|
+
transaction do
|
88
|
+
self.properties.each do |property|
|
89
|
+
next if property.key?
|
90
|
+
name = property.name
|
91
|
+
self.attribute_set(name, target.attribute_get(name))
|
92
|
+
end
|
93
|
+
pending_version_attributes.clear
|
94
|
+
return false unless save
|
95
|
+
versions.all(:id.gte => target.id).destroy!
|
96
|
+
end
|
97
|
+
end
|
98
|
+
!!target
|
76
99
|
end
|
77
100
|
end
|
78
101
|
end
|
@@ -34,5 +34,39 @@ describe "dm-has-versions" do
|
|
34
34
|
@story.update_attributes :title => 'test-3'
|
35
35
|
@story.versions.size.should == 2
|
36
36
|
end
|
37
|
+
|
38
|
+
it "should not generate versions on update of ignoring properties" do
|
39
|
+
@story.versions.should be_empty
|
40
|
+
@story.update_attributes :updated_at => Time.now
|
41
|
+
@story.versions.should be_empty
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "destory and revert of versions" do
|
45
|
+
before do
|
46
|
+
@story.update_attributes :title => 'test-2'
|
47
|
+
@story.update_attributes :title => 'test-3'
|
48
|
+
@story.update_attributes :title => 'test-4'
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be tested on 3 versions" do
|
52
|
+
@story.versions.count.should == 3
|
53
|
+
@story.version.should == 3
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should emptyfy by calling destroy!" do
|
57
|
+
@story.versions.destroy!
|
58
|
+
@story.versions.should be_empty
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should revert stories" do
|
62
|
+
@story.title.should == "test-4"
|
63
|
+
@story.revert_to(2).should be_true
|
64
|
+
@story.title.should == "test-3"
|
65
|
+
@story.versions.should be_present
|
66
|
+
@story.revert_to(0).should be_true
|
67
|
+
@story.version.should == 0
|
68
|
+
@story.title.should == "test-1"
|
69
|
+
end
|
70
|
+
end
|
37
71
|
end
|
38
72
|
end
|