From ca5a5bf3013be58ab7ba18c5dea01347bb4c144c Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 24 Jul 2019 09:37:39 +0100 Subject: [PATCH 1/6] Convert oc.rabl to oc serializer and use it in both haml file and controller --- app/controllers/shop_controller.rb | 4 ++-- app/helpers/injection_helper.rb | 4 ++++ app/serializers/api/order_cycle_serializer.rb | 11 +++++++++++ app/views/json/_order_cycle.rabl | 3 --- app/views/shopping_shared/_order_cycles.html.haml | 5 ++--- 5 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 app/serializers/api/order_cycle_serializer.rb delete mode 100644 app/views/json/_order_cycle.rabl diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index de783a1eb3..1d9d727aa2 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -27,12 +27,12 @@ class ShopController < BaseController if oc = OrderCycle.with_distributor(@distributor).active.find_by_id(params[:order_cycle_id]) current_order(true).set_order_cycle! oc @current_order_cycle = oc - render partial: "json/order_cycle" + render json: @current_order_cycle, serializer: Api::OrderCycleSerializer else render status: :not_found, json: "" end else - render partial: "json/order_cycle" + render json: current_order_cycle, serializer: Api::OrderCycleSerializer end end diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index aa85939fbe..4e1d1cd1ff 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -61,6 +61,10 @@ module InjectionHelper inject_json_ams "currentOrder", current_order, Api::CurrentOrderSerializer, current_distributor: current_distributor, current_order_cycle: current_order_cycle end + def inject_current_order_cycle + inject_json_ams "orderCycleData", current_order_cycle, Api::OrderCycleSerializer + end + def inject_available_shipping_methods inject_json_ams "shippingMethods", available_shipping_methods, Api::ShippingMethodSerializer, current_order: current_order diff --git a/app/serializers/api/order_cycle_serializer.rb b/app/serializers/api/order_cycle_serializer.rb new file mode 100644 index 0000000000..beb7af4e52 --- /dev/null +++ b/app/serializers/api/order_cycle_serializer.rb @@ -0,0 +1,11 @@ +class Api::OrderCycleSerializer < ActiveModel::Serializer + attributes :order_cycle_id, :orders_close_at + + def order_cycle_id + object.id + end + + def orders_close_at + object.orders_close_at.to_s + end +end diff --git a/app/views/json/_order_cycle.rabl b/app/views/json/_order_cycle.rabl deleted file mode 100644 index 0f5d0c990a..0000000000 --- a/app/views/json/_order_cycle.rabl +++ /dev/null @@ -1,3 +0,0 @@ -object current_order_cycle -attributes :orders_close_at -attribute id: :order_cycle_id diff --git a/app/views/shopping_shared/_order_cycles.html.haml b/app/views/shopping_shared/_order_cycles.html.haml index 7a8fe772f9..f8a60b14a3 100644 --- a/app/views/shopping_shared/_order_cycles.html.haml +++ b/app/views/shopping_shared/_order_cycles.html.haml @@ -1,6 +1,5 @@ -- content_for :scripts do - :javascript - angular.module('Darkswarm').value('orderCycleData', #{render "json/order_cycle"}) +- content_for :injection_data do + = inject_current_order_cycle %ordercycle{"ng-controller" => "OrderCycleCtrl"} From 3c0e6eeee28f5e295489e376a93b5b94f00a6bd7 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 24 Jul 2019 11:06:55 +0100 Subject: [PATCH 2/6] Make inject_current_order_cycle render {} instad of null if current_order_cycle is null --- app/helpers/injection_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 4e1d1cd1ff..4a026a7422 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -62,7 +62,9 @@ module InjectionHelper end def inject_current_order_cycle - inject_json_ams "orderCycleData", current_order_cycle, Api::OrderCycleSerializer + serializer = Api::OrderCycleSerializer.new(current_order_cycle) + json = serializer.object.present? ? serializer.to_json : "{}" + render partial: "json/injection_ams", locals: { name: "orderCycleData", json: json } end def inject_available_shipping_methods From c45194473bd0116afc05d17fde865da4b3ece727 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 29 Jul 2019 18:14:00 +0100 Subject: [PATCH 3/6] Add spec to cover inject_current_order_cycle --- spec/helpers/injection_helper_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/helpers/injection_helper_spec.rb b/spec/helpers/injection_helper_spec.rb index 204f5977a6..b502b9b737 100644 --- a/spec/helpers/injection_helper_spec.rb +++ b/spec/helpers/injection_helper_spec.rb @@ -49,6 +49,18 @@ describe InjectionHelper, type: :helper do expect(helper.inject_current_order).to match order.id.to_s end + describe "injects current order cycle" do + it "injects empty json object (not nil) when current OC is null" do + allow(helper).to receive(:current_order_cycle).and_return nil + expect(helper.inject_current_order_cycle).to match "{}" + end + + it "injects current OC when OC not null" do + allow(helper).to receive(:current_order_cycle).and_return order_cycle = create(:simple_order_cycle) + expect(helper.inject_current_order_cycle).to match order_cycle.id.to_s + end + end + it "injects taxons" do taxon = create(:taxon) expect(helper.inject_taxons).to match taxon.name From 2c6dab9c856362f000ab373017b65a2e50586253 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 29 Jul 2019 18:56:49 +0100 Subject: [PATCH 4/6] Add spec for Api::OrderCycleSerializer --- .../api/order_cycle_serializer_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 spec/serializers/api/order_cycle_serializer_spec.rb diff --git a/spec/serializers/api/order_cycle_serializer_spec.rb b/spec/serializers/api/order_cycle_serializer_spec.rb new file mode 100644 index 0000000000..04206e71f2 --- /dev/null +++ b/spec/serializers/api/order_cycle_serializer_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Api::OrderCycleSerializer do + let(:order_cycle) { create(:simple_order_cycle) } + let(:serializer) { Api::OrderCycleSerializer.new(order_cycle).to_json } + + it "serializers the OC id as order_cycle_id" do + expect(serializer).to match "order_cycle_id" + expect(serializer).to match order_cycle.id.to_s + end + + it "includes orders_close_at" do + expect(serializer).to match "orders_close_at" + expect(serializer).to match order_cycle.orders_close_at.to_date.to_s + end +end From 8868b7eb1245ad295dcb625ec6e02a5eb5c3aec5 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 29 Jul 2019 19:05:39 +0100 Subject: [PATCH 5/6] Fix rubocop issue: use nested class/module definition --- app/serializers/api/order_cycle_serializer.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/serializers/api/order_cycle_serializer.rb b/app/serializers/api/order_cycle_serializer.rb index beb7af4e52..f01d79a931 100644 --- a/app/serializers/api/order_cycle_serializer.rb +++ b/app/serializers/api/order_cycle_serializer.rb @@ -1,11 +1,13 @@ -class Api::OrderCycleSerializer < ActiveModel::Serializer - attributes :order_cycle_id, :orders_close_at +module Api + class OrderCycleSerializer < ActiveModel::Serializer + attributes :order_cycle_id, :orders_close_at - def order_cycle_id - object.id - end + def order_cycle_id + object.id + end - def orders_close_at - object.orders_close_at.to_s + def orders_close_at + object.orders_close_at.to_s + end end end From be41271038c62411a7090a1340e349e03e6fd3d3 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 29 Jul 2019 19:57:12 +0100 Subject: [PATCH 6/6] Make spelling coherent, as we have serializers with Z we get rid of the few serialisers with S we have --- app/controllers/admin/tag_rules_controller.rb | 4 ++-- .../serializers/api/enterprise_shopfront_serializer_spec.rb | 6 +++--- spec/serializers/api/group_list_serializer_spec.rb | 2 +- spec/serializers/api/order_cycle_serializer_spec.rb | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/tag_rules_controller.rb b/app/controllers/admin/tag_rules_controller.rb index c3f0730ccc..641790d0f0 100644 --- a/app/controllers/admin/tag_rules_controller.rb +++ b/app/controllers/admin/tag_rules_controller.rb @@ -9,8 +9,8 @@ module Admin def map_by_tag respond_to do |format| format.json do - serialiser = ActiveModel::ArraySerializer.new(collection) - render json: serialiser.to_json + serializer = ActiveModel::ArraySerializer.new(collection) + render json: serializer.to_json end end end diff --git a/spec/serializers/api/enterprise_shopfront_serializer_spec.rb b/spec/serializers/api/enterprise_shopfront_serializer_spec.rb index eb303cb49d..5a2ef88123 100644 --- a/spec/serializers/api/enterprise_shopfront_serializer_spec.rb +++ b/spec/serializers/api/enterprise_shopfront_serializer_spec.rb @@ -32,17 +32,17 @@ describe Api::EnterpriseShopfrontSerializer do expect(serializer.serializable_hash[:delivery]).to eq true end - it "serialises an array of hubs" do + it "serializes an array of hubs" do expect(serializer.serializable_hash[:hubs]).to be_a ActiveModel::ArraySerializer expect(serializer.serializable_hash[:hubs].to_json).to match hub.name end - it "serialises an array of producers" do + it "serializes an array of producers" do expect(serializer.serializable_hash[:producers]).to be_a ActiveModel::ArraySerializer expect(serializer.serializable_hash[:producers].to_json).to match producer.name end - it "serialises taxons" do + it "serializes taxons" do expect(serializer.serializable_hash[:taxons]).to be_a ActiveModel::ArraySerializer expect(serializer.serializable_hash[:taxons].to_json).to match 'Meat' expect(serializer.serializable_hash[:taxons].to_json).to match 'Veg' diff --git a/spec/serializers/api/group_list_serializer_spec.rb b/spec/serializers/api/group_list_serializer_spec.rb index df69cf75c1..41c805d19c 100644 --- a/spec/serializers/api/group_list_serializer_spec.rb +++ b/spec/serializers/api/group_list_serializer_spec.rb @@ -18,7 +18,7 @@ describe Api::GroupListSerializer do expect(serializer.serializable_hash[:state]).to eq group.address.state.abbr end - it "serialises an array of enterprises" do + it "serializes an array of enterprises" do expect(serializer.serializable_hash[:enterprises]).to be_a ActiveModel::ArraySerializer expect(serializer.serializable_hash[:enterprises].to_json).to match producer.name end diff --git a/spec/serializers/api/order_cycle_serializer_spec.rb b/spec/serializers/api/order_cycle_serializer_spec.rb index 04206e71f2..9e1700b4ea 100644 --- a/spec/serializers/api/order_cycle_serializer_spec.rb +++ b/spec/serializers/api/order_cycle_serializer_spec.rb @@ -4,7 +4,7 @@ describe Api::OrderCycleSerializer do let(:order_cycle) { create(:simple_order_cycle) } let(:serializer) { Api::OrderCycleSerializer.new(order_cycle).to_json } - it "serializers the OC id as order_cycle_id" do + it "serializes the OC id as order_cycle_id" do expect(serializer).to match "order_cycle_id" expect(serializer).to match order_cycle.id.to_s end