Created
February 3, 2011 20:53
-
-
Save javmorin/810169 to your computer and use it in GitHub Desktop.
Test demonstrating issue with update_attributes and associated objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
describe "Association Validation Inheritance" do | |
context "#update_attributes" do | |
before(:all) do | |
parent = ValInheritanceParent.new | |
embedded = ValInheritanceEmbed.new(:embed_value => 'start value') | |
referenced = ValInheritanceRef.new(:ref_value => 'start value') | |
parent.val_inheritance_embed << embedded | |
parent.val_inheritance_ref << referenced | |
parent.save | |
referenced.save # parent.save does not save the referenced object. Shouldn't it? | |
end | |
it "should allow valid updates to embedded object" do | |
obj = ValInheritanceParent.first | |
obj.update_attributes({'val_inheritance_embed_attributes' => { 0 => { "id" => obj.val_inheritance_embed[0].id, "embed_value" => 'Valid Value' }}}) | |
obj.should be_valid | |
obj.save! | |
obj.reload | |
obj.val_inheritance_embed[0].embed_value.should == 'Valid Value' | |
end | |
it "should not allow invalid updates to embedded object" do | |
obj = ValInheritanceParent.first | |
obj.update_attributes({'val_inheritance_embed_attributes' => { 0 => { "id" => obj.val_inheritance_embed[0].id, "embed_value" => '$$$' }}}) | |
obj.should_not be_valid | |
expect{obj.save!}.to raise_error(Mongoid::Errors::Validations) | |
obj.reload | |
obj.val_inheritance_embed[0].embed_value.should_not == '$$$' | |
end | |
it "should allow valid updates to referenced object" do | |
obj = ValInheritanceParent.first | |
obj.update_attributes({'val_inheritance_ref_attributes' => { 0 => { "id" => obj.val_inheritance_ref[0].id, "ref_value" => 'Valid Value' }}}) | |
obj.should be_valid | |
obj.save! | |
obj.reload | |
obj.val_inheritance_ref[0].ref_value.should == 'Valid Value' | |
end | |
it "should not allow invalid updates to referenced object" do | |
obj = ValInheritanceParent.first | |
obj.update_attributes({'val_inheritance_ref_attributes' => { 0 => { "id" => obj.val_inheritance_ref[0].id, "ref_value" => '$$$' }}}) | |
obj.should_not be_valid | |
expect{obj.save!}.to raise_error(Mongoid::Errors::Validations) | |
obj.val_inheritance_ref[0].ref_value.should_not == '$$$' | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ValInheritanceEmbed | |
include Mongoid::Document | |
field :embed_value | |
validates_format_of :embed_value, :without => /\$\$\$/ | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ValInheritanceParent | |
include Mongoid::Document | |
field :parent_value | |
embeds_many :val_inheritance_embed | |
references_many :val_inheritance_ref | |
accepts_nested_attributes_for :val_inheritance_embed | |
accepts_nested_attributes_for :val_inheritance_ref | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ValInheritanceRef | |
include Mongoid::Document | |
referenced_in :val_inheritance_parent | |
field :ref_value | |
validates_format_of :ref_value, :without => /\$\$\$/ | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment