这太干了吗,我是不是太过分了?
我一直在干燥一些代码,其中一个重构如下:
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
right_user
方法添加到ApplicationController
类中。在您的控制器中使用新方法作为过滤器方法:
Add the
correct_user
method to theApplicationController
class.In your controllers use the new method as a filter method:
把它清理干净绝对是件好事。我认为你可能让事情变得比必要的稍微复杂一些,尽管如此,对于所有的 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.我个人会进一步干燥它,并将过滤器移至超类(或 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.