tree_decorator 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +4 -7
- data/lib/tree_decorator/object_hanger.rb +3 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -5,7 +5,7 @@ TreeDecorator has been designed to make it easier to decorate trees or nested
|
|
5
5
|
sets of data. The tools provided, walk through a hash or nested object and
|
6
6
|
apply code to containers and elements based on user defined rules.
|
7
7
|
|
8
|
-
For example:
|
8
|
+
For an example of how TreeDecorator can be used in Rails:
|
9
9
|
|
10
10
|
class Thing < ActiveRecord::Base
|
11
11
|
acts_as_nested_set
|
@@ -15,16 +15,13 @@ For example:
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
hanger = TreeDecorator::ObjectHanger.new(
|
19
|
-
Thing.roots,
|
20
|
-
:children_method => :children
|
21
|
-
)
|
18
|
+
hanger = TreeDecorator::ObjectHanger.new(Thing.roots)
|
22
19
|
|
23
20
|
hanger.outer {|content| content_tag('ul', content.html_safe)}
|
24
21
|
hanger.inner {|content| content_tag('li', content.html_safe)}
|
25
|
-
hanger.element {|
|
22
|
+
hanger.element {|thing| link_to(thing.name, thing_path(thing))}
|
26
23
|
|
27
24
|
hanger.tree #----> outputs a nested HTML unordered list with each thing's name
|
28
|
-
#
|
25
|
+
# linking to it's default path.
|
29
26
|
|
30
27
|
See lib/example and tests for examples of usage
|
@@ -7,9 +7,11 @@ module TreeDecorator
|
|
7
7
|
class ObjectHanger < Hanger
|
8
8
|
attr_reader :children_method, :content_method
|
9
9
|
|
10
|
+
DEFAULT_CHILDREN_METHOD = :children
|
11
|
+
|
10
12
|
def initialize(root, args = {})
|
11
13
|
@root = root
|
12
|
-
@children_method = args[:children_method]
|
14
|
+
@children_method = args[:children_method] || DEFAULT_CHILDREN_METHOD
|
13
15
|
@tree = hash
|
14
16
|
end
|
15
17
|
|