首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >帮我重构这个讨厌的Ruby if/else语句

帮我重构这个讨厌的Ruby if/else语句
EN

Stack Overflow用户
提问于 2010-05-08 19:54:49
回答 3查看 1.5K关注 0票数 1

所以我在我的时事通讯分发应用程序中有一个很大的方法。方法是为了更新人造丝,我需要分配一个用户到人造丝。通过具有属性since_dateuntil_date的表colporteur_in_rayons,我有关系n:n。

我是一个初级程序员,我知道这段代码是相当愚蠢的:)我很欣赏每一个建议。

代码语言:javascript
复制
def update
  rayon = Rayon.find(params[:id])
  if rayon.update_attributes(params[:rayon])
    if params[:user_id] != ""
      unless rayon.users.empty?
        unless rayon.users.last.id.eql?(params[:user_id])
          rayon.colporteur_in_rayons.last.update_attributes(:until_date => Time.now)
          Rayon.assign_user(rayon.id,params[:user_id])
          flash[:success] = "Rayon #{rayon.name} has been succesuly assigned to #{rayon.actual_user.name}."
          return redirect_to rayons_path
        end
      else
         Rayon.assign_user(rayon.id,params[:user_id])
         flash[:success] = "Rayon #{rayon.name} has been successfully assigned to #{rayon.actual_user.name}."
         return redirect_to rayons_path
      end
    end
    flash[:success] = "Rayon has been successfully updated."
    return redirect_to rayons_path
  else
    flash[:error] = "Rayon has not been updated."
    return redirect_to :back
  end
end
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-05-08 22:25:20

代码语言:javascript
复制
def update
    rayon = Rayon.find(params[:id])

    unless rayon.update_attributes(params[:rayon])
        flash[:error] = "Rayon not updated."
        return redirect_to :back
    end

    puid = params[:user_id]
    empty = rayon.users.empty?

    if puid == "" or (not empty and rayon.users.last.id.eql?(puid))
        msg = "Rayon updated.",
    else
        msg = "Rayon #{rayon.name} assigned to #{rayon.actual_user.name}.",
        rayon.colporteur_in_rayons.last.update_attributes(
            :until_date => Time.now) unless empty
        Rayon.assign_user(rayon.id, puid)
    end

    flash[:success] = msg[msg_i]
    return redirect_to rayons_path
end
票数 5
EN

Stack Overflow用户

发布于 2010-05-09 02:50:36

有一个双重密码。所以把它去掉。但是,控制器中有一些业务代码,这些代码不应该在那里。所以我应该重构控制器代码,如下所示:

代码语言:javascript
复制
def update
  rayon = Rayon.find(params[:id])

  if rayon.update_attributes(params[:rayon])
    if params[:user_id] != ""
      rayon.handle_update_user(params[:user_id]
      flash[:success] = "Rayon #{rayon.name} has been succesuly assigned to #{rayon.actual_user.name}."
    else
      flash[:success] = "Rayon has been successfully updated."
    end
    return redirect_to rayons_path
  else
    flash[:error] = "Rayon has not been updated."
    return redirect_to :back
  end
end

控制器方法现在清楚地处理需要采取的操作,并在需要时相应地设置闪存方法。

在人造丝模型中,您可以编写当用户完成更新时需要执行的代码:

代码语言:javascript
复制
class Rayon

  def handle_update_user(user_id)
    if (!users.empty? && users.last.id.eql?(params[:user_id]))
      # do nothing! 
    else 
      colporteur_in_rayons.last.update_attributes(:until_date => Time.now) unless users.empty?
      Rayon.assign_user(rayon.id,params[:user_id])
    end

  end

end

这显然将这些问题分开了。人造丝应该知道当用户更新它时会发生什么(函数的名称可以改进为你真正想要的意思,因为这对我来说并不完全清楚)。它可以更短,但我喜欢明确地写,如果最后一个用户与当前用户相同,则不需要执行任何操作。否则,需要采取行动。如果我没理解错的话。

票数 1
EN

Stack Overflow用户

发布于 2010-05-08 21:47:16

代码语言:javascript
复制
def update
  rayon = Rayon.find(params[:id])

  unless rayon.update_attributes(params[:rayon])
    flash[:error] = "Rayon has not been updated."
    return redirect_to :back
  end

  if params[:user_id].empty?
    msg = "Rayon has been successfully updated."
  else
    if rayon.users.empty?
      Rayon.assign_user(rayon.id,params[:user_id])
      msg = "Rayon #{rayon.name} has been successfully assigned to #{rayon.actual_user.name}."
    elsif not rayon.users.last.id.eql?(params[:user_id])
      rayon.colporteur_in_rayons.last.update_attributes(:until_date => Time.now)
      Rayon.assign_user(rayon.id,params[:user_id])
      msg = "Rayon #{rayon.name} has been succesuly assigned to #{rayon.actual_user.name}."
    end
  end
  flash[:success] = msg
  return redirect_to rayons_path
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2793953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档