Skip to content

Instantly share code, notes, and snippets.

@visnup
Forked from javmorin/mj-test01_spec.rb
Created February 10, 2011 17:32
Show Gist options
  • Save visnup/820943 to your computer and use it in GitHub Desktop.
Save visnup/820943 to your computer and use it in GitHub Desktop.
require "spec_helper"
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
class ValInheritanceEmbed
include Mongoid::Document
field :embed_value
validates_format_of :embed_value, :without => /\$\$\$/
end
class ValInheritanceRef
include Mongoid::Document
referenced_in :val_inheritance_parent
field :ref_value
validates_format_of :ref_value, :without => /\$\$\$/
end
describe "Association Validation Inheritance" do
context "#update_attributes" do
before(:all) do
ValInheritanceParent.delete_all
ValInheritanceRef.delete_all
parent = ValInheritanceParent.create
parent.val_inheritance_embed << ValInheritanceEmbed.new(:embed_value => 'start value')
parent.val_inheritance_ref << ValInheritanceRef.new(:ref_value => 'start value')
parent.save
end
it "should allow valid updates to embedded object" do
obj = ValInheritanceParent.first
obj.val_inheritance_embed_attributes = { 0 => { "id" => obj.val_inheritance_embed[0].id, "embed_value" => 'Valid Value' }}
obj.should be_valid
obj.save.should be_true
obj.val_inheritance_embed[0].embed_value.should == 'Valid Value'
obj.reload
obj.val_inheritance_embed[0].embed_value.should == 'Valid Value'
end
it "should allow valid updates to referenced object" do
obj = ValInheritanceParent.first
obj.val_inheritance_ref_attributes = { 0 => { "id" => obj.val_inheritance_ref[0].id, "ref_value" => 'Valid Value' }}
obj.should be_valid
obj.save.should be_true
obj.val_inheritance_ref[0].ref_value.should == 'Valid Value'
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.val_inheritance_ref_attributes = { 0 => { "id" => obj.val_inheritance_ref[0].id, "ref_value" => '$$$' }}
obj.should_not be_valid
obj.save.should_not be_true
obj.val_inheritance_ref[0].ref_value.should_not == '$$$'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment