Guest User

Untitled

a guest
Jul 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. require "spec_helper"
  2.  
  3. class ValInheritanceParent
  4. include Mongoid::Document
  5.  
  6. field :parent_value
  7.  
  8. embeds_many :val_inheritance_embed
  9. references_many :val_inheritance_ref
  10.  
  11. accepts_nested_attributes_for :val_inheritance_embed
  12. accepts_nested_attributes_for :val_inheritance_ref
  13. end
  14.  
  15. class ValInheritanceEmbed
  16. include Mongoid::Document
  17.  
  18. field :embed_value
  19. validates_format_of :embed_value, :without => /\$\$\$/
  20. end
  21.  
  22. class ValInheritanceRef
  23. include Mongoid::Document
  24.  
  25. referenced_in :val_inheritance_parent
  26.  
  27. field :ref_value
  28. validates_format_of :ref_value, :without => /\$\$\$/
  29. end
  30.  
  31. describe "Association Validation Inheritance" do
  32.  
  33. context "#update_attributes" do
  34. before(:all) do
  35. ValInheritanceParent.delete_all
  36. ValInheritanceRef.delete_all
  37.  
  38. parent = ValInheritanceParent.create
  39. parent.val_inheritance_embed << ValInheritanceEmbed.new(:embed_value => 'start value')
  40. parent.val_inheritance_ref << ValInheritanceRef.new(:ref_value => 'start value')
  41.  
  42. parent.save
  43. end
  44.  
  45. it "should allow valid updates to embedded object" do
  46. obj = ValInheritanceParent.first
  47. obj.val_inheritance_embed_attributes = { 0 => { "id" => obj.val_inheritance_embed[0].id, "embed_value" => 'Valid Value' }}
  48. obj.should be_valid
  49. obj.save.should be_true
  50. obj.val_inheritance_embed[0].embed_value.should == 'Valid Value'
  51. obj.reload
  52. obj.val_inheritance_embed[0].embed_value.should == 'Valid Value'
  53. end
  54.  
  55. it "should allow valid updates to referenced object" do
  56. obj = ValInheritanceParent.first
  57. obj.val_inheritance_ref_attributes = { 0 => { "id" => obj.val_inheritance_ref[0].id, "ref_value" => 'Valid Value' }}
  58. obj.should be_valid
  59. obj.save.should be_true
  60. obj.val_inheritance_ref[0].ref_value.should == 'Valid Value'
  61. obj.reload
  62. obj.val_inheritance_ref[0].ref_value.should == 'Valid Value'
  63. end
  64.  
  65. it "should not allow invalid updates to referenced object" do
  66. obj = ValInheritanceParent.first
  67. obj.val_inheritance_ref_attributes = { 0 => { "id" => obj.val_inheritance_ref[0].id, "ref_value" => '$$$' }}
  68. obj.should_not be_valid
  69. obj.save.should_not be_true
  70. obj.val_inheritance_ref[0].ref_value.should_not == '$$$'
  71. end
  72. end
  73. end
Add Comment
Please, Sign In to add comment