Sha256: f0cf63129b6066472b8dcf33ffd8a3fcc7e7692e91a98455eafeba4f7003c9fe

Contents?: true

Size: 1.8 KB

Versions: 8

Compression:

Stored size: 1.8 KB

Contents

require 'spec_helper'

describe 'Defining one-to-many 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'

    setup.mappers do
      define(:users)

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

  it 'extends relation with association methods' do
    setup.relation(:users) do
      one_to_many :tasks, key: :user_id, on: { title: 'Finish ROM' }

      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

    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

  it 'allows setting :conditions' do
    setup.relation(:users) do
      one_to_many :piotrs_tasks, relation: :tasks, key: :user_id,
        conditions: { name: 'Piotr' }

      def with_piotrs_tasks
        association_left_join(:piotrs_tasks, select: [:id, :title])
      end

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

    users = rom.relations.users

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

    result = rom.relation(:users).map_with(:with_tasks)
      .all.with_piotrs_tasks.to_a

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rom-sql-0.6.1 spec/unit/one_to_many_spec.rb
rom-sql-0.6.0 spec/unit/one_to_many_spec.rb
rom-sql-0.6.0.rc1 spec/unit/one_to_many_spec.rb
rom-sql-0.6.0.beta1 spec/unit/one_to_many_spec.rb
rom-sql-0.5.3 spec/unit/one_to_many_spec.rb
rom-sql-0.5.2 spec/unit/one_to_many_spec.rb
rom-sql-0.5.1 spec/unit/one_to_many_spec.rb
rom-sql-0.5.0 spec/unit/one_to_many_spec.rb