diff --git a/app/models/australian_food_classification_data.rb b/app/models/australian_food_classification_data.rb index f97efb634..0d17010cc 100644 --- a/app/models/australian_food_classification_data.rb +++ b/app/models/australian_food_classification_data.rb @@ -1,2 +1,8 @@ +# frozen_string_literal: true + class AustralianFoodClassificationData < ApplicationRecord + belongs_to :crop, + foreign_key: :public_food_key, + primary_key: :public_food_key, + inverse_of: :australian_food_classification_data end diff --git a/app/models/crop.rb b/app/models/crop.rb index d9a919381..b04eb3325 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -28,6 +28,10 @@ class Crop < ApplicationRecord has_many :companions, through: :crop_companions, source: :crop_b, class_name: 'Crop' has_many :crop_posts, dependent: :delete_all has_many :posts, through: :crop_posts, dependent: :delete_all + has_one :australian_food_classification_data, + foreign_key: :public_food_key, + primary_key: :public_food_key, + inverse_of: :crop accepts_nested_attributes_for :scientific_names, allow_destroy: true, reject_if: :all_blank diff --git a/app/views/crops/_nutritional_data.html.haml b/app/views/crops/_nutritional_data.html.haml new file mode 100644 index 000000000..51be27e73 --- /dev/null +++ b/app/views/crops/_nutritional_data.html.haml @@ -0,0 +1,38 @@ +- data = crop.australian_food_classification_data +- if data + .card + .card-body + %h4.card-title Nutritional Data + %p.card-text A summary of nutritional data per 100g. + %table.table.table-sm.table-borderless + %tbody + - if data.energy_with_dietary_fibre_equated_kj.to_f > 0 + %tr + %th Energy + %td= "\#{data.energy_with_dietary_fibre_equated_kj.to_f.round(1)} kJ" + - if data.protein_g.to_f > 0 + %tr + %th Protein + %td= "\#{data.protein_g.to_f.round(1)} g" + - if data.fat_total_g.to_f > 0 + %tr + %th Fat, total + %td= "\#{data.fat_total_g.to_f.round(1)} g" + - if data.available_carbohydrate_with_sugar_alcohols_g.to_f > 0 + %tr + %th Carbohydrate + %td= "\#{data.available_carbohydrate_with_sugar_alcohols_g.to_f.round(1)} g" + - if data.total_sugars_g.to_f > 0 + %tr + %th - Sugars + %td= "\#{data.total_sugars_g.to_f.round(1)} g" + - if data.total_dietary_fibre_g.to_f > 0 + %tr + %th Fibre + %td= "\#{data.total_dietary_fibre_g.to_f.round(1)} g" + - if data.sodium_na_mg.to_f > 0 + %tr + %th Sodium + %td= "\#{data.sodium_na_mg.to_f.round(1)} mg" + .card-footer + = link_to "See more", "https://afcd.foodstandards.gov.au/fooddetails.aspx?PFKID=#{data.public_food_key}", target: "_blank", rel: "noopener noreferrer" diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index 36f001ee4..c6abcaeb1 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -128,6 +128,8 @@ = render 'openfarm_data', crop: @crop + = render 'nutritional_data', crop: @crop + = cute_icon .card .card-body diff --git a/app/views/harvests/show.html.haml b/app/views/harvests/show.html.haml index 858bb2194..23c80bb0e 100644 --- a/app/views/harvests/show.html.haml +++ b/app/views/harvests/show.html.haml @@ -71,3 +71,4 @@ .col-md-4.col-xs-12 = render @harvest.crop + = render 'crops/nutritional_data', crop: @harvest.crop diff --git a/db/migrate/20251201045000_add_index_to_crops_public_food_key.rb b/db/migrate/20251201045000_add_index_to_crops_public_food_key.rb new file mode 100644 index 000000000..932d63dd7 --- /dev/null +++ b/db/migrate/20251201045000_add_index_to_crops_public_food_key.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddIndexToCropsPublicFoodKey < ActiveRecord::Migration[7.2] + def change + add_index :crops, :public_food_key + end +end