File: test_joined_list.rb

package info (click to toggle)
ruby-acts-as-list 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 388 kB
  • sloc: ruby: 2,493; makefile: 2
file content (61 lines) | stat: -rw-r--r-- 1,601 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

require 'helper'

class Section < ActiveRecord::Base
  has_many :items
  acts_as_list

  scope :visible, -> { where(visible: true) }
end

class Item < ActiveRecord::Base
  belongs_to :section
  acts_as_list scope: :section

  scope :visible, -> { where(visible: true).joins(:section).merge(Section.visible) }
end

class JoinedTestCase < Minitest::Test
  def setup
    ActiveRecord::Base.connection.create_table :sections do |t|
      t.column :position, :integer
      t.column :visible, :boolean, default: true
    end

    ActiveRecord::Base.connection.create_table :items do |t|
      t.column :position, :integer
      t.column :section_id, :integer
      t.column :visible, :boolean, default: true
    end

    ActiveRecord::Base.connection.schema_cache.clear!
    [Section, Item].each(&:reset_column_information)
    super
  end

  def teardown
    teardown_db
    super
  end
end

# joining the relation returned by `#higher_items` or `#lower_items` to another table
# previously could result in ambiguous column names in the query
class TestHigherLowerItems < JoinedTestCase
  def test_higher_items
    section = Section.create
    item1 = Item.create section: section
    item2 = Item.create section: section
    item3 = Item.create section: section
    assert_equal item3.higher_items.visible, [item2, item1]
  end

  def test_lower_items
    section = Section.create
    item1 = Item.create section: section
    item2 = Item.create section: section
    item3 = Item.create section: section
    assert_equal item1.lower_items.visible, [item2, item3]
  end
end