acts_as_archive 0.3.1 → 0.3.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.
Files changed (50) hide show
  1. data/.gitignore +9 -0
  2. data/LICENSE +18 -0
  3. data/README.md +112 -0
  4. data/Rakefile +90 -0
  5. data/acts_as_archive.gemspec +32 -0
  6. data/bin/acts_as_archive +2 -0
  7. data/config/externals.yml +12 -0
  8. data/config/gemsets.yml +10 -0
  9. data/config/gemspec.yml +17 -0
  10. data/init.rb +1 -0
  11. data/lib/acts_as_archive.rb +192 -0
  12. data/lib/acts_as_archive/gems.rb +154 -0
  13. data/rails/init.rb +1 -0
  14. data/spec/Rakefile +9 -0
  15. data/spec/acts_as_archive/gems_spec.rb +249 -0
  16. data/spec/acts_as_archive_spec.rb +113 -0
  17. data/spec/fixtures/config/database.yml.example +6 -0
  18. data/spec/fixtures/db/migrate/001_belongs_tos.rb +14 -0
  19. data/spec/fixtures/db/migrate/002_records.rb +15 -0
  20. data/spec/fixtures/db/migrate/003_has_ones.rb +15 -0
  21. data/spec/fixtures/db/migrate/004_has_manies.rb +15 -0
  22. data/spec/fixtures/db/migrate/005_has_many_through_throughs.rb +16 -0
  23. data/spec/fixtures/db/migrate/006_has_many_throughs.rb +14 -0
  24. data/spec/fixtures/db/migrate/007_has_one_through_throughs.rb +15 -0
  25. data/spec/fixtures/db/migrate/008_has_one_throughs.rb +15 -0
  26. data/spec/fixtures/frameworks.yml +36 -0
  27. data/spec/fixtures/frameworks/rails2/application_controller.rb +58 -0
  28. data/spec/fixtures/frameworks/rails2/database.yml +12 -0
  29. data/spec/fixtures/frameworks/rails2/init.rb +1 -0
  30. data/spec/fixtures/frameworks/rails2/routes.rb +46 -0
  31. data/spec/fixtures/frameworks/rails3/Gemfile +34 -0
  32. data/spec/fixtures/frameworks/rails3/application_controller.rb +51 -0
  33. data/spec/fixtures/frameworks/rails3/database.yml +12 -0
  34. data/spec/fixtures/frameworks/rails3/routes.rb +60 -0
  35. data/spec/fixtures/frameworks/sinatra/application.rb +58 -0
  36. data/spec/fixtures/gemsets.yml +9 -0
  37. data/spec/fixtures/gemspec.yml +15 -0
  38. data/spec/fixtures/helpers/spec_helper.rb +210 -0
  39. data/spec/fixtures/models/belongs_to.rb +6 -0
  40. data/spec/fixtures/models/has_many.rb +6 -0
  41. data/spec/fixtures/models/has_many_through.rb +7 -0
  42. data/spec/fixtures/models/has_many_through_through.rb +7 -0
  43. data/spec/fixtures/models/has_one.rb +6 -0
  44. data/spec/fixtures/models/has_one_through.rb +6 -0
  45. data/spec/fixtures/models/has_one_through_through.rb +7 -0
  46. data/spec/fixtures/models/record.rb +16 -0
  47. data/spec/run +27 -0
  48. data/spec/spec.opts +1 -0
  49. data/spec/spec_helper.rb +73 -0
  50. metadata +96 -13
