File: shared_zero_based.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 (104 lines) | stat: -rw-r--r-- 2,990 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

module Shared
  module ZeroBased
    def setup
       (1..4).each { |counter| ZeroBasedMixin.create! pos: counter, parent_id: 5 }
    end

    def test_insert
      new = ZeroBasedMixin.create(parent_id: 20)
      assert_equal 0, new.pos
      assert new.first?
      assert new.last?

      new = ZeroBasedMixin.create(parent_id: 20)
      assert_equal 1, new.pos
      assert !new.first?
      assert new.last?

      new = ZeroBasedMixin.acts_as_list_no_update { ZeroBasedMixin.create(parent_id: 20) }
      assert_equal_or_nil $default_position, new.pos
      assert !new.first?
      assert !new.last?

      new = ZeroBasedMixin.create(parent_id: 20)
      assert_equal 2, new.pos
      assert !new.first?
      assert new.last?

      new = ZeroBasedMixin.create(parent_id: 0)
      assert_equal 0, new.pos
      assert new.first?
      assert new.last?

      new = ZeroBasedMixin.create(parent_id: 1, pos: -500)
      assert_equal 0, new.pos
      assert new.first?
      assert new.last?
    end

    def test_reordering
      assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)

      ListMixin.where(id: 2).first.move_lower
      assert_equal [1, 3, 2, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)

      ListMixin.where(id: 2).first.move_higher
      assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)

      ListMixin.where(id: 1).first.move_to_bottom
      assert_equal [2, 3, 4, 1], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)

      ListMixin.where(id: 1).first.move_to_top
      assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)

      ListMixin.where(id: 2).first.move_to_bottom
      assert_equal [1, 3, 4, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)

      ListMixin.where(id: 4).first.move_to_top
      assert_equal [4, 1, 3, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
    end

    def test_insert_at
      new = ZeroBasedMixin.create(parent_id: 20)
      assert_equal 0, new.pos

      new = ZeroBasedMixin.create(parent_id: 20)
      assert_equal 1, new.pos

      new = ZeroBasedMixin.create(parent_id: 20)
      assert_equal 2, new.pos

      new_noup = ZeroBasedMixin.acts_as_list_no_update { ZeroBasedMixin.create(parent_id: 20) }
      assert_equal_or_nil $default_position, new_noup.pos

      new4 = ZeroBasedMixin.create(parent_id: 20)
      assert_equal 3, new4.pos

      new4.insert_at(2)
      assert_equal 2, new4.pos

      new.reload
      assert_equal 3, new.pos

      new.insert_at(2)
      assert_equal 2, new.pos

      new4.reload
      assert_equal 3, new4.pos

      new5 = ListMixin.create(parent_id: 20)
      assert_equal 4, new5.pos

      new5.insert_at(1)
      assert_equal 1, new5.pos

      new4.reload
      assert_equal 4, new4.pos

      new_noup.reload
      assert_equal_or_nil $default_position, new_noup.pos
    end
  end
end