From faf7079780ccf93bd6357dfeb8c1971887ab534d Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Mon, 1 Oct 2018 13:58:51 +0800 Subject: [PATCH] Specify sort order of enterprise fee totals data --- .../report_data/enterprise_fee_type_total.rb | 17 +++++++ .../enterprise_fee_type_total_spec.rb | 48 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 spec/lib/order_management/reports/enterprise_fee_summary/report_data/enterprise_fee_type_total_spec.rb diff --git a/lib/order_management/reports/enterprise_fee_summary/report_data/enterprise_fee_type_total.rb b/lib/order_management/reports/enterprise_fee_summary/report_data/enterprise_fee_type_total.rb index 4a931a8b46..2a15ab8a8e 100644 --- a/lib/order_management/reports/enterprise_fee_summary/report_data/enterprise_fee_type_total.rb +++ b/lib/order_management/reports/enterprise_fee_summary/report_data/enterprise_fee_type_total.rb @@ -7,6 +7,23 @@ module OrderManagement class EnterpriseFeeTypeTotal < OpenFoodNetwork::Reports::ReportData::Base attr_accessor :fee_type, :enterprise_name, :fee_name, :customer_name, :fee_placement, :fee_calculated_on_transfer_through_name, :tax_category_name, :total_amount + + def <=>(other) + self.class.sortable_data(self) <=> self.class.sortable_data(other) + end + + def self.sortable_data(instance) + [ + instance.fee_type, + instance.enterprise_name, + instance.fee_name, + instance.customer_name, + instance.fee_placement, + instance.fee_calculated_on_transfer_through_name, + instance.tax_category_name, + instance.total_amount + ] + end end end end diff --git a/spec/lib/order_management/reports/enterprise_fee_summary/report_data/enterprise_fee_type_total_spec.rb b/spec/lib/order_management/reports/enterprise_fee_summary/report_data/enterprise_fee_type_total_spec.rb new file mode 100644 index 0000000000..85fd826613 --- /dev/null +++ b/spec/lib/order_management/reports/enterprise_fee_summary/report_data/enterprise_fee_type_total_spec.rb @@ -0,0 +1,48 @@ +require "spec_helper" + +require "order_management/reports/enterprise_fee_summary/report_data/enterprise_fee_type_total" + +describe OrderManagement::Reports::EnterpriseFeeSummary::ReportData::EnterpriseFeeTypeTotal do + it "sorts instances according to their attributes" do + instance_a = described_class.new( + fee_type: "sales", + enterprise_name: "Enterprise A", + fee_name: "A Sales", + customer_name: "Customer A", + fee_placement: "Incoming", + fee_calculated_on_transfer_through_name: "Transfer Enterprise B", + tax_category_name: "Sales 4%", + total_amount: "12.00" + ) + + instance_b = described_class.new( + fee_type: "sales", + enterprise_name: "Enterprise A", + fee_name: "B Sales", + customer_name: "Customer A", + fee_placement: "Incoming", + fee_calculated_on_transfer_through_name: "Transfer Enterprise B", + tax_category_name: "Sales 4%", + total_amount: "12.00" + ) + + instance_c = described_class.new( + fee_type: "admin", + enterprise_name: "Enterprise A", + fee_name: "C Admin", + customer_name: "Customer B", + fee_placement: "Incoming", + fee_calculated_on_transfer_through_name: nil, + tax_category_name: "Sales 6%", + total_amount: "12.00" + ) + + list = [ + instance_a, + instance_b, + instance_c + ] + + expect(list.sort).to eq([instance_c, instance_a, instance_b]) + end +end