acts_as_list 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,110 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Shared
4
- module TopAddition
5
- def setup
6
- (1..4).each { |counter| TopAdditionMixin.create! pos: counter, parent_id: 5 }
7
- end
8
-
9
- def test_setup_state
10
- # If we explicitly define a position (as above) then that position is what gets applied
11
- assert_equal [1, 2, 3, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
12
- end
13
-
14
- def test_reordering
15
- TopAdditionMixin.where(id: 2).first.move_lower
16
- assert_equal [1, 3, 2, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
17
-
18
- TopAdditionMixin.where(id: 2).first.move_higher
19
- assert_equal [1, 2, 3, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
20
-
21
- TopAdditionMixin.where(id: 1).first.move_to_bottom
22
- assert_equal [2, 3, 4, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
23
-
24
- TopAdditionMixin.where(id: 1).first.move_to_top
25
- assert_equal [1, 2, 3, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
26
-
27
- TopAdditionMixin.where(id: 2).first.move_to_bottom
28
- assert_equal [1, 3, 4, 2], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
29
-
30
- TopAdditionMixin.where(id: 4).first.move_to_top
31
- assert_equal [4, 1, 3, 2], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
32
- end
33
-
34
- def test_injection
35
- item = TopAdditionMixin.new(parent_id: 1)
36
- assert_equal({ parent_id: 1 }, item.scope_condition)
37
- assert_equal "pos", item.position_column
38
- end
39
-
40
- def test_insert
41
- new = TopAdditionMixin.create(parent_id: 20)
42
- assert_equal 1, new.pos
43
- assert new.first?
44
- assert new.last?
45
-
46
- new = TopAdditionMixin.create(parent_id: 20)
47
- assert_equal 1, new.pos
48
- assert new.first?
49
- assert !new.last?
50
-
51
- new = TopAdditionMixin.acts_as_list_no_update { TopAdditionMixin.create(parent_id: 20) }
52
- assert_equal_or_nil $default_position, new.pos
53
- assert_equal $default_position.is_a?(Integer), new.first?
54
- assert !new.last?
55
-
56
- new = TopAdditionMixin.create(parent_id: 20)
57
- assert_equal 1, new.pos
58
- assert_equal $default_position.nil?, new.first?
59
- assert !new.last?
60
-
61
- new = TopAdditionMixin.create(parent_id: 0)
62
- assert_equal 1, new.pos
63
- assert new.first?
64
- assert new.last?
65
- end
66
-
67
- def test_insert_at
68
- new = TopAdditionMixin.create(parent_id: 20)
69
- assert_equal 1, new.pos
70
-
71
- new = TopAdditionMixin.create(parent_id: 20)
72
- assert_equal 1, new.pos
73
-
74
- new = TopAdditionMixin.create(parent_id: 20)
75
- assert_equal 1, new.pos
76
-
77
- new = TopAdditionMixin.acts_as_list_no_update { TopAdditionMixin.create(parent_id: 20) }
78
- assert_equal_or_nil $default_position, new.pos
79
-
80
- new4 = TopAdditionMixin.create(parent_id: 20)
81
- assert_equal 1, new4.pos
82
-
83
- new4.insert_at(3)
84
- assert_equal 3, new4.pos
85
- end
86
-
87
- def test_supplied_position
88
- new = TopAdditionMixin.create(parent_id: 20, pos: 3)
89
- assert_equal 3, new.pos
90
- end
91
-
92
- def test_delete_middle
93
- TopAdditionMixin.where(id: 2).first.destroy
94
-
95
- assert_equal [1, 3, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
96
-
97
- assert_equal 1, TopAdditionMixin.where(id: 1).first.pos
98
- assert_equal 2, TopAdditionMixin.where(id: 3).first.pos
99
- assert_equal 3, TopAdditionMixin.where(id: 4).first.pos
100
-
101
- TopAdditionMixin.acts_as_list_no_update { TopAdditionMixin.where(id: 3).first.destroy }
102
-
103
- assert_equal [1, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
104
-
105
- assert_equal 1, TopAdditionMixin.where(id: 1).first.pos
106
- assert_equal 3, TopAdditionMixin.where(id: 4).first.pos
107
- end
108
-
109
- end
110
- end
@@ -1,104 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Shared
4
- module ZeroBased
5
- def setup
6
- (1..4).each { |counter| ZeroBasedMixin.create! pos: counter, parent_id: 5 }
7
- end
8
-
9
- def test_insert
10
- new = ZeroBasedMixin.create(parent_id: 20)
11
- assert_equal 0, new.pos
12
- assert new.first?
13
- assert new.last?
14
-
15
- new = ZeroBasedMixin.create(parent_id: 20)
16
- assert_equal 1, new.pos
17
- assert !new.first?
18
- assert new.last?
19
-
20
- new = ZeroBasedMixin.acts_as_list_no_update { ZeroBasedMixin.create(parent_id: 20) }
21
- assert_equal_or_nil $default_position, new.pos
22
- assert !new.first?
23
- assert !new.last?
24
-
25
- new = ZeroBasedMixin.create(parent_id: 20)
26
- assert_equal 2, new.pos
27
- assert !new.first?
28
- assert new.last?
29
-
30
- new = ZeroBasedMixin.create(parent_id: 0)
31
- assert_equal 0, new.pos
32
- assert new.first?
33
- assert new.last?
34
-
35
- new = ZeroBasedMixin.create(parent_id: 1, pos: -500)
36
- assert_equal 0, new.pos
37
- assert new.first?
38
- assert new.last?
39
- end
40
-
41
- def test_reordering
42
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
43
-
44
- ListMixin.where(id: 2).first.move_lower
45
- assert_equal [1, 3, 2, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
46
-
47
- ListMixin.where(id: 2).first.move_higher
48
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
49
-
50
- ListMixin.where(id: 1).first.move_to_bottom
51
- assert_equal [2, 3, 4, 1], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
52
-
53
- ListMixin.where(id: 1).first.move_to_top
54
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
55
-
56
- ListMixin.where(id: 2).first.move_to_bottom
57
- assert_equal [1, 3, 4, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
58
-
59
- ListMixin.where(id: 4).first.move_to_top
60
- assert_equal [4, 1, 3, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
61
- end
62
-
63
- def test_insert_at
64
- new = ZeroBasedMixin.create(parent_id: 20)
65
- assert_equal 0, new.pos
66
-
67
- new = ZeroBasedMixin.create(parent_id: 20)
68
- assert_equal 1, new.pos
69
-
70
- new = ZeroBasedMixin.create(parent_id: 20)
71
- assert_equal 2, new.pos
72
-
73
- new_noup = ZeroBasedMixin.acts_as_list_no_update { ZeroBasedMixin.create(parent_id: 20) }
74
- assert_equal_or_nil $default_position, new_noup.pos
75
-
76
- new4 = ZeroBasedMixin.create(parent_id: 20)
77
- assert_equal 3, new4.pos
78
-
79
- new4.insert_at(2)
80
- assert_equal 2, new4.pos
81
-
82
- new.reload
83
- assert_equal 3, new.pos
84
-
85
- new.insert_at(2)
86
- assert_equal 2, new.pos
87
-
88
- new4.reload
89
- assert_equal 3, new4.pos
90
-
91
- new5 = ListMixin.create(parent_id: 20)
92
- assert_equal 4, new5.pos
93
-
94
- new5.insert_at(1)
95
- assert_equal 1, new5.pos
96
-
97
- new4.reload
98
- assert_equal 4, new4.pos
99
-
100
- new_noup.reload
101
- assert_equal_or_nil $default_position, new_noup.pos
102
- end
103
- end
104
- end
@@ -1,20 +0,0 @@
1
- sqlite:
2
- adapter: sqlite3
3
- database: file::memory:?cache=shared
4
-
5
- mysql:
6
- adapter: mysql2
7
- database: runner
8
- pool: 5
9
- timeout: 5000
10
- username: root
11
- password: root
12
-
13
- postgresql:
14
- adapter: postgresql
15
- database: runner
16
- pool: 5
17
- timeout: 5000
18
- username: runner
19
- password:
20
- min_messages: ERROR
@@ -1,18 +0,0 @@
1
- sqlite:
2
- adapter: sqlite3
3
- database: file::memory:?cache=shared
4
-
5
- mysql:
6
- adapter: mysql2
7
- host: 127.0.0.1
8
- username: root
9
- password:
10
- database: acts_as_list
11
-
12
- postgresql:
13
- adapter: postgresql
14
- host: localhost
15
- username: postgres
16
- password: postgres
17
- database: acts_as_list
18
- min_messages: ERROR
@@ -1,33 +0,0 @@
1
- require 'helper'
2
-
3
- class Animal < ActiveRecord::Base
4
- acts_as_list
5
- default_scope -> { select(:name) }
6
- end
7
-
8
- class DefaultScopeWithSelectTest < Minitest::Test
9
- def setup
10
- ActiveRecord::Base.connection.create_table :animals do |t|
11
- t.column :position, :integer
12
- t.column :name, :string
13
- end
14
-
15
- ActiveRecord::Base.connection.schema_cache.clear!
16
- Animal.reset_column_information
17
- super
18
- end
19
-
20
- def teardown
21
- teardown_db
22
- super
23
- end
24
-
25
- def test_default_scope_with_select
26
- animal1 = Animal.create name: 'Fox'
27
- animal2 = Animal.create name: 'Panda'
28
- animal3 = Animal.create name: 'Wildebeast'
29
- assert_equal 1, animal1.position
30
- assert_equal 2, animal2.position
31
- assert_equal 3, animal3.position
32
- end
33
- end
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'helper'
4
-
5
- class Section < ActiveRecord::Base
6
- has_many :items
7
- acts_as_list
8
-
9
- scope :visible, -> { where(visible: true) }
10
- end
11
-
12
- class Item < ActiveRecord::Base
13
- belongs_to :section
14
- acts_as_list scope: :section
15
-
16
- scope :visible, -> { where(visible: true).joins(:section).merge(Section.visible) }
17
- end
18
-
19
- class JoinedTestCase < Minitest::Test
20
- def setup
21
- ActiveRecord::Base.connection.create_table :sections do |t|
22
- t.column :position, :integer
23
- t.column :visible, :boolean, default: true
24
- end
25
-
26
- ActiveRecord::Base.connection.create_table :items do |t|
27
- t.column :position, :integer
28
- t.column :section_id, :integer
29
- t.column :visible, :boolean, default: true
30
- end
31
-
32
- ActiveRecord::Base.connection.schema_cache.clear!
33
- [Section, Item].each(&:reset_column_information)
34
- super
35
- end
36
-
37
- def teardown
38
- teardown_db
39
- super
40
- end
41
- end
42
-
43
- # joining the relation returned by `#higher_items` or `#lower_items` to another table
44
- # previously could result in ambiguous column names in the query
45
- class TestHigherLowerItems < JoinedTestCase
46
- def test_higher_items
47
- section = Section.create
48
- item1 = Item.create section: section
49
- item2 = Item.create section: section
50
- item3 = Item.create section: section
51
- assert_equal item3.higher_items.visible, [item2, item1]
52
- end
53
-
54
- def test_lower_items
55
- section = Section.create
56
- item1 = Item.create section: section
57
- item2 = Item.create section: section
58
- item3 = Item.create section: section
59
- assert_equal item1.lower_items.visible, [item2, item3]
60
- end
61
- end