Guest User

Untitled

a guest
Jun 21st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. require "rubygems"
  2. require "dm-core"
  3. require "dm-validations"
  4. require "spec"
  5.  
  6. DataMapper.setup(:default, "sqlite3::memory:")
  7.  
  8. module MarkForDestruction
  9. def mark_for_destruction
  10. @marked_for_destruction = true
  11. end
  12. def marked_for_destruction?
  13. @marked_for_destruction
  14. end
  15. end
  16.  
  17. module Save
  18. def save(*args)
  19. if marked_for_destruction?
  20. destroy
  21. else
  22. super
  23. end
  24. end
  25. end
  26.  
  27. class Person
  28. include DataMapper::Resource
  29. include MarkForDestruction
  30. include Save
  31. property :id, Serial
  32. has 1, :profile
  33. end
  34.  
  35. class Profile
  36. include DataMapper::Resource
  37. include MarkForDestruction
  38. include Save
  39. property :id, Serial
  40. property :person_id, Integer, :nullable => false
  41. belongs_to :person
  42. end
  43.  
  44. describe "Resource#save" do
  45. before(:each) do
  46. DataMapper.auto_migrate!
  47. end
  48. it "should save a newly associated resource" do
  49. p = Person.new
  50. p.profile = Profile.new
  51. p.save
  52. Person.all.size.should == 1
  53. Profile.all.size.should == 1
  54. end
  55. it "should destroy a resource that is marked_for_destruction" do
  56. person = Person.create
  57. person.profile = Profile.create :person => person
  58. person.profile.mark_for_destruction
  59.  
  60. person.send(:child_associations).should_not be_empty
  61.  
  62. person.save
  63. Person.all.size.should == 1
  64. Profile.all.size.should == 0
  65. end
  66. end
Add Comment
Please, Sign In to add comment