From ffe48fe1898b6697d6ae1ee1d9900d7a2d2334c4 Mon Sep 17 00:00:00 2001
From: Sarah Mei <sarahmei@gmail.com>
Date: Sun, 27 Mar 2011 19:19:25 -0700
Subject: [PATCH] Refactor aspects#index to be easier to read; move some
 expensive operations after the guard clause

---
 app/controllers/aspects_controller.rb | 30 +++++++++++++--------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb
index e3af0af2db..f39b819596 100644
--- a/app/controllers/aspects_controller.rb
+++ b/app/controllers/aspects_controller.rb
@@ -12,34 +12,34 @@ class AspectsController < ApplicationController
 
   def index
     if params[:a_ids]
-      @aspects = current_user.aspects.where(:id => params[:a_ids]).includes(:contacts => {:person => :profile})
+      @aspects = current_user.aspects.where(:id => params[:a_ids])
     else
-      @aspects = current_user.aspects.includes(:contacts => {:person => :profile})
+      @aspects = current_user.aspects
     end
-    @selected_contacts = @aspects.inject([]) { |arr, aspect| arr.concat(aspect.contacts) }
-    @selected_contacts.uniq!
+    @aspects = @aspects.includes(:contacts => {:person => :profile})
 
     # redirect to signup
     if (current_user.getting_started == true || @aspects.blank?) && !request.format.mobile? && !request.format.js?
       redirect_to getting_started_path
-    else
-      @aspect_ids = @aspects.map { |a| a.id }
+      return
+    end
 
-      @posts = StatusMessage.joins(:aspects).where(:pending => false,
-                                                   :aspects => {:id => @aspect_ids}).includes(:comments, :photos, :likes, :dislikes).select('DISTINCT `posts`.*').paginate(
-        :page => params[:page], :per_page => 15, :order => session[:sort_order] + ' DESC')
-      @fakes = PostsFake.new(@posts)
+    @selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq
+    @aspect_ids = @aspects.map { |a| a.id }
+    @posts = StatusMessage.joins(:aspects).where(:pending => false,
+                                                 :aspects => {:id => @aspect_ids}).includes(:comments, :photos, :likes, :dislikes).select('DISTINCT `posts`.*').paginate(
+      :page => params[:page], :per_page => 15, :order => session[:sort_order] + ' DESC')
+    @fakes = PostsFake.new(@posts)
 
-      @contact_count = current_user.contacts.count
+    @contact_count = current_user.contacts.count
 
-      @aspect = :all unless params[:a_ids]
-      @aspect ||= @aspects.first #used in mobile
-    end
+    @aspect = :all unless params[:a_ids]
+    @aspect ||= @aspects.first # used in mobile
   end
 
   def create
     @aspect = current_user.aspects.create(params[:aspect])
-    #hack, we don't know why mass assignment is not working
+    # hack, we don't know why mass assignment is not working
     @aspect.contacts_visible = params[:aspect][:contacts_visible]
     @aspect.save
 
-- 
GitLab