mirror of
https://github.com/Growstuff/growstuff.git
synced 2025-12-24 01:57:46 -05:00
feat: Add nutritional data partial to crops and harvests
This commit introduces a new partial to display nutritional data from the Australian Food Composition Database. Key changes: - Adds a database index to the `public_food_key` on the `crops` table. - Establishes a `has_one` relationship between `Crop` and `AustralianFoodClassificationData`. - Creates a `_nutritional_data.html.haml` partial to display a summary of key nutritional information. - Renders the partial on the `show` pages for both crops and harvests.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
38
app/views/crops/_nutritional_data.html.haml
Normal file
38
app/views/crops/_nutritional_data.html.haml
Normal file
@@ -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"
|
||||
@@ -128,6 +128,8 @@
|
||||
|
||||
= render 'openfarm_data', crop: @crop
|
||||
|
||||
= render 'nutritional_data', crop: @crop
|
||||
|
||||
= cute_icon
|
||||
.card
|
||||
.card-body
|
||||
|
||||
@@ -71,3 +71,4 @@
|
||||
|
||||
.col-md-4.col-xs-12
|
||||
= render @harvest.crop
|
||||
= render 'crops/nutritional_data', crop: @harvest.crop
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AddIndexToCropsPublicFoodKey < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
add_index :crops, :public_food_key
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user