Skip to content

Commit

Permalink
update WeightsAndMeasures#scales_for_variant_unit to ignore available…
Browse files Browse the repository at this point in the history
… untis when needed
  • Loading branch information
abdellani committed Nov 13, 2023
1 parent b872d7c commit 847c3af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/services/weights_and_measures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def scale_for_unit_value
end

def system
return "custom" unless scales = scales_for_variant_unit
return "custom" unless scales = scales_for_variant_unit(ignore_available_units: true)
return "custom" unless product_scale = @variant.product.variant_unit_scale

scales[product_scale.to_f]['system']
Expand Down Expand Up @@ -45,8 +45,10 @@ def system
}
}.freeze

def scales_for_variant_unit
@scales_for_variant_unit ||= @units[@variant.product.variant_unit]&.reject { |_scale, unit_info|
def scales_for_variant_unit(ignore_available_units: false)
return @units[@variant.product.variant_unit] if ignore_available_units

@units[@variant.product.variant_unit]&.reject { |_scale, unit_info|
available_units.exclude?(unit_info['name'])
}
end
Expand Down
12 changes: 12 additions & 0 deletions spec/controllers/api/v0/order_cycles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ module Api
expect(product_ids).to include product1.id, product2.id, product3.id
end

context "when product's variant unit scale is not in the available units list" do
before do
allow(Spree::Config).to receive(:available_units).and_return("lb,oz,kg,T,mL,L,kL")
end

it "loads products for distributed products in the order cycle" do
api_get :products, id: order_cycle.id, distributor: distributor.id

expect(product_ids).to include product1.id, product2.id, product3.id
end
end

it "returns products that were searched for" do
ransack_param = "name_or_meta_keywords_or_variants_display_as_or_" \
"variants_display_name_or_supplier_name_cont"
Expand Down

0 comments on commit 847c3af

Please sign in to comment.