Skip to content

Commit

Permalink
Merge pull request #21 from tayloredwebsites/glyc4
Browse files Browse the repository at this point in the history
Update DB and Gui
  • Loading branch information
tayloredwebsites authored Feb 25, 2024
2 parents 4f689f3 + 1095a14 commit 6080438
Show file tree
Hide file tree
Showing 37 changed files with 1,106 additions and 372 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ The program is being designed to help you plan your diet, and to give you feedba

DONE
- Ability to enter foods, nutrients, and the nutrients in a food.
- Download the foods and nutrients in the USDA database, and populate the database with them.

NEXT STEPS
- Download the foods and nutrients in the USDA database, and populate the database with them.
- Foods listing page with various sorts and filters to help choose foods for a meal.
- Dynamically develop a meal with portions of foods, with the ability to adjust portions till goals are met.
- Save a meal for future reference.
Expand Down Expand Up @@ -90,6 +90,7 @@ Run the following rake tasks to load up the database tables from the .csv files
bin/rails db:reset

1. load up the two category lookup tables and the nutrients table
Note: the lookup table will be reloaded using the Lookup Table Seed program. see below

bin/rails import_usda_csv_files:perform[1]

Expand All @@ -109,6 +110,18 @@ Run the following rake tasks to load up the database tables from the .csv files

bin/rails import_usda_csv_files:perform[5]

1. Deactivate Food records with no nutrients

bin/rails import_usda_csv_files:perform[6]

1. Update nutrient unit codes

bin/rails import_usda_csv_files:perform[7]

1. Load the Lookup Table from the Lookup Table Seed program:

bin/rails runner LookupTableSeedHash.lookup_table_load development

### Tips and Hints

#### Markdown
Expand Down
66 changes: 54 additions & 12 deletions app/assets/stylesheets/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,33 @@
text-align: center;
}

