undestroy 0.1.0 → 0.1.1
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/lib/undestroy/archive.rb +6 -3
- data/lib/undestroy/binding/active_record.rb +11 -1
- data/lib/undestroy/transfer.rb +1 -1
- data/lib/undestroy/version.rb +1 -1
- data/test/fixtures/ar.rb +13 -0
- data/test/integration/active_record_test.rb +24 -0
- data/test/unit/archive_test.rb +24 -1
- data/test/unit/binding/active_record_test.rb +24 -1
- data/test/unit/transfer_test.rb +8 -1
- metadata +4 -4
data/lib/undestroy/archive.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
|
2
2
|
class Undestroy::Archive
|
3
|
-
attr_accessor :source, :config, :transfer
|
3
|
+
attr_accessor :source, :config, :transfer, :transfer_options
|
4
4
|
|
5
5
|
def initialize(args={})
|
6
6
|
validate_arguments(args)
|
@@ -8,12 +8,15 @@ class Undestroy::Archive
|
|
8
8
|
self.source = args[:source]
|
9
9
|
self.config = args[:config]
|
10
10
|
self.transfer = args[:transfer]
|
11
|
+
self.transfer_options = args[:transfer_options]
|
11
12
|
end
|
12
13
|
|
13
14
|
def transfer
|
14
15
|
@transfer ||= self.config.internals[:transfer].new(
|
15
|
-
|
16
|
-
|
16
|
+
{
|
17
|
+
:klass => self.config.target_class,
|
18
|
+
:fields => archive_fields
|
19
|
+
}.merge(self.transfer_options || {})
|
17
20
|
)
|
18
21
|
end
|
19
22
|
|
@@ -17,7 +17,11 @@ class Undestroy::Binding::ActiveRecord
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def before_destroy(instance)
|
20
|
-
config.internals[:archive].new(
|
20
|
+
config.internals[:archive].new(
|
21
|
+
:config => config,
|
22
|
+
:transfer_options => { :target => target(instance) },
|
23
|
+
:source => instance
|
24
|
+
).run if active?
|
21
25
|
end
|
22
26
|
|
23
27
|
def prefix_table_name(name)
|
@@ -34,6 +38,12 @@ class Undestroy::Binding::ActiveRecord
|
|
34
38
|
|
35
39
|
protected
|
36
40
|
|
41
|
+
def target(source)
|
42
|
+
source_pk = config.source_class.primary_key
|
43
|
+
target_pk = config.target_class.primary_key
|
44
|
+
config.target_class.where(target_pk => source[source_pk]).first
|
45
|
+
end
|
46
|
+
|
37
47
|
def set_defaults
|
38
48
|
self.config.source_class = self.model
|
39
49
|
self.config.table_name ||= prefix_table_name(self.model.table_name) if self.model.respond_to?(:table_name)
|
data/lib/undestroy/transfer.rb
CHANGED
@@ -6,7 +6,7 @@ class Undestroy::Transfer
|
|
6
6
|
raise ArgumentError, ":klass option required" unless args[:klass]
|
7
7
|
args[:fields] ||= {}
|
8
8
|
|
9
|
-
self.target = args[:klass].new
|
9
|
+
self.target = args[:target] || args[:klass].new
|
10
10
|
|
11
11
|
# Set instance values directly to avoid AR's filtering of protected fields
|
12
12
|
args[:fields].each do |field, value|
|
data/lib/undestroy/version.rb
CHANGED
data/test/fixtures/ar.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
|
2
2
|
class Undestroy::Test::Fixtures::ARFixture
|
3
|
+
cattr_accessor :calls, :primary_key
|
3
4
|
attr_accessor :attributes
|
4
5
|
attr_reader :saved
|
5
6
|
|
7
|
+
self.calls = []
|
8
|
+
self.primary_key = :id
|
9
|
+
|
6
10
|
alias :saved? :saved
|
7
11
|
|
8
12
|
def initialize(attributes={})
|
@@ -41,5 +45,14 @@ class Undestroy::Test::Fixtures::ARFixture
|
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
48
|
+
def self.where(*attrs)
|
49
|
+
self.calls << [:where, attrs]
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.first(*attrs)
|
54
|
+
self.calls << [:first, attrs]
|
55
|
+
end
|
56
|
+
|
44
57
|
end
|
45
58
|
|
@@ -85,6 +85,20 @@ module Undestroy::Test::Integration::ActiveRecordTest
|
|
85
85
|
assert_equal 0, @model.all.size
|
86
86
|
end
|
87
87
|
|
88
|
+
should "only create archive record once" do
|
89
|
+
@model.undestroy
|
90
|
+
target_class = @model.undestroy_model_binding.config.target_class
|
91
|
+
|
92
|
+
@model.create(:name => "foo")
|
93
|
+
original1 = @model.first
|
94
|
+
original2 = @model.first
|
95
|
+
original1.destroy
|
96
|
+
assert_not_raises { original2.destroy }
|
97
|
+
|
98
|
+
assert target_class.first
|
99
|
+
assert_equal 1, target_class.count
|
100
|
+
end
|
101
|
+
|
88
102
|
should "restore an archived record removing the archive" do
|
89
103
|
@model.undestroy
|
90
104
|
@model.create(:name => "Fart")
|
@@ -128,6 +142,16 @@ module Undestroy::Test::Integration::ActiveRecordTest
|
|
128
142
|
assert_equal 2, @model.count
|
129
143
|
assert_equal 1, @model.archived.count
|
130
144
|
end
|
145
|
+
|
146
|
+
should "destroy without archive when destory! called" do
|
147
|
+
@model.undestroy
|
148
|
+
record = @model.create :name => "Foo"
|
149
|
+
assert_equal 1, @model.count
|
150
|
+
|
151
|
+
record.destroy!
|
152
|
+
assert_equal 0, @model.count
|
153
|
+
assert_equal 0, @model.archived.count
|
154
|
+
end
|
131
155
|
end
|
132
156
|
|
133
157
|
class BasicModelWithDifferentBase < Base
|
data/test/unit/archive_test.rb
CHANGED
@@ -21,7 +21,7 @@ module Undestroy::Archive::Test
|
|
21
21
|
subject { archive_instance }
|
22
22
|
desc 'basic instance'
|
23
23
|
|
24
|
-
should have_accessors :source, :config, :transfer
|
24
|
+
should have_accessors :source, :config, :transfer, :transfer_options
|
25
25
|
end
|
26
26
|
|
27
27
|
class InitMethod < Base
|
@@ -47,6 +47,11 @@ module Undestroy::Archive::Test
|
|
47
47
|
obj = archive_instance :transfer => "foo"
|
48
48
|
assert_equal "foo", obj.transfer
|
49
49
|
end
|
50
|
+
|
51
|
+
should "set optional :transfer_options to transfer_options attr" do
|
52
|
+
obj = archive_instance :transfer_options => { :foo => 'bar' }
|
53
|
+
assert_equal({ :foo => 'bar' }, obj.transfer_options)
|
54
|
+
end
|
50
55
|
end
|
51
56
|
|
52
57
|
class TransferMethod < BasicInstance
|
@@ -80,6 +85,24 @@ module Undestroy::Archive::Test
|
|
80
85
|
assert_equal @archive.source, val
|
81
86
|
assert_equal "FOO", target.attributes[:test]
|
82
87
|
end
|
88
|
+
|
89
|
+
should "merge transfer_options attr onto arguments" do
|
90
|
+
fixture = Class.new do
|
91
|
+
attr_accessor :args
|
92
|
+
def initialize(args)
|
93
|
+
@args = args
|
94
|
+
end
|
95
|
+
def [](key)
|
96
|
+
@args[key]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
@archive.config.internals[:transfer] = fixture
|
100
|
+
@archive.transfer_options = { :foo => 'bar', :fields => 123 }
|
101
|
+
|
102
|
+
assert_equal 'bar', @archive.transfer[:foo]
|
103
|
+
assert_equal 123, @archive.transfer[:fields]
|
104
|
+
assert_equal @archive.config.target_class, @archive.transfer[:klass]
|
105
|
+
end
|
83
106
|
end
|
84
107
|
|
85
108
|
class RunMethod < Base
|
@@ -307,9 +307,32 @@ module Undestroy::Binding::ActiveRecord::Test
|
|
307
307
|
subject.config.internals[:archive] = test_class
|
308
308
|
subject.before_destroy(ar_source)
|
309
309
|
|
310
|
-
assert_equal
|
310
|
+
assert_equal subject.config, test_class.data[:args][:config]
|
311
|
+
assert_equal ar_source, test_class.data[:args][:source]
|
311
312
|
assert_equal [[:run]], test_class.data[:calls]
|
312
313
|
end
|
314
|
+
|
315
|
+
should "pass :target value in :transfer_options if archive record exists and nil otherwise" do
|
316
|
+
archive_class = Undestroy::Test::Fixtures::Archive
|
317
|
+
ar_source = Undestroy::Test::Fixtures::ARFixture.new
|
318
|
+
ar_source[:id] = 1
|
319
|
+
subject.config.internals[:archive] = archive_class
|
320
|
+
subject.config.source_class = ar_source.class
|
321
|
+
subject.config.target_class = ar_source.class
|
322
|
+
subject.before_destroy(ar_source)
|
323
|
+
|
324
|
+
assert_equal [:where, [{ :id => 1 }]], ar_source.calls[0]
|
325
|
+
assert_equal [:first, []], ar_source.calls[1]
|
326
|
+
end
|
327
|
+
|
328
|
+
should "not run the archival if active is false" do
|
329
|
+
archive_class = Undestroy::Test::Fixtures::Archive
|
330
|
+
ar_source = Undestroy::Test::Fixtures::ARFixture.new
|
331
|
+
subject.config.internals[:archive] = archive_class
|
332
|
+
subject.active = false
|
333
|
+
subject.before_destroy(ar_source)
|
334
|
+
assert_equal 0, archive_class.data[:calls].size
|
335
|
+
end
|
313
336
|
end
|
314
337
|
|
315
338
|
class PrefixTableNameMethod < Base
|
data/test/unit/transfer_test.rb
CHANGED
@@ -23,7 +23,8 @@ module Undestroy::Transfer::Test
|
|
23
23
|
:name => "Foo",
|
24
24
|
:description => "Foo Description"
|
25
25
|
}
|
26
|
-
@
|
26
|
+
@init_args = { :klass => Undestroy::Test::Fixtures::ARFixture, :fields => @fields }
|
27
|
+
@transfer = subject.new @init_args
|
27
28
|
end
|
28
29
|
|
29
30
|
should "raise ArgumentError if no :klass key" do
|
@@ -34,6 +35,12 @@ module Undestroy::Transfer::Test
|
|
34
35
|
assert_not_raises { subject.new :klass => Undestroy::Test::Fixtures::ARFixture }
|
35
36
|
end
|
36
37
|
|
38
|
+
should "use :target arg if passed" do
|
39
|
+
target = @init_args[:klass].new
|
40
|
+
@transfer = subject.new @init_args.merge(:target => target)
|
41
|
+
assert_equal target.object_id, @transfer.target.object_id
|
42
|
+
end
|
43
|
+
|
37
44
|
should "create :klass instance with :fields" do
|
38
45
|
assert_instance_of Undestroy::Test::Fixtures::ARFixture, @transfer.target
|
39
46
|
assert_equal @fields[:id], @transfer.target[:id]
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: undestroy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Travis Petticrew
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-04-09 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
type: :runtime
|