undestroy 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. data/ARCH.md +13 -24
  2. data/README.md +72 -27
  3. data/lib/undestroy/binding.rb +1 -0
  4. data/lib/undestroy/binding/active_record.rb +53 -11
  5. data/lib/undestroy/binding/active_record/migration_statement.rb +118 -0
  6. data/lib/undestroy/binding/active_record/restorable.rb +28 -0
  7. data/lib/undestroy/config.rb +32 -8
  8. data/lib/undestroy/config/field.rb +20 -0
  9. data/lib/undestroy/restore.rb +44 -0
  10. data/lib/undestroy/version.rb +1 -1
  11. data/lib/undestroy/without_binding.rb +1 -0
  12. data/test/fixtures/ar.rb +8 -4
  13. data/test/fixtures/load_test/models1/test_model001.rb +2 -0
  14. data/test/fixtures/load_test/models1/test_model002.rb +2 -0
  15. data/test/fixtures/load_test/models1/test_module001.rb +2 -0
  16. data/test/fixtures/load_test/models1/test_module001/test_model003.rb +2 -0
  17. data/test/fixtures/load_test/models2/test_model001.rb +2 -0
  18. data/test/fixtures/load_test/models2/test_model002.rb +2 -0
  19. data/test/fixtures/load_test/models2/test_module001.rb +2 -0
  20. data/test/fixtures/load_test/models2/test_module001/test_model003.rb +2 -0
  21. data/test/helper.rb +12 -0
  22. data/test/helpers/model_loading.rb +20 -0
  23. data/test/integration/active_record_test.rb +50 -1
  24. data/test/irb.rb +2 -0
  25. data/test/unit/archive_test.rb +1 -1
  26. data/test/unit/binding/active_record/migration_statement_test.rb +306 -0
  27. data/test/unit/binding/active_record/restorable_test.rb +132 -0
  28. data/test/unit/binding/active_record_test.rb +187 -19
  29. data/test/unit/config/field_test.rb +86 -0
  30. data/test/unit/config_test.rb +114 -8
  31. data/test/unit/restore_test.rb +95 -0
  32. metadata +35 -3
@@ -53,12 +53,42 @@ class Undestroy::Config::Test
53
53
  end
54
54
  end
55
55
 
56
+ class CatalogClassMethod < Base
57
+ desc 'catalog class method'
58
+
59
+ should "exist" do
60
+ assert_respond_to :catalog, subject
61
+ end
62
+
63
+ should "return Array" do
64
+ assert_kind_of Array, subject.catalog
65
+ end
66
+
67
+ should "persist Array" do
68
+ assert_equal subject.catalog.object_id, subject.catalog.object_id
69
+ end
70
+ end
71
+
72
+ class ResetCatalogClassMethod < Base
73
+ desc 'reset_catalog class method'
74
+
75
+ should "exist" do
76
+ assert_respond_to :reset_catalog, subject
77
+ end
78
+
79
+ should "reset catalog cache" do
80
+ catalog = subject.catalog
81
+ subject.reset_catalog
82
+ assert_not_equal catalog.object_id, subject.catalog.object_id
83
+ end
84
+ end
85
+
56
86
  class BasicInstance < Base
57
87
  desc 'basic instance'
58
88
  subject { Undestroy::Config.new }
59
89
 
60
- should have_accessors :table_name, :abstract_class, :fields, :migrate
61
- should have_accessors :source_class, :target_class, :internals
90
+ should have_accessors :table_name, :abstract_class, :fields, :migrate, :indexes, :prefix
91
+ should have_accessors :source_class, :target_class, :internals, :model_paths
62
92
  end
63
93
 
64
94
  class InitMethod < Base
@@ -72,9 +102,9 @@ class Undestroy::Config::Test
72
102
  should "default fields to delayed deleted_at" do
73
103
  config = subject.new
74
104
  assert_equal [:deleted_at], config.fields.keys
