Sha256: 7a262fe68eecb3d47ac07e0a128dba3892bcdd876fea5867f155c64c76d1e1b4

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require 'spec_helper'

describe 'Relation registration DSL' do
  include_context 'users and tasks'

  it 'allows to expose chainable relations' do
    setup.relation(:tasks) do
      def high_priority
        restrict { |tuple| tuple[:priority] < 2 }
      end

      def by_title(title)
        restrict(title: title)
      end
    end

    setup.relation(:users) do
      include ROM::RA

      def with_tasks
        join(tasks)
      end
    end

    tasks = rom.relations.tasks

    expect(tasks.class.name).to eql("ROM::Relation[Tasks]")
    expect(tasks.high_priority.inspect).to include("#<ROM::Relation[Tasks]")

    expect(tasks.high_priority.by_title("be nice")).to match_array(
      [name: "Joe", title: "be nice", priority: 1]
    )

    expect(tasks.by_title("be cool")).to match_array(
      [name: "Jane", title: "be cool", priority: 2]
    )

    users = rom.relations.users

    expect(users.with_tasks.to_a).to eql(
      [{ name: "Joe", email: "joe@doe.org", title: "be nice", priority: 1 },
       { name: "Joe", email: "joe@doe.org", title: "sleep well", priority: 2 },
       { name: "Jane", email: "jane@doe.org", title: "be cool", priority: 2 }]
    )
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.4.2 spec/integration/relations/registry_dsl_spec.rb