acts_as_many_trees 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/acts_as_many_trees/base.rb +33 -23
- data/lib/acts_as_many_trees/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efa129f338ec6582601314bec7c0c121eed62ea1
|
4
|
+
data.tar.gz: e424e02dd6da7331ae1453c622be5b52ccb2c975
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4589e5f43d8d3897938598b08cfa217f300dd5a9dc1c59b843ff7490bd416e6be668cc6cfb1f2f0bdc428af9cbc373b715b873d21ae2fca80b2430763542c93
|
7
|
+
data.tar.gz: 0b5723cb07c17feccc683a08dbe531c433fa5aabfa1ca945231bfe4023a952fd0446a260e7b76107dec214c2bc5966b2893f590cc24c1ff6e9023b63737c7937
|
@@ -20,6 +20,11 @@ module ActsAsManyTrees
|
|
20
20
|
def hierarchy_table_name
|
21
21
|
hierarchy_class.table_name
|
22
22
|
end
|
23
|
+
|
24
|
+
def default_tree_name
|
25
|
+
''
|
26
|
+
end
|
27
|
+
|
23
28
|
end
|
24
29
|
module InstanceMethods
|
25
30
|
extend ActiveSupport::Concern
|
@@ -55,9 +60,9 @@ module ActsAsManyTrees
|
|
55
60
|
:source=>:item_siblings
|
56
61
|
}
|
57
62
|
|
58
|
-
scope :roots , ->(
|
63
|
+
scope :roots , ->(tree_name=self.default_tree_name){
|
59
64
|
on = Arel::Nodes::On.new(Arel::Nodes::Equality.new(arel_table[:id],hierarchy_class.arel_table[:descendant_id])
|
60
|
-
.and(hierarchy_class.arel_table[:hierarchy_scope].eq(
|
65
|
+
.and(hierarchy_class.arel_table[:hierarchy_scope].eq(tree_name))
|
61
66
|
.and(hierarchy_class.arel_table[:generation].not_eq(0))
|
62
67
|
)
|
63
68
|
outer_join = Arel::Nodes::OuterJoin.new(hierarchy_class.arel_table,on)
|
@@ -66,47 +71,52 @@ module ActsAsManyTrees
|
|
66
71
|
scope :not_this,->(this_id) { where.not(id: this_id)}
|
67
72
|
end
|
68
73
|
delegate :hierarchy_class, to: :class
|
74
|
+
#can be over-ridden in the instance
|
75
|
+
def default_tree_name
|
76
|
+
''
|
77
|
+
end
|
78
|
+
|
69
79
|
def parent=(inpt_parent)
|
70
80
|
if inpt_parent.is_a?(Hash)
|
71
81
|
new_parent=inpt_parent[:new_parent]
|
72
82
|
after_node=inpt_parent[:after_node]
|
73
83
|
before_node=inpt_parent[:before_node]
|
74
|
-
|
84
|
+
tree_name=inpt_parent[:tree_name] || ''
|
75
85
|
else
|
76
86
|
new_parent=inpt_parent
|
77
87
|
after_node=inpt_parent.children.last unless inpt_parent.nil?
|
78
88
|
before_node=inpt_parent.next_sibling unless inpt_parent.nil?
|
79
|
-
|
89
|
+
tree_name = inpt_parent ? inpt_parent.default_tree_name : self.default_tree_name
|
80
90
|
end
|
81
|
-
hierarchy_class.set_parent_of(self,new_parent,
|
91
|
+
hierarchy_class.set_parent_of(self,new_parent,tree_name,after_node,before_node)
|
82
92
|
end
|
83
93
|
|
84
|
-
def set_parent(new_parent,
|
85
|
-
hierarchy_class.set_parent_of(self,new_parent,
|
94
|
+
def set_parent(new_parent,tree_name=self.default_tree_name)
|
95
|
+
hierarchy_class.set_parent_of(self,new_parent,tree_name)
|
86
96
|
end
|
87
97
|
|
88
|
-
def add_child(new_child,
|
89
|
-
hierarchy_class.set_parent_of(new_child,self,
|
98
|
+
def add_child(new_child,tree_name=self.default_tree_name)
|
99
|
+
hierarchy_class.set_parent_of(new_child,self,tree_name)
|
90
100
|
end
|
91
101
|
|
92
|
-
def parent(
|
93
|
-
ancestors(
|
102
|
+
def parent(tree_name=self.default_tree_name)
|
103
|
+
ancestors(tree_name).where('generation=1').first
|
94
104
|
end
|
95
105
|
|
96
|
-
def children(
|
97
|
-
descendants(
|
106
|
+
def children(tree_name=self.default_tree_name)
|
107
|
+
descendants(tree_name).where('generation=1')
|
98
108
|
end
|
99
109
|
|
100
|
-
def self_and_ancestors(
|
101
|
-
unscoped_ancestors.merge(hierarchy_class.scope_hierarchy(
|
110
|
+
def self_and_ancestors(tree_name=self.default_tree_name)
|
111
|
+
unscoped_ancestors.merge(hierarchy_class.scope_hierarchy(tree_name))
|
102
112
|
end
|
103
113
|
|
104
|
-
def ancestors(
|
105
|
-
self_and_ancestors(
|
114
|
+
def ancestors(tree_name=self.default_tree_name)
|
115
|
+
self_and_ancestors(tree_name).not_this(self.id)
|
106
116
|
end
|
107
117
|
|
108
|
-
def self_and_descendants(
|
109
|
-
unscoped_descendants.merge(hierarchy_class.scope_hierarchy(
|
118
|
+
def self_and_descendants(tree_name=self.default_tree_name)
|
119
|
+
unscoped_descendants.merge(hierarchy_class.scope_hierarchy(tree_name))
|
110
120
|
end
|
111
121
|
|
112
122
|
def siblings
|
@@ -121,12 +131,12 @@ module ActsAsManyTrees
|
|
121
131
|
siblings_after.first
|
122
132
|
end
|
123
133
|
|
124
|
-
def descendants(
|
125
|
-
self_and_descendants(
|
134
|
+
def descendants(tree_name=self.default_tree_name)
|
135
|
+
self_and_descendants(tree_name).not_this(self.id)
|
126
136
|
end
|
127
137
|
|
128
|
-
def position(
|
129
|
-
unscoped_ancestor_links.where(ancestor_id: id,hierarchy_scope:
|
138
|
+
def position(tree_name=self.default_tree_name)
|
139
|
+
unscoped_ancestor_links.where(ancestor_id: id,hierarchy_scope: tree_name).first.position
|
130
140
|
end
|
131
141
|
end
|
132
142
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_many_trees
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Small
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|