Sha256: 6dfb469f2094e114fe13682a48fb9ebb0fa5414eb879833a814d8cb2c3f28b69

Contents?: true

Size: 1.18 KB

Versions: 22

Compression:

Stored size: 1.18 KB

Contents

require 'spec_helper'

RSpec.describe 'deferred accepts_nested_attributes' do

  before :each do
    Person.create!(name: 'Alice')
    Person.create!(name: 'Bob')

    Team.create!(name: 'Database Administration')
    Team.create!(name: 'End-User Support')
    Team.create!(name: 'Operations')
  end

  let(:bob) { Person.where(name: 'Bob').first }

  let(:dba) { Team.where(name: 'Database Administration').first }
  let(:support) { Team.where(name: 'End-User Support').first }
  let(:operations) { Team.where(name: 'Operations').first }

  # TODO: Write more tests.

  it 'should mass assign' do
    p = Person.first
    p.teams << Team.first << Team.last << Team.find(2)
    p.save

    # Destroy team 2 and 3. Keep team 1.
    p = Person.first
    p.attributes = {
      teams_attributes: [
        { id: 1 },
        { id: 3, _destroy: '1' },
        { id: 2, _destroy: '1' }
      ]
    }
    expect(p.teams.length).to eq(1)
    expect(p.team_ids.sort).to eq([1])

    Person.first
    expect(Person.first.teams.length).to eq(3)
    expect(Person.first.team_ids.sort).to eq([1,2,3])

    p.save!

    p = Person.first
    expect(p.teams.length).to eq(1)
    expect(p.team_ids.sort).to eq([1])
  end

end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
deferring-0.8.2 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.8.1 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.8.0 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.7.1 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.7.0 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.6.1 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.6.0 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.5.0 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.4.1 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.4.0 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.3.0 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.2.1 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.2.0 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.1.5 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.1.4 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.1.3 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.1.2 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.1.1 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.1.0 spec/lib/deferring_nested_attributes_spec.rb
deferring-0.0.10 spec/lib/deferring_nested_attributes_spec.rb