Skip to content

Commit

Permalink
Automatically generating associations that have a dependent option fo…
Browse files Browse the repository at this point in the history
…r Archive (restoring a record automatically restores archived associations), starting on specs
  • Loading branch information
winton committed Dec 14, 2010
1 parent e63c07a commit 079d585
Show file tree
Hide file tree
Showing 22 changed files with 199 additions and 44 deletions.
52 changes: 30 additions & 22 deletions lib/acts_as_archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class <<self
attr_accessor :configuration

def find(from)
from = [ from ] unless from.is_a?(::Array)
(@configuration || []).select do |hash|
if from[0].is_a?(::String)
from.include?(hash[:from].table_name)
Expand Down Expand Up @@ -44,7 +45,7 @@ def self.included(base)

module ActMethods
def acts_as_archive(*args)
return if ActsAsArchive.find(self)
return unless ActsAsArchive.find(self).empty?

options = args.last.is_a?(::Hash) ? args.pop : {}
options[:copy] = true
Expand All @@ -53,28 +54,35 @@ def acts_as_archive(*args)
options[:ignore] = options[:magic]
options[:timestamps] = false if options[:timestamps].nil?

if args.empty?
class_eval <<-EVAL
class Archive < ActiveRecord::Base
set_table_name "archived_#{self.table_name}"
end
EVAL
args << self::Archive
end
unless options[:archive]
if args.empty?
class_eval <<-EVAL
class Archive < ActiveRecord::Base
set_table_name "archived_#{self.table_name}"
end
EVAL
args << self::Archive
end

args.each do |klass|
klass.class_eval <<-EVAL
record_timestamps = #{options[:timestamps].inspect}
acts_as_archive(
#{self},
:magic => nil,
:migrate => false,
:timestamps => true,
:subtract => #{options[:magic] ? options[:magic].inspect : 'nil'}
)
EVAL
unless options[:migrate] == false
self.also_migrate klass.table_name, options
args.each do |klass|
klass.class_eval <<-EVAL
record_timestamps = #{options[:timestamps].inspect}
acts_as_archive(
#{self},
:archive => true,
:magic => nil,
:subtract => #{options[:magic] ? options[:magic].inspect : 'nil'}
)
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
end
end
unless options[:migrate] == false
self.also_migrate klass.table_name, options
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/acts_as_archive/gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ActsAsArchive
class Gems

VERSIONS = {
:activesupport => '=3.0.3',
:active_wrapper => '=0.3.4',
:also_migrate => '0.2.1',
:mover => '0.3.3',
Expand All @@ -18,7 +19,7 @@ class Gems
:gemspec_dev => [ :active_wrapper, :rspec ],
:lib => [ :also_migrate, :mover ],
:rake => [ :rake, :rspec ],
:spec => [ :active_wrapper, :rspec ],
:spec => [ :activesupport, :active_wrapper, :rspec ],
:spec_rake => [ :active_wrapper ]
}

Expand Down
4 changes: 2 additions & 2 deletions spec/acts_as_archive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

describe ActsAsArchive do
it "should" do
[ 1, 0, 1 ].each { |v| $db.migrate(v) }
a = Article.create
[ 8, 0, 8 ].each { |v| $db.migrate(v) }
a = Record.create
a.destroy
end
end
11 changes: 0 additions & 11 deletions spec/db/migrate/001_articles.rb

This file was deleted.

14 changes: 14 additions & 0 deletions spec/db/migrate/001_records.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Records < ActiveRecord::Migration
def self.up
create_table :records do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :belongs_to_id
t.timestamps
end
end

def self.down
drop_table :records
end
end
13 changes: 13 additions & 0 deletions spec/db/migrate/002_belongs_tos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class BelongsTos < ActiveRecord::Migration
def self.up
create_table :belongs_tos do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.timestamps
end
end

def self.down
drop_table :belongs_tos
end
end
14 changes: 14 additions & 0 deletions spec/db/migrate/003_has_ones.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class HasOnes < ActiveRecord::Migration
def self.up
create_table :has_ones do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :record_id
t.timestamps
end
end

def self.down
drop_table :has_ones
end
end
14 changes: 14 additions & 0 deletions spec/db/migrate/004_has_manies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class HasManies < ActiveRecord::Migration
def self.up
create_table :has_manies do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :record_id
t.timestamps
end
end

