From 1b7d6cb5cecc7988ea74757d5acf948b0dab6eea Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Mon, 6 Mar 2017 14:39:35 +0800 Subject: [PATCH 1/3] test two associations --- test/deep_pluck_test.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/deep_pluck_test.rb b/test/deep_pluck_test.rb index 869843d..d922bfa 100644 --- a/test/deep_pluck_test.rb +++ b/test/deep_pluck_test.rb @@ -9,14 +9,14 @@ def test_that_it_has_a_version_number refute_nil ::DeepPluck::VERSION end - def test_pluck_with_1_level_deep + def test_1_level_deep assert_equal [ {'name' => 'John'}, {'name' => 'Pearl'}, ], User.where(:name => %w(John Pearl)).deep_pluck(:name) end - def test_pluck_with_2_level_deep + def test_2_level_deep assert_equal [ {'name' => 'Pearl' , :posts => [{'name' => "post4"}, {'name' => "post5"}]}, {'name' => 'Kathenrie', :posts => [{'name' => "post6"}]}, @@ -27,7 +27,14 @@ def test_pluck_with_2_level_deep ], User.where(:name => %w(John Pearl)).deep_pluck(:name, :contact => [:address]) end - def test_pluck_with_2_level_deep_and_reverse_association + def test_two_associations + assert_equal [ + {'name' => 'Pearl' , :posts => [{'name' => "post4"}, {'name' => "post5"}], :contact => [{'address' => "Pearl's Home"}]}, + {'name' => 'Kathenrie', :posts => [{'name' => "post6"}], :contact => [{'address' => "Kathenrie's Home"}]}, + ], User.where(:name => %w(Pearl Kathenrie)).deep_pluck(:name, :contact => :address, :posts => :name) + end + + def test_2_level_deep_and_reverse_association assert_equal [ {'name' => 'post4', :user => {'name' => "Pearl"}}, {'name' => 'post5', :user => {'name' => "Pearl"}}, From d07737ffd0ca4b1b26754cc9dfcd4f610a15f2da Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Mon, 6 Mar 2017 14:54:34 +0800 Subject: [PATCH 2/3] =?UTF-8?q?has=5Fone=20=E5=87=BA=E4=BE=86=E7=9A=84?= =?UTF-8?q?=E7=B5=90=E6=9E=9C=E4=B8=8D=E6=87=89=E8=A9=B2=E6=98=AF=E9=99=A3?= =?UTF-8?q?=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/deep_pluck/model.rb | 8 ++++++-- test/deep_pluck_test.rb | 10 +++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/deep_pluck/model.rb b/lib/deep_pluck/model.rb index 36687bb..467450b 100644 --- a/lib/deep_pluck/model.rb +++ b/lib/deep_pluck/model.rb @@ -60,12 +60,16 @@ def set_includes_data(parent, children_store_name, model, order_by = nil) s[children_store_name] = children_hash[id] } else #Child.where(:parent_id => parent.pluck(:id)) - parent.each{|s| s[children_store_name] = [] } + parent.each{|s| s[children_store_name] = [] } if !reflect.has_one? parent_hash = Hash[parent.map{|s| [s["id"], s]}] children = model.load_data{|relaction| relaction.where(reflect.foreign_key => parent.map{|s| s["id"]}.uniq.compact).order(order_by) } children.each{|s| next if (id = s[reflect.foreign_key]) == nil - parent_hash[id][children_store_name] << s + if reflect.has_one? + parent_hash[id][children_store_name] = s + else + parent_hash[id][children_store_name] << s + end } end return children diff --git a/test/deep_pluck_test.rb b/test/deep_pluck_test.rb index d922bfa..3e32572 100644 --- a/test/deep_pluck_test.rb +++ b/test/deep_pluck_test.rb @@ -22,15 +22,15 @@ def test_2_level_deep {'name' => 'Kathenrie', :posts => [{'name' => "post6"}]}, ], User.where(:name => %w(Pearl Kathenrie)).deep_pluck(:name, :posts => [:name]) assert_equal [ - {'name' => 'John' , :contact => [{'address' => "John's Home"}]}, - {'name' => 'Pearl', :contact => [{'address' => "Pearl's Home"}]}, - ], User.where(:name => %w(John Pearl)).deep_pluck(:name, :contact => [:address]) + {'name' => 'John' , :contact => {'address' => "John's Home"}}, + {'name' => 'Pearl', :contact => {'address' => "Pearl's Home"}}, + ], User.where(:name => %w(John Pearl)).deep_pluck(:name, :contact => :address) end def test_two_associations assert_equal [ - {'name' => 'Pearl' , :posts => [{'name' => "post4"}, {'name' => "post5"}], :contact => [{'address' => "Pearl's Home"}]}, - {'name' => 'Kathenrie', :posts => [{'name' => "post6"}], :contact => [{'address' => "Kathenrie's Home"}]}, + {'name' => 'Pearl' , :posts => [{'name' => "post4"}, {'name' => "post5"}], :contact => {'address' => "Pearl's Home"}}, + {'name' => 'Kathenrie', :posts => [{'name' => "post6"}], :contact => {'address' => "Kathenrie's Home"}}, ], User.where(:name => %w(Pearl Kathenrie)).deep_pluck(:name, :contact => :address, :posts => :name) end From a161b74ff472f09daca4684259bd0059087bda25 Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Mon, 6 Mar 2017 16:44:30 +0800 Subject: [PATCH 3/3] fix rails 3 doesn't have #has_one? method --- lib/deep_pluck/model.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/deep_pluck/model.rb b/lib/deep_pluck/model.rb index 467450b..f3e7274 100644 --- a/lib/deep_pluck/model.rb +++ b/lib/deep_pluck/model.rb @@ -60,15 +60,15 @@ def set_includes_data(parent, children_store_name, model, order_by = nil) s[children_store_name] = children_hash[id] } else #Child.where(:parent_id => parent.pluck(:id)) - parent.each{|s| s[children_store_name] = [] } if !reflect.has_one? + parent.each{|s| s[children_store_name] = [] } if reflect.collection? parent_hash = Hash[parent.map{|s| [s["id"], s]}] children = model.load_data{|relaction| relaction.where(reflect.foreign_key => parent.map{|s| s["id"]}.uniq.compact).order(order_by) } children.each{|s| next if (id = s[reflect.foreign_key]) == nil - if reflect.has_one? - parent_hash[id][children_store_name] = s - else + if reflect.collection? parent_hash[id][children_store_name] << s + else + parent_hash[id][children_store_name] = s end } end