optimize DataPresenter#checkout_adjustments

reduce the complexity of the method
This commit is contained in:
Mohamed ABDELLANI
2023-10-30 21:53:59 +01:00
parent c8f69c8778
commit e16df2777a

View File

@@ -50,27 +50,19 @@ class Invoice
Time.zone.parse(data[:completed_at])
end
def checkout_adjustments(exclude: [], reject_zero_amount: true)
adjustments = all_eligible_adjustments.map(&:clone)
def checkout_adjustments(exclude: [])
adjustments = all_eligible_adjustments
.reject { |a| a.originator.type == 'Spree::TaxRate' }
.map(&:clone)
adjustments.reject! { |a| a.originator.type == 'Spree::TaxRate' }
adjustments.reject! { |a| a.amount == 0 }
[:line_item, :shipment].each do |type|
next unless exclude.include? type
if exclude.include? :line_item
adjustments.reject! { |a|
a.adjustable_type == 'Spree::LineItem'
a.adjustable_type == "Spree::#{type.to_s.classify}"
}
end
if exclude.include? :shipment
adjustments.reject! { |a|
a.adjustable_type == 'Spree::Shipment'
}
end
if reject_zero_amount
adjustments.reject! { |a| a.amount == 0 }
end
adjustments
end