75
- assert_instance_of Proc, config.fields[:deleted_at]
76
- assert_instance_of Time, config.fields[:deleted_at].call
77
- assert Time.now - config.fields[:deleted_at].call < 1
105
+ assert_instance_of Undestroy::Config::Field, config.fields[:deleted_at]
106
+ assert_instance_of Time, config.fields[:deleted_at].value(1)
107
+ assert Time.now - config.fields[:deleted_at].value(1)< 1
78
108
  end
79
109
 
80
110
  should "default internals to internal classes" do
@@ -83,6 +113,46 @@ class Undestroy::Config::Test
83
113
  assert_instance_of Hash, config.internals
84
114
  assert_equal Undestroy::Archive, config.internals[:archive]
85
115
  assert_equal Undestroy::Transfer, config.internals[:transfer]
116
+ assert_equal Undestroy::Restore, config.internals[:restore]
117
+ end
118
+
119
+ should "default indexes to false" do
120
+ config = subject.new
121
+ assert_equal false, config.indexes
122
+ end
123
+
124
+ should "default prefix to 'archive_'" do
125
+ config = subject.new
126
+ assert_equal 'archive_', config.prefix
127
+ end
128
+
129
+ should "default model_paths to [] if Rails is not defined" do
130
+ config = subject.new
131
+ assert_equal [], config.model_paths
132
+ end
133
+
134
+ should "dup passed in objects" do
135
+ fields = {}
136
+ config = subject.new :fields => fields
137
+ config.add_field :foo, :bar, :baz
138
+ assert fields.empty?
139
+ end
140
+
141
+ should "default model_paths to [Rails.root.join('app', 'models')] if Rails is defined" do
142
+ module ::Rails
143
+ @@base_root = "/foo/bar"
144
+ def self.root
145
+ self
146
+ end
147
+
148
+ def self.join(*args)
149
+ File.join(@@base_root, *args)
150
+ end
151
+ end
152
+
153
+ config = subject.new
154
+ assert_equal ["/foo/bar/app/models"], config.model_paths
155
+ Object.send(:remove_const, :Rails)
86
156
  end
87
157
 
88
158
  should "set config options using provided hash" do
@@ -90,13 +160,20 @@ class Undestroy::Config::Test
90
160
  :abstract_class => "test_archive",
91
161
  :target_class => "foo",
92
162
  :fields => {},
93
- :migrate => false
163
+ :migrate => false,
164
+ :indexes => true
94
165
 
95
166
  assert_equal "foo", config.table_name
96
167
  assert_equal "foo", config.target_class
97
168
  assert_equal "test_archive", config.abstract_class
98
169
  assert_equal Hash.new, config.fields
99
170
  assert_equal false, config.migrate
171
+ assert_equal true, config.indexes
172
+ end
173
+
174
+ should "track instances in catalog" do
175
+ config = subject.new
176
+ assert_includes config, subject.catalog
100
177
  end
101
178
 
102
179
  end
@@ -118,7 +195,7 @@ class Undestroy::Config::Test
118
195
  end
119
196
 
120
197
 
121
- class PrimitiveFields < Base
198
+ class PrimitiveFieldsMethod < Base
122
199
  desc 'primitive_fields method'
123
200
  subject { @config ||= Undestroy::Config.new }
124
201
 
@@ -137,10 +214,39 @@ class Undestroy::Config::Test
137
214
 
138
215
  should "pass argument in to proc" do
139
216
  val = {}
140
- subject.fields = { :test => proc { |arg| val[:arg] = arg } }
217
+ subject.fields = { :test => Undestroy::Config::Field.new(:test, :string) { |arg| val[:arg] = arg } }
141
218
  subject.primitive_fields("FOOO!")
142
219
  assert_equal "FOOO!", val[:arg]
143
220
  end
144
221
  end
145
222
 
