Skip to content

Commit

Permalink
Re-arrange imperial units
Browse files Browse the repository at this point in the history
Who would have guessed it was this complicated.

Fingers crossed this doesn't break any other functionality...
  • Loading branch information
dacook committed Feb 21, 2024
1 parent a653e71 commit e170c0a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 11 additions & 4 deletions app/services/weights_and_measures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ def self.available_units

def self.available_units_sorted
self::UNITS.transform_values do |measurement_info|
# Filter to only include available units
measurement_info.filter do |_scale, unit_info|
available_units.include?(unit_info['name'])
end.sort.to_h # sort by unit (hash key)
self.available_units.include?(unit_info['name'])

Check warning on line 50 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:50:9: C: Style/RedundantSelf: Redundant `self` detected.
end.
# Remove duplicates by name
uniq do |_scale, unit_info|

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

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Use 2 (not 0) spaces for indenting an expression spanning multiple lines. Raw Output: app/services/weights_and_measures.rb:53:7: C: Layout/MultilineMethodCallIndentation: Use 2 (not 0) spaces for indenting an expression spanning multiple lines.
unit_info['name']
end.
# Sort by unit number
sort.to_h

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

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Use 2 (not 0) spaces for indenting an expression spanning multiple lines. Raw Output: app/services/weights_and_measures.rb:57:7: C: Layout/MultilineMethodCallIndentation: Use 2 (not 0) spaces for indenting an expression spanning multiple lines.
end
end

Expand All @@ -60,10 +67,10 @@ def self.available_units_sorted
1000.0 => { 'name' => 'kg', 'system' => 'metric' },
1_000_000.0 => { 'name' => 'T', 'system' => 'metric' },

28.349523125 => { 'name' => 'oz', 'system' => 'imperial' },
28.35 => { 'name' => 'oz', 'system' => 'imperial' },
453.59237 => { 'name' => 'lb', 'system' => 'imperial' },
28.349523125 => { 'name' => 'oz', 'system' => 'imperial' },
453.6 => { 'name' => 'lb', 'system' => 'imperial' },
453.59237 => { 'name' => 'lb', 'system' => 'imperial' },
},
'volume' => {
0.001 => { 'name' => 'mL', 'system' => 'metric' },
Expand Down
7 changes: 5 additions & 2 deletions spec/services/weights_and_measures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
allow(product).to receive(:variant_unit_scale) { 28.35 }
expect(subject.system).to eq("imperial")
end

it "when precise scale is for an imperial unit" do
allow(product).to receive(:variant_unit_scale) { 28.349523125 }
expect(subject.system).to eq("imperial")
end
end

context "volume" do
Expand Down Expand Up @@ -79,7 +84,6 @@
["Volume (kL)", "volume_1000"],
["Items", "items"],
]
pending "imperial measurements are duplicated"
expect(subject).to match_array expected_array # diff each element
expect(subject).to eq expected_array # test ordering also
end
Expand All @@ -97,7 +101,6 @@
["Volume (L)", "volume_1"],
["Items", "items"],
]
pending "imperial measurements are duplicated"
expect(subject).to match_array expected_array # diff each element
expect(subject).to eq expected_array # test ordering also
end
Expand Down

0 comments on commit e170c0a

Please sign in to comment.