Sha256: 828390b9cbe286b6007ab29c562121af76993cdafa75697411c8b4741a4b0078

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 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

  it '' do
    issue = Issue.create!(subject: 'Foo', person: bob)

    p = Person.where(name: 'Alice').take
    p.attributes = {
      issues_attributes: [
        { id: issue.id },
      ]
    }
    p.save!

    p.reload
    expect(p.issues.size).to eq(1)

    bob.reload
    expect(bob.issues.size).to eq(0)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
deferring-0.6.2 spec/lib/deferring_nested_attributes_spec.rb