From 53dbbbaf46e84e7ffe18c3befd6eeecd472e5c60 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 11 Oct 2026 19:42:07 +1300 Subject: [PATCH] Remove ruby-units gem and implement weight unit conversion directly in Harvest model --- Gemfile | 1 - Gemfile.lock | 2 -- app/models/harvest.rb | 10 ++++++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 522a0c158..5543aa6a6 100644 --- a/Gemfile +++ b/Gemfile @@ -58,7 +58,6 @@ gem 'gibbon', '~>1.2.0' # for Mailchimp newsletter subscriptions gem 'leaflet-rails', '>= 1.9.2' gem 'pg' -gem 'ruby-units' # for unit conversion gem "comfortable_mexican_sofa", git: "https://github.com/restarone/comfortable-mexican-sofa.git" diff --git a/Gemfile.lock b/Gemfile.lock index c42f4e1e2..5a1534de2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -660,7 +660,6 @@ GEM rubocop (~> 1.72, >= 1.72.1) rubocop-rspec (~> 3.5) ruby-progressbar (1.13.0) - ruby-units (4.1.0) ruby-vips (2.3.0) ffi (~> 1.12) logger @@ -851,7 +850,6 @@ DEPENDENCIES rubocop-rake rubocop-rspec rubocop-rspec_rails - ruby-units sassc-rails scout_apm searchkick diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 18900319c..519c9ab3d 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -31,6 +31,13 @@ class Harvest < ApplicationRecord "oz" => "oz" }.freeze + # Exact definitions of the international avoirdupois units, in kilograms. + KG_PER_WEIGHT_UNIT = { + "kg" => BigDecimal("1"), + "lb" => BigDecimal("0.45359237"), + "oz" => BigDecimal("0.028349523125") + }.freeze + ## ## Triggers after_validation :cleanup_quantities @@ -91,8 +98,7 @@ class Harvest < ApplicationRecord def set_si_weight return if weight_unit.nil? - weight_string = "#{weight_quantity} #{weight_unit}" - self.si_weight = Unit.new(weight_string).convert_to("kg").to_s("%0.3f").delete(" kg").to_f + self.si_weight = (weight_quantity * KG_PER_WEIGHT_UNIT.fetch(weight_unit)).round(3).to_f end def cleanup_quantities