Skip to content

Commit

Permalink
[wip] Generate variant unit options in Ruby
Browse files Browse the repository at this point in the history
This re-implements Angular JS function VariantUnitManager.variantUnitOptions()
  • Loading branch information
dacook committed Feb 21, 2024
1 parent 4abae4c commit c71d850
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
28 changes: 26 additions & 2 deletions app/services/weights_and_measures.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class WeightsAndMeasures
include ActionView::Helpers::NumberHelper

def initialize(variant)
@variant = variant
@units = UNITS
Expand All @@ -20,6 +22,28 @@ def system
scales[product_scale.to_f]['system']
end

# @returns enumerable with label and value for select
def self.variant_unit_options
self::UNITS.flat_map do |measurement, measurement_info|
measurement_info.filter_map do |scale, unit_info|
return unless self.available_units.include?(unit_info['name'])

Check warning on line 29 in app/services/weights_and_measures.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Non-local exit from iterator, without return value. `next`, `break`, `Array#find`, `Array#any?`, etc. is preferred. Raw Output: app/services/weights_and_measures.rb:29:9: W: Lint/NonLocalExitFromIterator: Non-local exit from iterator, without return value. `next`, `break`, `Array#find`, `Array#any?`, etc. is preferred.

Check warning on line 29 in app/services/weights_and_measures.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Redundant `self` detected. Raw Output: app/services/weights_and_measures.rb:29:23: C: Style/RedundantSelf: Redundant `self` detected.

# scale_clean = "%0.3g" % scale # 🆇 converts large numbers to scientific notation
# scale_clean = number_with_precision(scale, strip_insignificant_zeros: true) # why oh why won't the numbe rhelper work??

Check warning on line 32 in app/services/weights_and_measures.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Line is too long. [129/100] Raw Output: app/services/weights_and_measures.rb:32:101: C: Layout/LineLength: Line is too long. [129/100]
scale_clean = scale

[
"#{I18n.t(measurement)} (#{unit_info['name']})", # Label (eg "Weight (g)")
"#{measurement}_#{scale_clean}", # Scale ID (eg "weight_1")
]
end
end <<
[
I18n.t('items'),
'items'
]
end

private

UNITS = {
Expand Down Expand Up @@ -49,7 +73,7 @@ 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'])
self.available_units.exclude?(unit_info['name'])

Check warning on line 76 in app/services/weights_and_measures.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Redundant `self` detected. Raw Output: app/services/weights_and_measures.rb:76:7: C: Style/RedundantSelf: Redundant `self` detected.
}
end

Expand All @@ -68,7 +92,7 @@ def find_largest_unit(scales, product_scale_system)
largest_unit
end

def available_units
def self.available_units

Check warning on line 95 in app/services/weights_and_measures.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 `private` (on line 47) does not make singleton methods private. Use `private_class_method` or `private` inside a `class << self` block instead. Raw Output: app/services/weights_and_measures.rb:95:3: W: Lint/IneffectiveAccessModifier: `private` (on line 47) does not make singleton methods private. Use `private_class_method` or `private` inside a `class << self` block instead.
Spree::Config.available_units.split(",")
end
end
13 changes: 8 additions & 5 deletions app/views/admin/products_v3/_product_row.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
%td.field
= f.text_field :sku, 'aria-label': t('admin.products_page.columns.sku')
= error_message_on product, :sku
%td.align-right
.content
= product.variant_unit.upcase_first
/ TODO: properly handle custom unit names
= WeightsAndMeasures::UNITS[product.variant_unit] && "(" + WeightsAndMeasures::UNITS[product.variant_unit][product.variant_unit_scale]["name"] + ")"
%td.field
= product.variant_unit.upcase_first
/ TODO: properly handle custom unit names
= WeightsAndMeasures::UNITS[product.variant_unit] && "(" + WeightsAndMeasures::UNITS[product.variant_unit][product.variant_unit_scale]["name"] + ")"
- if WeightsAndMeasures::UNITS[product.variant_unit]
= select_tag :variant_unit_with_scale, WeightsAndMeasures.variant_unit_options,
class: "fullwidth",
data: { "controller": "tom-select" }
%td.align-right
-# empty
%td.align-right
Expand Down
46 changes: 45 additions & 1 deletion spec/services/weights_and_measures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,51 @@
end
end

describe "#scale_for_unit_value" do
describe "#variant_unit_options" do
subject { WeightsAndMeasures.variant_unit_options }

it "returns options for each unit" do
expect(subject).to eq [
["Weight (mg)", "weight_0.001"],
["Weight (g)", "weight_1"],
["Weight (oz)", "weight_28.35"],
["Weight (lb)", "weight_453.6"],
["Weight (kg)", "weight_1000"],
["Weight (T)", "weight_1000000"],
["Volume (mL)", "volume_0.001"],
["Volume (cL)", "volume_0.01"],
["Volume (dL)", "volume_0.1"],
["Volume (L)", "volume_1"],
["Volume (kL)", "volume_1000"],
["Volume (gal)", "volume_4.54609"],
["Items", "items"],
]
end

describe "filtering available units" do
let(:available_units) { "g,kg,T,mL,L,kL,lb,oz" }

before do
allow(Spree::Config).to receive(:available_units).and_return(available_units)
end

it "returns options for available units only" do
expect(subject).to eq [
["Weight (g)", "weight_1"],
["Weight (oz)", "weight_28.35"],
["Weight (lb)", "weight_453.6"],
["Weight (kg)", "weight_1000"],
["Weight (T)", "weight_1000000"],
["Volume (mL)", "volume_0.001"],
["Volume (L)", "volume_1"],
["Volume (kL)", "volume_1000"],
["Items", "items"],
]
end
end
end

describe "#scales_for_unit_value" do
context "weight" do
before do
allow(product).to receive(:variant_unit) { "weight" }
Expand Down

0 comments on commit c71d850

Please sign in to comment.