Data manipulation and balance calc in rails instead

This commit is contained in:
stveep
2015-12-21 17:40:36 +00:00
parent 7e50cc9ce8
commit 90e627ad60
5 changed files with 22 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
= inject_orders_for_user
= inject_orders_by_distributor
.row.pad-top
.small-12.columns.pad-top