223
+ class AddFieldMethod < Base
224
+ desc 'add_field method'
225
+ subject { @config ||= Undestroy::Config.new }
226
+
227
+ should "pass args to Config::Field constructor" do
228
+ field = subject.add_field :foo, :string, 'val'
229
+ assert_instance_of Undestroy::Config::Field, field
230
+ assert_equal :foo, field.name
231
+ assert_equal :string, field.type
232
+ assert_equal 'val', field.raw_value
233
+ end
234
+
235
+ should "pass block to Config::Field constructor" do
236
+ block = proc { |i| "foo" }
237
+ field = subject.add_field :foo, :string, &block
238
+ assert_equal block, field.raw_value
239
+ end
240
+
241
+ should "store new Field on fields hash" do
242
+ field = subject.add_field :foo, :string, 'val'
243
+ assert_equal field, subject.fields[:foo]
244
+ end
245
+
246
+ should "store field name as symbol" do
247
+ field = subject.add_field 'foo', :string, 'val'
248
+ assert_equal field, subject.fields[:foo]
249
+ end
250
+ end
251
+
146
252
  end
@@ -0,0 +1,95 @@
1
+ require 'assert'
2
+
3
+ module Undestroy::Restore::Test
4
+
5
+ class Base < Undestroy::Test::Base
6
+ desc 'Undestroy::Restore class'
7
+ subject { @subject_klass }
8
+
9
+ setup do
10
+ @source_class = Class.new(Undestroy::Test::Fixtures::ARFixture)
11
+ @target_class = Class.new(Undestroy::Test::Fixtures::ARFixture)
12
+ @subject_klass = Undestroy::Restore
13
+ @default_init = {
14
+ :target => @target_class.construct(:id => 1, 'name' => "Foo", :deleted_at => Time.now),
15
+ :config => Undestroy::Config.new(
16
+ :source_class => @source_class,
17
+ :target_class => @target_class
18
+ )
19
+ }
20
+ end
21
+ end
22
+
23
+ class BasicInstance < Base
24
+ desc 'basic instance'
25
+ subject { @restore ||= @subject_klass.new @default_init }
26
+
27
+ should have_accessors :target, :config, :transfer
28
+ end
29
+
30
+ class InitMethod < Base
31
+ desc 'init method'
32
+
33
+ should "accept hash of arguments and set them to attributes" do
34
+ restore = subject.new :target => 1, :config => 2, :transfer => 3
35
+ assert_equal 1, restore.target
36
+ assert_equal 2, restore.config
37
+ assert_equal 3, restore.transfer
38
+ end
39
+
40
+ should "require :target and :config keys" do
41
+ assert_raises(ArgumentError) { subject.new :transfer => :foo }
42
+ assert_raises(ArgumentError) { subject.new :target => :foo }
43
+ assert_raises(ArgumentError) { subject.new :config => :foo }
44
+ end
45
+ end
46
+
47
+ class TransferMethod < BasicInstance
48
+ desc 'transfer method'
49
+
50
+ should "return instance of config.internals[:transfer]" do
51
+ assert_instance_of @default_init[:config].internals[:transfer], subject.transfer
52
+ end
53
+
54
+ should ":klass should be config.source_class" do
55
+ assert_instance_of @default_init[:config].source_class, subject.transfer.target
56
+ end
57
+
58
+ should ":fields should be target.attributes - config.fields.keys" do
59
+ subject.config.add_field 'deleted_by_id', :string, 'foo'
60
+ @default_init[:target]['deleted_by_id'] = "Foo"
61
+ assert_equal({ 'id' => 1, 'name' => "Foo" }, subject.transfer.target.attributes)
62
+ end
63
+
64
+ should "cache transfer object" do
65
+ assert_equal subject.transfer.object_id, subject.transfer.object_id
66
+ end
67
+
68
+ end
69
+
70
+ class RunMethod < BasicInstance
71
+ desc 'run method'
72
+
73
+ should "call run on the transfer" do
74
+ foo = Class.new do
75
+ @@called = false
76
+ def initialize(options={})
77
+ end
78
+
79
+ def run
80
+ @@called = true
81
+ end
82
+
83
+ def self.called
84
+ @@called
85
+ end
86
+ end
87
+
88
+ subject.transfer = foo.new
89
+ subject.run
90
+ assert foo.called, "Foo was not called"
91
+ end
92
+ end
93
+
94
+ end
95
+
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 0.0.2
10
+ version: 0.1.0
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-03-15 00:00:00 Z
18
+ date: 2012-03-26 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :runtime
@@ -82,7 +82,11 @@ files:
82
82
  - lib/undestroy/archive.rb
