0

所以,我有一个叫做“状态更新助手”的助手和一个叫做“膳食助手”的助手。

我需要从 Meals Helper 中访问 Status Updates Helper 中的变量...

我尝试在 Meals Helper 中使用“include StatusUpdatesHelper”,尽管这似乎不起作用。

这是我的膳食助手文件:

module MealsHelper
 def total_of(macro)
 current_user.meal_foods.map(&macro).inject(:+)
end

def pct_fat_satisfied
   #how much of a macro is needed?
   #  fat_needed = fat factor * current lbm
   fat_factor = current_user.fat_factor
   current_lbm = current_user.status_update.first.current_lbm
   fat_needed = fat_factor * current_lbm
   #how much is in the meal?
   fat_provided = total_of(:fat)
   #percent needed
   pct_fulfilled = fat_provided.to_f/fat_needed.to_f
   return BigDecimal(pct_fulfilled, 2)*100
end     

def pct_carbs_satisfied( tdee, deficit_pct )
 #how many carbs are needed?
 cals_needed = tdee.to_f * (1 - deficit_pct.to_f)
 carbs_needed = cals_needed * 4
 #how many carbs are provided?
 carbs_provided = total_of(:carbs)
 #what is the pct satisfied?
 pct_fulfilled = carbs_provided.to_f/carbs_needed.to_f
 return tdee 
end  

def pct_protein_satisfied
  #how much protien is needed?
  protein_factor = current_user.protein_factor 
  current_lbm = current_user.status_update.first.current_lbm
  protein_needed = protein_factor * current_lbm
  #how much protien is provided?
  protein_provided = total_of(:protien)
  #pct of protien satisfied?
  pct_fulfilled = protein_provided.to_f/protein_needed.to_f
  return BigDecimal(pct_fulfilled, 2)*100
end

end

这是状态更新帮助文件:

module StatusUpdatesHelper

def bmr(lbm) 
  lbm *= 0.45
  return '%.2f' % (370 + (21.6 * lbm.to_d))
end

def target_weight(total_weight, target_bf_pct, lbm)
 target_bf_pct /= 100
 return '%.2f' %  ((total_weight*target_bf_pct)+lbm)
end 

def fat_to_burn(total_weight, target_weight)
 return '%.2f' % (total_weight.to_d - target_weight.to_d)
end

def tdee(bmr, activity_factor)
  return '%.2f' % (bmr.to_d*activity_factor.to_d)
end

def deficit_pct(deficit_amnt, tdee)
 daily_cal_def = ((deficit_amnt.to_f * 3500)/7)
 return (daily_cal_def.to_d/tdee.to_d)
end

def daily_calorie_target(tdee, deficit_pct)
 return '%.2f' % (tdee.to_d * deficit_pct.to_d)  
end

def weekly_burn_rate(tdee, daily_calorie_target)
  return '%.2f' % (daily_calorie_target.to_d*7) 
end

def time_to_goal(weekly_burn_rate, fat_to_burn)
  return '%.2f' %  (fat_to_burn.to_d*3500/weekly_burn_rate.to_d) 
end                  

def daily_intake( tdee, daily_calorie_target )
  return '%.2f' % (tdee.to_d - daily_calorie_target.to_d)
end


