mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-07 22:46:06 +00:00
Fix rubocop warnings on weight and weight_spec
This commit is contained in:
@@ -156,7 +156,6 @@ Layout/EmptyLines:
|
||||
- 'app/models/exchange.rb'
|
||||
- 'app/models/exchange_fee.rb'
|
||||
- 'app/models/inventory_item.rb'
|
||||
- 'app/models/open_food_network/calculator/weight.rb'
|
||||
- 'app/models/order_cycle.rb'
|
||||
- 'app/models/producer_property.rb'
|
||||
- 'app/models/product_distribution.rb'
|
||||
@@ -679,7 +678,6 @@ Layout/SpaceAroundOperators:
|
||||
- 'spec/jobs/update_billable_periods_spec.rb'
|
||||
- 'spec/lib/open_food_network/order_grouper_spec.rb'
|
||||
- 'spec/lib/stripe/account_connector_spec.rb'
|
||||
- 'spec/models/calculator/weight_spec.rb'
|
||||
- 'spec/spec_helper.rb'
|
||||
- 'spec/support/cancan_helper.rb'
|
||||
- 'spec/support/seeds.rb'
|
||||
@@ -1279,7 +1277,6 @@ Naming/VariableNumber:
|
||||
Exclude:
|
||||
- 'spec/archive/features/consumer/checkout_spec.rb'
|
||||
- 'spec/lib/open_food_network/products_and_inventory_report_spec.rb'
|
||||
- 'spec/models/calculator/weight_spec.rb'
|
||||
|
||||
# Offense count: 1
|
||||
Performance/Caller:
|
||||
@@ -1682,7 +1679,6 @@ Style/ClassAndModuleChildren:
|
||||
- 'app/controllers/spree/store_controller_decorator.rb'
|
||||
- 'app/helpers/angular_form_helper.rb'
|
||||
- 'app/models/calculator/flat_percent_per_item.rb'
|
||||
- 'app/models/open_food_network/calculator/weight.rb'
|
||||
- 'app/models/spree/gateway/migs.rb'
|
||||
- 'app/models/spree/gateway/pin.rb'
|
||||
- 'app/models/spree/preferences/file_configuration.rb'
|
||||
@@ -1932,7 +1928,6 @@ Style/HashSyntax:
|
||||
- 'app/models/enterprise_group.rb'
|
||||
- 'app/models/enterprise_role.rb'
|
||||
- 'app/models/exchange_variant.rb'
|
||||
- 'app/models/open_food_network/calculator/weight.rb'
|
||||
- 'app/models/order_cycle.rb'
|
||||
- 'app/models/product_distribution.rb'
|
||||
- 'app/models/spree/adjustment_decorator.rb'
|
||||
@@ -2022,7 +2017,6 @@ Style/HashSyntax:
|
||||
- 'spec/lib/open_food_network/order_grouper_spec.rb'
|
||||
- 'spec/lib/open_food_network/tag_rule_applicator_spec.rb'
|
||||
- 'spec/mailers/order_mailer_spec.rb'
|
||||
- 'spec/models/calculator/weight_spec.rb'
|
||||
- 'spec/models/enterprise_fee_spec.rb'
|
||||
- 'spec/models/enterprise_spec.rb'
|
||||
- 'spec/models/exchange_spec.rb'
|
||||
@@ -2288,7 +2282,6 @@ Style/RedundantSelf:
|
||||
- 'app/models/calculator/flat_percent_per_item.rb'
|
||||
- 'app/models/enterprise.rb'
|
||||
- 'app/models/exchange.rb'
|
||||
- 'app/models/open_food_network/calculator/weight.rb'
|
||||
- 'app/models/order_cycle.rb'
|
||||
- 'app/models/producer_property.rb'
|
||||
- 'app/models/spree/calculator/flat_percent_item_total_decorator.rb'
|
||||
|
||||
@@ -1,30 +1,31 @@
|
||||
module OpenFoodNetwork
|
||||
class Calculator::Weight < Spree::Calculator
|
||||
preference :per_kg, :decimal, :default => 0.0
|
||||
attr_accessible :preferred_per_kg
|
||||
module Calculator
|
||||
class Weight < Spree::Calculator
|
||||
preference :per_kg, :decimal, default: 0.0
|
||||
attr_accessible :preferred_per_kg
|
||||
|
||||
def self.description
|
||||
I18n.t('spree.weight')
|
||||
end
|
||||
def self.description
|
||||
I18n.t('spree.weight')
|
||||
end
|
||||
|
||||
def compute(object)
|
||||
line_items = line_items_for object
|
||||
total_weight = line_items.sum { |li| ((li.variant.andand.weight || 0) * li.quantity) }
|
||||
total_weight * self.preferred_per_kg
|
||||
end
|
||||
def compute(object)
|
||||
line_items = line_items_for object
|
||||
total_weight = line_items.sum { |li| ((li.variant.andand.weight || 0) * li.quantity) }
|
||||
total_weight * preferred_per_kg
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
private
|
||||
|
||||
def line_items_for(object)
|
||||
if object.respond_to? :order
|
||||
object.order.line_items
|
||||
elsif object.respond_to? :line_items
|
||||
object.line_items
|
||||
elsif object.respond_to?(:variant) && object.respond_to?(:quantity)
|
||||
[object]
|
||||
else
|
||||
raise "Unknown object type: #{object.inspect}"
|
||||
def line_items_for(object)
|
||||
if object.respond_to? :order
|
||||
object.order.line_items
|
||||
elsif object.respond_to? :line_items
|
||||
object.line_items
|
||||
elsif object.respond_to?(:variant) && object.respond_to?(:quantity)
|
||||
[object]
|
||||
else
|
||||
raise "Unknown object type: #{object.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,40 +2,40 @@ require 'spec_helper'
|
||||
|
||||
describe OpenFoodNetwork::Calculator::Weight do
|
||||
it "computes shipping cost for an order by total weight" do
|
||||
variant_1 = double(:variant, :weight => 10)
|
||||
variant_2 = double(:variant, :weight => 20)
|
||||
variant_3 = double(:variant, :weight => nil)
|
||||
variant1 = double(:variant, weight: 10)
|
||||
variant2 = double(:variant, weight: 20)
|
||||
variant3 = double(:variant, weight: nil)
|
||||
|
||||
line_item_1 = double(:line_item, :variant => variant_1, :quantity => 1)
|
||||
line_item_2 = double(:line_item, :variant => variant_2, :quantity => 3)
|
||||
line_item_3 = double(:line_item, :variant => variant_3, :quantity => 5)
|
||||
line_item1 = double(:line_item, variant: variant1, quantity: 1)
|
||||
line_item2 = double(:line_item, variant: variant2, quantity: 3)
|
||||
line_item3 = double(:line_item, variant: variant3, quantity: 5)
|
||||
|
||||
order = double(:order, :line_items => [line_item_1, line_item_2, line_item_3])
|
||||
order = double(:order, line_items: [line_item1, line_item2, line_item3])
|
||||
|
||||
subject.set_preference(:per_kg, 10)
|
||||
expect(subject.compute(order)).to eq((10*1 + 20*3) * 10)
|
||||
expect(subject.compute(order)).to eq((10 * 1 + 20 * 3) * 10)
|
||||
end
|
||||
|
||||
it "computes shipping cost for a line item" do
|
||||
variant = double(:variant, :weight => 10)
|
||||
variant = double(:variant, weight: 10)
|
||||
|
||||
line_item = double(:line_item, :variant => variant, :quantity => 2)
|
||||
line_item = double(:line_item, variant: variant, quantity: 2)
|
||||
|
||||
subject.set_preference(:per_kg, 10)
|
||||
expect(subject.compute(line_item)).to eq(10*2 * 10)
|
||||
expect(subject.compute(line_item)).to eq(10 * 2 * 10)
|
||||
end
|
||||
|
||||
it "computes shipping cost for an object with an order" do
|
||||
variant_1 = double(:variant, :weight => 10)
|
||||
variant_2 = double(:variant, :weight => 5)
|
||||
variant1 = double(:variant, weight: 10)
|
||||
variant2 = double(:variant, weight: 5)
|
||||
|
||||
line_item_1 = double(:line_item, :variant => variant_1, :quantity => 1)
|
||||
line_item_2 = double(:line_item, :variant => variant_2, :quantity => 2)
|
||||
line_item1 = double(:line_item, variant: variant1, quantity: 1)
|
||||
line_item2 = double(:line_item, variant: variant2, quantity: 2)
|
||||
|
||||
order = double(:order, :line_items => [line_item_1, line_item_2])
|
||||
order = double(:order, line_items: [line_item1, line_item2])
|
||||
object_with_order = double(:object_with_order, order: order)
|
||||
|
||||
subject.set_preference(:per_kg, 10)
|
||||
expect(subject.compute(object_with_order)).to eq((10*1 + 5*2) * 10)
|
||||
expect(subject.compute(object_with_order)).to eq((10 * 1 + 5 * 2) * 10)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user