acts_as_sane_tree 1.1 → 1.2
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.
- data/CHANGELOG.rdoc +4 -0
- data/acts_as_sane_tree.gemspec +1 -1
- data/lib/acts_as_sane_tree.rb +9 -3
- data/lib/version.rb +2 -2
- metadata +8 -7
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
=== v1.2
|
2
|
+
* Allow nodes_and_descendents to accept no base nodes resulting in complete build of all trees
|
3
|
+
* Add default max depth to catch runaway recursions (can be turned off by setting :max_depth to nil)
|
4
|
+
|
1
5
|
=== v1.1
|
2
6
|
* New class method nodes_and_descendents
|
3
7
|
* Updated instance method #descendents to call class method nodes_and_descendents
|
data/acts_as_sane_tree.gemspec
CHANGED
data/lib/acts_as_sane_tree.rb
CHANGED
@@ -41,11 +41,15 @@ module ActsAsSaneTree
|
|
41
41
|
# * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
|
42
42
|
# * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
|
43
43
|
def acts_as_sane_tree(options = {})
|
44
|
-
configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil }
|
44
|
+
configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil, :max_depth => 10000 }
|
45
45
|
configuration.update(options) if options.is_a?(Hash)
|
46
46
|
|
47
47
|
belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
|
48
48
|
has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
|
49
|
+
|
50
|
+
validates_each configuration[:foreign_key] do |record, attr, value|
|
51
|
+
record.errors.add attr, 'Cannot be own parent.' if !record.id.nil? && value.to_i == record.id.to_i
|
52
|
+
end
|
49
53
|
|
50
54
|
class_eval <<-EOV
|
51
55
|
include ActsAsSaneTree::InstanceMethods
|
@@ -86,6 +90,7 @@ module ActsAsSaneTree
|
|
86
90
|
SELECT #{self.table_name}.*, 0 AS level FROM #{self.table_name} WHERE id in (\#{s.join(', ')})
|
87
91
|
UNION ALL
|
88
92
|
SELECT alias1.*, crumbs.level + 1 FROM crumbs JOIN #{self.table_name} alias1 on alias1.parent_id = crumbs.id
|
93
|
+
#{configuration[:max_depth] ? "WHERE crumbs.level + 1 < #{configuration[:max_depth].to_i}" : ''}
|
89
94
|
) SELECT * FROM crumbs WHERE id in (\#{c.join(', ')})"
|
90
95
|
)
|
91
96
|
end
|
@@ -99,10 +104,11 @@ module ActsAsSaneTree
|
|
99
104
|
args.delete(depth)
|
100
105
|
depth = depth[:depth]
|
101
106
|
end
|
107
|
+
depth = #{configuration[:max_depth]} unless depth
|
102
108
|
base_ids = args.map{|x| x.is_a?(ActiveRecord::Base) ? x.id : x.to_i}
|
103
109
|
q = self.find_by_sql(
|
104
110
|
"WITH RECURSIVE crumbs AS (
|
105
|
-
SELECT #{self.table_name}.*, \#{no_self ? -1 : 0} AS level FROM #{self.table_name} WHERE id in (\#{base_ids.join(', ')})
|
111
|
+
SELECT #{self.table_name}.*, \#{no_self ? -1 : 0} AS level FROM #{self.table_name} WHERE \#{base_ids.empty? ? 'parent_id IS NULL' : "id in (\#{base_ids.join(', ')})"}
|
106
112
|
UNION ALL
|
107
113
|
SELECT alias1.*, crumbs.level + 1 FROM crumbs JOIN #{self.table_name} alias1 on alias1.parent_id = crumbs.id
|
108
114
|
\#{depth ? "WHERE crumbs.level + 1 < \#{depth.to_i}" : ''}
|
@@ -143,7 +149,7 @@ module ActsAsSaneTree
|
|
143
149
|
level + 1
|
144
150
|
FROM crumbs
|
145
151
|
JOIN #{self.class.table_name} alias1 ON alias1.id = crumbs.parent_id
|
146
|
-
) SELECT * FROM crumbs ORDER BY level DESC"
|
152
|
+
) SELECT * FROM crumbs WHERE id != #{id} ORDER BY level DESC"
|
147
153
|
end
|
148
154
|
|
149
155
|
# Returns the root node of the tree.
|
data/lib/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module ActsAsSaneTree
|
2
|
-
VERSION = '1.
|
3
|
-
end
|
2
|
+
VERSION = '1.2'
|
3
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_sane_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: "1.
|
8
|
+
- 2
|
9
|
+
version: "1.2"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Chris Roberts
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10
|
17
|
+
date: 2010-11-10 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -25,10 +25,11 @@ dependencies:
|
|
25
25
|
requirements:
|
26
26
|
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
hash:
|
28
|
+
hash: 5
|
29
29
|
segments:
|
30
|
-
-
|
31
|
-
|
30
|
+
- 2
|
31
|
+
- 3
|
32
|
+
version: "2.3"
|
32
33
|
type: :runtime
|
33
34
|
version_requirements: *id001
|
34
35
|
description: Sane ActiveRecord tree builder
|