35 lines
939 B
Ruby
35 lines
939 B
Ruby
class Recipe < ActiveRecord::Base
|
|
|
|
has_many :recipe_ingredients, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy
|
|
has_many :recipe_steps, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy
|
|
belongs_to :user
|
|
|
|
scope :active, -> { where('deleted <> ? OR deleted IS NULL', true) }
|
|
|
|
accepts_nested_attributes_for :recipe_ingredients, allow_destroy: true
|
|
accepts_nested_attributes_for :recipe_steps, allow_destroy: true
|
|
|
|
validates :name, presence: true
|
|
validates :yields, numericality: true, allow_blank: true
|
|
validates :total_time, numericality: true, allow_blank: true
|
|
validates :active_time, numericality: true, allow_blank: true
|
|
|
|
NAME_CUTOFF = 35
|
|
|
|
def display_name
|
|
if self.name.length >= NAME_CUTOFF
|
|
self.name[0...NAME_CUTOFF] + '...'
|
|
else
|
|
self.name
|
|
end
|
|
end
|
|
|
|
def scale(factor)
|
|
recipe_ingredients.each do |ri|
|
|
ri.scale(factor)
|
|
end
|
|
end
|
|
|
|
|
|
end
|