Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added specs for schema, migrate_from_acts_as_paranoid, restore_all, a…

…nd bang (!) destroy methods
  • Loading branch information...
commit 678edf285111a0e99b4140a73da09b133349d9c1 1 parent 1b16ce8
@winton authored
View
20 lib/acts_as_archive.rb
@@ -101,10 +101,10 @@ class Archive < ActiveRecord::Base
EVAL
self.reflect_on_all_associations.each do |association|
if !ActsAsArchive.find(association.klass).empty? && association.options[:dependent]
- options = association.options.dup
- options[:class_name] = "::#{association.class_name}::Archive"
- options[:foreign_key] = association.primary_key_name
- klass.send association.macro, association.name, options
+ opts = association.options.dup
+ opts[:class_name] = "::#{association.class_name}::Archive"
+ opts[:foreign_key] = association.primary_key_name
+ klass.send association.macro, association.name, opts
end
end
unless options[:migrate] == false
@@ -128,22 +128,22 @@ def destroy_all!(*args)
def migrate_from_acts_as_paranoid
time = Benchmark.measure do
ActsAsArchive.find(self).each do |config|
+ config = config.dup
+ config[:options][:copy] = false
ActsAsArchive.move(
config,
- "#{config[:options][:magic]} IS NOT NULL",
+ "`#{config[:options][:magic]}` IS NOT NULL",
:migrate => true
)
end
end
- $stdout.puts "-- #{self.class}.migrate_from_acts_as_paranoid"
+ $stdout.puts "-- #{self}.migrate_from_acts_as_paranoid"
$stdout.puts " -> #{"%.4fs" % time.real}"
end
def restore_all(*args)
- ActsAsArchive.deprecate "#{self}.restore_all is deprecated, please use #{self}::Archive.delete_all."
- if defined?(self::Archive)
- self::Archive.delete_all *args
- end
+ ActsAsArchive.deprecate "#{self}.restore_all is deprecated, please use #{self}.delete_all."
+ self.delete_all *args
end
end
View
2  spec/Rakefile
@@ -4,6 +4,6 @@ ActsAsArchive::Gems.activate :active_wrapper
require 'active_wrapper/tasks'
ActiveWrapper::Tasks.new(
- :base => File.dirname(__FILE__),
+ :base => File.dirname(__FILE__) + "/fixtures",
:env => ENV['ENV']
)
View
48 spec/acts_as_archive_spec.rb
@@ -6,10 +6,26 @@
before(:each) do
before_each
end
+
+ it "should have valid schema" do
+ should_have_valid_schema
+ end
it "should create records" do
should_create_records
end
+
+ describe :migrate_from_acts_as_paranoid do
+ it "should migrate record and preserve deleted_at" do
+ should_migrate_record_and_preserve_deleted_at
+ end
+ end
+
+ describe :restore_all do
+ it "should emulate delete_all" do
+ should_emulate_delete_all
+ end
+ end
%w(delete delete_all destroy destroy_all).each do |type|
describe type do
@@ -21,6 +37,12 @@
should_move_records_back_to_original_tables(type)
end
end
+
+ describe "#{type}!" do
+ it "should delete records without archiving" do
+ should_delete_records_without_archiving(type)
+ end
+ end
end
end
end
@@ -43,10 +65,29 @@ def app
last_response.body.should == '1'
end
+ it "should have valid schema" do
+ get "/should_have_valid_schema_action"
+ last_response.body.should == '1'
+ end
+
it "should create records" do
get "/should_create_records_action"
last_response.body.should == '1'
end
+
+ describe :migrate_from_acts_as_paranoid do
+ it "should migrate record and preserve deleted_at" do
+ get "/should_migrate_record_and_preserve_deleted_at_action"
+ last_response.body.should == '1'
+ end
+ end
+
+ describe :restore_all do
+ it "should emulate delete_all" do
+ get "/should_emulate_delete_all_action"
+ last_response.body.should == '1'
+ end
+ end
%w(delete delete_all destroy destroy_all).each do |type|
describe type do
@@ -60,6 +101,13 @@ def app
last_response.body.should == '1'
end
end
+
+ describe "#{type}!" do
+ it "should delete records without archiving" do
+ get "/should_delete_records_without_archiving_action", :type => type
+ last_response.body.should == '1'
+ end
+ end
end
end
end
View
1  spec/fixtures/db/migrate/001_belongs_tos.rb
@@ -3,6 +3,7 @@ def self.up
create_table :belongs_tos do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
+ t.datetime :restored_at
t.timestamps
end
end
View
1  spec/fixtures/db/migrate/002_records.rb
@@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :belongs_to_id
+ t.datetime :restored_at
t.timestamps
end
end
View
1  spec/fixtures/db/migrate/003_has_ones.rb
@@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :record_id
+ t.datetime :restored_at
t.timestamps
end
end
View
1  spec/fixtures/db/migrate/004_has_manies.rb
@@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :record_id
+ t.datetime :restored_at
t.timestamps
end
end
View
1  spec/fixtures/db/migrate/005_has_many_through_throughs.rb
@@ -5,6 +5,7 @@ def self.up
t.integer :integer, :default => '1'
t.integer :has_many_through_id
t.integer :record_id
+ t.datetime :restored_at
t.timestamps
end
end
View
1  spec/fixtures/db/migrate/006_has_many_throughs.rb
@@ -3,6 +3,7 @@ def self.up
create_table :has_many_throughs do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
+ t.datetime :restored_at
t.timestamps
end
end
View
1  spec/fixtures/db/migrate/007_has_one_through_throughs.rb
@@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :record_id
+ t.datetime :restored_at
t.timestamps
end
end
View
1  spec/fixtures/db/migrate/008_has_one_throughs.rb
@@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :has_one_through_through_id
+ t.datetime :restored_at
t.timestamps
end
end
View
24 spec/fixtures/frameworks/rails2/application_controller.rb
@@ -14,12 +14,30 @@ def pulse
render :text => '1'
end
+ def should_have_valid_schema_action
+ before_each false, true
+ should_have_valid_schema
+ render :text => '1'
+ end
+
def should_create_records_action
before_each false, true
should_create_records
render :text => '1'
end
+ def should_migrate_record_and_preserve_deleted_at_action
+ before_each false, true
+ should_migrate_record_and_preserve_deleted_at
+ render :text => '1'
+ end
+
+ def should_emulate_delete_all_action
+ before_each false, true
+ should_emulate_delete_all
+ render :text => '1'
+ end
+
def should_move_records_back_to_original_tables_action
before_each false, true
should_move_records_back_to_original_tables(params[:type])
@@ -31,4 +49,10 @@ def should_move_records_to_archive_tables_action
should_move_records_to_archive_tables(params[:type])
render :text => '1'
end
+
+ def should_delete_records_without_archiving_action
+ before_each false, true
+ should_delete_records_without_archiving(params[:type])
+ render :text => '1'
+ end
end
View
24 spec/fixtures/frameworks/rails3/application_controller.rb
@@ -7,12 +7,30 @@ def pulse
render :text => '1'
end
+ def should_have_valid_schema_action
+ before_each false, true
+ should_have_valid_schema
+ render :text => '1'
+ end
+
def should_create_records_action
before_each false, true
should_create_records
render :text => '1'
end
+ def should_migrate_record_and_preserve_deleted_at_action
+ before_each false, true
+ should_migrate_record_and_preserve_deleted_at
+ render :text => '1'
+ end
+
+ def should_emulate_delete_all_action
+ before_each false, true
+ should_emulate_delete_all
+ render :text => '1'
+ end
+
def should_move_records_back_to_original_tables_action
before_each false, true
should_move_records_back_to_original_tables(params[:type])
@@ -24,4 +42,10 @@ def should_move_records_to_archive_tables_action
should_move_records_to_archive_tables(params[:type])
render :text => '1'
end
+
+ def should_delete_records_without_archiving_action
+ before_each false, true
+ should_delete_records_without_archiving(params[:type])
+ render :text => '1'
+ end
end
View
24 spec/fixtures/frameworks/sinatra/application.rb
@@ -14,12 +14,30 @@ class Application < Sinatra::Base
'1'
end
+ get "/should_have_valid_schema_action" do
+ before_each false, true
+ should_have_valid_schema
+ '1'
+ end
+
get "/should_create_records_action" do
before_each false, true
should_create_records
'1'
end
+ get "/should_migrate_record_and_preserve_deleted_at_action" do
+ before_each false, true
+ should_migrate_record_and_preserve_deleted_at
+ '1'
+ end
+
+ get "/should_emulate_delete_all_action" do
+ before_each false, true
+ should_emulate_delete_all
+ '1'
+ end
+
get "/should_move_records_back_to_original_tables_action" do
before_each false, true
should_move_records_back_to_original_tables(params[:type])
@@ -31,4 +49,10 @@ class Application < Sinatra::Base
should_move_records_to_archive_tables(params[:type])
'1'
end
+
+ get "/should_delete_records_without_archiving_action" do
+ before_each false, true
+ should_delete_records_without_archiving(params[:type])
+ '1'
+ end
end
View
53 spec/fixtures/helpers/spec_helper.rb
@@ -28,6 +28,7 @@ def all_records
def before_each(migrate=true, setup=true)
if migrate
[ 8, 0, 8 ].each { |v| $db.migrate(v) }
+ Record.reset_column_information
end
if setup
@record, @lengths, @zero_lengths = setup_records
@@ -72,6 +73,58 @@ def should_create_records
verify_attributes original
end
+ def should_delete_records_without_archiving(type)
+ type = "#{type}!"
+ case type
+ when 'delete!', 'destroy!'
+ @record.send type
+ when 'delete_all!', 'destroy_all!'
+ Record.send type
+ end
+
+ Record.count.should == 0
+ Record::Archive.count.should == 0
+ end
+
+ def should_emulate_delete_all
+ id = @record.id
+ @record.destroy
+ Record.count.should == 0
+ Record::Archive.count.should == 1
+ Record::Archive.restore_all [ "id = ?", id ]
+ Record.count.should == 1
+ end
+
+ def should_have_valid_schema
+ [
+ BelongsTo, Record, HasOne, HasMany, HasManyThroughThrough,
+ HasManyThrough, HasOneThroughThrough, HasOneThrough
+ ].each do |klass|
+ cols = [ 'string', 'integer', 'restored_at', 'created_at', 'updated_at' ]
+ archive_cols = [ 'string', 'integer', 'created_at', 'updated_at', 'deleted_at' ]
+ (klass.column_names & cols).should == cols
+ (klass::Archive.column_names & archive_cols).should == archive_cols
+ end
+ end
+
+ def should_migrate_record_and_preserve_deleted_at
+ Record.connection.execute <<-SQL
+ ALTER TABLE records ADD deleted_at DATETIME
+ SQL
+ Record.reset_column_information
+
+ time = Time.now.utc - 24 * 60 * 60
+ first = Record.first
+ first.update_attribute :deleted_at, time
+ Record.create
+
+ Record.migrate_from_acts_as_paranoid
+ Record::Archive.first.deleted_at.to_s.should == time.to_s
+ Record::Archive.first.id.should == first.id
+ Record::Archive.count.should == 1
+ Record.count.should == 1
+ end
+
def should_move_records_back_to_original_tables(type)
case type
when 'delete', 'destroy'
View
2  spec/spec_helper.rb
@@ -7,6 +7,7 @@
require 'framework_fixture'
if FrameworkFixture.framework == 'rails'
+ ENV['RAILS_ENV'] = 'test'
FrameworkFixture.generate File.dirname(__FILE__) + '/fixtures'
end
@@ -64,6 +65,7 @@
def before_each(migrate=true, setup=true)
if migrate
[ 8, 0, 8 ].each { |v| $db.migrate(v) }
+ Record.reset_column_information
end
if setup
@record, @lengths, @zero_lengths = setup_records
Please sign in to comment.
Something went wrong with that request. Please try again.