def total_progress
 if user_signed_in?
   if current_user.status_update.empty?
    @total_weight_change   = 0
    @total_fat_change      = 0
    @total_lbm_change      = 0

    @time_to_goal          = 0
    @fat_to_burn           = 0
    @target_bf_pct         = 0
    @lbm                   = 0
    @activity_factor       = 0
    @bmr                   = 0
    @total_weight          = 0
    @target_weight         = 0
    @fat_to_burn           = 0
    @tdee                  = 0
    @deficit_amnt          = 0
    @deficit_pct           = 0
    @daily_calorie_target  = 0
    @daily_intake          = 0
    @weekly_burn_rate      = 0
    @time_to_goal          = 0

    @current_weight        = 0
    @current_bf_pct        = 0
    @current_lbm           = 0
    @current_fat_weight    = 0

    @daily_caloric_deficit = 0
  end

  if current_user.status_update.any?

    @first = current_user.status_update.first

    @last  = current_user.status_update.last

    @beginning_date       = current_user.status_update
                           .first.created_at.strftime("%m/%d/%Y")
    @last_date            = current_user.status_update
                           .last.created_at.strftime("%m/%d/%Y")
    @total_weight_change  = BigDecimal(@first.current_weight - 
                                       @last.current_weight, 3)
    @total_fat_change     = BigDecimal(@first.current_fat_weight - 
                                       @last.current_fat_weight, 3)
    @total_lbm_change     = BigDecimal(@first.current_lbm - 
                                       @last.current_lbm, 3)
    @recent_fat_change    = BigDecimal(@first.current_fat_weight -
                                       @first.previous_status_update.current_fat_weight, 3)
    @recent_lbm_change    = BigDecimal(@first.current_lbm -
                                       @first.previous_status_update.current_lbm, 2)
    @recent_weight_change = BigDecimal(@first.current_weight -
                                       @first.previous_status_update.current_weight, 2) 
    @lbm                  = @first.current_lbm
    @activity_factor      = current_user.activity_factor
    @bmr                  = bmr(@lbm)
    @total_weight         = @first.current_weight
    @target_bf_pct        = (current_user.target_bf_pct) 
    @target_weight        = target_weight(@total_weight, @target_bf_pct, @lbm)
    @fat_to_burn          = fat_to_burn(@total_weight, @target_weight)
    @tdee                 = tdee(@bmr, @activity_factor)
    @deficit_amnt         = current_user.deficit_amnt
    @deficit_pct          = deficit_pct(@deficit_amnt, @tdee)
    @daily_calorie_target = daily_calorie_target(@tdee, @deficit_pct)
    @daily_intake         = daily_intake(@tdee, @daily_calorie_target)
    @weekly_burn_rate     = weekly_burn_rate(@tdee, @daily_calorie_target)
    @time_to_goal         = time_to_goal(@weekly_burn_rate, @fat_to_burn)
    @current_weight       = BigDecimal(@first.current_weight, 4)
    @current_bf_pct       = BigDecimal(@first.current_bf_pct * 100, 4)
    @current_lbm          = BigDecimal(@first.current_lbm, 4)
    @current_fat_weight   = BigDecimal(@first.current_fat_weight, 4)
    @daily_caloric_deficit = @tdee.to_d - @daily_intake.to_d
    #End Date
    @start_date           = current_user.status_update.first.created_at
    @end_date             = (@start_date + @time_to_goal.to_i.weeks).strftime("%m/%d/%Y")
  end           
 end
 end
 end

在餐点显示视图中,当我要求 pct_carbs_fulfilled 时,它返回“infinity”。

如果我将“100”放入他返回的部分,它将返回“100”。

状态更新助手工作得很好。

我知道这不是一个宁静的策略,所以任何关于如何让它更宁静的建议将不胜感激。

所以我的问题是,为什么我没有做的工作,我怎样才能将这些变量传递到餐显示视图中?

这是正在呈现变量的餐点显示视图的一部分

<td> <%= pct_fat_satisfied     %>% </td>
<td> <%= @tdee                 %>% </td>
<td> <%= pct_protein_satisfied %>% </td> 

谢谢!

4

1 回答 1

0

我意识到我需要将所有这些逻辑转移到用户模型中,所以这就是我所做的。

我删除了整个“total_progress”方法,并从其中的每个变量中创建了实例方法。

我发现我可以使用从其他对象提供的新数据和属于用户的相对静态属性来使用这些方法解决任何属性,而不是尝试将所有这些变量保存在一个对象中。

这使得所有这些其他“计算”变量更加动态,因为我不必保存它们。每次用户更新其他对象时,这些值都会立即更新,因为找到它们需要每次都重新计算它们。

我发现在模型中放置尽可能多的逻辑,同时保持我的控制器精简,我的视图也没有任何逻辑,这非常有用。

它使生活变得轻松 9,000,000,002,839,420,934,820,394 倍。

所以这个故事的寓意是“使用胖模型和瘦控制器”。

于 2013-05-17T17:55:27.103 回答