Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix for Issue 24, polymorphic association #26

Open
wants to merge 1 commit into from

4 participants

@elmatou
Collaborator

Hi Winton,
here is a fix for the polymorphic association issue.
The point is to check the kind of association (polymorphic and dependent) before calling ReflexionAssociation#klass

@elmatou elmatou ActsAsArchive#acts_as_archive should check if belongs_to association …
…is polymorphic before trying to access parent class.

+ 2 typo "class << self" need spaces (for highlighting mostly)
18b7f65
@Nothus

Also switching "!ActsAsArchive.find(association.klass).empty?" and "association.options[:dependent]" makes it faster when caching classes are turned off (like in development).

@elmatou
Collaborator

The only problem is the repo owner doesn't maintain anymore this gem.

@winton
Owner

@elmatou Do you have any interest in taking over ownership of ActsAsArchive?

@elmatou
Collaborator

Hi @winton,
I'm not sure to be the best to maintain ActsAsArchive as it relies on others of your gems, and as I'm less on github than before.
But I would be glad to fix the little issues we found in the library.

I suggest, you keep the ownership, and add me as a maintener on github and rubygems, and I will push important fixes.

If you are not comfortable with this solution, maybe can you just accept a Pull Request for the two main issues (polymorphic + loading) and then release a new and last version of your gem.

See you

@timocratic

Would it perhaps be a good idea to put a big bold note in the readme that it is unmaintained?

@winton
Owner

Added @elmatou to collaborators and added a note about the project not being maintained.

@timocratic

Thanks winton. I'll see if the client that is using it is willing to be the official maintainer or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 15, 2011
  1. @elmatou

    ActsAsArchive#acts_as_archive should check if belongs_to association …

    elmatou authored
    …is polymorphic before trying to access parent class.
    
    + 2 typo "class << self" need spaces (for highlighting mostly)
This page is out of date. Refresh to see the latest.
Showing with 56 additions and 55 deletions.
  1. +33 −32 lib/acts_as_archive.rb
  2. +23 −23 lib/acts_as_archive/gems.rb
View
65 lib/acts_as_archive.rb
@@ -9,10 +9,10 @@
$:.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
@@ -20,7 +20,7 @@ def deprecate(msg)
$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,7 +201,7 @@ def self.included(base)
end
end
end
-
+
module InstanceMethods
def delete_sql_with_archive(sql, name = nil)
@mutex ||= Mutex.new
@@ -209,13 +209,13 @@ def delete_sql_with_archive(sql, name = nil)
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)
+
View
46 lib/acts_as_archive/gems.rb
@@ -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
Something went wrong with that request. Please try again.