From d7dbaa9129de043496a4b8f925cb3aebe63561d7 Mon Sep 17 00:00:00 2001 From: Viktar Basharymau Date: Sat, 2 Jan 2016 21:42:22 +0300 Subject: [PATCH 1/2] Simplify Chewy::Type::Import#bulk_body a little --- lib/chewy/type/import.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chewy/type/import.rb b/lib/chewy/type/import.rb index 53b5d44fa..72c2507de 100644 --- a/lib/chewy/type/import.rb +++ b/lib/chewy/type/import.rb @@ -76,10 +76,10 @@ def bulk options = {} private def bulk_body(action_objects, indexed_objects = nil) - action_objects.inject([]) do |result, (action, objects)| + action_objects.flat_map do |action, objects| method = "#{action}_bulk_entry" crutches = Chewy::Type::Crutch::Crutches.new self, objects - result.concat(objects.map { |object| send(method, object, indexed_objects, crutches) }.flatten) + objects.map { |object| send(method, object, indexed_objects, crutches) }.flatten end end From 145ead95e539d5c3136e0e3bbf6b2662b46f9cdd Mon Sep 17 00:00:00 2001 From: Viktar Basharymau Date: Sat, 2 Jan 2016 21:46:05 +0300 Subject: [PATCH 2/2] Use .flat_map{} instead of .map{}.flatten In this particular case, they are semantically equivalent, but flat_map is shorter and faster. --- lib/chewy/type/import.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chewy/type/import.rb b/lib/chewy/type/import.rb index 72c2507de..7a369fed2 100644 --- a/lib/chewy/type/import.rb +++ b/lib/chewy/type/import.rb @@ -79,7 +79,7 @@ def bulk_body(action_objects, indexed_objects = nil) action_objects.flat_map do |action, objects| method = "#{action}_bulk_entry" crutches = Chewy::Type::Crutch::Crutches.new self, objects - objects.map { |object| send(method, object, indexed_objects, crutches) }.flatten + objects.flat_map { |object| send(method, object, indexed_objects, crutches) } end end