def self.down
drop_table :has_manies
end
end
12 changes: 12 additions & 0 deletions spec/db/migrate/005_has_many_through_throughs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class HasManyThroughThroughs < ActiveRecord::Migration
def self.up
create_table :has_many_through_throughs do |t|
t.integer :has_many_through_id
t.integer :record_id
end
end

def self.down
drop_table :has_many_through_throughs
end
end
13 changes: 13 additions & 0 deletions spec/db/migrate/006_has_many_throughs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class HasManyThroughs < ActiveRecord::Migration
def self.up
create_table :has_many_throughs do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.timestamps
end
end

def self.down
drop_table :has_many_throughs
end
end
11 changes: 11 additions & 0 deletions spec/db/migrate/007_has_one_through_throughs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class HasOneThroughThroughs < ActiveRecord::Migration
def self.up
create_table :has_one_through_throughs do |t|
t.integer :record_id
end
end

def self.down
drop_table :has_one_through_throughs
end
end
14 changes: 14 additions & 0 deletions spec/db/migrate/008_has_one_throughs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class HasOneThroughs < ActiveRecord::Migration
def self.up
create_table :has_one_throughs do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :has_one_through_through_id
t.timestamps
end
end

def self.down
drop_table :has_one_throughs
end
end
3 changes: 0 additions & 3 deletions spec/fixtures/article.rb

This file was deleted.

6 changes: 6 additions & 0 deletions spec/fixtures/belongs_to.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class BelongsTo < ActiveRecord::Base

has_many :records, :dependent => :delete_all

acts_as_archive
end
4 changes: 4 additions & 0 deletions spec/fixtures/has_many.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class HasMany < ActiveRecord::Base

belongs_to :record, :dependent => :delete
end
5 changes: 5 additions & 0 deletions spec/fixtures/has_many_through.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HasManyThrough < ActiveRecord::Base

has_many :has_many_through_throughs, :dependent => :delete_all
has_many :records, :dependent => :delete_all, :through => :has_many_through_throughs
end
5 changes: 5 additions & 0 deletions spec/fixtures/has_many_through_through.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HasManyThroughThrough < ActiveRecord::Base

belongs_to :record, :dependent => :delete
belongs_to :has_many_through, :dependent => :delete
end
4 changes: 4 additions & 0 deletions spec/fixtures/has_one.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class HasOne < ActiveRecord::Base

belongs_to :record, :dependent => :delete
end
4 changes: 4 additions & 0 deletions spec/fixtures/has_one_through.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class HasOneThrough < ActiveRecord::Base

belongs_to :has_one_through_through, :dependent => :delete
end
5 changes: 5 additions & 0 deletions spec/fixtures/has_one_through_through.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HasOneThroughThrough < ActiveRecord::Base

belongs_to :record, :dependent => :delete
has_one :has_one_through, :dependent => :delete
end
16 changes: 16 additions & 0 deletions spec/fixtures/record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Record < ActiveRecord::Base

belongs_to :belongs_to, :dependent => :destroy

has_one :has_one, :dependent => :destroy

has_many :has_manies, :dependent => :destroy

has_many :has_many_through_throughs, :dependent => :destroy
has_many :has_many_throughs, :dependent => :destroy, :through => :has_many_through_throughs

has_one :has_one_through_through, :dependent => :destroy
has_one :has_one_through, :dependent => :destroy, :through => :has_one_through_through

acts_as_archive
end
16 changes: 11 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@

ActsAsArchive::Gems.require(:spec)

require 'active_support/dependencies'
require 'active_wrapper'

require "#{$root}/lib/acts_as_archive"
require "#{$root}/spec/fixtures/article"
require 'pp'

Spec::Runner.configure do |config|
end
ActiveSupport::Dependencies.autoload_paths << "#{$root}/spec/fixtures"

$db, $log, $mail = ActiveWrapper.setup(
:base => File.dirname(__FILE__),
:env => 'test'
)
$db.establish_connection

Dir["#{$root}/spec/fixtures/*.rb"].each do |path|
require path
end

require 'pp'

Spec::Runner.configure do |config|
end

# For use with rspec textmate bundle
def debug(object)
puts "<pre>"
Expand Down

0 comments on commit 079d585

Please sign in to comment.