Satisfy style police

This commit is contained in:
Steve Pettitt
2016-02-24 20:39:26 +00:00
parent 8e88cd6255
commit b84f49a1c3
5 changed files with 56 additions and 47 deletions

View File

@@ -54,8 +54,12 @@ module InjectionHelper
def inject_orders_by_distributor
# Convert ActiveRecord::Relation to array for serialization
data_array = spree_current_user.orders_by_distributor.to_a
data_array.each{|enterprise| enterprise.distributed_orders.each{|order| order.payments.keep_if{|payment| payment.state == "completed"} }}
data_array.sort!{|a,b| b.distributed_orders.length <=> a.distributed_orders.length}
data_array.each do |enterprise|
enterprise.distributed_orders.each do |order|
order.payments.keep_if{ |payment| payment.state == "completed" }
end
end
data_array.sort!{ |a, b| b.distributed_orders.length <=> a.distributed_orders.length }
inject_json_ams "orders_by_distributor", data_array, Api::OrdersByDistributorSerializer
end

View File

@@ -56,7 +56,7 @@ Spree.user_class.class_eval do
# Returns orders and their associated payments for all distributors that have been ordered from
def orders_by_distributor
Enterprise.includes(distributed_orders: :payments).where(enterprises: {id: self.enterprises_ordered_from }, spree_orders: {state: 'complete', user_id: self.id}).order('spree_orders.completed_at DESC')
Enterprise.includes(distributed_orders: :payments).where(enterprises: { id: self.enterprises_ordered_from }, spree_orders: { state: 'complete', user_id: self.id }).order('spree_orders.completed_at DESC')
end
private

View File

@@ -1,29 +1,31 @@
class Api::OrderSerializer < ActiveModel::Serializer
attributes :number, :completed_at, :total, :state, :shipment_state, :payment_state, :outstanding_balance, :payments, :path
module Api
class OrderSerializer < ActiveModel::Serializer
attributes :number, :completed_at, :total, :state, :shipment_state, :payment_state, :outstanding_balance, :payments, :path
has_many :payments, serializer: Api::PaymentSerializer
has_many :payments, serializer: Api::PaymentSerializer
def completed_at
object.completed_at.blank? ? "" : I18n.l(object.completed_at, format: :long) #.to_formatted_s(:long_ordinal)
end
def completed_at
object.completed_at.blank? ? "" : I18n.l(object.completed_at, format: :long) #.to_formatted_s(:long_ordinal)
end
def total
object.total.to_money.to_s
end
def total
object.total.to_money.to_s
end
def shipment_state
object.shipment_state ? object.shipment_state.humanize : nil # Or a call to t() here?
end
def shipment_state
object.shipment_state ? object.shipment_state.humanize : nil # Or a call to t() here?
end
def payment_state
object.payment_state ? object.payment_state.humanize : nil # Or a call to t() here?
end
def payment_state
object.payment_state ? object.payment_state.humanize : nil # Or a call to t() here?
end
def state
object.state ? object.state.humanize : nil # Or a call to t() here?
end
def state
object.state ? object.state.humanize : nil # Or a call to t() here?
end
def path
Spree::Core::Engine.routes_url_helpers.order_url(object.number, only_path: true)
def path
Spree::Core::Engine.routes_url_helpers.order_url(object.number, only_path: true)
end
end
end

View File

@@ -1,17 +1,18 @@
class Api::OrdersByDistributorSerializer < ActiveModel::Serializer
attributes :name, :id, :hash, :balance, :logo, :distributed_orders
has_many :distributed_orders, serializer: Api::OrderSerializer
module Api
class OrdersByDistributorSerializer < ActiveModel::Serializer
attributes :name, :id, :hash, :balance, :logo, :distributed_orders
has_many :distributed_orders, serializer: Api::OrderSerializer
def balance
object.distributed_orders.map(&:outstanding_balance).reduce(:+).to_money.to_s
def balance
object.distributed_orders.map(&:outstanding_balance).reduce(:+).to_money.to_s
end
def hash
object.to_param
end
def logo
object.logo(:small) if object.logo?
end
end
def hash
object.to_param
end
def logo
object.logo(:small) if object.logo?
end
end

View File

@@ -1,14 +1,16 @@
class Api::PaymentSerializer < ActiveModel::Serializer
attributes :amount, :updated_at, :payment_method
def payment_method
object.payment_method.name
end
module Api
class PaymentSerializer < ActiveModel::Serializer
attributes :amount, :updated_at, :payment_method
def payment_method
object.payment_method.name
end
def amount
object.amount.to_money.to_s
end
def amount
object.amount.to_money.to_s
end
def updated_at
I18n.l(object.updated_at, format: :long)
def updated_at
I18n.l(object.updated_at, format: :long)
end
end
end