From 90e627ad60b074109bf87ee62a534d00568cf83f Mon Sep 17 00:00:00 2001 From: stveep Date: Mon, 21 Dec 2015 17:40:36 +0000 Subject: [PATCH] Data manipulation and balance calc in rails instead --- app/helpers/injection_helper.rb | 7 +++++++ app/models/spree/user_decorator.rb | 6 ++++++ app/serializers/api/order_serializer.rb | 3 --- app/serializers/api/orders_by_distributor_serializer.rb | 8 ++++++++ app/views/spree/users/show.html.haml | 1 + 5 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 app/serializers/api/orders_by_distributor_serializer.rb diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 0354bcdeab..e5c311b7a3 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -56,6 +56,13 @@ module InjectionHelper inject_json_ams "orders", spree_current_user.orders.where(state: :complete).to_a, Api::OrderSerializer end + def inject_orders_by_distributor + #render partial: "json/injection_ams", locals: {name:"orders_by_distributor", json: spree_current_user.orders_by_distributor} + inject_json_ams "orders_by_distributor", + Enterprise.includes(:distributed_orders).where(id: spree_current_user.enterprises_ordered_from).to_a, + Api::OrdersByDistributorSerializer + end + def inject_json(name, partial, opts = {}) render partial: "json/injection", locals: {name: name, partial: partial}.merge(opts) end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index b724a41d1b..aa61e63e33 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -49,6 +49,12 @@ Spree.user_class.class_eval do owned_enterprises(:reload).size < enterprise_limit end + # Returns Enterprise IDs for distributors that the user has shopped at + def enterprises_ordered_from + self.orders.where(state: :complete).map(&:distributor_id).uniq + end + + private def limit_owned_enterprises diff --git a/app/serializers/api/order_serializer.rb b/app/serializers/api/order_serializer.rb index d7fb8e6b6d..fb1c3f28d3 100644 --- a/app/serializers/api/order_serializer.rb +++ b/app/serializers/api/order_serializer.rb @@ -1,9 +1,6 @@ class Api::OrderSerializer < ActiveModel::Serializer attributes :id, :completed_at, :total, :state, :shipment_state, :payment_state, :outstanding_balance, :total_money, :balance_money - has_one :distributor, serializer: Api::IdNameSerializer - - def completed_at object.completed_at.blank? ? "" : object.completed_at.strftime("%F %T") end diff --git a/app/serializers/api/orders_by_distributor_serializer.rb b/app/serializers/api/orders_by_distributor_serializer.rb new file mode 100644 index 0000000000..35315a5cc9 --- /dev/null +++ b/app/serializers/api/orders_by_distributor_serializer.rb @@ -0,0 +1,8 @@ +class Api::OrdersByDistributorSerializer < ActiveModel::Serializer + attributes :name, :balance, :distributed_orders + has_many :distributed_orders, serializer: Api::OrderSerializer + + def balance + object.distributed_orders.map(&:outstanding_balance).reduce(:+).to_money.to_s + end +end diff --git a/app/views/spree/users/show.html.haml b/app/views/spree/users/show.html.haml index 55ccf737a0..a9e82ebfce 100644 --- a/app/views/spree/users/show.html.haml +++ b/app/views/spree/users/show.html.haml @@ -1,4 +1,5 @@ = inject_orders_for_user += inject_orders_by_distributor .row.pad-top .small-12.columns.pad-top