2013-05-11 68 views
0

所以,我有一个名为“状态更新助手”的助手和一个名为“餐助手”。需要访问帮助变量

我需要从膳食助手内访问状态更新辅助变量...

我已经尝试使用“包括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,它返回“无穷”。

如果我把“100”int返回部分它将返回“100”。

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

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

所以我的问题是,为什么我在做什么不工作,我怎样才能得到这些变量传递到膳食显示视图?

这里的饭菜的一部分展示图,其中的变量被渲染

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

谢谢!

+0

我该怎么做才能改善这个问题?从我这里得到21个观点,没有答案。 – 2013-05-13 01:53:25

+1

您以错误的方式使用助手。我意识到这并不能帮助你。我最好的建议:尝试在模型的方法中解决更多的逻辑。使用助手只用于显示的东西。 假装帮助者不存在。 – wintermeyer 2013-05-13 09:09:52

+0

Grr lol我有一种直觉,我必须将这些方法转化为模型。这将会破坏我的代码的一大部分。好吧。我最好比拖延更快地做到这一点。 – 2013-05-14 03:20:34

回答

0

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

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

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

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

我发现了在模型中放置尽可能多的逻辑,同时保持我的控制器很薄并且我的视图也缺少任何逻辑的难以置信的有用性。

它使生活更容易9,000,000,002,839,420,934,820,394倍。

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