Sha256: 203cb8b51de9344519e68e343f23842e294848dec7bac600e9f11cb430d4df66

Contents?: true

Size: 1.36 KB

Versions: 14

Compression:

Stored size: 1.36 KB

Contents

require 'spec_helper'

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

  before do
    conn[:tasks].insert id: 2, user_id: 1, title: 'Go to sleep'
  end

  it 'extends relation with association methods' do
    setup.relation(:tasks) do
      many_to_many :tags,
        join_table: :task_tags,
        left_key: :task_id,
        right_key: :tag_id

      def with_tags
        association_left_join(:tags, select: [:name])
      end

      def with_tags_and_tag_id
        association_left_join(:tags, select: {
                                tags: [:name], task_tags: [:tag_id]
                              })
      end

      def by_tag(name)
        with_tags.where(name: name)
      end

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

    setup.relation(:tags)

    tasks = rom.relations.tasks

    expect(tasks.all.with_tags.to_a).to eql([
      { id: 1, title: 'Finish ROM', name: 'important' },
      { id: 2, title: 'Go to sleep', name: nil }
    ])

    expect(tasks.all.with_tags_and_tag_id.to_a).to eql([
      { id: 1, title: 'Finish ROM', tag_id: 1, name: 'important' },
      { id: 2, title: 'Go to sleep', tag_id: nil, name: nil }
    ])

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

    expect(tasks.by_tag("not-here").to_a).to be_empty
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rom-sql-0.6.1 spec/unit/many_to_many_spec.rb
rom-sql-0.6.0 spec/unit/many_to_many_spec.rb
rom-sql-0.6.0.rc1 spec/unit/many_to_many_spec.rb
rom-sql-0.6.0.beta1 spec/unit/many_to_many_spec.rb
rom-sql-0.5.3 spec/unit/many_to_many_spec.rb
rom-sql-0.5.2 spec/unit/many_to_many_spec.rb
rom-sql-0.5.1 spec/unit/many_to_many_spec.rb
rom-sql-0.5.0 spec/unit/many_to_many_spec.rb
rom-sql-0.4.3 spec/unit/many_to_many_spec.rb
rom-sql-0.4.1 spec/unit/many_to_many_spec.rb
rom-sql-0.4.0 spec/unit/many_to_many_spec.rb
rom-sql-0.4.0.rc1 spec/unit/many_to_many_spec.rb
rom-sql-0.4.0.beta2 spec/unit/many_to_many_spec.rb
rom-sql-0.4.0.beta1 spec/unit/many_to_many_spec.rb