将类放在不适合任何地方的 Rails 应用程序中的位置指南 [英] guidelines for where to put classes in Rails apps that don't fit anywhere

查看:13
本文介绍了将类放在不适合任何地方的 Rails 应用程序中的位置指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何关于将非标准 Ruby 文件放在 Rails 应用程序中的位置的最佳实践,这些文件不适合任何默认目录(controllers/模型等).

I'm wondering if there are any best practices about where to put non-standard Ruby files in Rails apps, those that don't fit in any of the default directories (controllers/models etc.).

我说的是控制器/模型等使用的类,但不是任何 Rails 基类的子类.包含从模型中提取的功能的类,以减少它们的脂肪.有些看起来像模型但不是 AR 模型,有些看起来更像是服务",有些介于两者之间或其他什么东西.

I'm talking about classes that are used by controllers/models etc., but are not subclasses of any of the Rails base classes. Classes that include functionality extracted from models to make them less fat. Some of them kind of look like models but aren't AR models, some of them look more like "services", some are something in between or something else.

一些随机示例:

  • 通过 facebook 等处理密码身份验证的策略"类
  • 封装参数的XParams"对象或处理参数处理的XCreator"对象,以执行一些复杂的动作,最终创建一些AR模型
  • 向外部 API 发出请求或封装这些请求和响应的类
  • 可以替代真实 AR 模型的假模型(例如访客用户)
  • 重新安排工作
  • 从 Redis 存储和读取信息的类
  • 执行某些特定操作(例如处理数据、生成报告等)并从 Resque 作业或 rake 任务调用的类

我现在有很多这些,其中一些被添加到 lib 中,最终变成一堆随机的类和模块,一些潜入 app/models.我想以某种方式组织这个,但我不知道从哪里开始.

I've got quite a lot of these now, some of them are added to lib which ends up as a pile of random classes and modules, some sneak into app/models. I'd like to organize this somehow, but I don't know where to start.

是否应该只有 AR 模型进入 app/models?或者也可以放置任何域或辅助模型?您如何确定某物是否是模型?

Should only AR models go into app/models? Or is it ok to also put there any domain or helper models? How you decide if something is a model?

所有不适合 app 的东西都应该放到 lib 中吗?或者我应该向 app 添加一些新的自定义子目录?什么子目录,如何划分自定义类?

Should everything that doesn't fit into app go into lib? Or maybe I should add a few new custom subdirectories to app? What subdirectories, and how do I divide the custom classes?

您在项目中如何处理这个问题?我知道每个项目都有点不同,但一定有一些相似之处.

How do you handle this in your projects? I know every project is a bit different, but there must be some similarities.

推荐答案

好问题 - 我没有具体的答案给你

Good question - i don't have a concrete answer for you

但我建议查看这篇文章- http://blog.codeclimate.com/blog/2012/02/07/what-c​​ode-goes-in-the-lib-directory/- 请务必通读所有评论

but I recommend checking out this post - http://blog.codeclimate.com/blog/2012/02/07/what-code-goes-in-the-lib-directory/ - be sure to read through all the comments

在当前项目中,我在应用程序/模型下有大量非 ActiveRecord 对象,它可以工作但并不理想我将可重复使用"的非应用程序特定代码放在 lib 下

on a current project i have a ton of non-ActiveRecord objects under app/models, it works but not ideal i put 're-useable' non application specific code under lib

我在辅助项目中尝试过的其他替代方案(假设我们有一堆命令对象)rails 在 app 下的命名空间很麻烦,默认情况下它会将所有内容加载到同一个命名空间中

other alternatives I have tried on side projects (say we have a bunch of command objects) rails is a pain when it comes to namespaces under app, it loads everything up into the same namespace by default

app/
  commands/
    products/create_command.rb         # Products::CreateCommand
    products/update_price_command.rb   # Products::UpdatePriceCommand

替代,src 或 app_name 目录下除 rails 之外的所有内容

alternate, everything besides rails under src or an app_name directory

app/
  src/
    commands/
      create_product.rb         # Commands::CreateProduct
      update_product_price.rb   # Commands::UpdateProductPrice

我还没有遇到一个很好的解决方案,理想情况下第二个更好,但最好不要在应用程序下有额外的目录,这样你就可以打开应用程序并查看控制器、命令、模型等..

I haven't come across a good solution for this, ideally the 2nd one is better, but would be nice to not have the additional directory under app, that way you open app and see controllers, commands, models etc...

这篇关于将类放在不适合任何地方的 Rails 应用程序中的位置指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