form label {
form.thin_labels label {
width: 100px;
}

form.wide_labels label {
width: 300px;
}

form label, form input[type="submit"] {
font-weight: bold;
display: inline-block;
width: 170px;
width: 200px;
margin-right: 5px;
}
form label {
text-align: right;
&:after {
content: ":";
}
margin-right: 5px;
}
form input[type="submit"] {
text-align: center;
}

// keep two column form with rail adds div with class field_with_errors
form .field_with_errors {
display: inline-block
}

form .thinnest {
Expand All @@ -67,13 +85,6 @@ ul.viewItems {

// Page Layout
footer {
position: absolute;
width: 100%;
bottom:0;
font-size: 0.7em;
line-height: 2.0em;
text-align: center;
padding-bottom: 5px;
}

// Index page Table styles
Expand All @@ -91,11 +102,13 @@ footer {
text-decoration: none;
}

// why aren't these working ???
td, th {
text-align: center;
}
th.pct20, td.pct20, .pct20 {
th.pct10, td.pct10 {
width: 10%;
}
th.pct20, td.pct20 {
width: 20%;
}
th.pct30, td.pct30 {
Expand All @@ -104,3 +117,32 @@ th.pct30, td.pct30 {
th.pct40, td.pct40 {
width: 40%;
}
th.pct50, td.pct50 {
width: 50%;
}
th.pct60, td.pct60 {
width: 60%;
}
th.pct70, td.pct70 {
width: 70%;
}
th.pct80, td.pct80 {
width: 80%;
}
th.pct90, td.pct90 {
width: 90%;
}

.font-smallest {
font-size: .40em;
}.font-smaller {
font-size: .60em;
}.font-small {
font-size: .80em;
}.font-large {
font-size: 1.20em;
}.font-larger {
font-size: 1.40em;
}.font-largest {
font-size: 1.60em;
}
93 changes: 43 additions & 50 deletions app/controllers/food_nutrients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Licensed under AGPL-3.0-only. See https://opensource.org/license/agpl-v3/

class FoodNutrientsController < ApplicationController
before_action :set_food_nutrient_from_params, except: %i[]
before_action :set_food_from_params, except: %i[]
# before_action :set_food_nutrient, only: %i[ show edit update destroy ]
# before_action :set_food, only: %i[ show new create edit update destroy ]
before_action :set_foods, only: %i[nutrients_of_food new create edit update]
Expand All @@ -17,7 +19,6 @@ class FoodNutrientsController < ApplicationController
def nutrients_of_food
Rails.logger.debug("*** params: #{params.inspect}")
@showing_active = params[:showing_active]
# set_food_nutrient_from_params('', params['food_id'])
food_id = params[:food_id].to_i()
# find food, regardless if it is active or not
@food = Food.find_by(id: food_id)
Expand All @@ -35,7 +36,7 @@ def nutrients_of_food
Rails.logger.debug("$$$ Show deactivated FoodNutrient records")
@food_nutrients = @food_nutrients.deact_food_nutrients.where(food_id: @food.id)
else
# default - show active food nutrients
# default - show_nutrient active food nutrients
Rails.logger.debug("$$$ Show active FoodNutrient records")
@food_nutrients = @food_nutrients.active_food_nutrients.where(food_id: @food.id)
end
Expand All @@ -45,39 +46,42 @@ def nutrients_of_food

# GET /food_nutrients/1 or /food_nutrients/1.json
def show
set_food_nutrient_from_params(params['id'], '') # set the FoodNutrient from its id and its Food
set_flash_msg('', '')
end

# GET /food_nutrients/new
def new
set_food_nutrient_from_params('', params['food_id']) # set a new FoodNutrient for this Food
@food_nutrient = FoodNutrient.new
Rails.logger.debug("*** params: #{params.inspect}")
@food = Food.find(params[:food_id])
Rails.logger.debug("*** @food: #{@food.inspect}")
@food_nutrient = FoodNutrient.new(food_id: @food.id)
set_unused_nutrients
set_flash_msg('', '')
end

# GET /food_nutrients/1/edit
def edit
set_food_nutrient_from_params(params['id'], '') # set the FoodNutrient from its id and its Food
set_unused_nutrients
set_flash_msg('', '')
end

# POST /food_nutrients or /food_nutrients.json
def create
set_food_nutrient_from_params('', food_nutrient_params['food_id']) # set a new FoodNutrient for this Food
@food_nutrient = FoodNutrient.new(food_nutrient_params)
@food_nutrient.samples_json = "" if @food_nutrient.samples_json.nil?

respond_to do |format|
if @food_nutrient.save
set_flash_msg("Food nutrient was successfully created.", '')
format.html { redirect_to nutrients_of_food_url(@food), notice: "Food nutrient was successfully created." }
msg = "Food nutrient was successfully created, redirect_to '/nutrients_of_food/#{@food_nutrient.food_id}'"
Rails.logger.debug(msg)
set_flash_msg(msg, '')
format.html { redirect_to "/nutrients_of_food/#{@food_nutrient.food_id}", notice: "Food nutrient was successfully created." }
format.json { render :show, status: :created, location: @food_nutrient }
else
msg = @errors + @food_nutrient.errors.full_messages
set_flash_msg('', "ERROR: unable to create food nutrient: #{msg.join('; ')}")
errMsg = "ERROR: unable to create food nutrient: #{msg.join('; ')}"
Rails.logger.error(errMsg)
set_flash_msg(errMsg)
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @food_nutrient.errors, status: :unprocessable_entity }
end
Expand All @@ -86,16 +90,17 @@ def create

# PATCH/PUT /food_nutrients/1 or /food_nutrients/1.json
def update
set_food_nutrient_from_params(params['id'], '') # set the FoodNutrient from its id and its Food
cleaned_food_nutrient_params = food_nutrient_params.except(
:nutrient_id, # do not let foreign key be changed
:food_id, # do not let foreign key be changed
)
@food_nutrient.assign_attributes(cleaned_food_nutrient_params)
respond_to do |format|
if @food_nutrient.save
set_flash_msg("Food nutrient was successfully updated.", '')
format.html { redirect_to nutrients_of_food_url(@food), notice: "Food nutrient was successfully updated." }
msg = "Food nutrient was successfully updated, redirect_to '/nutrients_of_food/#{@food_nutrient.food_id}'"
Rails.logger.debug(msg)
set_flash_msg(msg, '')
format.html { redirect_to "/nutrients_of_food/#{@food_nutrient.food_id}", notice: "Food nutrient was successfully created." }
format.json { render :show, status: :ok, location: @food_nutrient }
else
# @errors + @food_nutrient.errors.full_messages
Expand All @@ -108,7 +113,6 @@ def update

# DELETE /food_nutrients/1 or /food_nutrients/1.json
def destroy
set_food_nutrient_from_params(params['id'], '') # set the FoodNutrient from its id and its Food
@food_nutrient.active = false
save_id = @food_nutrient.id
save_food_id = @food_nutrient.food_id
Expand Down Expand Up @@ -136,7 +140,6 @@ def destroy

def reactivate
Rails.logger.debug("$$$ Reactivate - params: #{params.inspect}")
set_food_nutrient_from_params(params['id'], '') # set the FoodNutrient from its id and its Food
respond_to do |format|
if @food_nutrient.update(active: true)
format.html { redirect_to nutrients_of_food_url(@food_nutrient.food_id), notice: "Food nutrient was successfully reactivated." }
Expand Down Expand Up @@ -176,10 +179,6 @@ def get_food(id)
return food
end

def set_food_nutrients()
@food_nutrients.where(food_id: food_id)
end

def set_foods()
@foods = Food.active_foods
end
Expand All @@ -197,40 +196,34 @@ def set_unused_nutrients()
end

# get food_nutrient from either the food_nutrient id or food id params
# TODO: consider splitting this and the next into two methods
# TODO: review this
def set_food_nutrient_from_params(id_param, food_id_param)
def set_food_nutrient_from_params()
Rails.logger.debug("$$$ set_food_nutrient_from_params - param: #{params.inspect}")
# Rails.logger.debug("$$$ set_food_nutrient_from_params - food_nutrient_params: #{food_nutrient_params.inspect}")
Rails.logger.debug("$$$ set_food_nutrient_from_params - id_param: #{id_param.inspect}")
Rails.logger.debug("$$$ set_food_nutrient_from_params - food_id_param: #{food_id_param.inspect}")
id = id_param.to_i()
food_id = food_id_param.to_i()
set_food_nutrient_from_ids(id, food_id)
end

def set_food_nutrient_from_ids(id, food_id)
if id > 0
Rails.logger.debug("$$$ set_food_nutrient_from_ids(#{id}, #{food_id})")
@food = nil
@food_nutrient = get_food_nutrient(id)
if @food_nutrient.present?
if @food_nutrient.food_id.present? && @food_nutrient.food_id > 0
Rails.logger.debug("$$$ set_food_nutrient_from_ids - @food_nutrient: #{@food_nutrient.inspect}")
@food = get_food(@food_nutrient.food_id)
else
@errors << "Missing food in Food Nutrient id: #{@food_nutrient.id}"
end
end
# else
# @errors << "Invalid Food Nutrient Id #{id}"
end
if @food.present? && @food.id.present?
# food has been found from food_nutrient
if params[:id]
@food_nutrient = FoodNutrient.find_by(id: params[:id])
else
Rails.logger.debug("$$$ set_food_nutrient_from_ids get_food @food: #{@food.inspect} & food_id: #{food_id}")
@food = get_food(food_id)
@food_nutrient = FoodNutrient.new()
end
Rails.logger.debug("*** set_food_nutrient_from_params - @food_nutrient.id: #{@food_nutrient.id}")
end

# get food from either the food_nutrient id or food id params
# run set_food_nutrient_from_params before this
def set_food_from_params()
# Rails.logger.debug("$$$ set_food_from_params - param: #{params.inspect}")
# Rails.logger.debug("*** set_food_from_params - @food_nutrient: #{@food_nutrient.inspect}")
# Rails.logger.debug("*** set_food_from_params - defined?(@food_nutrient).present?: #{defined?(@food_nutrient).present?}")
if defined?(@food_nutrient).present?
@food = Food.find_by(id: @food_nutrient.food_id)
# Rails.logger.debug("*** set_food_from_params - @food_nutrient - @food.id: #{@food.id}")
elsif params[:food_id].present?
@food = Food.find_by(id: params[:food_id])
# Rails.logger.debug("*** set_food_from_params - @food.id: #{@food.id}")
else
@food_nutrient.errors.add(:food_id, "Missing food_id for food nutrient")
@food = Food.new
end
# Rails.logger.debug("*** set_food_from_params done - @food.id: #{@food.id}")
# Rails.logger.debug("*** set_food_from_params done - @food_nutrient.id: #{@food_nutrient.id}")
end

# Only allow a list of trusted parameters through.
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/foods_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ def show
# GET /foods/new
def new
@food = Food.new
@lookups_usda_cats = LookupTable.where(lu_table: "usda_cat").order(:lu_desc)
@lookups_wweia_cats = LookupTable.where(lu_table: "wweia_cat").order(:lu_desc)
end

# GET /foods/1/edit
def edit
@lookups_usda_cats = LookupTable.where(lu_table: "usda_cat").order(:lu_desc)
@lookups_wweia_cats = LookupTable.where(lu_table: "wweia_cat").order(:lu_desc)
end

# POST /foods or /foods.json
Expand Down Expand Up @@ -114,7 +118,7 @@ def food_params
:food_portion_amount,
:usda_food_cat_id,
:wweia_food_cat_id,
:usda_fdc_ids_json,
# :usda_fdc_ids_json,
:active,
)
end
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
class HomeController < ApplicationController
def index
end
def about
end
def copyright
end
end
2 changes: 1 addition & 1 deletion app/controllers/nutrients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ def set_nutrient

# Only allow a list of trusted parameters through.
def nutrient_params
params.require(:nutrient).permit(:id, :name, :usda_ndb_num, :desc)
params.require(:nutrient).permit(:id, :name, :usda_nutrient_id, :usda_nutrient_num, :unit_code)
end
end
8 changes: 6 additions & 2 deletions app/models/food.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ class Food < ApplicationRecord

belongs_to :usda_food_cat,
class_name: "LookupTable",
foreign_key: "usda_food_cat_id"
foreign_key: "usda_food_cat_id",
optional: true
belongs_to :wweia_food_cat,
class_name: "LookupTable",
foreign_key: "wweia_food_cat_id"
foreign_key: "wweia_food_cat_id",
optional: true

serialize :usda_fdc_ids_json, JSON

validates :name, presence: true, allow_blank: false

end
Loading

0 comments on commit 6080438

Please sign in to comment.