83
83
  - lib/undestroy/binding.rb
84
84
  - lib/undestroy/binding/active_record.rb
85
+ - lib/undestroy/binding/active_record/migration_statement.rb
86
+ - lib/undestroy/binding/active_record/restorable.rb
85
87
  - lib/undestroy/config.rb
88
+ - lib/undestroy/config/field.rb
89
+ - lib/undestroy/restore.rb
86
90
  - lib/undestroy/transfer.rb
87
91
  - lib/undestroy/version.rb
88
92
  - lib/undestroy/with_binding.rb
@@ -90,11 +94,25 @@ files:
90
94
  - test/fixtures/active_record_models.rb
91
95
  - test/fixtures/ar.rb
92
96
  - test/fixtures/archive.rb
97
+ - test/fixtures/load_test/models1/test_model001.rb
98
+ - test/fixtures/load_test/models1/test_model002.rb
99
+ - test/fixtures/load_test/models1/test_module001.rb
100
+ - test/fixtures/load_test/models1/test_module001/test_model003.rb
101
+ - test/fixtures/load_test/models2/test_model001.rb
102
+ - test/fixtures/load_test/models2/test_model002.rb
103
+ - test/fixtures/load_test/models2/test_module001.rb
104
+ - test/fixtures/load_test/models2/test_module001/test_model003.rb
93
105
  - test/helper.rb
106
+ - test/helpers/model_loading.rb
94
107
  - test/integration/active_record_test.rb
108
+ - test/irb.rb
95
109
  - test/unit/archive_test.rb
110
+ - test/unit/binding/active_record/migration_statement_test.rb
111
+ - test/unit/binding/active_record/restorable_test.rb
96
112
  - test/unit/binding/active_record_test.rb
113
+ - test/unit/config/field_test.rb
97
114
  - test/unit/config_test.rb
115
+ - test/unit/restore_test.rb
98
116
  - test/unit/transfer_test.rb
99
117
  - tmp/.gitkeep
100
118
  - undestroy.gemspec
@@ -135,9 +153,23 @@ test_files:
135
153
  - test/fixtures/active_record_models.rb
136
154
  - test/fixtures/ar.rb
137
155
  - test/fixtures/archive.rb
156
+ - test/fixtures/load_test/models1/test_model001.rb
157
+ - test/fixtures/load_test/models1/test_model002.rb
158
+ - test/fixtures/load_test/models1/test_module001.rb
159
+ - test/fixtures/load_test/models1/test_module001/test_model003.rb
160
+ - test/fixtures/load_test/models2/test_model001.rb
161
+ - test/fixtures/load_test/models2/test_model002.rb
162
+ - test/fixtures/load_test/models2/test_module001.rb
163
+ - test/fixtures/load_test/models2/test_module001/test_model003.rb
138
164
  - test/helper.rb
165
+ - test/helpers/model_loading.rb
139
166
  - test/integration/active_record_test.rb
167
+ - test/irb.rb
140
168
  - test/unit/archive_test.rb
169
+ - test/unit/binding/active_record/migration_statement_test.rb
170
+ - test/unit/binding/active_record/restorable_test.rb
141
171
  - test/unit/binding/active_record_test.rb
172
+ - test/unit/config/field_test.rb
142
173
  - test/unit/config_test.rb
174
+ - test/unit/restore_test.rb
143
175
  - test/unit/transfer_test.rb