Skip to content
This repository
Browse code

Specs mostly complete, ready to start testing frameworks

  • Loading branch information...
commit 7622898b8c255500ecb1be1ae3514b16f809dc16 1 parent 68719e6
Winton Welsh authored
3  .gitignore
@@ -4,4 +4,5 @@ coverage
4 4 pkg
5 5 spec/config/database.yml
6 6 spec/log
7   -tmp
  7 +tmp
  8 +vendor
6 config/externals.yml
... ... @@ -0,0 +1,6 @@
  1 +also_migrate:
  2 + repo: git://github.com/winton/also_migrate.git
  3 + path: vendor
  4 +mover:
  5 + repo: git://github.com/winton/mover.git
  6 + path: vendor
20 lib/acts_as_archive.rb
@@ -2,6 +2,9 @@
2 2
3 3 ActsAsArchive::Gems.require(:lib)
4 4
  5 +$:.unshift File.expand_path(File.dirname(__FILE__) + '/../vendor/also_migrate/lib')
  6 +$:.unshift File.expand_path(File.dirname(__FILE__) + '/../vendor/mover/lib')
  7 +
5 8 require 'also_migrate'
6 9 require 'mover'
7 10
@@ -47,6 +50,9 @@ module ActMethods
47 50 def acts_as_archive(*args)
48 51 return unless ActsAsArchive.find(self).empty?
49 52
  53 + ActsAsArchive.configuration ||= []
  54 + ActsAsArchive.configuration << (config = { :from => self })
  55 +
