From 7622898b8c255500ecb1be1ae3514b16f809dc16 Mon Sep 17 00:00:00 2001 From: winton Date: Tue, 21 Dec 2010 22:51:10 -0800 Subject: [PATCH] Specs mostly complete, ready to start testing frameworks --- .gitignore | 3 +- config/externals.yml | 6 ++ lib/acts_as_archive.rb | 20 +++-- lib/acts_as_archive/gems.rb | 3 +- spec/acts_as_archive_spec.rb | 85 ++++++++++++++++++- ...{002_belongs_tos.rb => 001_belongs_tos.rb} | 0 .../{001_records.rb => 002_records.rb} | 0 .../migrate/005_has_many_through_throughs.rb | 3 + .../migrate/007_has_one_through_throughs.rb | 3 + spec/fixtures/has_many.rb | 2 + spec/fixtures/has_many_through.rb | 2 + spec/fixtures/has_many_through_through.rb | 2 + spec/fixtures/has_one.rb | 2 + spec/fixtures/has_one_through.rb | 2 + spec/fixtures/has_one_through_through.rb | 2 + spec/fixtures/record.rb | 4 +- spec/spec_helper.rb | 53 ++++++++++-- 17 files changed, 169 insertions(+), 23 deletions(-) create mode 100644 config/externals.yml rename spec/db/migrate/{002_belongs_tos.rb => 001_belongs_tos.rb} (100%) rename spec/db/migrate/{001_records.rb => 002_records.rb} (100%) diff --git a/.gitignore b/.gitignore index f6643bd..52caa34 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ coverage pkg spec/config/database.yml spec/log -tmp \ No newline at end of file +tmp +vendor \ No newline at end of file diff --git a/config/externals.yml b/config/externals.yml new file mode 100644 index 0000000..abf3f85 --- /dev/null +++ b/config/externals.yml @@ -0,0 +1,6 @@ +also_migrate: + repo: git://github.com/winton/also_migrate.git + path: vendor +mover: + repo: git://github.com/winton/mover.git + path: vendor \ No newline at end of file diff --git a/lib/acts_as_archive.rb b/lib/acts_as_archive.rb index 82d8ab8..30e62c2 100644 --- a/lib/acts_as_archive.rb +++ b/lib/acts_as_archive.rb @@ -2,6 +2,9 @@ ActsAsArchive::Gems.require(:lib) +$:.unshift File.expand_path(File.dirname(__FILE__) + '/../vendor/also_migrate/lib') +$:.unshift File.expand_path(File.dirname(__FILE__) + '/../vendor/mover/lib') + require 'also_migrate' require 'mover' @@ -47,6 +50,9 @@ module ActMethods def acts_as_archive(*args) return unless ActsAsArchive.find(self).empty? + ActsAsArchive.configuration ||= [] + ActsAsArchive.configuration << (config = { :from => self }) + options = args.last.is_a?(::Hash) ? args.pop : {} options[:copy] = true @@ -74,9 +80,11 @@ class Archive < ActiveRecord::Base acts_as_archive(#{self}, :archive => true) EVAL self.reflect_on_all_associations.each do |association| - puts association.klass.inspect if !ActsAsArchive.find(association.klass).empty? && association.options[:dependent] - klass.send association.macro, association.name, association.options + 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 end end unless options[:migrate] == false @@ -85,12 +93,8 @@ class Archive < ActiveRecord::Base end end - ActsAsArchive.configuration ||= [] - ActsAsArchive.configuration << { - :from => self, - :to => args, - :options => options - } + config[:to] = args + config[:options] = options end end end diff --git a/lib/acts_as_archive/gems.rb b/lib/acts_as_archive/gems.rb index ab8d024..a3d26b1 100644 --- a/lib/acts_as_archive/gems.rb +++ b/lib/acts_as_archive/gems.rb @@ -9,6 +9,7 @@ class Gems :activesupport => '=3.0.3', :active_wrapper => '=0.3.4', :also_migrate => '0.2.1', + :externals => '1.0.2', :mover => '0.3.3', :rake => '=0.8.7', :rspec => '=1.3.1' @@ -16,7 +17,7 @@ class Gems TYPES = { :gemspec => [ :also_migrate, :mover ], - :gemspec_dev => [ :active_wrapper, :rspec ], + :gemspec_dev => [ :active_wrapper, :externals, :rspec ], :lib => [ :also_migrate, :mover ], :rake => [ :rake, :rspec ], :spec => [ :activesupport, :active_wrapper, :rspec ], diff --git a/spec/acts_as_archive_spec.rb b/spec/acts_as_archive_spec.rb index 9ff1993..9c220d7 100644 --- a/spec/acts_as_archive_spec.rb +++ b/spec/acts_as_archive_spec.rb @@ -1,9 +1,86 @@ require 'spec_helper' describe ActsAsArchive do - it "should" do + + before(:each) do [ 8, 0, 8 ].each { |v| $db.migrate(v) } - a = Record.create - a.destroy + + @record = Record.create :belongs_to_id => BelongsTo.create.id + + HasOne.create :record_id => @record.id + + HasMany.create :record_id => @record.id + HasMany.create :record_id => @record.id + + Record.first.has_many_throughs.create + Record.first.has_many_throughs.create + + Record.first.create_has_one_through_through.create_has_one_through + + @lengths = { + :record => 1, + :belongs_to => 1, + :has_one => 1, + :has_many => 2, + :has_many_through => 2, + :has_many_through_through => 2, + :has_one_through => 1, + :has_one_through_through => 1 + } + + @zero_lengths = @lengths.inject({}) do |hash, (key, value)| + hash[key] = 0 + hash + end end -end + + it "should create records" do + original, archive = all_records + verify_lengths original, @lengths + verify_attributes original + end + + %w(delete delete_all destroy destroy_all).each do |type| + describe type do + it "should move records to archive tables" do + case type + when 'delete', 'destroy' + @record.send type + when 'delete_all', 'destroy_all' + Record.send type + end + + original, archive = all_records + + case type + when 'delete', 'delete_all' + archive[:record].length.should == 1 + original[:record].length.should == 0 + + verify_lengths archive, @zero_lengths, :exclude => [ :record ] + verify_lengths original, @lengths, :exclude => [ :record ] + + verify_attributes archive, :only => [ :record ] + + when 'destroy', 'destroy_all' + verify_lengths archive, @lengths + verify_lengths original, @zero_lengths + + verify_attributes archive + end + end + + it "should move records back to original tables" do + @record.destroy + Record::Archive.first.destroy + + original, archive = all_records + + verify_lengths original, @lengths + verify_lengths archive, @zero_lengths + + verify_attributes original + end + end + end +end \ No newline at end of file diff --git a/spec/db/migrate/002_belongs_tos.rb b/spec/db/migrate/001_belongs_tos.rb similarity index 100% rename from spec/db/migrate/002_belongs_tos.rb rename to spec/db/migrate/001_belongs_tos.rb diff --git a/spec/db/migrate/001_records.rb b/spec/db/migrate/002_records.rb similarity index 100% rename from spec/db/migrate/001_records.rb rename to spec/db/migrate/002_records.rb diff --git a/spec/db/migrate/005_has_many_through_throughs.rb b/spec/db/migrate/005_has_many_through_throughs.rb index c471821..853e8a3 100644 --- a/spec/db/migrate/005_has_many_through_throughs.rb +++ b/spec/db/migrate/005_has_many_through_throughs.rb @@ -1,8 +1,11 @@ class HasManyThroughThroughs < ActiveRecord::Migration def self.up create_table :has_many_through_throughs do |t| + t.string :string, :default => 'string' + t.integer :integer, :default => '1' t.integer :has_many_through_id t.integer :record_id + t.timestamps end end diff --git a/spec/db/migrate/007_has_one_through_throughs.rb b/spec/db/migrate/007_has_one_through_throughs.rb index 764f193..ed07571 100644 --- a/spec/db/migrate/007_has_one_through_throughs.rb +++ b/spec/db/migrate/007_has_one_through_throughs.rb @@ -1,7 +1,10 @@ class HasOneThroughThroughs < ActiveRecord::Migration def self.up create_table :has_one_through_throughs do |t| + t.string :string, :default => 'string' + t.integer :integer, :default => '1' t.integer :record_id + t.timestamps end end diff --git a/spec/fixtures/has_many.rb b/spec/fixtures/has_many.rb index e997773..56a9825 100644 --- a/spec/fixtures/has_many.rb +++ b/spec/fixtures/has_many.rb @@ -1,4 +1,6 @@ class HasMany < ActiveRecord::Base belongs_to :record, :dependent => :delete + + acts_as_archive end \ No newline at end of file diff --git a/spec/fixtures/has_many_through.rb b/spec/fixtures/has_many_through.rb index 1ea69b0..aac3542 100644 --- a/spec/fixtures/has_many_through.rb +++ b/spec/fixtures/has_many_through.rb @@ -2,4 +2,6 @@ class HasManyThrough < ActiveRecord::Base has_many :has_many_through_throughs, :dependent => :delete_all has_many :records, :dependent => :delete_all, :through => :has_many_through_throughs + + acts_as_archive end \ No newline at end of file diff --git a/spec/fixtures/has_many_through_through.rb b/spec/fixtures/has_many_through_through.rb index 4f35a29..e20fe70 100644 --- a/spec/fixtures/has_many_through_through.rb +++ b/spec/fixtures/has_many_through_through.rb @@ -2,4 +2,6 @@ class HasManyThroughThrough < ActiveRecord::Base belongs_to :record, :dependent => :delete belongs_to :has_many_through, :dependent => :delete + + acts_as_archive end \ No newline at end of file diff --git a/spec/fixtures/has_one.rb b/spec/fixtures/has_one.rb index 8f0a08f..b5bc493 100644 --- a/spec/fixtures/has_one.rb +++ b/spec/fixtures/has_one.rb @@ -1,4 +1,6 @@ class HasOne < ActiveRecord::Base belongs_to :record, :dependent => :delete + + acts_as_archive end \ No newline at end of file diff --git a/spec/fixtures/has_one_through.rb b/spec/fixtures/has_one_through.rb index 1a507b7..6198009 100644 --- a/spec/fixtures/has_one_through.rb +++ b/spec/fixtures/has_one_through.rb @@ -1,4 +1,6 @@ class HasOneThrough < ActiveRecord::Base belongs_to :has_one_through_through, :dependent => :delete + + acts_as_archive end \ No newline at end of file diff --git a/spec/fixtures/has_one_through_through.rb b/spec/fixtures/has_one_through_through.rb index fc81428..0ffa1bc 100644 --- a/spec/fixtures/has_one_through_through.rb +++ b/spec/fixtures/has_one_through_through.rb @@ -2,4 +2,6 @@ class HasOneThroughThrough < ActiveRecord::Base belongs_to :record, :dependent => :delete has_one :has_one_through, :dependent => :delete + + acts_as_archive end \ No newline at end of file diff --git a/spec/fixtures/record.rb b/spec/fixtures/record.rb index eab58ca..fbac076 100644 --- a/spec/fixtures/record.rb +++ b/spec/fixtures/record.rb @@ -1,10 +1,10 @@ class Record < ActiveRecord::Base - belongs_to :belongs_to, :dependent => :destroy + belongs_to :belongs_to, :dependent => :delete has_one :has_one, :dependent => :destroy - has_many :has_manies, :dependent => :destroy + has_many :has_manies, :dependent => :delete_all has_many :has_many_through_throughs, :dependent => :destroy has_many :has_many_throughs, :dependent => :destroy, :through => :has_many_through_throughs diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bf53732..255b27c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,14 +19,53 @@ require path end -require 'pp' - Spec::Runner.configure do |config| end -# For use with rspec textmate bundle -def debug(object) - puts "
"
-  puts object.pretty_inspect.gsub('<', '<').gsub('>', '>')
-  puts "
" +def all_records + [ + { + :record => Record.all, + :belongs_to => BelongsTo.all, + :has_one => HasOne.all, + :has_many => HasMany.all, + :has_many_through => HasManyThrough.all, + :has_many_through_through => HasManyThroughThrough.all, + :has_one_through => HasOneThrough.all, + :has_one_through_through => HasOneThroughThrough.all + }, + { + :record => Record::Archive.all, + :belongs_to => BelongsTo::Archive.all, + :has_one => HasOne::Archive.all, + :has_many => HasMany::Archive.all, + :has_many_through => HasManyThrough::Archive.all, + :has_many_through_through => HasManyThroughThrough::Archive.all, + :has_one_through => HasOneThrough::Archive.all, + :has_one_through_through => HasOneThroughThrough::Archive.all + } + ] +end + +def verify_attributes(records, options={}) + options[:exclude] ||= [] + records.each do |key, value| + if !options[:exclude].include?(key) && (!options[:only] || options[:only].include?(key)) + value.each do |record| + record[:string].should == 'string' + record[:integer].should == 1 + record[:created_at].is_a?(::Time).should == true + record[:updated_at].is_a?(::Time).should == true + end + end + end +end + +def verify_lengths(records, lengths, options={}) + options[:exclude] ||= [] + records.each do |key, value| + if !options[:exclude].include?(key) && (!options[:only] || options[:only].include?(key)) + value.length.should == lengths[key] + end + end end \ No newline at end of file