2016-01-12 18:43:00 -06:00
|
|
|
class Ingredient < ActiveRecord::Base
|
2016-01-28 14:19:51 -06:00
|
|
|
include TokenizedLike
|
2016-01-13 17:10:43 -06:00
|
|
|
|
2016-03-03 13:12:42 -06:00
|
|
|
belongs_to :user
|
|
|
|
|
2016-01-13 17:10:43 -06:00
|
|
|
validates :name, presence: true
|
2016-01-14 15:22:15 -06:00
|
|
|
validates :density, density: true, allow_blank: true
|
2016-01-13 17:10:43 -06:00
|
|
|
|
2016-03-03 13:12:42 -06:00
|
|
|
scope :has_density, -> { where.not(density: nil) }
|
|
|
|
|
2016-01-28 14:19:51 -06:00
|
|
|
def self.search(query)
|
|
|
|
tokens = query.to_s.split(' ')
|
|
|
|
|
|
|
|
if tokens.empty?
|
|
|
|
Ingredient.none
|
|
|
|
else
|
|
|
|
Ingredient.matches_tokens(:name, tokens)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-30 15:49:17 -06:00
|
|
|
def ndbn=(value)
|
|
|
|
@usda_food = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def usda_food
|
|
|
|
if self.ndbn.present?
|
|
|
|
@usda_food ||= UsdaFood.find_by_ndbn(self.ndbn)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def usda_food_name
|
|
|
|
if usda_food
|
|
|
|
usda_food.long_description
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-24 17:10:43 -06:00
|
|
|
def set_usda_food(food)
|
2016-01-28 14:19:51 -06:00
|
|
|
return unless food
|
|
|
|
|
2016-01-24 17:10:43 -06:00
|
|
|
self.ndbn = food.ndbn
|
2016-01-24 19:06:26 -06:00
|
|
|
self.water = food.water
|
|
|
|
self.protein = food.protein
|
|
|
|
self.lipids = food.lipid
|
2016-02-14 19:29:34 -06:00
|
|
|
self.carbohydrates = food.carbohydrates
|
2016-01-24 19:06:26 -06:00
|
|
|
self.ash = food.ash
|
|
|
|
self.kcal = food.kcal
|
|
|
|
self.fiber = food.fiber
|
|
|
|
self.sugar = food.sugar
|
2016-03-10 16:06:39 -06:00
|
|
|
self.density = food.density_best_guess
|
2016-01-24 17:10:43 -06:00
|
|
|
end
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|