diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 399194610c..fe07b57355 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -52,7 +52,6 @@ class Harvest < ApplicationRecord delegate :name, :slug, to: :crop, prefix: true delegate :login_name, :slug, to: :owner, prefix: true - delegate :name, to: :plant_part, prefix: true ## ## Validations @@ -108,7 +107,7 @@ def harvest_slug def to_s # 50 individual apples, weighing 3lb # 2 buckets of apricots, weighing 10kg - "#{quantity_to_human} #{unit_to_human} #{crop_name_to_human} #{weight_to_human}".strip + "#{quantity_to_human} #{unit_to_human} #{plant_part_name_to_human} of #{crop_name} #{weight_to_human}".strip end def quantity_to_human @@ -131,13 +130,13 @@ def weight_to_human "weighing #{number_to_human(weight_quantity, strip_insignificant_zeros: true)} #{weight_unit}" end - def crop_name_to_human + def plant_part_name_to_human if unit != 'individual' # buckets of apricot*s* - crop.name.pluralize + plant_part.name.pluralize elsif quantity == 1 - crop.name + plant_part.name else - crop.name.pluralize + plant_part.name.pluralize end.to_s end diff --git a/app/models/plant_part.rb b/app/models/plant_part.rb index e7e8083090..6750cbdb6b 100644 --- a/app/models/plant_part.rb +++ b/app/models/plant_part.rb @@ -11,6 +11,10 @@ class PlantPart < ApplicationRecord scope :joins_members, -> { joins("INNER JOIN members ON members.id = harvests.owner_id") } + def whole_plant? + name == 'whole plant' + end + def to_s name end diff --git a/spec/models/harvest_spec.rb b/spec/models/harvest_spec.rb index da2bb95280..34fd23d1c0 100644 --- a/spec/models/harvest_spec.rb +++ b/spec/models/harvest_spec.rb @@ -149,78 +149,94 @@ end context "stringification" do - let(:crop) { FactoryBot.create(:crop, name: "apricot") } - - it "apricots" do - @h = FactoryBot.create(:harvest, crop:, - quantity: nil, - unit: nil, - weight_quantity: nil, - weight_unit: nil) - expect(@h.to_s).to eq "apricots" - end + let(:whole_plant) { FactoryBot.create(:plant_part, name: "whole plant") } + let(:leaf) { FactoryBot.create(:plant_part, name: "leaf") } + let(:fruit) { FactoryBot.create(:plant_part, name: "fruit") } + let(:other) { FactoryBot.create(:plant_part, name: "other") } + + let(:apricot) { FactoryBot.create(:crop, name: "apricot") } + let(:lettuce) { FactoryBot.create(:crop, name: "lettuce") } + + context "apricots" do + it "apricots" do + @h = FactoryBot.create(:harvest, crop: apricot, + plant_part: fruit, + quantity: nil, + unit: nil, + weight_quantity: nil, + weight_unit: nil) + expect(@h.to_s).to eq "fruits of apricot" + end - it "1 individual apricot" do - @h = FactoryBot.create(:harvest, crop:, - quantity: 1, - unit: 'individual', - weight_quantity: nil, - weight_unit: nil) - expect(@h.to_s).to eq "1 individual apricot" - end + it "1 individual apricot" do + @h = FactoryBot.create(:harvest, crop: apricot, + plant_part: fruit, + quantity: 1, + unit: 'individual', + weight_quantity: nil, + weight_unit: nil) + expect(@h.to_s).to eq "1 individual fruit of apricot" + end - it "10 individual apricots" do - @h = FactoryBot.create(:harvest, crop:, - quantity: 10, - unit: 'individual', - weight_quantity: nil, - weight_unit: nil) - expect(@h.to_s).to eq "10 individual apricots" - end + it "10 individual apricots" do + @h = FactoryBot.create(:harvest, crop: apricot, + plant_part: fruit, + quantity: 10, + unit: 'individual', + weight_quantity: nil, + weight_unit: nil) + expect(@h.to_s).to eq "10 individual fruits of apricot" + end - it "1 bushel of apricots" do - @h = FactoryBot.create(:harvest, crop:, - quantity: 1, - unit: 'bushel', - weight_quantity: nil, - weight_unit: nil) - expect(@h.to_s).to eq "1 bushel of apricots" - end + it "1 bushel of apricots" do + @h = FactoryBot.create(:harvest, crop: apricot, + plant_part: fruit, + quantity: 1, + unit: 'bushel', + weight_quantity: nil, + weight_unit: nil) + expect(@h.to_s).to eq "1 bushel of fruits of apricot" + end - it "1.5 bushels of apricots" do - @h = FactoryBot.create(:harvest, crop:, - quantity: 1.5, - unit: 'bushel', - weight_quantity: nil, - weight_unit: nil) - expect(@h.to_s).to eq "1.5 bushels of apricots" - end + it "1.5 bushels of apricots" do + @h = FactoryBot.create(:harvest, crop: apricot, + plant_part: fruit, + quantity: 1.5, + unit: 'bushel', + weight_quantity: nil, + weight_unit: nil) + expect(@h.to_s).to eq "1.5 bushels of fruits of apricot" + end - it "10 bushels of apricots" do - @h = FactoryBot.create(:harvest, crop:, - quantity: 10, - unit: 'bushel', - weight_quantity: nil, - weight_unit: nil) - expect(@h.to_s).to eq "10 bushels of apricots" - end + it "10 bushels of apricots" do + @h = FactoryBot.create(:harvest, crop: apricot, + plant_part: fruit, + quantity: 10, + unit: 'bushel', + weight_quantity: nil, + weight_unit: nil) + expect(@h.to_s).to eq "10 bushels of fruits of apricot" + end - it "apricots weighing 1.2 kg" do - @h = FactoryBot.create(:harvest, crop:, - quantity: nil, - unit: nil, - weight_quantity: 1.2, - weight_unit: 'kg') - expect(@h.to_s).to eq "apricots weighing 1.2 kg" - end + it "apricots weighing 1.2 kg" do + @h = FactoryBot.create(:harvest, crop: apricot, + plant_part: fruit, + quantity: nil, + unit: nil, + weight_quantity: 1.2, + weight_unit: 'kg') + expect(@h.to_s).to eq "fruits of apricot weighing 1.2 kg" + end - it "10 bushels of apricots weighing 100 kg" do - @h = FactoryBot.create(:harvest, crop:, - quantity: 10, - unit: 'bushel', - weight_quantity: 100, - weight_unit: 'kg') - expect(@h.to_s).to eq "10 bushels of apricots weighing 100 kg" + it "10 bushels of apricots weighing 100 kg" do + @h = FactoryBot.create(:harvest, crop: apricot, + plant_part: fruit, + quantity: 10, + unit: 'bushel', + weight_quantity: 100, + weight_unit: 'kg') + expect(@h.to_s).to eq "10 bushels of fruits of apricot weighing 100 kg" + end end end