Skip to content

Commit

Permalink
Specs mostly complete, ready to start testing frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
winton committed Dec 22, 2010
1 parent 68719e6 commit 7622898
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -4,4 +4,5 @@ coverage
pkg
spec/config/database.yml
spec/log
tmp
tmp
vendor
6 changes: 6 additions & 0 deletions 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
20 changes: 12 additions & 8 deletions lib/acts_as_archive.rb
Expand Up @@ -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'

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/acts_as_archive/gems.rb
Expand Up @@ -9,14 +9,15 @@ 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'
}

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 ],
Expand Down
85 changes: 81 additions & 4 deletions 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
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions 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

Expand Down
3 changes: 3 additions & 0 deletions 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

Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/has_many.rb
@@ -1,4 +1,6 @@
class HasMany < ActiveRecord::Base

belongs_to :record, :dependent => :delete

acts_as_archive
end
2 changes: 2 additions & 0 deletions spec/fixtures/has_many_through.rb
Expand Up @@ -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
2 changes: 2 additions & 0 deletions spec/fixtures/has_many_through_through.rb
Expand Up @@ -2,4 +2,6 @@ class HasManyThroughThrough < ActiveRecord::Base

belongs_to :record, :dependent => :delete
belongs_to :has_many_through, :dependent => :delete

acts_as_archive
end
2 changes: 2 additions & 0 deletions spec/fixtures/has_one.rb
@@ -1,4 +1,6 @@
class HasOne < ActiveRecord::Base

belongs_to :record, :dependent => :delete

acts_as_archive
end
2 changes: 2 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions spec/fixtures/has_one_through_through.rb
Expand Up @@ -2,4 +2,6 @@ class HasOneThroughThrough < ActiveRecord::Base

belongs_to :record, :dependent => :delete
has_one :has_one_through, :dependent => :delete

acts_as_archive
end
4 changes: 2 additions & 2 deletions 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
Expand Down
53 changes: 46 additions & 7 deletions spec/spec_helper.rb
Expand Up @@ -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 "<pre>"
puts object.pretty_inspect.gsub('<', '&lt;').gsub('>', '&gt;')
puts "</pre>"
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

0 comments on commit 7622898

Please sign in to comment.