collectiveidea-awesome_nested_set 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/awesome_nested_set.gemspec +8 -4
- data/lib/awesome_nested_set.rb +24 -0
- data/test/awesome_nested_set_test.rb +45 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.4.
|
1
|
+
1.4.1
|
data/awesome_nested_set.gemspec
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
1
4
|
# -*- encoding: utf-8 -*-
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = %q{awesome_nested_set}
|
5
|
-
s.version = "1.4.
|
8
|
+
s.version = "1.4.1"
|
6
9
|
|
7
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
11
|
s.authors = ["Brandon Keepers", "Daniel Morrison"]
|
9
|
-
s.date = %q{2009-09-
|
12
|
+
s.date = %q{2009-09-06}
|
10
13
|
s.description = %q{An awesome nested set implementation for Active Record}
|
11
14
|
s.email = %q{info@collectiveidea.com}
|
12
15
|
s.extra_rdoc_files = [
|
@@ -35,10 +38,11 @@ Gem::Specification.new do |s|
|
|
35
38
|
"test/fixtures/notes.yml",
|
36
39
|
"test/test_helper.rb"
|
37
40
|
]
|
41
|
+
s.has_rdoc = true
|
38
42
|
s.homepage = %q{http://github.com/collectiveidea/awesome_nested_set}
|
39
43
|
s.rdoc_options = ["--main", "README.rdoc", "--inline-source", "--line-numbers"]
|
40
44
|
s.require_paths = ["lib"]
|
41
|
-
s.rubygems_version = %q{1.3.
|
45
|
+
s.rubygems_version = %q{1.3.1}
|
42
46
|
s.summary = %q{An awesome nested set implementation for Active Record}
|
43
47
|
s.test_files = [
|
44
48
|
"test/db/database.yml",
|
@@ -55,7 +59,7 @@ Gem::Specification.new do |s|
|
|
55
59
|
|
56
60
|
if s.respond_to? :specification_version then
|
57
61
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
58
|
-
s.specification_version =
|
62
|
+
s.specification_version = 2
|
59
63
|
|
60
64
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
61
65
|
s.add_runtime_dependency(%q<activerecord>, [">= 1.1"])
|
data/lib/awesome_nested_set.rb
CHANGED
@@ -187,6 +187,30 @@ module CollectiveIdea #:nodoc:
|
|
187
187
|
set_left_and_rights.call(root_node)
|
188
188
|
end
|
189
189
|
end
|
190
|
+
|
191
|
+
# Iterates over tree elements and determines the current level in the tree.
|
192
|
+
# Only accepts default ordering, odering by an other column than lft
|
193
|
+
# does not work. This method is much more efficent than calling level
|
194
|
+
# because it doesn't require any additional database queries.
|
195
|
+
#
|
196
|
+
# Example:
|
197
|
+
# Category.each_with_level(Category.root.self_and_descendants) do |o, level|
|
198
|
+
#
|
199
|
+
def each_with_level(objects)
|
200
|
+
path = [nil]
|
201
|
+
objects.each do |o|
|
202
|
+
if o.parent_id != path.last
|
203
|
+
# we are on a new level, did we decent or ascent?
|
204
|
+
if path.include?(o.parent_id)
|
205
|
+
# remove wrong wrong tailing paths elements
|
206
|
+
path.pop while path.last != o.parent_id
|
207
|
+
else
|
208
|
+
path << o.parent_id
|
209
|
+
end
|
210
|
+
end
|
211
|
+
yield(o, path.length - 1)
|
212
|
+
end
|
213
|
+
end
|
190
214
|
end
|
191
215
|
|
192
216
|
# Mixed into both classes and instances to provide easy access to the column names
|
@@ -678,4 +678,49 @@ class AwesomeNestedSetTest < TestCaseClass
|
|
678
678
|
assert Category.valid?
|
679
679
|
end
|
680
680
|
|
681
|
+
def check_structure(entries, structure)
|
682
|
+
structure = structure.dup
|
683
|
+
Category.each_with_level(entries) do |category, level|
|
684
|
+
expected_level, expected_name = structure.shift
|
685
|
+
assert_equal expected_name, category.name, "wrong category"
|
686
|
+
assert_equal expected_level, level, "wrong level for #{category.name}"
|
687
|
+
end
|
688
|
+
end
|
689
|
+
|
690
|
+
def test_each_with_level
|
691
|
+
levels = [
|
692
|
+
[0, "Top Level"],
|
693
|
+
[1, "Child 1"],
|
694
|
+
[1, "Child 2"],
|
695
|
+
[2, "Child 2.1"],
|
696
|
+
[1, "Child 3" ]]
|
697
|
+
|
698
|
+
check_structure(Category.root.self_and_descendants, levels)
|
699
|
+
|
700
|
+
# test some deeper structures
|
701
|
+
category = Category.find_by_name("Child 1")
|
702
|
+
c1 = Category.new(:name => "Child 1.1")
|
703
|
+
c2 = Category.new(:name => "Child 1.1.1")
|
704
|
+
c3 = Category.new(:name => "Child 1.1.1.1")
|
705
|
+
c4 = Category.new(:name => "Child 1.2")
|
706
|
+
[c1, c2, c3, c4].each(&:save!)
|
707
|
+
|
708
|
+
c1.move_to_child_of(category)
|
709
|
+
c2.move_to_child_of(c1)
|
710
|
+
c3.move_to_child_of(c2)
|
711
|
+
c4.move_to_child_of(category)
|
712
|
+
|
713
|
+
levels = [
|
714
|
+
[0, "Top Level"],
|
715
|
+
[1, "Child 1"],
|
716
|
+
[2, "Child 1.1"],
|
717
|
+
[3, "Child 1.1.1"],
|
718
|
+
[4, "Child 1.1.1.1"],
|
719
|
+
[2, "Child 1.2"],
|
720
|
+
[1, "Child 2"],
|
721
|
+
[2, "Child 2.1"],
|
722
|
+
[1, "Child 3" ]]
|
723
|
+
|
724
|
+
check_structure(Category.root.self_and_descendants, levels)
|
725
|
+
end
|
681
726
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collectiveidea-awesome_nested_set
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Keepers
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-09-
|
13
|
+
date: 2009-09-06 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -53,7 +53,7 @@ files:
|
|
53
53
|
- test/fixtures/departments.yml
|
54
54
|
- test/fixtures/notes.yml
|
55
55
|
- test/test_helper.rb
|
56
|
-
has_rdoc:
|
56
|
+
has_rdoc: true
|
57
57
|
homepage: http://github.com/collectiveidea/awesome_nested_set
|
58
58
|
post_install_message:
|
59
59
|
rdoc_options:
|
@@ -80,7 +80,7 @@ requirements: []
|
|
80
80
|
rubyforge_project:
|
81
81
|
rubygems_version: 1.2.0
|
82
82
|
signing_key:
|
83
|
-
specification_version:
|
83
|
+
specification_version: 2
|
84
84
|
summary: An awesome nested set implementation for Active Record
|
85
85
|
test_files:
|
86
86
|
- test/db/database.yml
|