我的應用程序中有這個非常大的控制器。我真的很想盡可能地瘦。下面是一些代碼,顯示了我目前正在做的事情的類型。我想知道我可以從中移出哪些東西?在rails 3中製作胖控制器瘦身
注意 - 這不是我確切的代碼,它有很多相似之處。基本上每個實例變量都用在視圖中 - 這就是爲什麼我不明白如何將邏輯放入模型中?模型可以返回實例變量的值嗎?
def mine
#For Pusher
@push_ch = "#{current_user.company.id}"+"#{current_user.id}"+"#{current_user.profile.id}"
#Creating a limit for how many items to show on the page
@limit = 10
if params[:limit].to_i >= 10
@limit = @limit + params[:limit].to_i
end
#Setting page location
@ploc="mine"
@yourTeam = User.where(:company_id => current_user.company.id)
#Set the user from the param
if params[:user]
@selectedUser = @yourTeam.find_by_id(params[:user])
end
#Get all of the user tags
@tags = Tag.where(:user_id => current_user.id)
#Load the user's views
@views = View.where(:user_id => current_user.id)
if !params[:inbox]
#Hitting the DB just once for all the posts
@main_posts = Post.where(:company_id => current_user.company.id).includes(:status).includes(:views)
@main_posts.group_by(&:status).each do |status, posts|
if status.id == @status.id
if @posts_count == nil
@posts_count = posts
else
@posts_count = @posts_count + posts
end
elsif status.id == @status_act.id
if @posts_count == nil
@posts_count = posts
else
@posts_count = @posts_count + posts
end
end
end
if params[:status] == "All" || params[:status] == nil
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).order(sort_column + " " + sort_direction).where(:company_id => current_user.company.id, :status_id => [@status.id, @status_act.id, @status_def.id, @status_dep.id, @status_up.id]).limit(@limit).includes(:views)
else
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).order(sort_column + " " + sort_direction).where(:company_id => current_user.company.id).limit(@limit).includes(:views)
end
elsif params[:inbox] == "sent"
@yourcompanylist = User.where(:company_id => current_user.company.id).select(:id).map(&:id)
@yourcompany = []
@yourcompanylist.each do |user|
if user != current_user.id
@[email protected]([user])
end
end
if params[:t]=="all"
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).tag_filter(params[:tag], current_user).order(sort_column + " " + sort_direction).where(:user_id => current_user.id).includes(:views, :tags).limit(@limit)
elsif params[:status]!="complete"
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).tag_filter(params[:tag], current_user).order(sort_column + " " + sort_direction).where(:user_id => current_user.id).includes(:views, :tags).limit(@limit)
elsif params[:status]!=nil
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).tag_filter(params[:tag], current_user).order(sort_column + " " + sort_direction).where(:user_id => current_user.id).includes(:views, :tags).limit(@limit)
end
end
respond_to do |format|
format.html # index.html.erb
format.js # index.html.erb
format.xml { render :xml => @posts }
end
end
你可以使用的模塊,包括適當的方法 – sushant