From 158bdd145fe0996b0ab7889fc2376f63773cac72 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Thu, 17 Jul 2014 14:23:09 +1000 Subject: [PATCH] All the new serializers and some specs --- .../services/payment_methods.js.coffee | 8 ++++ .../services/shipping_methods.js.coffee | 8 ++++ app/helpers/injection_helper.rb | 36 +++++++++++++++ .../api/current_order_serializer.rb | 13 ++++++ app/serializers/api/line_item_serializer.rb | 3 ++ .../api/payment_method_serializer.rb | 3 ++ .../api/shipping_method_serializer.rb | 8 ++++ app/views/checkout/_payment.html.haml | 4 +- spec/helpers/injection_helper_spec.rb | 44 +++++++++++++++++++ .../darkswarm/services/cart_spec.js.coffee | 9 ++++ 10 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/darkswarm/services/payment_methods.js.coffee create mode 100644 app/assets/javascripts/darkswarm/services/shipping_methods.js.coffee create mode 100644 app/helpers/injection_helper.rb create mode 100644 app/serializers/api/current_order_serializer.rb create mode 100644 app/serializers/api/line_item_serializer.rb create mode 100644 app/serializers/api/payment_method_serializer.rb create mode 100644 app/serializers/api/shipping_method_serializer.rb create mode 100644 spec/helpers/injection_helper_spec.rb create mode 100644 spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee diff --git a/app/assets/javascripts/darkswarm/services/payment_methods.js.coffee b/app/assets/javascripts/darkswarm/services/payment_methods.js.coffee new file mode 100644 index 0000000000..8b44d3e80a --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/payment_methods.js.coffee @@ -0,0 +1,8 @@ +Darkswarm.factory "PaymentMethods", (paymentMethods)-> + new class PaymentMethods + payment_methods: paymentMethods + payment_methods_by_id: {} + constructor: -> + for method in @payment_methods + @payment_methods_by_id[method.id] = method + diff --git a/app/assets/javascripts/darkswarm/services/shipping_methods.js.coffee b/app/assets/javascripts/darkswarm/services/shipping_methods.js.coffee new file mode 100644 index 0000000000..3980aa5fed --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/shipping_methods.js.coffee @@ -0,0 +1,8 @@ +Darkswarm.factory "ShippingMethods", (shippingMethods)-> + new class ShippingMethods + shipping_methods: shippingMethods + shipping_methods_by_id: {} + constructor: -> + for method in @shipping_methods + @shipping_methods_by_id[method.id] = method + diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb new file mode 100644 index 0000000000..183d8e128f --- /dev/null +++ b/app/helpers/injection_helper.rb @@ -0,0 +1,36 @@ +module InjectionHelper + def inject_enterprises + inject_json_ams "enterprises", Enterprise.all, Api::EnterpriseSerializer, active_distributors: @active_distributors + end + + def inject_current_order + inject_json_ams "currentOrder", current_order, Api::CurrentOrderSerializer + end + + def inject_available_shipping_methods + inject_json_ams "shippingMethods", available_shipping_methods, + Api::ShippingMethodSerializer, current_order: current_order + end + + def inject_available_payment_methods + inject_json_ams "paymentMethods", current_order.available_payment_methods, + Api::PaymentMethodSerializer + end + + def inject_taxons + inject_json_ams "taxons", Spree::Taxon.all, Api::TaxonSerializer + end + + def inject_json(name, partial, opts = {}) + render partial: "json/injection", locals: {name: name, partial: partial}.merge(opts) + end + + def inject_json_ams(name, data, serializer, opts = {}) + if data.is_a?(Array) + json = ActiveModel::ArraySerializer.new(data, {each_serializer: serializer}.merge(opts)).to_json + else + json = serializer.new(data, opts).to_json + end + render partial: "json/injection_ams", locals: {name: name, json: json} + end +end diff --git a/app/serializers/api/current_order_serializer.rb b/app/serializers/api/current_order_serializer.rb new file mode 100644 index 0000000000..6275710789 --- /dev/null +++ b/app/serializers/api/current_order_serializer.rb @@ -0,0 +1,13 @@ +class Api::CurrentOrderSerializer < ActiveModel::Serializer + attributes :id, :item_total, :email, :shipping_method_id, + :display_total, :payment_method_id + + has_one :bill_address, serializer: Api::AddressSerializer + has_one :ship_address, serializer: Api::AddressSerializer + + has_many :line_items, serializer: Api::LineItemSerializer + + def payment_method_id + object.payments.first.andand.payment_method_id + end +end diff --git a/app/serializers/api/line_item_serializer.rb b/app/serializers/api/line_item_serializer.rb new file mode 100644 index 0000000000..f0d2c0de63 --- /dev/null +++ b/app/serializers/api/line_item_serializer.rb @@ -0,0 +1,3 @@ +class Api::LineItemSerializer < ActiveModel::Serializer + attributes :id, :variant_id, :quantity, :price +end diff --git a/app/serializers/api/payment_method_serializer.rb b/app/serializers/api/payment_method_serializer.rb new file mode 100644 index 0000000000..6c2203083d --- /dev/null +++ b/app/serializers/api/payment_method_serializer.rb @@ -0,0 +1,3 @@ +class Api::PaymentMethodSerializer < ActiveModel::Serializer + attributes :name, :id, :method_type +end diff --git a/app/serializers/api/shipping_method_serializer.rb b/app/serializers/api/shipping_method_serializer.rb new file mode 100644 index 0000000000..fc17366a89 --- /dev/null +++ b/app/serializers/api/shipping_method_serializer.rb @@ -0,0 +1,8 @@ +class Api::ShippingMethodSerializer < ActiveModel::Serializer + attributes :id, :require_ship_address, :name, :description, + :price + + def price + object.compute_amount(options[:current_order]) + end +end diff --git a/app/views/checkout/_payment.html.haml b/app/views/checkout/_payment.html.haml index c3c2ad8d4c..789127a39b 100644 --- a/app/views/checkout/_payment.html.haml +++ b/app/views/checkout/_payment.html.haml @@ -23,6 +23,7 @@ %span.accordion-down.right %i.ofn-i_005-caret-down + -# TODO render this in Angular instead of server-side - current_order.available_payment_methods.each do |method| .row .small-12.columns @@ -31,7 +32,8 @@ required: true, "ng-model" => "order.payment_method_id" = method.name - .row{"ng-if" => "order.payment_method_id == #{method.id}"} + + .row{"ng-if" => "order.payment_method_id == method.id"} .small-12.columns = render partial: "spree/checkout/payment/#{method.method_type}", :locals => { :payment_method => method } diff --git a/spec/helpers/injection_helper_spec.rb b/spec/helpers/injection_helper_spec.rb new file mode 100644 index 0000000000..246f18d330 --- /dev/null +++ b/spec/helpers/injection_helper_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +describe InjectionHelper do + let!(:enterprise) { create(:distributor_enterprise, facebook: "roger") } + + it "will inject via AMS" do + helper.inject_json_ams("test", [enterprise], Api::EnterpriseSerializer).should match enterprise.name + end + + it "injects enterprises" do + helper.inject_enterprises.should match enterprise.name + helper.inject_enterprises.should match enterprise.facebook + end + + it "injects shipping_methods" do + sm = create(:shipping_method) + helper.stub(:current_order).and_return order = create(:order) + helper.stub_chain(:current_distributor, :shipping_methods, :uniq).and_return [sm] + helper.inject_available_shipping_methods.should match sm.id.to_s + helper.inject_available_shipping_methods.should match sm.compute_amount(order).to_s + end + + it "injects payment methods" do + pm = create(:payment_method) + helper.stub_chain(:current_order, :available_payment_methods).and_return [pm] + helper.inject_available_payment_methods.should match pm.id.to_s + helper.inject_available_payment_methods.should match pm.name + end + + it "injects current order" do + helper.stub(:current_order).and_return order = create(:order) + helper.inject_current_order.should match order.id.to_s + end + + it "injects taxons" do + taxon = create(:taxon) + helper.inject_taxons.should match taxon.name + end + + it "injects taxons" do + taxon = create(:taxon) + helper.inject_taxons.should match taxon.name + end +end diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee new file mode 100644 index 0000000000..595592cdec --- /dev/null +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -0,0 +1,9 @@ +describe 'Cart service', -> + Cart = null + orders = [] + + beforeEach -> + module 'Darkswarm' + angular.module('Darkswarm').value('order', orders) + inject ($injector)-> + Cart = $injector.get("Cart")