acts_as_sane_tree 1.2 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ === v1.3
2
+ * Allow restricting results to given depth
3
+ * Load properly within rails if provided via rubygems (bundler)
4
+
1
5
  === v1.2
2
6
  * Allow nodes_and_descendents to accept no base nodes resulting in complete build of all trees
3
7
  * Add default max depth to catch runaway recursions (can be turned off by setting :max_depth to nil)
@@ -7,4 +11,4 @@
7
11
  * Updated instance method #descendents to call class method nodes_and_descendents
8
12
 
9
13
  === v1.0
10
- * Initial release
14
+ * Initial release
@@ -1,5 +1,5 @@
1
1
  $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/'
2
- require 'lib/version'
2
+ require 'acts_as_sane_tree/version'
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'acts_as_sane_tree'
5
5
  s.version = ActsAsSaneTree::VERSION
@@ -11,13 +11,13 @@ 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', '>= 2.3'
14
+ s.add_dependency 'activerecord', '> 0'
15
15
  s.files = %w{
16
16
  acts_as_sane_tree.gemspec
17
17
  init.rb
18
18
  README.rdoc
19
19
  CHANGELOG.rdoc
20
20
  lib/acts_as_sane_tree.rb
21
- lib/version.rb
21
+ lib/acts_as_sane_tree/version.rb
22
22
  }
23
23
  end
@@ -1,3 +1,5 @@
1
+ require 'active_record'
2
+
1
3
  module ActsAsSaneTree
2
4
 
3
5
  def self.included(base) # :nodoc:
@@ -34,6 +36,8 @@ module ActsAsSaneTree
34
36
  # * :raw - No Hash nesting
35
37
  # * :no_self - Will not return given nodes in result set
36
38
  # * {:depth => n} - Will set maximum depth to query
39
+ # * {:to_depth => n} - Alias for :depth
40
+ # * {:at_depth => n} - Will return times at given depth (takes precedence over :depth/:to_depth)
37
41
  module ClassMethods
38
42
  # Configuration options are:
39
43
  #
@@ -99,20 +103,30 @@ module ActsAsSaneTree
99
103
  def self.nodes_and_descendents(*args)
100
104
  raw = args.delete(:raw)
101
105
  no_self = args.delete(:no_self)
102
- depth = args.detect{|x|x.is_a?(Hash)}
103
- if(depth)
104
- args.delete(depth)
105
- depth = depth[:depth]
106
+ at_depth = nil
107
+ depth = nil
108
+ hash = args.detect{|x|x.is_a?(Hash)}
109
+ if(hash)
110
+ args.delete(hash)
111
+ depth = hash[:depth] || hash[:to_depth]
112
+ at_depth = hash[:at_depth]
113
+ end
114
+ depth ||= #{configuration[:max_depth].to_i}
115
+ depth_restriction = "WHERE crumbs.level + 1 < \#{depth}" if depth
116
+ depth_clause = nil
117
+ if(at_depth)
118
+ depth_clause = "level + 1 = \#{at_depth.to_i}"
119
+ elsif(depth)
120
+ depth_clause = "level + 1 < \#{depth.to_i}"
106
121
  end
107
- depth = #{configuration[:max_depth]} unless depth
108
122
  base_ids = args.map{|x| x.is_a?(ActiveRecord::Base) ? x.id : x.to_i}
109
123
  q = self.find_by_sql(
110
124
  "WITH RECURSIVE crumbs AS (
111
125
  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(', ')})"}
112
126
  UNION ALL
113
127
  SELECT alias1.*, crumbs.level + 1 FROM crumbs JOIN #{self.table_name} alias1 on alias1.parent_id = crumbs.id
114
- \#{depth ? "WHERE crumbs.level + 1 < \#{depth.to_i}" : ''}
115
- ) SELECT * FROM crumbs WHERE level >= 0 ORDER BY level, parent_id ASC"
128
+ \#{depth_restriction}
129
+ ) SELECT * FROM crumbs WHERE level >= 0 \#{"AND " + depth_clause if depth_clause} ORDER BY level, parent_id ASC"
116
130
  )
117
131
  unless(raw)
118
132
  res = {}
@@ -216,4 +230,4 @@ module ActsAsSaneTree
216
230
  end
217
231
  end
218
232
 
219
- ActiveRecord::Base.send :include, ActsAsSaneTree
233
+ ActiveRecord::Base.send :include, ActsAsSaneTree
@@ -1,3 +1,3 @@
1
1
  module ActsAsSaneTree
2
- VERSION = '1.2'
2
+ VERSION = '1.3'
3
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: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 2
9
- version: "1.2"
8
+ - 3
9
+ version: "1.3"
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-11-10 00:00:00 -08:00
17
+ date: 2011-01-24 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -23,13 +23,12 @@ dependencies:
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ">="
26
+ - - ">"
27
27
  - !ruby/object:Gem::Version
28
- hash: 5
28
+ hash: 3
29
29
  segments:
30
- - 2
31
- - 3
32
- version: "2.3"
30
+ - 0
31
+ version: "0"
33
32
  type: :runtime
34
33
  version_requirements: *id001
35
34
  description: Sane ActiveRecord tree builder
@@ -46,7 +45,7 @@ files:
46
45
  - README.rdoc
47
46
  - CHANGELOG.rdoc
48
47
  - lib/acts_as_sane_tree.rb
49
- - lib/version.rb
48
+ - lib/acts_as_sane_tree/version.rb
50
49
  has_rdoc: true
51
50
  homepage: http://github.com/chrisroberts/acts_as_sane_tree
52
51
  licenses: []