From 01d5417069f900d5abb6b155a4d80901648b367f Mon Sep 17 00:00:00 2001 From: Daniel Puscher Date: Wed, 11 Dec 2019 16:27:23 +0100 Subject: [PATCH 1/3] Use an array of jobs instead of hash Enables support for rendering multiple components with the same name in one request --- README.md | 2 +- lib/hypernova/controller_helpers.rb | 14 ++++++-------- lib/hypernova/plugin_helper.rb | 16 ++++++++-------- spec/controller_helpers_spec.rb | 16 ++++++++-------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 30c9aab..d62b3d6 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ class HypernovaPlugin # prepare_request allows you to alter the request object in any way that you # need. # Unless manipulated by another plugin, request takes the shape: - # { 'component_name.js': { :name => 'component_name.js', :data => {} } } + # [{ :name => 'component_name.js', :data => {} }, ...] def prepare_request(current_request, original_request) current_request.keys.each do |key| phrase_hash = req[key][:data][:phrases] diff --git a/lib/hypernova/controller_helpers.rb b/lib/hypernova/controller_helpers.rb index f3d4b80..7d89b91 100644 --- a/lib/hypernova/controller_helpers.rb +++ b/lib/hypernova/controller_helpers.rb @@ -78,17 +78,15 @@ def hypernova_batch_after return if @hypernova_batch.empty? jobs = @hypernova_batch.jobs - hash = jobs.each_with_object({}) do |job, h| - h[job[:name]] = job - end - hash = prepare_request(hash, hash) - if send_request?(hash) + jobs = prepare_request(jobs, jobs) + + if send_request?(jobs) begin - will_send_request(hash) + will_send_request(jobs) result = @hypernova_batch.submit! - on_success(result, hash) + on_success(result, jobs) rescue StandardError => e - on_error(e, nil, hash) + on_error(e, nil, jobs) result = @hypernova_batch.submit_fallback! end else diff --git a/lib/hypernova/plugin_helper.rb b/lib/hypernova/plugin_helper.rb index cf92b9c..b3963bd 100644 --- a/lib/hypernova/plugin_helper.rb +++ b/lib/hypernova/plugin_helper.rb @@ -19,20 +19,20 @@ def prepare_request(current_request, original_request) end end - def send_request?(jobs_hash) + def send_request?(jobs) Hypernova.plugins.all? do |plugin| if plugin.respond_to?(:send_request?) - plugin.send_request?(jobs_hash) + plugin.send_request?(jobs) else true end end end - def will_send_request(jobs_hash) + def will_send_request(jobs) Hypernova.plugins.each do |plugin| if plugin.respond_to?(:will_send_request) - plugin.will_send_request(jobs_hash) + plugin.will_send_request(jobs) end end end @@ -47,13 +47,13 @@ def after_response(current_response, original_response) end end - def on_error(error, job = nil, jobs_hash = nil) - Hypernova.plugins.each { |plugin| plugin.on_error(error, job, jobs_hash) if plugin.respond_to?(:on_error) } + def on_error(error, job = nil, jobs = nil) + Hypernova.plugins.each { |plugin| plugin.on_error(error, job, jobs) if plugin.respond_to?(:on_error) } end - def on_success(res, jobs_hash) + def on_success(res, jobs) Hypernova.plugins.each do |plugin| - plugin.on_success(res, jobs_hash) if plugin.respond_to?(:on_success) + plugin.on_success(res, jobs) if plugin.respond_to?(:on_success) end end end diff --git a/spec/controller_helpers_spec.rb b/spec/controller_helpers_spec.rb index 932f6d3..d18ac21 100644 --- a/spec/controller_helpers_spec.rb +++ b/spec/controller_helpers_spec.rb @@ -109,7 +109,7 @@ def initialize allow(test).to receive(:will_send_request).and_raise(error) allow(test).to receive(:response).and_return(response) - expect(test).to receive(:on_error).with(error, nil, hash_including('mordor.js')) + expect(test).to receive(:on_error).with(error, nil, array_including(hash_including({name: "mordor.js"}))) expect(batch).to receive(:submit_fallback!) test.hypernova_render_support {} @@ -137,14 +137,14 @@ def initialize test = TestClass.new batch = Hypernova::Batch.new(test.hypernova_service) response = TestResponse.new - jobs = { name: "mordor.js", data: {} } + job = { name: "mordor.js", data: {} } - batch.render(jobs) + batch.render(job) allow(Hypernova::Batch).to receive(:new).with(test.hypernova_service).and_return(batch) allow(test).to receive(:response).and_return(response) - expect(test).to receive(:on_success).with({}, { "mordor.js" => jobs }) + expect(test).to receive(:on_success).with({}, [job]) test.hypernova_render_support {} end @@ -282,19 +282,19 @@ def send_request?(request) describe "prepare_request plugins" do class PrepareRequestPlugin def prepare_request(current_request, _) - current_request['test'][:data][:what] = 'who?' + current_request[0][:data][:what] = 'who?' end end it 'should be able to alter the request data' do plugin = PrepareRequestPlugin.new Hypernova.add_plugin!(plugin) - request = { - 'test' => { + request = [ + { :name => 'test', :data => request_data, }, - } + ] expect(plugin).to receive(:prepare_request).with(request, request).and_call_original expect(full_controller.hypernova_service).to receive(:render_batch).with({ '0' => { From 8268f75db739d61c332f49fb50e51db1e4be6c2e Mon Sep 17 00:00:00 2001 From: Daniel Puscher Date: Fri, 13 Dec 2019 10:13:51 +0100 Subject: [PATCH 2/3] Update spec/controller_helpers_spec.rb Use hash rockets Co-Authored-By: Jordan Harband --- spec/controller_helpers_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controller_helpers_spec.rb b/spec/controller_helpers_spec.rb index d18ac21..ddffd95 100644 --- a/spec/controller_helpers_spec.rb +++ b/spec/controller_helpers_spec.rb @@ -109,7 +109,7 @@ def initialize allow(test).to receive(:will_send_request).and_raise(error) allow(test).to receive(:response).and_return(response) - expect(test).to receive(:on_error).with(error, nil, array_including(hash_including({name: "mordor.js"}))) + expect(test).to receive(:on_error).with(error, nil, array_including(hash_including({ :name => "mordor.js" }))) expect(batch).to receive(:submit_fallback!) test.hypernova_render_support {} From c20ae9a8126179fc8389b0ba2caa7590f99544cc Mon Sep 17 00:00:00 2001 From: Daniel Puscher Date: Fri, 13 Dec 2019 10:14:00 +0100 Subject: [PATCH 3/3] Update spec/controller_helpers_spec.rb Use hash rockets Co-Authored-By: Jordan Harband --- spec/controller_helpers_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controller_helpers_spec.rb b/spec/controller_helpers_spec.rb index ddffd95..e357083 100644 --- a/spec/controller_helpers_spec.rb +++ b/spec/controller_helpers_spec.rb @@ -137,7 +137,7 @@ def initialize test = TestClass.new batch = Hypernova::Batch.new(test.hypernova_service) response = TestResponse.new - job = { name: "mordor.js", data: {} } + job = { :name => "mordor.js", :data => {} } batch.render(job)