From 0c14723dc6e0047a28b90f950584a75e7c86f8fb Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Wed, 13 May 2015 14:41:05 +1000 Subject: [PATCH 1/4] allow arrange_serializable to accept block --- Gemfile.lock | 3 +-- lib/ancestry/class_methods.rb | 8 ++++++-- test/concerns/arrangement_test.rb | 24 +++++++++++++++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b772fcdb..7ada7240 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - ancestry (2.0.0) + ancestry (2.1.0) activerecord (>= 3.0.0) GEM @@ -29,7 +29,6 @@ GEM arel (4.0.1) atomic (1.1.14) builder (3.1.4) - columnize (0.3.6) coveralls (0.7.0) multi_json (~> 1.3) rest-client diff --git a/lib/ancestry/class_methods.rb b/lib/ancestry/class_methods.rb index 00b4a9ad..eef55cdb 100644 --- a/lib/ancestry/class_methods.rb +++ b/lib/ancestry/class_methods.rb @@ -57,10 +57,14 @@ def arrange_nodes(nodes) end # Arrangement to nested array - def arrange_serializable options={}, nodes=nil + def arrange_serializable options={}, nodes=nil, &block nodes = arrange(options) if nodes.nil? nodes.map do |parent, children| - parent.serializable_hash.merge 'children' => arrange_serializable(options, children) + if block_given? + yield parent, arrange_serializable(options, children, &block) + else + parent.serializable_hash.merge 'children' => arrange_serializable(options, children) + end end end diff --git a/test/concerns/arrangement_test.rb b/test/concerns/arrangement_test.rb index 630ed837..f776e075 100644 --- a/test/concerns/arrangement_test.rb +++ b/test/concerns/arrangement_test.rb @@ -35,6 +35,28 @@ def test_arrange_serializable end end + def test_arrange_serializable_with_block + AncestryTestDatabase.with_model :depth => 2, :width => 2 do |model, roots| + expected_result = [{ + "id"=>4, + "childs"=> + [{"id"=>6}, + {"id"=>5}]}, + { + "id"=>1, + "childs"=> + [{"id"=>3}, + {"id"=>2}]}] + result = model.arrange_serializable(order: "id desc") do |parent, children| + out = {} + out["id"] = parent.id + out["childs"] = children if children.count > 1 + out + end + assert_equal result, expected_result + end + end + def test_arrange_order_option AncestryTestDatabase.with_model :width => 3, :depth => 3 do |model, roots| descending_nodes_lvl0 = model.arrange :order => 'id desc' @@ -77,4 +99,4 @@ def test_arrangement_nesting assert_equal 1, model.arrange.count end end -end \ No newline at end of file +end From d2e09a3c2e798c41f587c06367acd76141c8fd14 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Wed, 13 May 2015 15:02:59 +1000 Subject: [PATCH 2/4] update readme --- README.rdoc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.rdoc b/README.rdoc index 7bb806ac..fa3157bd 100644 --- a/README.rdoc +++ b/README.rdoc @@ -177,6 +177,23 @@ To get the arranged nodes as a nested array of hashes for serialization: } ] +You can also supply your own serialization logic using blocks: + +For example, using Active Model Serializers: + + TreeNode.arrange_serializable do |parent, children| + MySerializer.new(parent, children: children) + end + +Or plain hashes: + + TreeNode.arrange_serializable do |parent, children| + { + my_id: parent.id + my_childs: children + } + end + The result of arrange_serializable can easily be serialized to json with 'to_json', or some other format: TreeNode.arrange_serializable.to_json From 2c7eed7286227a45bd0e20d3dd7b57111f336ae1 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Wed, 13 May 2015 15:06:24 +1000 Subject: [PATCH 3/4] revert gemfile lock change --- Gemfile.lock | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 7ada7240..b772fcdb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - ancestry (2.1.0) + ancestry (2.0.0) activerecord (>= 3.0.0) GEM @@ -29,6 +29,7 @@ GEM arel (4.0.1) atomic (1.1.14) builder (3.1.4) + columnize (0.3.6) coveralls (0.7.0) multi_json (~> 1.3) rest-client From 3173e87515f61d59982a90cba907397a881231a8 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Wed, 13 May 2015 17:32:19 +1000 Subject: [PATCH 4/4] rename childs to children --- README.rdoc | 2 +- test/concerns/arrangement_test.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rdoc b/README.rdoc index fa3157bd..0a146b26 100644 --- a/README.rdoc +++ b/README.rdoc @@ -190,7 +190,7 @@ Or plain hashes: TreeNode.arrange_serializable do |parent, children| { my_id: parent.id - my_childs: children + my_children: children } end diff --git a/test/concerns/arrangement_test.rb b/test/concerns/arrangement_test.rb index f776e075..38eb21f1 100644 --- a/test/concerns/arrangement_test.rb +++ b/test/concerns/arrangement_test.rb @@ -39,18 +39,18 @@ def test_arrange_serializable_with_block AncestryTestDatabase.with_model :depth => 2, :width => 2 do |model, roots| expected_result = [{ "id"=>4, - "childs"=> + "children"=> [{"id"=>6}, {"id"=>5}]}, { "id"=>1, - "childs"=> + "children"=> [{"id"=>3}, {"id"=>2}]}] result = model.arrange_serializable(order: "id desc") do |parent, children| out = {} out["id"] = parent.id - out["childs"] = children if children.count > 1 + out["children"] = children if children.count > 1 out end assert_equal result, expected_result