File: shared_array_scope_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 (177 lines) | stat: -rw-r--r-- 7,481 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# frozen_string_literal: true

module Shared
  module ArrayScopeList
    def setup
      (1..4).each { |counter| ArrayScopeListMixin.create! pos: counter, parent_id: 5, parent_type: 'ParentClass' }
      (1..4).each { |counter| ArrayScopeListMixin.create! pos: counter, parent_id: 6, parent_type: 'ParentClass' }
    end

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

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

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

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

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

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

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

      ArrayScopeListMixin.where(id: 4).first.insert_at(4)
      assert_equal [1, 3, 2, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
      assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:pos)
    end

    def test_move_to_bottom_with_next_to_last_item
      assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
      ArrayScopeListMixin.where(id: 3).first.move_to_bottom
      assert_equal [1, 2, 4, 3], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
    end

    def test_next_prev
      assert_equal ArrayScopeListMixin.where(id: 2).first, ArrayScopeListMixin.where(id: 1).first.lower_item
      assert_nil ArrayScopeListMixin.where(id: 1).first.higher_item
      assert_equal ArrayScopeListMixin.where(id: 3).first, ArrayScopeListMixin.where(id: 4).first.higher_item
      assert_nil ArrayScopeListMixin.where(id: 4).first.lower_item
    end

    def test_injection
      item = ArrayScopeListMixin.new(parent_id: 1, parent_type: 'ParentClass')
      assert_equal "pos", item.position_column
    end

    def test_insert
      new = ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass')
      assert_equal 1, new.pos
      assert new.first?
      assert new.last?

      new = ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass')
      assert_equal 2, new.pos
      assert !new.first?
      assert new.last?

      new = ArrayScopeListMixin.acts_as_list_no_update { ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass') }
      assert_equal_or_nil $default_position,new.pos
      assert_equal $default_position.is_a?(Integer), new.first?
      assert !new.last?

      new = ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass')
      assert_equal 3, new.pos
      assert !new.first?
      assert new.last?

      new = ArrayScopeListMixin.create(parent_id: 0, parent_type: 'ParentClass')
      assert_equal 1, new.pos
      assert new.first?
      assert new.last?
    end

    def test_insert_at
      new = ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass')
      assert_equal 1, new.pos

      new = ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass')
      assert_equal 2, new.pos

      new = ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass')
      assert_equal 3, new.pos

      new_noup = ArrayScopeListMixin.acts_as_list_no_update { ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass') }
      assert_equal_or_nil $default_position,new_noup.pos

      new4 = ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass')
      assert_equal 4, new4.pos

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

      new.reload
      assert_equal 4, new.pos

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

      new4.reload
      assert_equal 4, new4.pos

      new5 = ArrayScopeListMixin.create(parent_id: 20, parent_type: 'ParentClass')
      assert_equal 5, new5.pos

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

      new4.reload
      assert_equal 5, new4.pos

      new_noup.reload
      assert_equal_or_nil $default_position, new_noup.pos
    end

    def test_delete_middle
      assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)

      ArrayScopeListMixin.where(id: 2).first.destroy

      assert_equal [1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)

      assert_equal 1, ArrayScopeListMixin.where(id: 1).first.pos
      assert_equal 2, ArrayScopeListMixin.where(id: 3).first.pos
      assert_equal 3, ArrayScopeListMixin.where(id: 4).first.pos

      ArrayScopeListMixin.where(id: 1).first.destroy

      assert_equal [3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)

      assert_equal 1, ArrayScopeListMixin.where(id: 3).first.pos
      assert_equal 2, ArrayScopeListMixin.where(id: 4).first.pos

      ArrayScopeListMixin.acts_as_list_no_update { ArrayScopeListMixin.where(id: 3).first.destroy }

      assert_equal [4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)

      assert_equal 2, ArrayScopeListMixin.where(id: 4).first.pos
    end

    def test_remove_from_list_should_then_fail_in_list?
      assert_equal true, ArrayScopeListMixin.where(id: 1).first.in_list?
      ArrayScopeListMixin.where(id: 1).first.remove_from_list
      assert_equal false, ArrayScopeListMixin.where(id: 1).first.in_list?
    end

    def test_remove_from_list_should_set_position_to_nil
      assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)

      ArrayScopeListMixin.where(id: 2).first.remove_from_list

      assert_equal 1,   ArrayScopeListMixin.where(id: 1).first.pos
      assert_nil        ArrayScopeListMixin.where(id: 2).first.pos
      assert_equal 2,   ArrayScopeListMixin.where(id: 3).first.pos
      assert_equal 3,   ArrayScopeListMixin.where(id: 4).first.pos
    end

    def test_remove_before_destroy_does_not_shift_lower_items_twice
      assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)

      ArrayScopeListMixin.where(id: 2).first.remove_from_list
      ArrayScopeListMixin.where(id: 2).first.destroy

      assert_equal [1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)

      assert_equal 1, ArrayScopeListMixin.where(id: 1).first.pos
      assert_equal 2, ArrayScopeListMixin.where(id: 3).first.pos
      assert_equal 3, ArrayScopeListMixin.where(id: 4).first.pos
    end
  end
end