سؤال

I am worried about the amount of information that is presented on a controller method for a dashboard view. Basically this dashboard view shows user informations and activity inside my application, but I think this is quickly turning into a mess, due to the amount of information being passed to the view.

@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?

Is there a more elegantly way of writing all this information? I'm only worried about how the code looks, not so much about its performance.

هل كانت مفيدة؟

المحلول

This would be good occasion to use hashes. For example,

@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}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top