hierarchy 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Tim Morgan
1
+ Copyright (c) 2010 Tim Morgan
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.textile CHANGED
@@ -1,7 +1,7 @@
1
1
  h1. Hierarchy -- Use PostgreSQL @LTREE@ columns in ActiveRecord
2
2
 
3
3
  | *Author* | Tim Morgan |
4
- | *Version* | 1.0 (Oct 30, 2010) |
4
+ | *Version* | 1.0.2 (Nov 11, 2010) |
5
5
  | *License* | Released under the MIT license. |
6
6
 
7
7
  h2. About
data/Rakefile CHANGED
@@ -16,8 +16,7 @@ Jeweler::Tasks.new do |gem|
16
16
  gem.homepage = "http://github.com/riscfuture/hierarchy"
17
17
  gem.authors = [ "Tim Morgan" ]
18
18
  gem.required_ruby_version = '>= 1.9'
19
- gem.add_dependency 'activerecord', '>= 3.0'
20
- gem.add_dependency 'activesupport', '>= 3.0'
19
+ gem.add_dependency 'rails', '>= 3.0'
21
20
  end
22
21
  Jeweler::GemcutterTasks.new
23
22
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.2
data/hierarchy.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{hierarchy}
8
- s.version = "1.0.0"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tim Morgan"]
12
- s.date = %q{2010-10-30}
12
+ s.date = %q{2010-11-11}
13
13
  s.description = %q{Adds ActiveRecord support for hierarchial data structures using PostgreSQL's LTREE column type.}
14
14
  s.email = %q{git@timothymorgan.info}
15
15
  s.extra_rdoc_files = [
@@ -53,15 +53,12 @@ Gem::Specification.new do |s|
53
53
  s.specification_version = 3
54
54
 
55
55
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
56
- s.add_runtime_dependency(%q<activerecord>, [">= 3.0"])
57
- s.add_runtime_dependency(%q<activesupport>, [">= 3.0"])
56
+ s.add_runtime_dependency(%q<rails>, [">= 3.0"])
58
57
  else
59
- s.add_dependency(%q<activerecord>, [">= 3.0"])
60
- s.add_dependency(%q<activesupport>, [">= 3.0"])
58
+ s.add_dependency(%q<rails>, [">= 3.0"])
61
59
  end
62
60
  else
63
- s.add_dependency(%q<activerecord>, [">= 3.0"])
64
- s.add_dependency(%q<activesupport>, [">= 3.0"])
61
+ s.add_dependency(%q<rails>, [">= 3.0"])
65
62
  end
66
63
  end
67
64
 
data/lib/hierarchy.rb CHANGED
@@ -53,7 +53,7 @@ module Hierarchy
53
53
 
54
54
  base.scope :parent_of, ->(obj) { obj.top_level? ? base.where('false') : base.where(id: obj.index_path.last) }
55
55
  base.scope :children_of, ->(obj) { base.where(path: obj.my_path) }
56
- base.scope :ancestors_of, ->(obj) { base.where(id: obj.index_path.to_a) }
56
+ base.scope :ancestors_of, ->(obj) { obj.top_level? ? base.where('false') : base.where(id: obj.index_path.to_a) }
57
57
  base.scope :descendants_of, ->(obj) { base.where([ "path <@ ?", obj.my_path ]) }
58
58
  base.scope :siblings_of, ->(obj) { base.where(path: obj.path) }
59
59
  base.scope :priority_order, base.order("NLEVEL(path) ASC")
@@ -109,6 +109,7 @@ module Hierarchy
109
109
  # @return [Array] The objects above this one in the hierarchy.
110
110
 
111
111
  def ancestors(options={})
112
+ return [] if top_level?
112
113
  objects = self.class.ancestors_of(self).scoped(options).group_by(&:id)
113
114
  index_path.map { |id| objects[id].first }
114
115
  end
@@ -143,7 +144,7 @@ module Hierarchy
143
144
  # @return [true, false] Whether or not this object has no parents.
144
145
 
145
146
  def top_level?
146
- path == ''
147
+ path.blank?
147
148
  end
148
149
 
149
150
  # @return [true, false] Whether or not this object has no children. Makes a
@@ -160,7 +161,7 @@ module Hierarchy
160
161
 
161
162
  # @private
162
163
  def index_path
163
- IndexPath.from_ltree path
164
+ IndexPath.from_ltree path.to_s
164
165
  end
165
166
  end
166
167
  end
@@ -107,4 +107,10 @@ describe Hierarchy do
107
107
  parent.should_not be_bottom_level
108
108
  end
109
109
  end
110
+
111
+ describe "#ancestors" do
112
+ it "should return an empty array for a top-level object" do
113
+ Model.create!.ancestors.should eql([])
114
+ end
115
+ end
110
116
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 2
9
+ version: 1.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tim Morgan
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-30 00:00:00 -07:00
17
+ date: 2010-11-11 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: activerecord
21
+ name: rails
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
23
23
  none: false
24
24
  requirements:
@@ -31,20 +31,6 @@ dependencies:
31
31
  type: :runtime
32
32
  prerelease: false
33
33
  version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: activesupport
36
- requirement: &id002 !ruby/object:Gem::Requirement
37
- none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 3
43
- - 0
44
- version: "3.0"
45
- type: :runtime
46
- prerelease: false
47
- version_requirements: *id002
48
34
  description: Adds ActiveRecord support for hierarchial data structures using PostgreSQL's LTREE column type.
49
35
  email: git@timothymorgan.info
50
36
  executables: []