nested_set 1.5.3 → 1.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -0
- data/VERSION +1 -1
- data/lib/nested_set/helper.rb +51 -2
- data/nested_set.gemspec +2 -2
- data/test/nested_set/helper_test.rb +36 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -62,6 +62,13 @@ Example usage:
|
|
62
62
|
|
63
63
|
<%= select_tag 'parent_id', options_for_select(nested_set_options(Category) {|i| "#{'-' * i.level} #{i.name}" } ) %>
|
64
64
|
|
65
|
+
or sorted select:
|
66
|
+
|
67
|
+
<%= f.select :parent_id, sorted_nested_set_options(Category, lambda(&:name)) {|i| "#{'-' * i.level} #{i.name}" } %>
|
68
|
+
|
69
|
+
<% sort_method = lambda{|x, y| x.name.downcase <=> y.name.downcase} %>
|
70
|
+
<%= select_tag 'parent_id', options_for_select(sorted_nested_set_options(Category, sort_method){|i| "#{'-' * i.level} #{i.name}" } ) %>
|
71
|
+
|
65
72
|
See CollectiveIdea::Acts::NestedSet::Helper for more information about the helpers.
|
66
73
|
|
67
74
|
== References
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.5.
|
1
|
+
1.5.4
|
data/lib/nested_set/helper.rb
CHANGED
@@ -17,8 +17,8 @@ module CollectiveIdea #:nodoc:
|
|
17
17
|
#
|
18
18
|
# == Usage
|
19
19
|
#
|
20
|
-
# <%= f.select :parent_id, nested_set_options(Category, @category) {|i|
|
21
|
-
# "#{'–' *
|
20
|
+
# <%= f.select :parent_id, nested_set_options(Category, @category) {|i, level|
|
21
|
+
# "#{'–' * level} #{i.name}"
|
22
22
|
# }) %>
|
23
23
|
#
|
24
24
|
def nested_set_options(class_or_item, mover = nil)
|
@@ -41,6 +41,55 @@ module CollectiveIdea #:nodoc:
|
|
41
41
|
end
|
42
42
|
result
|
43
43
|
end
|
44
|
+
|
45
|
+
# Returns options for select.
|
46
|
+
# You can sort node's child by any method
|
47
|
+
# You can exclude some items from the tree.
|
48
|
+
# You can pass a block receiving an item and returning the string displayed in the select.
|
49
|
+
#
|
50
|
+
# == Params
|
51
|
+
# * +class_or_item+ - Class name or top level times
|
52
|
+
# * +sort_proc+ sorting proc for node's child, ex. lambda{|x| x.name}
|
53
|
+
# * +mover+ - The item that is being move, used to exlude impossible moves
|
54
|
+
# * +level+ - start level, :default => 0
|
55
|
+
# * +&block+ - a block that will be used to display: { |itemi, level| "#{'–' * level} #{i.name}" }
|
56
|
+
# == Usage
|
57
|
+
#
|
58
|
+
# <%= f.select :parent_id, sorted_nested_set_options(Category, lambda(&:name)) {|i, level|
|
59
|
+
# "#{'–' * level} #{i.name}"
|
60
|
+
# }) %>
|
61
|
+
#
|
62
|
+
# OR
|
63
|
+
#
|
64
|
+
# sort_method = lambda{|x,y| x.name.downcase <=> y.name.downcase}
|
65
|
+
#
|
66
|
+
# <%= f.select :parent_id, nested_set_options(Category, sort_method) {|i, level|
|
67
|
+
# "#{'–' * level} #{i.name}"
|
68
|
+
# }) %>
|
69
|
+
#
|
70
|
+
def sorted_nested_set_options(class_or_item, sort_proc, mover = nil, level = 0)
|
71
|
+
class_or_item = class_or_item.roots if class_or_item.is_a?(Class)
|
72
|
+
items = Array(class_or_item)
|
73
|
+
result = []
|
74
|
+
items.sort_by(&sort_proc).each do |root|
|
75
|
+
set = root.self_and_descendants
|
76
|
+
result += build_node(set[0], set, sort_proc, mover, level){|x, level| yield(x, level)}
|
77
|
+
end
|
78
|
+
result
|
79
|
+
end
|
80
|
+
|
81
|
+
def build_node(node, set, sort_proc, mover = nil, level = nil)
|
82
|
+
result ||= []
|
83
|
+
if mover.nil? || mover.new_record? || mover.move_possible?(i)
|
84
|
+
result << [yield(node, level), node.id]
|
85
|
+
unless node.leaf?
|
86
|
+
set.select{|i| i.parent_id == node.id}.sort_by(&sort_proc).map{ |i|
|
87
|
+
result.push(*build_node(i, set, sort_proc, mover, level.to_i + 1){|x, level| yield(x, level)})
|
88
|
+
}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
result
|
92
|
+
end
|
44
93
|
end
|
45
94
|
end
|
46
95
|
end
|
data/nested_set.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{nested_set}
|
8
|
-
s.version = "1.5.
|
8
|
+
s.version = "1.5.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brandon Keepers", "Daniel Morrison"]
|
12
|
-
s.date = %q{2010-11-
|
12
|
+
s.date = %q{2010-11-12}
|
13
13
|
s.description = %q{An awesome nested set implementation for Active Record}
|
14
14
|
s.email = %q{info@collectiveidea.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -35,6 +35,42 @@ module CollectiveIdea
|
|
35
35
|
assert_equal expected, actual
|
36
36
|
end
|
37
37
|
|
38
|
+
def test_build_node
|
39
|
+
set = categories(:top_level).self_and_descendants
|
40
|
+
expected = set.map{|i| [i.name, i.id]}
|
41
|
+
actual = build_node(set[0], set, lambda(&:lft)){|i, level| i.name }
|
42
|
+
assert_equal expected, actual
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_build_node_with_back_id_order
|
46
|
+
set = categories(:top_level).self_and_descendants
|
47
|
+
expected = [
|
48
|
+
["Top Level", 1],
|
49
|
+
["Child 3", 5],
|
50
|
+
["Child 2", 3],
|
51
|
+
["Child 2.1", 4],
|
52
|
+
["Child 1", 2]
|
53
|
+
]
|
54
|
+
actual = build_node(set[0], set, lambda{|x| -x.id}){|i, level| i.name }
|
55
|
+
assert_equal expected, actual
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_sorted_nested_set
|
59
|
+
expected = [
|
60
|
+
[" Top Level 2", 6],
|
61
|
+
[" Top Level", 1],
|
62
|
+
['- Child 3', 5],
|
63
|
+
['- Child 2', 3],
|
64
|
+
['-- Child 2.1', 4],
|
65
|
+
["- Child 1", 2]
|
66
|
+
]
|
67
|
+
|
68
|
+
actual = sorted_nested_set_options(Category, lambda{|x| -x.id}) do |c, level|
|
69
|
+
"#{'-' * level} #{c.name}"
|
70
|
+
end
|
71
|
+
assert_equal expected, actual
|
72
|
+
end
|
73
|
+
|
38
74
|
end
|
39
75
|
end
|
40
76
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 1.5.
|
8
|
+
- 4
|
9
|
+
version: 1.5.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Brandon Keepers
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-12 00:00:00 +03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|