Fix for Issue 24, polymorphic association #26

Open
wants to merge 1 commit into
from
@@ -9,18 +9,18 @@
$:.unshift File.dirname(__FILE__)
class ActsAsArchive
- class <<self
-
+ class << self
+
attr_accessor :configuration, :disabled
-
+
def deprecate(msg)
if defined?(::ActiveSupport::Deprecation)
::ActiveSupport::Deprecation.warn msg
else
$stdout.puts msg
end
end
-
+
def disable(&block)
@mutex ||= Mutex.new
@mutex.synchronize do
@@ -30,7 +30,7 @@ def disable(&block)
ensure
self.disabled = false
end
-
+
def find(from)
from = [ from ] unless from.is_a?(::Array)
(@configuration || []).select do |hash|
@@ -41,7 +41,7 @@ def find(from)
end
end
end
-
+
def load_from_yaml(root)
if File.exists?(yaml = "#{root}/config/acts_as_archive.yml")
YAML.load(File.read(yaml)).each do |klass, config|
@@ -59,7 +59,7 @@ def load_from_yaml(root)
end
end
end
-
+
def move(config, where, merge_options={})
options = config[:options].dup.merge(merge_options)
if options[:conditions]
@@ -69,12 +69,12 @@ def move(config, where, merge_options={})
end
config[:from].move_to(config[:to], options)
end
-
+
def update(*args)
deprecate "ActsAsArchive.update is deprecated and no longer necessary."
end
end
-
+
module Base
def self.included(base)
unless base.included_modules.include?(InstanceMethods)
@@ -86,12 +86,12 @@ def self.included(base)
module ClassMethods
def acts_as_archive(options={})
return unless ActsAsArchive.find(self).empty?
-
+
ActsAsArchive.configuration ||= []
ActsAsArchive.configuration << (config = { :from => self })
-
+
options[:copy] = true
-
+
if options[:archive]
options[:magic] = 'restored_at'
klass = options[:class]
@@ -101,17 +101,17 @@ def acts_as_archive(options={})
options[:ignore] = options[:magic]
options[:subtract] = 'restored_at'
options[:timestamps] = false if options[:timestamps].nil?
-
+
unless options[:class]
options[:class] = "#{self}::Archive"
end
-
+
unless options[:table]
options[:table] = "archived_#{self.table_name}"
end
-
+
klass = eval(options[:class]) rescue nil
-
+
if klass
klass.send :set_table_name, options[:table]
else
@@ -122,19 +122,19 @@ class ::#{options[:class]} < ActiveRecord::Base
EVAL
klass = eval("::#{options[:class]}")
end
-
+
klass.record_timestamps = options[:timestamps].inspect
klass.acts_as_archive(:class => self, :archive => true)
-
+
self.reflect_on_all_associations.each do |association|
- if !ActsAsArchive.find(association.klass).empty? && association.options[:dependent]
+ if association.options[:dependent] && !association.options[:polymorphic] && !ActsAsArchive.find(association.klass).empty?
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
AlsoMigrate.configuration ||= []
AlsoMigrate.configuration << options.merge(
@@ -143,19 +143,19 @@ class ::#{options[:class]} < ActiveRecord::Base
)
end
end
-
+
config[:to] = klass
config[:options] = options
end
-
+
def delete_all!(*args)
ActsAsArchive.disable { self.delete_all(*args) }
end
-
+
def destroy_all!(*args)
ActsAsArchive.disable { self.destroy_all(*args) }
end
-
+
def migrate_from_acts_as_paranoid
time = Benchmark.measure do
ActsAsArchive.find(self).each do |config|
@@ -171,24 +171,24 @@ def 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}.delete_all."
self.delete_all *args
end
end
-
+
module InstanceMethods
def delete!(*args)
ActsAsArchive.disable { self.delete(*args) }
end
-
+
def destroy!(*args)
ActsAsArchive.disable { self.destroy(*args) }
end
end
end
-
+
module DatabaseStatements
def self.included(base)
unless base.included_modules.include?(InstanceMethods)
@@ -201,21 +201,21 @@ def self.included(base)
end
end
end
-
+
module InstanceMethods
def delete_sql_with_archive(sql, name = nil)
@mutex ||= Mutex.new
@mutex.synchronize do
unless ActsAsArchive.disabled
from, where = /DELETE FROM (.+)/i.match(sql)[1].split(/\s+WHERE\s+/i, 2)
from = from.strip.gsub(/`/, '').split(/\s*,\s*/)
-
+
ActsAsArchive.find(from).each do |config|
ActsAsArchive.move(config, where)
end
end
end
-
+
delete_sql_without_archive(sql, name)
end
end
@@ -226,4 +226,5 @@ def delete_sql_with_archive(sql, name = nil)
::ActiveRecord::ConnectionAdapters::DatabaseStatements.send(:include, ::ActsAsArchive::DatabaseStatements)
require "acts_as_archive/adapters/rails#{Rails.version[0..0]}" if defined?(Rails)
-require "acts_as_archive/adapters/sinatra" if defined?(Sinatra)
+require "acts_as_archive/adapters/sinatra" if defined?(Sinatra)
+
@@ -1,17 +1,17 @@
unless defined?(ActsAsArchive::Gems)
-
+
require 'yaml'
-
+
class ActsAsArchive
module Gems
- class <<self
-
+ class << self
+
attr_accessor :config
attr_reader :gemset, :gemsets, :versions
-
+
class SimpleStruct
attr_reader :hash
-
+
def initialize(hash)
@hash = hash
@hash.each do |key, value|
@@ -20,22 +20,22 @@ def initialize(hash)
end
end
end
-
+
Gems.config = SimpleStruct.new(
:gemsets => [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ],
:gemspec => "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml",
:warn => true
)
-
+
def activate(*gems)
begin
require 'rubygems' unless defined?(::Gem)
rescue LoadError
puts "rubygems library could not be required" if @config.warn
end
-
+
self.gemset ||= gemset_from_loaded_specs
-
+
gems.flatten.collect(&:to_sym).each do |name|
version = @versions[name]
vendor = File.expand_path("../../../vendor/#{name}/lib", __FILE__)
@@ -48,19 +48,19 @@ def activate(*gems)
end
end
end
-
+
def dependencies
dependency_filter(@gemspec.dependencies, @gemset)
end
-
+
def development_dependencies
dependency_filter(@gemspec.development_dependencies, @gemset)
end
-
+
def gemset=(gemset)
if gemset
@gemset = gemset.to_sym
-
+
@gemsets = @config.gemsets.reverse.collect { |config|
if config.is_a?(::String)
YAML::load(File.read(config)) rescue {}
@@ -70,7 +70,7 @@ def gemset=(gemset)
}.inject({}) do |hash, config|
deep_merge(hash, symbolize_keys(config))
end
-
+
@versions = (@gemsets[gemspec.name.to_sym] || {}).inject({}) do |hash, (key, value)|
if !value.is_a?(::Hash) && value
hash[key] = value
@@ -85,7 +85,7 @@ def gemset=(gemset)
@versions = nil
end
end
-
+
def gemset_names
(
[ :default ] +
@@ -95,7 +95,7 @@ def gemset_names
}
).uniq
end
-
+
def gemspec(reload=false)
if @gemspec && !reload
@gemspec
@@ -104,16 +104,16 @@ def gemspec(reload=false)
@gemspec = SimpleStruct.new(data)
end
end
-
+
private
-
+
def deep_merge(first, second)
merger = lambda do |key, v1, v2|
Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
end
first.merge(second, &merger)
end
-
+
def dependency_filter(dependencies, match)
(dependencies || []).inject([]) { |array, value|
if value.is_a?(::Hash)
@@ -124,7 +124,7 @@ def dependency_filter(dependencies, match)
array
}.uniq.collect(&:to_sym)
end
-
+
def gemset_from_loaded_specs
if defined?(Gem)
Gem.loaded_specs.each do |name, spec|
@@ -139,7 +139,7 @@ def gemset_from_loaded_specs
:none
end
end
-
+
def symbolize_keys(hash)
return {} unless hash.is_a?(::Hash)
hash.inject({}) do |options, (key, value)|
@@ -151,4 +151,4 @@ def symbolize_keys(hash)
end
end
end
-end
+end