Sha256: 3767ad36bb51c4806571c172f63aff241a67a409ed74ccbee31fe269e1b4282b

Contents?: true

Size: 1013 Bytes

Versions: 3

Compression:

Stored size: 1013 Bytes

Contents

require 'spec_helper'

describe 'Defining many-to-one association' do
  include_context 'users and tasks'

  before do
    conn[:users].insert id: 2, name: 'Jane'
    conn[:tasks].insert id: 2, user_id: 2, title: 'Task one'
  end

  it 'extends relation with association methods' do
    setup.relation(:tasks) do
      many_to_one :users, key: :user_id, on: { name: 'Piotr' }

      def all
        select(:id, :title)
      end

      def with_user
        association_join(:users, select: [:name])
      end
    end

    setup.mappers do
      define(:tasks)

      define(:with_user, parent: :tasks) do
        wrap :user do
          attribute :name
        end
      end
    end

    setup.relation(:users)

    tasks = rom.relations.tasks

    expect(tasks.all.with_user.to_a).to eql(
      [{ id: 1, name: 'Piotr', title: 'Finish ROM' }]
    )

    expect(rom.relation(:tasks).map_with(:with_user).all.with_user.to_a).to eql(
      [{ id: 1, title: 'Finish ROM', user: { name: 'Piotr' } }]
    )
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-sql-0.5.2 spec/unit/many_to_one_spec.rb
rom-sql-0.5.1 spec/unit/many_to_one_spec.rb
rom-sql-0.5.0 spec/unit/many_to_one_spec.rb