这个 Rails 3 Controller 方法会让我看起来很胖吗?
这是一个新的应用程序,我在搜索控制器上有一个索引方法。这也作为应用程序的主页,我试图从设计模式的角度确定我是否走上了错误的道路。
该方法已经有 35 行长。该方法的作用如下:
3 行设置变量来确定正在搜索的分层数据的“级别”。
另外 10 行用于根据请求中是否包含子域来填充一些视图变量。
重定向到两个页面之一的 10 行部分,基于:
1) 如果用户没有访问权限,但已登录,并且尚未请求访问权限,请告诉他们“单击此处请求访问该品牌”。
2) 如果用户没有访问权限、已登录且已请求访问权限,请告诉他们“某某正在审核您的请求”。
另外 10 条线用于构建动态区域。
我无法弄清楚如何区分这些问题,或者即使它们应该分开。我感谢您提供的任何帮助!
This is a new application, and I have an index method on a Search controller. This also serves as the home page for the application, and I'm trying to decide if I am headed down the wrong path from a design pattern perspective.
The method is already 35 lines long. Here is what the method does:
3 lines of setting variables to determine what "level" of hierarchical data is being searched.
Another 10 lines to populate some view variables based on whether a subdomain was in the request or not.
A 10 line section to redirect to one of two pages based on:
1) If the user does not have access, and is signed in, and has not yet requested access, tell them "click here to request access to this brand".
2) If the user does not have access, is signed in, and has already requested access, tell them "so and so is reviewing your request".
Another 10 lines to build the dynamic arel.
I can't get it straight in my head how to separate these concerns, or even if they should be separated. I appreciate any help you can offer!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
总结一下你在类似代码中所说的内容(抱歉,不了解 ruby;将其视为伪代码):
14 行而不是 35 行(当然,提取方法的主体会延长整体代码,但你可以看看这个并知道它在做什么)。值得做吗?这实际上取决于您或后续程序员是否更清楚。我的猜测是值得这样做,将小代码块拆分为自己的方法将使代码更易于维护。
Summarizing what you've said in something codelike (sorry, don't know ruby; consider it pseudocode):
14 lines instead of 35 (of course, the bodies of the extracted methods will lengthen the overall code, but you can look at this and know what it's doing). Is it worth doing? That really depends on whether it's clearer to you or subsequent programmers. My guess is it's worth doing, that splitting out little code blocks into their own method will make the code easier to maintain.
设置了很多变量。也许这对于某种模块来说是一个很好的机会?也许你的模块可以为你做出很多这样的决定,并且充当很多这样的变量的包装器。抱歉我没有更具体的答案。
That's a lot of variables being set. Maybe this is a good opportunity for a module of some kind? Perhaps your module can make a lot of these decisions for you, as well as acting as a wrapper for a lot of these variables. Sorry I don't have a more specific answer.
如果没有您的代码,就很难提出实际的修复建议,但这听起来绝对是一种真正错误的方法,并且您使事情变得比需要的更加困难:
,我认为您会希望将这些数据直接从 params 哈希传递到范围或 Model.where() 调用中。根据需要在模型上设置范围。
在我看来,它最多应该是 1 行。或者在您看来,您应该使用 if 语句来根据您的子域更改您希望的输出内容。
您对两个视图的解释中唯一不同的是“用户是否已请求访问”这肯定只是一个布尔变量?您只需要 1 个视图。将差异包装成 2 个部分,然后在您的视图中编写一个 if 语句以在它们之间进行选择。
可能有必要进入阿雷尔,但我非常怀疑。在大多数情况下,您的实际搜索调用可以(并且应该是)1 行,通过标准 ActiveRecord 查询接口完成。您希望在模型中设置强大的范围,以通过 ActiveRecord 查询接口连接到其他模型/缩小条件等。
Without your code it's somewhat difficult to suggest actual fixes, but it definitely sounds like a really wrong approach and that you're making things much harder than they need to be:
if there is a search form, I would think you would want to pass those straight from the params hash into scopes or Model.where() calls. Setup scopes on your model as appropriate.
This seems to me like it should be at most 1 line. or that in your view, you should use if statements to change what you'd like your output to be depending on your subdomain.
the only thing different in your explanation of the 2 views is "whether the user has requested access" surely this is just a boolean variable? You only need 1 view. Wrap the differences into 2 partials and then in your view and write one if statement to choose between them.
It might be necessary to go into Arel, but I highly highly doubt it. Your actual search call can in most cases (and should aim to be) 1 line, done through the standard ActiveRecord query interface. You want to setup strong scopes in your models that take care of joining to other models/narrowing conditions, etc. through the ActiveRecord Query interface.