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?

このすべての情報をよりエレガントに記述する方法はありますか? 私はコードがどのように見えるかだけを気にしており、そのパフォーマンスについてはあまり気にしていません。

4

1 に答える 1

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}
于 2013-07-25T19:33:24.390 に答える