Skip to content

Instantly share code, notes, and snippets.

@semanticart
Created May 13, 2009 13:31
Show Gist options
  • Select an option

  • Save semanticart/111011 to your computer and use it in GitHub Desktop.

Select an option

Save semanticart/111011 to your computer and use it in GitHub Desktop.
diff --git a/lib/is_paranoid.rb b/lib/is_paranoid.rb
index 3c11248..c74adf1 100644
--- a/lib/is_paranoid.rb
+++ b/lib/is_paranoid.rb
@@ -30,21 +30,33 @@ module IsParanoid
# NOTE: is_paranoid declaration must follow assocation declarations.
[:has_many, :has_one].each do |macro|
self.reflect_on_all_associations(macro).each do |assoc|
- if (a = assoc.klass.reflect_on_all_associations(:belongs_to).detect{ |a| a.class_name == self.class_name })
- assoc.klass.send(
+ child_klass = begin
+ Object.module_eval("::#{assoc.class_name}", __FILE__, __LINE__)
+ rescue NameError
+ eval("class ::#{assoc.class_name} < ActiveRecord::Base;end")
+ Object.module_eval("::#{assoc.class_name}", __FILE__, __LINE__)
+ end
+
+ # NOTE: this doesn't reflect back through the child association
+ # (which might not even exist). So if the child belongs_to the parent
+ # as :parent instead of the parent's class name (i.e. :android), this
+ # fails miserably. Neither is the primary key below correct since
+ # it is pulled from the parent class.
+ parent_klass = self
+ child_klass.send(
:include,
- Module.new{ # Example:
- define_method "#{a.name}_with_destroyed" do |*args| # def android_with_destroyed
- a.klass.first_with_destroyed( # Android.first_with_destroyed(
- :conditions => { # :conditions => {
- a.klass.primary_key => # :id =>
- self.send(a.primary_key_name) # self.send(:android_id)
- } # }
- ) # )
- end # end
+ Module.new{ # Example:
+ define_method "#{parent_klass.to_s.underscore}_with_destroyed" do |*args| # def android_with_destroyed
+ parent_klass.first_with_destroyed( # Android.first_with_destroyed(
+ :conditions => { # :conditions => {
+ child_klass.primary_key => # :id =>
+ self.send(assoc.primary_key_name) # self.send(:android_id)
+ } # }
+ ) # )
+ end # end
}
)
- end
+
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment