class Portion < ApplicationRecord

	validates_presence_of :name #, :unit, :quantity
	validates_uniqueness_of :name
	has_many :meal_portions

	attr_accessor :portions

	# searching
	include PgSearch::Model
	pg_search_scope :search, against: [:name], using: {
	  tsearch: { prefix: true }
	}, ignoring: :accents

	def shortcut
		if unit != "portion"
			return "#{quantity} #{unit} de #{name}"
		else
			return "#{name}"
		end
	end

	def calories
		((proteins.to_f * 4) + (fats.to_f * 9) + (carbs.to_f * 4)).round(1).to_s
	end

	def abbrv
		_clean = name.downcase
		_clean = _clean.split(" o ")[0]
		_clean = _clean.split(" y ")[0]
		_clean = _clean.split("(")[0]
		_clean = _clean.split("/")[0]
		_clean.titleize
	end

	def unidad
		case self.unit
		when "portion"
			return "porción de"
		when "tz"
			return "tz de"
		when "pz"
			return "pz de"
		when "gr"
			return "g. de"
		when "cda"
			return "cda de"
		else
			return self.unit
		end
	end

end