Skip to content

Commit 0c952bb

Browse files
feat(ancestry): Add types for v5.0 (#1032)
* feat(ancestry): Add types for v5.0 Copied from 4.9 * feat(ancestry): Update class_methods types for v5.0 * feat(ancestry): Update has_ancestry types for v5.0 * feat(ancestry): Update instance_methods types for v5.0 * feat(ancestry): Update materialized_path types for v5.0 * feat(ancestry): Update materialized_path2 types for v5.0 * feat(ancestry): Update materialized_path_pg types for v5.0
1 parent e7a9964 commit 0c952bb

12 files changed

Lines changed: 547 additions & 0 deletions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# If the test needs gem's RBS files, declare them here.
2+
#
3+
additional_gems:
4+
- activerecord

gems/ancestry/5.0/_test/test.rb

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
require 'ancestry'
2+
3+
Ancestry.default_ancestry_format = :materialized_path2
4+
Ancestry.default_ancestry_format
5+
Ancestry.default_update_strategy = :sql
6+
Ancestry.default_update_strategy
7+
Ancestry.default_primary_key_format = '[-A-Fa-f0-9]{36}'
8+
Ancestry.default_primary_key_format
9+
10+
class Directory < ActiveRecord::Base
11+
has_ancestry
12+
13+
def rank
14+
1
15+
end
16+
end
17+
18+
class Group < ActiveRecord::Base
19+
has_ancestry ancestry_column: :ancestry, orphan_strategy: :rootify, touch: true, counter_cache: true, primary_key_format: '[-A-Fa-f0-9]{36}', update_strategy: :sql, ancestry_format: :materialized_path2
20+
end
21+
22+
class Category < ActiveRecord::Base
23+
has_ancestry orphan_strategy: :adopt, cache_depth: true, depth_cache_column: :ancestry_depth_cache, update_strategy: :ruby, ancestry_format: :materialized_path
24+
end
25+
26+
class Tag < ActiveRecord::Base
27+
has_ancestry orphan_strategy: :none
28+
end
29+
30+
class Item < ActiveRecord::Base
31+
has_ancestry cache_depth: 'item_depth'
32+
end
33+
34+
class Node < ActiveRecord::Base
35+
has_ancestry cache_depth: :virtual
36+
end
37+
38+
class MySerializer
39+
def initialize(parent, children:)
40+
@parent = parent
41+
@children = children
42+
end
43+
end
44+
45+
Category.build_ancestry_from_parent_ids!
46+
Category.check_ancestry_integrity!
47+
Category.rebuild_depth_cache!
48+
Category.rebuild_depth_cache_sql!
49+
50+
Group.rebuild_counter_cache!
51+
52+
Directory.arrange_nodes(Directory.all)
53+
Directory.arrange_nodes(Directory.all, orphan_strategy: :rootify)
54+
Directory.arrange_nodes(Directory.all, orphan_strategy: :destroy)
55+
Directory.arrange_nodes(Directory.roots.to_a, orphan_strategy: :rootify)
56+
57+
Directory.ancestry_depth_change('1/2', '1/2/3')
58+
Directory.ancestry_depth_change(nil, '1')
59+
60+
node = Directory.find(1)
61+
Directory.find(2).in_subtree_of?(node)
62+
63+
Directory.find(1).ancestors(from_depth: -6, to_depth: -4)
64+
Directory.find(1).descendants(from_depth: 2, to_depth: 4)
65+
Directory.find(1).path(from_depth: 3, to_depth: 4)
66+
Directory.find(1).subtree(from_depth: 10, to_depth: 12)
67+
68+
Directory.arrange(order: 'id desc')
69+
Directory.find_by!(name: 'Crunchy').subtree.arrange
70+
Directory.find_by!(name: 'Crunchy').subtree.arrange(order: :name)
71+
72+
Directory.roots.to_a
73+
Directory.ancestors_of(Directory.find(1).ancestors.first!).ancestors_of(1)
74+
Directory.children_of(Directory.find(1).children.first!).children_of(1)
75+
Directory.descendants_of(Directory.find(1).descendants.first!).descendants_of(1)
76+
Directory.indirects_of(Directory.find(1).indirects.first!).indirects_of(1)
77+
Directory.subtree_of(Directory.find(1).subtree.first!).subtree_of(1)
78+
Directory.siblings_of(Directory.find(1).siblings.first!).siblings_of(1)
79+
Directory.path_of(Directory.find(1).path.first!).path_of(1)
80+
81+
Directory.sort_by_ancestry(Directory.all.ordered_by_ancestry_and(id: :desc))
82+
Directory.sort_by_ancestry(Directory.all.ordered_by_ancestry_and(id: :desc).offset(3).take(2))
83+
Directory.sort_by_ancestry(Directory.all.ordered_by_ancestry_and(:rank))
84+
Directory.sort_by_ancestry(Directory.all.ordered_by_ancestry_and(:rank)) { |a, b| a.rank <=> b.rank }
85+
86+
Group.arrange_serializable(order: :name)
87+
Group.arrange_serializable { |parent, children| MySerializer.new(parent, children:) }
88+
Group.arrange_serializable do |parent, children|
89+
{
90+
my_parent: parent,
91+
my_children: children
92+
}
93+
end
94+
Group.arrange_serializable(order: 'id desc') do |parent, children|
95+
out = {} #: Hash[String, untyped]
96+
out['parent'] = parent
97+
out['children'] = children if children.count > 1
98+
out
99+
end

gems/ancestry/5.0/_test/test.rbs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
class ActiveRecord::Base
2+
extend Ancestry::HasAncestry
3+
end
4+
5+
class Directory < ActiveRecord::Base
6+
include Ancestry::InstanceMethods[Directory, ActiveRecord_Relation, Integer]
7+
extend Ancestry::ClassMethods[Directory, ActiveRecord_Relation, Integer]
8+
extend Ancestry::MaterializedPath[Directory, ActiveRecord_Relation, Integer]
9+
include Ancestry::MaterializedPath::InstanceMethods[Directory, Integer]
10+
extend ActiveRecord::Base::ClassMethods[Directory, ActiveRecord_Relation, Integer]
11+
12+
def rank: () -> Integer
13+
14+
class ActiveRecord_Relation < ::ActiveRecord::Relation
15+
include ::ActiveRecord::Relation::Methods[Directory, ::Integer]
16+
include Ancestry::ClassMethods[Directory, ActiveRecord_Relation, Integer]
17+
include Ancestry::MaterializedPath[Directory, ActiveRecord_Relation, Integer]
18+
end
19+
end
20+
21+
class Group < ActiveRecord::Base
22+
include Ancestry::InstanceMethods[Group, ActiveRecord_Relation, Integer]
23+
extend Ancestry::ClassMethods[Group, ActiveRecord_Relation, Integer]
24+
extend Ancestry::MaterializedPath2[Group, ActiveRecord_Relation, Integer]
25+
include Ancestry::MaterializedPathPg
26+
include Ancestry::MaterializedPath::InstanceMethods[Group, Integer]
27+
include Ancestry::MaterializedPath2::InstanceMethods[Integer]
28+
29+
class ActiveRecord_Relation < ::ActiveRecord::Relation
30+
include Ancestry::ClassMethods[Group, ActiveRecord_Relation, Integer]
31+
include Ancestry::MaterializedPath2[Directory, ActiveRecord_Relation, Integer]
32+
end
33+
end
34+
35+
class Category < ActiveRecord::Base
36+
include Ancestry::InstanceMethods[Category, ActiveRecord_Relation, Integer]
37+
extend Ancestry::ClassMethods[Category, ActiveRecord_Relation, Integer]
38+
extend Ancestry::MaterializedPath[Category, ActiveRecord_Relation, Integer]
39+
include Ancestry::MaterializedPath::InstanceMethods[Category, Integer]
40+
41+
class ActiveRecord_Relation < ::ActiveRecord::Relation
42+
include Ancestry::ClassMethods[Category, ActiveRecord_Relation, Integer]
43+
include Ancestry::MaterializedPath[Category, ActiveRecord_Relation, Integer]
44+
end
45+
end
46+
47+
class Tag < ActiveRecord::Base
48+
include Ancestry::InstanceMethods[Tag, ActiveRecord_Relation, Integer]
49+
extend Ancestry::ClassMethods[Tag, ActiveRecord_Relation, Integer]
50+
extend Ancestry::MaterializedPath[Tag, ActiveRecord_Relation, Integer]
51+
include Ancestry::MaterializedPath::InstanceMethods[Tag, Integer]
52+
53+
class ActiveRecord_Relation < ::ActiveRecord::Relation
54+
include Ancestry::ClassMethods[Tag, ActiveRecord_Relation, Integer]
55+
include Ancestry::MaterializedPath[Tag, ActiveRecord_Relation, Integer]
56+
end
57+
end
58+
59+
class Item < ActiveRecord::Base
60+
include Ancestry::InstanceMethods[Item, ActiveRecord_Relation, Integer]
61+
extend Ancestry::ClassMethods[Item, ActiveRecord_Relation, Integer]
62+
extend Ancestry::MaterializedPath[Item, ActiveRecord_Relation, Integer]
63+
include Ancestry::MaterializedPath::InstanceMethods[Item, Integer]
64+
65+
class ActiveRecord_Relation < ::ActiveRecord::Relation
66+
include Ancestry::ClassMethods[Item, ActiveRecord_Relation, Integer]
67+
include Ancestry::MaterializedPath[Item, ActiveRecord_Relation, Integer]
68+
end
69+
end
70+
71+
class Node < ActiveRecord::Base
72+
include Ancestry::InstanceMethods[Node, ActiveRecord_Relation, Integer]
73+
extend Ancestry::ClassMethods[Node, ActiveRecord_Relation, Integer]
74+
extend Ancestry::MaterializedPath[Node, ActiveRecord_Relation, Integer]
75+
include Ancestry::MaterializedPath::InstanceMethods[Node, Integer]
76+
77+
class ActiveRecord_Relation < ::ActiveRecord::Relation
78+
include Ancestry::ClassMethods[Node, ActiveRecord_Relation, Integer]
79+
include Ancestry::MaterializedPath[Node, ActiveRecord_Relation, Integer]
80+
end
81+
end
82+
83+
class MySerializer
84+
@parent: untyped
85+
@children: Array[untyped]
86+
87+
def initialize: (untyped parent, children: Array[untyped]) -> void
88+
end

gems/ancestry/5.0/ancestry.rbs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Ancestry
2+
type depth_options = Hash[:before_depth | :to_depth | :at_depth | :from_depth | :after_depth, Integer]
3+
4+
def self.default_update_strategy: () -> (:sql | :ruby)
5+
6+
def self.default_update_strategy=: (:sql | :ruby value) -> void
7+
8+
def self.default_ancestry_format: () -> (:materialized_path | :materialized_path2)
9+
10+
def self.default_ancestry_format=: (:materialized_path | :materialized_path2 value) -> void
11+
12+
def self.default_primary_key_format: () -> ('[0-9]+' | '[-A-Fa-f0-9]{36}')
13+
14+
def self.default_primary_key_format=: ('[0-9]+' | '[-A-Fa-f0-9]{36}' value) -> void
15+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Ancestry
2+
module ClassMethods[AncestryBaseClass, Relation, PrimaryKey]
3+
def to_node: (AncestryBaseClass | PrimaryKey object) -> AncestryBaseClass
4+
| (untyped object) -> AncestryBaseClass
5+
6+
def scope_depth: (depth_options depth_options, Integer depth) -> Relation
7+
8+
def arrange: (?Hash[untyped, untyped] options) -> Hash[AncestryBaseClass, Hash[AncestryBaseClass, untyped]]
9+
10+
def arrange_nodes: (Relation | Array[AncestryBaseClass] nodes, ?orphan_strategy: :rootify | :destroy) -> Hash[AncestryBaseClass, Hash[AncestryBaseClass, untyped]]
11+
12+
def flatten_arranged_nodes: (Hash[AncestryBaseClass, Hash[AncestryBaseClass, untyped]] arranged, ?Array[AncestryBaseClass] nodes) -> Array[AncestryBaseClass]
13+
14+
def arrange_serializable: [R] (?Hash[untyped, untyped] options, ?Hash[AncestryBaseClass, Hash[AncestryBaseClass, untyped]]? nodes) ?{ (AncestryBaseClass parent, Array[R]) -> R } -> Array[R]
15+
| (?Hash[untyped, untyped] options, ?Hash[AncestryBaseClass, Hash[AncestryBaseClass, untyped]]? nodes) -> Array[Hash[String, untyped]]
16+
17+
def tree_view: (String | Symbol column, ?Hash[AncestryBaseClass, Hash[AncestryBaseClass, untyped]]? data) -> void
18+
19+
def sort_by_ancestry: (Relation | Array[AncestryBaseClass] | Hash[AncestryBaseClass, Hash[AncestryBaseClass, untyped]]) ?{ (AncestryBaseClass a, AncestryBaseClass b) -> Integer? } -> Array[AncestryBaseClass]
20+
21+
def check_ancestry_integrity!: (?{ ?report: :list | :echo } options) -> void
22+
23+
def restore_ancestry_integrity!: () -> void
24+
25+
def build_ancestry_from_parent_ids!: (?String | Symbol column, ?PrimaryKey? parent_id, ?Array[PrimaryKey] ancestor_ids) -> void
26+
27+
def rebuild_depth_cache!: () -> void
28+
29+
def rebuild_depth_cache_sql!: () -> void
30+
31+
def rebuild_counter_cache!: () -> void
32+
33+
def unscoped_where: [R] () { (Relation scope) -> R } -> R
34+
35+
ANCESTRY_UNCAST_TYPES: [:string, :uuid, :text]
36+
37+
def primary_key_is_an_integer?: () -> bool
38+
end
39+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Ancestry
2+
class AncestryException < RuntimeError
3+
end
4+
5+
class AncestryIntegrityException < AncestryException
6+
end
7+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Ancestry
2+
module HasAncestry
3+
def has_ancestry: (?{
4+
?ancestry_column: String | Symbol,
5+
?orphan_strategy: :destroy | :rootify | :restrict | :adopt | :none,
6+
?cache_depth: bool | String | :virtual | Symbol,
7+
?depth_cache_column: String | Symbol,
8+
?touch: bool,
9+
?counter_cache: bool | String | Symbol,
10+
?primary_key_format: '[0-9]+' | '[-A-Fa-f0-9]{36}',
11+
?update_strategy: :sql | :ruby,
12+
?ancestry_format: :materialized_path | :materialized_path2 } options) -> void
13+
14+
def acts_as_tree: (*untyped args) -> void
15+
16+
def self.ancestry_format_module: (:materialized_path2 ancestry_format) -> singleton(MaterializedPath2)
17+
| (:materialized_path ancestry_format) -> singleton(MaterializedPath)
18+
| (nil ancestry_format) -> (singleton(MaterializedPath2) | singleton(MaterializedPath))
19+
end
20+
end

0 commit comments

Comments
 (0)