50 56 options = args.last.is_a?(::Hash) ? args.pop : {}
51 57 options[:copy] = true
52 58
@@ -74,9 +80,11 @@ class Archive < ActiveRecord::Base
74 80 acts_as_archive(#{self}, :archive => true)
75 81 EVAL
76 82 self.reflect_on_all_associations.each do |association|
77   - puts association.klass.inspect
78 83 if !ActsAsArchive.find(association.klass).empty? && association.options[:dependent]
79   - klass.send association.macro, association.name, association.options
  84 + options = association.options.dup
  85 + options[:class_name] = "::#{association.class_name}::Archive"
  86 + options[:foreign_key] = association.primary_key_name
  87 + klass.send association.macro, association.name, options
80 88 end
81 89 end
82 90 unless options[:migrate] == false
@@ -85,12 +93,8 @@ class Archive < ActiveRecord::Base
85 93 end
86 94 end
87 95
88   - ActsAsArchive.configuration ||= []
89   - ActsAsArchive.configuration << {
90   - :from => self,
91   - :to => args,
92   - :options => options
93   - }
  96 + config[:to] = args
  97 + config[:options] = options
94 98 end
95 99 end
96 100 end
3  lib/acts_as_archive/gems.rb
@@ -9,6 +9,7 @@ class Gems
9 9 :activesupport => '=3.0.3',
10 10 :active_wrapper => '=0.3.4',
11 11 :also_migrate => '0.2.1',
  12 + :externals => '1.0.2',
12 13 :mover => '0.3.3',
13 14 :rake => '=0.8.7',
14 15 :rspec => '=1.3.1'
@@ -16,7 +17,7 @@ class Gems
16 17
17 18 TYPES = {
18 19 :gemspec => [ :also_migrate, :mover ],
19   - :gemspec_dev => [ :active_wrapper, :rspec ],
  20 + :gemspec_dev => [ :active_wrapper, :externals, :rspec ],
20 21 :lib => [ :also_migrate, :mover ],
21 22 :rake => [ :rake, :rspec ],
22 23 :spec => [ :activesupport, :active_wrapper, :rspec ],
85 spec/acts_as_archive_spec.rb
... ... @@ -1,9 +1,86 @@
1 1 require 'spec_helper'
2 2
3 3 describe ActsAsArchive do
4   - it "should" do
  4 +
  5 + before(:each) do
5 6 [ 8, 0, 8 ].each { |v| $db.migrate(v) }
6   - a = Record.create
7   - a.destroy
  7 +
  8 + @record = Record.create :belongs_to_id => BelongsTo.create.id
  9 +
  10 + HasOne.create :record_id => @record.id
  11 +
  12 + HasMany.create :record_id => @record.id
  13 + HasMany.create :record_id => @record.id
  14 +
  15 + Record.first.has_many_throughs.create
  16 + Record.first.has_many_throughs.create
  17 +
  18 + Record.first.create_has_one_through_through.create_has_one_through
  19 +
  20 + @lengths = {
  21 + :record => 1,
  22 + :belongs_to => 1,
  23 + :has_one => 1,
  24 + :has_many => 2,
  25 + :has_many_through => 2,
  26 + :has_many_through_through => 2,
  27 + :has_one_through => 1,
  28 + :has_one_through_through => 1
  29 + }
  30 +
  31 + @zero_lengths = @lengths.inject({}) do |hash, (key, value)|
  32 + hash[key] = 0
  33 + hash
  34 + end
8 35 end
9   -end
  36 +
  37 + it "should create records" do
  38 + original, archive = all_records
  39 + verify_lengths original, @lengths
  40 + verify_attributes original
  41 + end
  42 +
  43 + %w(delete delete_all destroy destroy_all).each do |type|
  44 + describe type do
  45 + it "should move records to archive tables" do
  46 + case type
  47 + when 'delete', 'destroy'
  48 + @record.send type
  49 + when 'delete_all', 'destroy_all'
  50 + Record.send type
  51 + end
  52 +
  53 + original, archive = all_records
  54 +
  55 + case type
  56 + when 'delete', 'delete_all'
  57 + archive[:record].length.should == 1
  58 + original[:record].length.should == 0
  59 +
  60 + verify_lengths archive, @zero_lengths, :exclude => [ :record ]
  61 + verify_lengths original, @lengths, :exclude => [ :record ]
  62 +
  63 + verify_attributes archive, :only => [ :record ]
  64 +
  65 + when 'destroy', 'destroy_all'
  66 + verify_lengths archive, @lengths
  67 + verify_lengths original, @zero_lengths
  68 +
  69 + verify_attributes archive
  70 + end
  71 + end
  72 +
  73 + it "should move records back to original tables" do
  74 + @record.destroy
  75 + Record::Archive.first.destroy
  76 +
  77 + original, archive = all_records
  78 +
  79 + verify_lengths original, @lengths
  80 + verify_lengths archive, @zero_lengths
  81 +
  82 + verify_attributes original
  83 + end
  84 + end
  85 + end
  86 +end
0  spec/db/migrate/002_belongs_tos.rb → spec/db/migrate/001_belongs_tos.rb
File renamed without changes
0  spec/db/migrate/001_records.rb → spec/db/migrate/002_records.rb
File renamed without changes
3  spec/db/migrate/005_has_many_through_throughs.rb
... ... @@ -1,8 +1,11 @@
1 1 class HasManyThroughThroughs < ActiveRecord::Migration
2 2 def self.up
3 3 create_table :has_many_through_throughs do |t|
  4 + t.string :string, :default => 'string'
  5 + t.integer :integer, :default => '1'
4 6 t.integer :has_many_through_id
5 7 t.integer :record_id
  8 + t.timestamps
6 9 end
7 10 end
8 11
3  spec/db/migrate/007_has_one_through_throughs.rb
... ... @@ -1,7 +1,10 @@
1 1 class HasOneThroughThroughs < ActiveRecord::Migration
2 2 def self.up
3 3 create_table :has_one_through_throughs do |t|
  4 + t.string :string, :default => 'string'
  5 + t.integer :integer, :default => '1'
4 6 t.integer :record_id
  7 + t.timestamps
5 8 end
6 9 end
7 10
2  spec/fixtures/has_many.rb
... ... @@ -1,4 +1,6 @@
1 1 class HasMany < ActiveRecord::Base
2 2
3 3 belongs_to :record, :dependent => :delete
  4 +
  5 + acts_as_archive
4 6 end
2  spec/fixtures/has_many_through.rb
@@ -2,4 +2,6 @@ class HasManyThrough < ActiveRecord::Base
2 2
3 3 has_many :has_many_through_throughs, :dependent => :delete_all
4 4 has_many :records, :dependent => :delete_all, :through => :has_many_through_throughs
  5 +
  6 + acts_as_archive
5 7 end
2  spec/fixtures/has_many_through_through.rb
@@ -2,4 +2,6 @@ class HasManyThroughThrough < ActiveRecord::Base
2 2
3 3 belongs_to :record, :dependent => :delete
4 4 belongs_to :has_many_through, :dependent => :delete
  5 +
  6 + acts_as_archive
5 7 end
2  spec/fixtures/has_one.rb
... ... @@ -1,4 +1,6 @@
1 1 class HasOne < ActiveRecord::Base
2 2
3 3 belongs_to :record, :dependent => :delete
  4 +
  5 + acts_as_archive
4 6 end
2  spec/fixtures/has_one_through.rb
... ... @@ -1,4 +1,6 @@
1 1 class HasOneThrough < ActiveRecord::Base
2 2
3 3 belongs_to :has_one_through_through, :dependent => :delete
  4 +
  5 + acts_as_archive
4 6 end
2  spec/fixtures/has_one_through_through.rb
@@ -2,4 +2,6 @@ class HasOneThroughThrough < ActiveRecord::Base
2 2
3 3 belongs_to :record, :dependent => :delete
4 4 has_one :has_one_through, :dependent => :delete
  5 +
  6 + acts_as_archive
5 7 end
4 spec/fixtures/record.rb
... ... @@ -1,10 +1,10 @@
1 1 class Record < ActiveRecord::Base
2 2
3   - belongs_to :belongs_to, :dependent => :destroy
  3 + belongs_to :belongs_to, :dependent => :delete
4 4
5 5 has_one :has_one, :dependent => :destroy
6 6
7   - has_many :has_manies, :dependent => :destroy
  7 + has_many :has_manies, :dependent => :delete_all
8 8
9 9 has_many :has_many_through_throughs, :dependent => :destroy
10 10 has_many :has_many_throughs, :dependent => :destroy, :through => :has_many_through_throughs
53 spec/spec_helper.rb
@@ -19,14 +19,53 @@
19 19 require path
20 20 end
21 21
22   -require 'pp'
23   -
24 22 Spec::Runner.configure do |config|
25 23 end
26 24
27   -# For use with rspec textmate bundle
28   -def debug(object)
29   - puts "<pre>"
30   - puts object.pretty_inspect.gsub('<', '&lt;').gsub('>', '&gt;')
31   - puts "</pre>"
  25 +def all_records
  26 + [
  27 + {
  28 + :record => Record.all,
  29 + :belongs_to => BelongsTo.all,
  30 + :has_one => HasOne.all,
  31 + :has_many => HasMany.all,
  32 + :has_many_through => HasManyThrough.all,
  33 + :has_many_through_through => HasManyThroughThrough.all,
  34 + :has_one_through => HasOneThrough.all,
  35 + :has_one_through_through => HasOneThroughThrough.all
  36 + },
  37 + {
  38 + :record => Record::Archive.all,
  39 + :belongs_to => BelongsTo::Archive.all,
  40 + :has_one => HasOne::Archive.all,
  41 + :has_many => HasMany::Archive.all,
  42 + :has_many_through => HasManyThrough::Archive.all,
  43 + :has_many_through_through => HasManyThroughThrough::Archive.all,
  44 + :has_one_through => HasOneThrough::Archive.all,
  45 + :has_one_through_through => HasOneThroughThrough::Archive.all
  46 + }
  47 + ]
  48 +end
  49 +
  50 +def verify_attributes(records, options={})
  51 + options[:exclude] ||= []
  52 + records.each do |key, value|
  53 + if !options[:exclude].include?(key) && (!options[:only] || options[:only].include?(key))
  54 + value.each do |record|
  55 + record[:string].should == 'string'
  56 + record[:integer].should == 1
  57 + record[:created_at].is_a?(::Time).should == true
  58 + record[:updated_at].is_a?(::Time).should == true
  59 + end
  60 + end
  61 + end
  62 +end
  63 +
  64 +def verify_lengths(records, lengths, options={})
  65 + options[:exclude] ||= []
  66 + records.each do |key, value|
  67 + if !options[:exclude].include?(key) && (!options[:only] || options[:only].include?(key))
  68 + value.length.should == lengths[key]
  69 + end
  70 + end
32 71 end

0 comments on commit 7622898

Please sign in to comment.
Something went wrong with that request. Please try again.