gpt4 book ai didi

ruby-on-rails - 在 rails 3 skinny 中制作胖 Controller

转载 作者:数据小太阳 更新时间:2023-10-29 07:47:04 26 4
gpt4 key购买 nike

我的应用程序中有这个非常大的 Controller 。我真的很想让它尽可能瘦。下面是一些代码,显示了我目前正在做的事情的类型。我想知道我可以从中移出哪些东西?

请注意 - 这不是我的确切代码,很多都是相似的。基本上每个实例变量都在 View 中使用——这就是为什么我不明白如何将逻辑放入模型中的原因?模型可以返回实例变量的值吗?

  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
@yourcompany=@yourcompany.concat([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

最佳答案

您可以从将逻辑移入模型开始...

像这样的一行令人羡慕不已:

@push_ch = "#{current_user.company.id}"+"#{current_user.id}"+"#{current_user.profile.id}"

我建议将其移至模型中:

#user.rb
def to_pusher_identity
"#{self.company_id}#{self.id}#{self.profile_id}"
end

然后在你的 Controller 中

@push_ch = current_user.to_pusher_identity

此时您甚至可以将其移动到 before_filter 中。

before_filter :supports_pusher, :only => :mine

您可以做的另一件事是创建更丰富的联想,这样您就可以表达:

@tags = Tag.where(:user_id => current_user.id)    

作为

@tags = current_user.tags

另一个例子是主要帖子,而不是

Post.where(:company_id => current_user.company.id).includes(:status).includes(:views)

你会经历这些联想:

current_user.company.posts.includes(:status).includes(:views)

关于ruby-on-rails - 在 rails 3 skinny 中制作胖 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7043268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com