Sha256: 0766914f5bc591d84f2fd17cf4916c58c9223bd65903c13f59038d99949c7008

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'spec_helper'

describe Encore::Entity::Input::Associations do
  describe :ClassMethods do
    describe :has_one do
      before do
        spawn_model(:User) { belongs_to :organization }
        spawn_model(:Organization) { has_many :users }

        spawn_entity :UserEntity do
          expose :name
          expose_one :organization
        end

        run_migration do
          create_table(:users, force: true) do |t|
            t.string :name
            t.integer :organization_id
            t.timestamps
          end

          create_table(:organizations, force: true)
        end
      end

      let(:organization) { Organization.create }
      let(:other_organization) { Organization.create }
      let(:user) { organization.users.create(name: 'Art Vandelay') }
      let(:entity) { UserEntity.new(user) }

      context 'when assigning attributes' do
        before { entity.assign_attributes organization: other_organization.id }
        it { expect(entity.object.organization_id).to eql organization.id }
        it { expect(entity.encore_tmp_organization_id).to eql other_organization.id }
      end

      context 'when saving the entity' do
        before do
          entity.assign_attributes organization: other_organization.id
          entity.save
        end

        it { expect(entity.object.organization_id).to eql other_organization.id }
        it { expect(entity.encore_tmp_organization_id).to be_nil }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
encore-0.0.3 spec/encore/entity/input/associations_spec.rb