Sha256: fbbc4d437d190d6d4314a66d10e0c6cdbec1c163718a8c986b6b3e204d804c80

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

# encoding: UTF-8
require 'spec_helper'

describe ActiveData::Model::Associations do

  class NestedAssoc
    include ActiveData::Model

    attribute :name
  end

  context do
    let(:klass) do
      Class.new do
        include ActiveData::Model

        attribute :name
        embeds_one :assoc, class_name: NestedAssoc

        accepts_nested_attributes_for :assoc
      end
    end
    let(:instance) { klass.new( name: 'world') }

    context do
      before { instance.assoc_attributes = { name: 'foo'} }
      specify { instance.assoc.should be_instance_of NestedAssoc }
      specify { instance.assoc.name.should == 'foo' }
    end
  end

  context do
    let(:klass) do
      Class.new do
        include ActiveData::Model

        attribute :name
        embeds_many :assocs, class_name: NestedAssoc

        accepts_nested_attributes_for :assocs
      end
    end
    let(:instance) { klass.new(name: 'world') }

    context do
      before { instance.assocs_attributes = [{ name: 'foo' }, { name: 'bar' }] }
      specify { instance.assocs.count.should == 2 }
      specify { instance.assocs.first.name.should == 'foo' }
      specify { instance.assocs.last.name.should == 'bar' }
    end

    context do
      before { instance.assocs_attributes = {1 => { name: 'baz' }, 2 => { name: 'foo' }} }
      specify { instance.assocs.count.should == 2 }
      specify { instance.assocs.first.name.should == 'baz' }
      specify { instance.assocs.last.name.should == 'foo' }
    end

    context do
      before { instance.assocs_attributes = {1 => { name: 'baz' }, 2 => { name: 'foo' }} }
      specify { instance.to_params.should == {
        "name" => "world",
        "assocs_attributes" => {'0' => {"name" => "baz"}, '1' => {"name" => "foo"}}
      } }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_data-0.3.0 spec/lib/active_data/model/nested_attributes_spec.rb
active_data-0.2.0 spec/lib/active_data/model/nested_attributes_spec.rb