All the new serializers and some specs

This commit is contained in:
Will Marshall
2014-07-17 14:23:09 +10:00
parent 547d9f9f5c
commit 158bdd145f
10 changed files with 135 additions and 1 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,3 @@
class Api::LineItemSerializer < ActiveModel::Serializer
attributes :id, :variant_id, :quantity, :price
end

View File

@@ -0,0 +1,3 @@
class Api::PaymentMethodSerializer < ActiveModel::Serializer
attributes :name, :id, :method_type
end

View File

@@ -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

View File

@@ -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 }

View File

@@ -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

View File

@@ -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")