这太干了吗,我是不是太过分了?

发布于 2024-10-01 06:00:31 字数 1674 浏览 1 评论 0原文

我一直在干燥一些代码,其中一个重构如下:
我有 3 个控制器(ConstructionCompanies、RealEstateCompanies、People),所有控制器都具有以下模式:


class ConstructionCompaniesController < ApplicationController
before_filter :correct_user, :only => [:edit, :update]

private
   def correct_user
      @company = ConstructionCompany.find(params[:id]) 
      if(current_user.owner != @company.user)
        redirect_to(root_path)
      end
    end   

class RealEstateCompaniesController < ApplicationController
  before_filter :correct_user, :only => [:edit, :update]
...

private
   def correct_user
      @company = RealEstateCompany.find(params[:id]) 
      if(current_user.owner != @company.user)
        redirect_to(root_path)
      end
    end   

如您所见,每个控制器中都会重复正确的用户。
因此,我在包含所有这些内容的助手中所做的事情是我创建了一个方法:


def correct_user_for_seller_of_controller(controller)
    #"User".classify will return the class User etc.
    @seller = controller.controller_name.classify.constantize.find(params[:id])     
    redirect_to(root_path) unless (current_user == @seller.user)
  end                            

了解我拥有的每个控制器的内部:


class ConstructionCompaniesController < ApplicationController

  before_filter :only => [:edit, :update] do |controller| correct_user_for_seller_of_controller(controller) end           


class RealEstateCompaniesController < ApplicationController

 before_filter :only => [:edit, :update] do |controller| correct_user_for_seller_of_controller(controller) end           

我喜欢现在是 DRY 的事实,但问题是它对我来说似乎有点复杂,很难去理解。我是不是太过分了?

I've been drying some code, one of this refactors is as following:
I have 3 controllers ( ConstructionCompanies, RealEstateCompanies, People) all of which had the following pattern:


class ConstructionCompaniesController < ApplicationController
before_filter :correct_user, :only => [:edit, :update]

private
   def correct_user
      @company = ConstructionCompany.find(params[:id]) 
      if(current_user.owner != @company.user)
        redirect_to(root_path)
      end
    end   

class RealEstateCompaniesController < ApplicationController
  before_filter :correct_user, :only => [:edit, :update]
...

private
   def correct_user
      @company = RealEstateCompany.find(params[:id]) 
      if(current_user.owner != @company.user)
        redirect_to(root_path)
      end
    end   

As you can see the correct_user is repeated in each controller.
So what I did inside I helper that is included for all of them I created a method:


def correct_user_for_seller_of_controller(controller)
    #"User".classify will return the class User etc.
    @seller = controller.controller_name.classify.constantize.find(params[:id])     
    redirect_to(root_path) unless (current_user == @seller.user)
  end                            

Know inside each controller I have:


class ConstructionCompaniesController < ApplicationController

  before_filter :only => [:edit, :update] do |controller| correct_user_for_seller_of_controller(controller) end           


class RealEstateCompaniesController < ApplicationController

 before_filter :only => [:edit, :update] do |controller| correct_user_for_seller_of_controller(controller) end           

I like the fact that is DRY now, but the problem is that it seems a little to complex for me, hard to understand. Did I went too far ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为一人 2024-10-08 06:00:31

right_user 方法添加到 ApplicationController 类中。

class ApplicationController
  def correct_user_for_seller_of_controller
    #"User".classify will return the class User etc.
    @seller = controller_name.classify.constantize.find(params[:id])     
    redirect_to(root_path) unless (current_user == @seller.user)
  end  
end

在您的控制器中使用新方法作为过滤器方法:

class RealEstateCompaniesController < ApplicationController

 before_filter :correct_user_for_seller_of_controller, :only => [:edit, :update]

end

Add the correct_user method to the ApplicationController class.

class ApplicationController
  def correct_user_for_seller_of_controller
    #"User".classify will return the class User etc.
    @seller = controller_name.classify.constantize.find(params[:id])     
    redirect_to(root_path) unless (current_user == @seller.user)
  end  
end

In your controllers use the new method as a filter method:

class RealEstateCompaniesController < ApplicationController

 before_filter :correct_user_for_seller_of_controller, :only => [:edit, :update]

end
·深蓝 2024-10-08 06:00:31

把它清理干净绝对是件好事。我认为你可能让事情变得比必要的稍微复杂一些,尽管如此,对于所有的 Proc 之类的东西。

如果过滤器在控制器的实例上运行,则无需将控制器传递给其自身。只需在方法中调用 controller_name 即可。

It's definitely good to clean that up. I think you may have made things slightly more complex than necessary, though, what with all the Proc whatnot.

If the filter is being run on an instance of the controller, then there's no need to pass the controller to itself. Just call controller_name within the method and you're good to go.

挖个坑埋了你 2024-10-08 06:00:31

我个人会进一步干燥它,并将过滤器移至超类(或 AppplicationController)。

该方法本身也绝对可以简化。例如,进行一些内省。

I'd personally DRY it a bit more and move the filter to a superclass (or AppplicationController).

The method itself could also definitely be simplified. Use some introspection for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文