@@ -0,0 +1,58 @@
1
+ require 'sinatra/base'
2
+ require "#{$root}/lib/acts_as_archive"
3
+
4
+ class Application < Sinatra::Base
5
+
6
+ set :app_file, __FILE__
7
+ set :dump_errors, true
8
+ set :raise_errors, true
9
+ set :show_exceptions, false
10
+
11
+ include SpecHelper
12
+
13
+ get '/pulse' do
14
+ '1'
15
+ end
16
+
17
+ get "/should_have_valid_schema_action" do
18
+ before_each false, true
19
+ should_have_valid_schema
20
+ '1'
21
+ end
22
+
23
+ get "/should_create_records_action" do
24
+ before_each false, true
25
+ should_create_records
26
+ '1'
27
+ end
28
+
29
+ get "/should_migrate_record_and_preserve_deleted_at_action" do
30
+ before_each false, true
31
+ should_migrate_record_and_preserve_deleted_at
32
+ '1'
33
+ end
34
+
35
+ get "/should_emulate_delete_all_action" do
36
+ before_each false, true
37
+ should_emulate_delete_all
38
+ '1'
39
+ end
40
+
41
+ get "/should_move_records_back_to_original_tables_action" do
42
+ before_each false, true
43
+ should_move_records_back_to_original_tables(params[:type])
44
+ '1'
45
+ end
46
+
47
+ get "/should_move_records_to_archive_tables_action" do
48
+ before_each false, true
49
+ should_move_records_to_archive_tables(params[:type])
50
+ '1'
51
+ end
52
+
53
+ get "/should_delete_records_without_archiving_action" do
54
+ before_each false, true
55
+ should_delete_records_without_archiving(params[:type])
56
+ '1'
57
+ end
58
+ end
@@ -0,0 +1,9 @@
1
+ name:
2
+ rake: =0.8.7
3
+ default:
4
+ mysql: =2.8.1
5
+ rspec: =1.3.1
6
+ rspec2:
7
+ mysql2: =0.2.6
8
+ rspec: =2.3.0
9
+ solo: null
@@ -0,0 +1,15 @@
1
+ name: name
2
+ version: 0.1.0
3
+ authors:
4
+ - Author
5
+ email: email@email.com
6
+ homepage: http://github.com/author/name
7
+ summary: Summary
8
+ description: Description
9
+ dependencies:
10
+ - rake
11
+ - default:
12
+ - mysql
13
+ - rspec2:
14
+ - mysql2
15
+ development_dependencies: null
@@ -0,0 +1,210 @@
1
+ module SpecHelper
2
+
3
+ def all_records
4
+ [
5
+ {
6
+ :record => Record.all,
7
+ :belongs_to => BelongsTo.all,
8
+ :has_one => HasOne.all,
9
+ :has_many => HasMany.all,
10
+ :has_many_through => HasManyThrough.all,
11
+ :has_many_through_through => HasManyThroughThrough.all,
12
+ :has_one_through => HasOneThrough.all,
13
+ :has_one_through_through => HasOneThroughThrough.all
14
+ },
15
+ {
16
+ :record => Record::Archive.all,
17
+ :belongs_to => BelongsTo::Archive.all,
18
+ :has_one => HasOne::Archive.all,
19
+ :has_many => HasMany::Archive.all,
20
+ :has_many_through => HasManyThrough::Archive.all,
21
+ :has_many_through_through => HasManyThroughThrough::Archive.all,
22
+ :has_one_through => HasOneThrough::Archive.all,
23
+ :has_one_through_through => HasOneThroughThrough::Archive.all
24
+ }
25
+ ]
26
+ end
27
+
28
+ def before_each(migrate=true, setup=true)
29
+ if migrate
30
+ [ 8, 0, 8 ].each { |v| $db.migrate(v) }
31
+ Record.reset_column_information
32
+ end
33
+ if setup
34
+ @record, @lengths, @zero_lengths = setup_records
35
+ end
36
+ end
37
+
38
+ def setup_records
39
+ record = Record.create :belongs_to_id => BelongsTo.create.id
40
+
41
+ HasOne.create :record_id => record.id
42
+
43
+ HasMany.create :record_id => record.id
44
+ HasMany.create :record_id => record.id
45
+
46
+ Record.first.has_many_throughs.create
47
+ Record.first.has_many_throughs.create
48
+
49
+ Record.first.create_has_one_through_through.create_has_one_through
50
+
51
+ lengths = {
52
+ :record => 1,
53
+ :belongs_to => 1,
54
+ :has_one => 1,
55
+ :has_many => 2,
56
+ :has_many_through => 2,
57
+ :has_many_through_through => 2,
58
+ :has_one_through => 1,
59
+ :has_one_through_through => 1
60
+ }
61
+
62
+ zero_lengths = lengths.inject({}) do |hash, (key, value)|
63
+ hash[key] = 0
64
+ hash
65
+ end
66
+
67
+ [ record, lengths, zero_lengths ]
68
+ end
69
+
70
+ def should_create_records
71
+ original, archive = all_records
72
+ verify_lengths original, @lengths
73
+ verify_attributes original
74
+ end
75
+
76
+ def should_delete_records_without_archiving(type)
77
+ type = "#{type}!"
78
+ case type
79
+ when 'delete!', 'destroy!'
80
+ @record.send type
81
+ when 'delete_all!', 'destroy_all!'
82
+ Record.send type
83
+ end
84
+
85
+ Record.count.should == 0
86
+ Record::Archive.count.should == 0
87
+ end
88
+
89
+ def should_emulate_delete_all
90
+ id = @record.id
91
+ @record.destroy
92
+ Record.count.should == 0
93
+ Record::Archive.count.should == 1
94
+ Record::Archive.restore_all [ "id = ?", id ]
95
+ Record.count.should == 1
96
+ end
97
+
98
+ def should_have_valid_schema
99
+ [
100
+ BelongsTo, Record, HasOne, HasMany, HasManyThroughThrough,
101
+ HasManyThrough, HasOneThroughThrough, HasOneThrough
102
+ ].each do |klass|
103
+ cols = [ 'string', 'integer', 'restored_at', 'created_at', 'updated_at' ]
104
+ archive_cols = [ 'string', 'integer', 'created_at', 'updated_at', 'deleted_at' ]
105
+ (klass.column_names & cols).should == cols
106
+ (klass::Archive.column_names & archive_cols).should == archive_cols
107
+ end
108
+ end
109
+
110
+ def should_migrate_record_and_preserve_deleted_at
111
+ Record.connection.execute <<-SQL
112
+ ALTER TABLE records ADD deleted_at DATETIME
113
+ SQL
114
+ Record.reset_column_information
115
+
116
+ time = Time.now.utc - 24 * 60 * 60
117
+ first = Record.first
118
+ first.update_attribute :deleted_at, time
119
+ Record.create
120
+
121
+ Record.migrate_from_acts_as_paranoid
122
+ Record::Archive.first.deleted_at.to_s.should == time.to_s
123
+ Record::Archive.first.id.should == first.id
124
+ Record::Archive.count.should == 1
125
+ Record.count.should == 1
126
+ end
127
+
128
+ def should_move_records_back_to_original_tables(type)
129
+ case type
130
+ when 'delete', 'destroy'
131
+ @record.send type
132
+ Record::Archive.first.send type
133
+ when 'delete_all', 'destroy_all'
134
+ Record.send type
135
+ Record::Archive.send type
136
+ end
137
+
138
+ original, archive = all_records
139
+
140
+ verify_lengths original, @lengths
141
+ verify_lengths archive, @zero_lengths
142
+
143
+ verify_attributes original
144
+
145
+ case type
146
+ when 'delete', 'delete_all' then
147
+ original[:record].first.restored_at.is_a?(::Time).should == true
148
+ when 'destroy', 'destroy_all' then
149
+ original.values.each do |records|
150
+ records.each do |record|
151
+ record.restored_at.is_a?(::Time).should == true
152
+ end
153
+ end
154
+ end
155
+ end
156
+
157
+ def should_move_records_to_archive_tables(type)
158
+ case type
159
+ when 'delete', 'destroy'
160
+ @record.send type
161
+ when 'delete_all', 'destroy_all'
162
+ Record.send type
163
+ end
164
+
165
+ original, archive = all_records
166
+
167
+ case type
168
+ when 'delete', 'delete_all'
169
+ archive[:record].length.should == 1
170
+ original[:record].length.should == 0
171
+
172
+ verify_lengths archive, @zero_lengths, :exclude => [ :record ]
173
+ verify_lengths original, @lengths, :exclude => [ :record ]
174
+
175
+ verify_attributes archive, :only => [ :record ]
176
+
177
+ when 'destroy', 'destroy_all'
178
+ verify_lengths archive, @lengths
179
+ verify_lengths original, @zero_lengths
180
+
181
+ verify_attributes archive
182
+ end
183
+ end
184
+
185
+ def verify_attributes(records, options={})
186
+ options[:exclude] ||= []
187
+ records.each do |key, value|
188
+ if !options[:exclude].include?(key) && (!options[:only] || options[:only].include?(key))
189
+ value.each do |record|
190
+ record[:string].should == 'string'
191
+ record[:integer].should == 1
192
+ record[:created_at].is_a?(::Time).should == true
193
+ record[:updated_at].is_a?(::Time).should == true
194
+ if record.respond_to?(:deleted_at)
195
+ record[:deleted_at].is_a?(::Time).should == true
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
201
+
202
+ def verify_lengths(records, lengths, options={})
203
+ options[:exclude] ||= []
204
+ records.each do |key, value|
205
+ if !options[:exclude].include?(key) && (!options[:only] || options[:only].include?(key))
206
+ value.length.should == lengths[key]
207
+ end
208
+ end
209
+ end
210
+ end
@@ -0,0 +1,6 @@
1
+ class BelongsTo < ActiveRecord::Base
2
+
3
+ has_many :records, :dependent => :delete_all
4
+
5
+ acts_as_archive
6
+ end
@@ -0,0 +1,6 @@
1
+ class HasMany < ActiveRecord::Base
2
+
3
+ belongs_to :record, :dependent => :delete
4
+
5
+ acts_as_archive
6
+ end
@@ -0,0 +1,7 @@
1
+ class HasManyThrough < ActiveRecord::Base
2
+
3
+ has_many :has_many_through_throughs, :dependent => :delete_all
4
+ has_many :records, :dependent => :delete_all, :through => :has_many_through_throughs
5
+
6
+ acts_as_archive
7
+ end
@@ -0,0 +1,7 @@
1
+ class HasManyThroughThrough < ActiveRecord::Base
2
+
3
+ belongs_to :record, :dependent => :delete
4
+ belongs_to :has_many_through, :dependent => :delete
5
+
6
+ acts_as_archive
7
+ end
@@ -0,0 +1,6 @@
1
+ class HasOne < ActiveRecord::Base
2
+
3
+ belongs_to :record, :dependent => :delete
4
+
5
+ acts_as_archive
6
+ end
@@ -0,0 +1,6 @@
1
+ class HasOneThrough < ActiveRecord::Base
2
+
3
+ belongs_to :has_one_through_through, :dependent => :delete
4
+
5
+ acts_as_archive
6
+ end
@@ -0,0 +1,7 @@
1
+ class HasOneThroughThrough < ActiveRecord::Base
2
+
3
+ belongs_to :record, :dependent => :delete
4
+ has_one :has_one_through, :dependent => :delete
5
+
6
+ acts_as_archive
7
+ end
@@ -0,0 +1,16 @@
1
+ class Record < ActiveRecord::Base
2
+
3
+ belongs_to :belongs_to, :dependent => :delete
4
+
5
+ has_one :has_one, :dependent => :destroy
6
+
7
+ has_many :has_manies, :dependent => :delete_all
8
+
9
+ has_many :has_many_through_throughs, :dependent => :destroy
10
+ has_many :has_many_throughs, :dependent => :destroy, :through => :has_many_through_throughs
11
+
12
+ has_one :has_one_through_through, :dependent => :destroy
13
+ has_one :has_one_through, :dependent => :destroy, :through => :has_one_through_through
14
+
15
+ acts_as_archive
16
+ end
data/spec/run ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ system "spec -f n -c spec"
4
+
5
+ puts "\n"
6
+
7
+ ENV['ACTIVERECORD'] = '2'
8
+ system "spec -f n -c spec/acts_as_archive_spec.rb"
9
+
10
+ ENV.delete 'ACTIVERECORD'
11
+
12
+ puts "\n"
13
+
14
+ ENV['RAILS'] = '2'
15
+ system "spec -f n -c spec/acts_as_archive_spec.rb"
16
+
17
+ puts "\n"
18
+
19
+ ENV['RAILS'] = '3'
20
+ system "spec -f n -c spec/acts_as_archive_spec.rb"
21
+
22
+ ENV.delete 'RAILS'
23
+
24
+ puts "\n"
25
+
26
+ ENV['SINATRA'] = '1'
27
+ system "spec -f n -c spec/acts_as_archive_spec.rb"
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,73 @@
1
+ require 'pp'
2
+
3
+ $root = File.expand_path('../../', __FILE__)
4
+ require "#{$root}/lib/acts_as_archive/gems"
5
+
6
+ ActsAsArchive::Gems.activate :framework_fixture
7
+ require 'framework_fixture'
8
+
9
+ if FrameworkFixture.framework == 'rails'
10
+ ENV['RAILS_ENV'] = 'test'
11
+ FrameworkFixture.generate File.dirname(__FILE__) + '/fixtures'
12
+ end
13
+
14
+ ActsAsArchive::Gems.activate %w(active_wrapper-solo rack-test rspec)
15
+ require 'active_wrapper/gems'
16
+
17
+ # Framework specs
18
+ if FrameworkFixture.framework
19
+ require 'rack/test'
20
+
21
+ if FrameworkFixture.rails == '<3'
22
+ ActiveWrapper::Gems.gemset = :ar2
23
+ elsif FrameworkFixture.sinatra
24
+ ActiveWrapper::Gems.activate %w(activesupport)
25
+ require 'active_support/dependencies'
26
+ ActiveSupport::Dependencies.autoload_paths << "#{$root}/spec/fixtures/builds/sinatra#{ENV['SINATRA']}/app/models"
27
+ ActiveSupport::Dependencies.autoload_paths << "#{$root}/spec/fixtures/builds/sinatra#{ENV['SINATRA']}/app/helpers"
28
+ end
29
+
30
+ require 'active_wrapper'
31
+
32
+ # Normal specs
33
+ else
34
+ if ENV['ACTIVERECORD'] == '2'
35
+ ActiveWrapper::Gems.gemset = :ar2
36
+ else
37
+ ActiveWrapper::Gems.activate %w(activesupport)
38
+ require 'active_support/dependencies'
39
+ end
40
+
41
+ require 'active_wrapper'
42
+
43
+ require "#{$root}/lib/acts_as_archive"
44
+
45
+ ActiveSupport::Dependencies.autoload_paths << "#{$root}/spec/fixtures/models"
46
+ ActiveSupport::Dependencies.autoload_paths << "#{$root}/spec/fixtures/helpers"
47
+
48
+ include SpecHelper
49
+ end
50
+
51
+ $db, $log, $mail = ActiveWrapper.setup(
52
+ :adapter => ActiveWrapper::Gems.gemset == :ar2 ? 'mysql' : 'mysql2',
53
+ :base => "#{$root}/spec/fixtures",
54
+ :env => 'test'
55
+ )
56
+ $db.establish_connection
57
+
58
+ if FrameworkFixture.framework == 'sinatra'
59
+ FrameworkFixture.generate File.dirname(__FILE__) + '/fixtures'
60
+ end
61
+
62
+ Spec::Runner.configure do |config|
63
+ end
64
+
65
+ def before_each(migrate=true, setup=true)
66
+ if migrate
67
+ [ 8, 0, 8 ].each { |v| $db.migrate(v) }
68
+ Record.reset_column_information
69
+ end
70
+ if setup
71
+ @record, @lengths, @zero_lengths = setup_records
72
+ end
73
+ end