acts_as_sane_tree 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.require_path = 'lib'
12
12
  s.has_rdoc = true
13
13
  s.extra_rdoc_files = ['README.rdoc']
14
- s.add_dependency 'activerecord'
14
+ s.add_dependency 'activerecord', '>= 2.3'
15
15
  s.files = %w{
16
16
  acts_as_sane_tree.gemspec
17
17
  init.rb
@@ -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.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: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 1
9
- version: "1.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-25 00:00:00 -07:00
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: 3
28
+ hash: 5
29
29
  segments:
30
- - 0
31
- version: "0"
30
+ - 2
31
+ - 3
32
+ version: "2.3"
32
33
  type: :runtime
33
34
  version_requirements: *id001
34
35
  description: Sane ActiveRecord tree builder