2013-07-25 156 views
0

我很擔心在儀表板視圖的控制器方法中顯示的信息量。基本上,該儀表板視圖顯示了我的應用程序中的用戶信息和活動,但由於大量信息傳遞到視圖,我認爲這很快變成了一團糟。控制器方法 - 導軌信息量

@answered = answered.length 
@asked = asked.length 
@pending = pending.length 
@favorited = favorited.length 
@medal_gold = thanks_received(:awesome).length 
@medal_silver = thanks_received(:great).length 
@medal_bronze = thanks_received(:good).length 
@notification_pending = question_users_not_seen(pending, current_user.user_notification.pending_last_seen).count 
@notification_silver = question_users_not_seen(thanks_received(:great), current_user.user_notification.silver_last_seen).count 
@notification_gold = question_users_not_seen(thanks_received(:awesome), current_user.user_notification.gold_last_seen).count 
@notification_bronze = question_users_not_seen(thanks_received(:good), current_user.user_notification.bronze_last_seen).count 
@notification_asked = question_users_not_seen(asked, current_user.user_notification.asked_last_seen).any? 
@notification_answered = question_users_not_seen(answered, current_user.user_notification.answered_last_seen).any? 
@notification_favorited = question_users_not_seen(favorited, current_user.user_notification.favorited_last_seen).any? 

是否有更優雅的方式來編寫所有這些信息?我只關心代碼的外觀,而不是太在乎它的性能。

+0

您可能需要使用更多的幫手,或者類方法上與這些數值模型.. –

回答

1

這將是使用哈希的好機會。例如,

@attr_counts = {:answered => answered.length, :asked => asked.length, :pending => pending.length, :favorited => favorited.length} 
@medal_lengths = {:gold => thanks_received(:awesome).length, :silver => thanks_received(:great).length, :bronze => thanks_received(:good).length}