Sha256: c6772f0d392b6f6e4064e27656aea9624ed283f95037ba93eafff8119e09cd34

Contents?: true

Size: 982 Bytes

Versions: 3

Compression:

Stored size: 982 Bytes

Contents

require 'spec_helper'

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

  it 'extends relation with association methods' do
    setup.relation(:users) do
      one_to_many :tasks, key: :user_id

      def by_name(name)
        where(name: name)
      end

      def with_tasks
        association_left_join(:tasks, select: [:id, :title])
      end

      def all
        select(:id, :name)
      end
    end

    setup.mappers do
      define(:users)

      define(:with_tasks, parent: :users) do
        group tasks: [:tasks_id, :title]
      end
    end

    users = rom.relations.users

    expect(users.with_tasks.by_name("Piotr").to_a).to eql(
      [{ id: 1, name: 'Piotr', tasks_id: 1, title: 'Finish ROM' }]
    )

    result = rom.relation(:users).map_with(:with_tasks)
      .all.with_tasks.by_name("Piotr").to_a

    expect(result).to eql(
      [{ id: 1, name: 'Piotr', tasks: [{ tasks_id: 1, title: 'Finish ROM' }] }]
    )
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-sql-0.4.1 spec/unit/one_to_many_spec.rb
rom-sql-0.4.0 spec/unit/one_to_many_spec.rb
rom-sql-0.4.0.rc1 spec/unit/one_to_many_spec.rb