Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

has_many associations now work with (parent)_with_destroyed

  • Loading branch information...
commit 59b902fdc08d74fb2c0e5e22320d34f080fd9f40 1 parent ccd7849
@amiel amiel authored
Showing with 32 additions and 10 deletions.
  1. +24 −9 lib/is_paranoid.rb
  2. +1 −1  spec/database.yml
  3. +7 −0 spec/is_paranoid_spec.rb
View
33 lib/is_paranoid.rb
@@ -176,15 +176,30 @@ def is_paranoid_method_missing name, *args, &block
self.class.send(
:include,
- Module.new{ # Example:
- define_method name do |*args| # def android_with_destroyed
- parent_klass.first_with_destroyed( # Android.first_with_destroyed(
- :conditions => { # :conditions => {
- parent_klass.primary_key => # :id =>
- self.send(assoc.primary_key_name) # self.send(:android_id)
- } # }
- ) # )
- end # end
+ Module.new {
+ if assoc.macro.to_s =~ /^has/
+ parent_method = assoc.macro.to_s =~ /^has_one/ ? 'first_with_destroyed' : 'all_with_destroyed'
+ # Example:
+ define_method name do |*args| # def android_with_destroyed
+ parent_klass.send("#{parent_method}", # Android.all_with_destroyed(
+ :conditions => { # :conditions => {
+ assoc.primary_key_name => # :person_id =>
+ self.send(parent_klass.primary_key) # self.send(:id)
+ } # }
+ ) # )
+ end # end
+
+ else
+ # Example:
+ define_method name do |*args| # def android_with_destroyed
+ parent_klass.first_with_destroyed( # Android.first_with_destroyed(
+ :conditions => { # :conditions => {
+ parent_klass.primary_key => # :id =>
+ self.send(assoc.primary_key_name) # self.send(:android_id)
+ } # }
+ ) # )
+ end # end
+ end
}
)
self.send(name, *args, &block)
View
2  spec/database.yml
@@ -1,3 +1,3 @@
test:
:adapter: sqlite3
- :dbfile: is_paranoid.db
+ :database: is_paranoid.db
View
7 spec/is_paranoid_spec.rb
@@ -203,6 +203,13 @@
end
end
+ it "should be able to access destroyed children" do
+ comps = @r2d2.components
+ comps.to_s # I have no idea why this makes it pass, but hey, here it is
+ @r2d2.components.first.destroy
+ @r2d2.components_with_destroyed.should == comps
+ end
+
it "should return nil if no destroyed parent exists" do
sticker = Sticker.new(:name => 'Rainbows')
# because the default relationship works this way, i.e.
Please sign in to comment.
Something went wrong with that request. Please try again.