简体   繁体   English

在名称空间中组织Rails控制器

[英]Organizing rails controllers within namespaces

I have a fairly simple question about the code organization of rails controllers: 我对rails控制器的代码组织有一个非常简单的问题:

If we have an app store platform (ie Google Play) which consists of Developers (They create/upload the apps), Admins (Review and approve the apps) and Users (Who consume the apps via the store). 如果我们拥有一个由开发人员(他们创建/上传应用程序),管理员(审查和批准应用程序)和用户(通过商店使用应用程序)组成的应用程序商店平台(即Google Play)。

The developers do everything through the developers platform, so it would make sense to have a developers namespace to group related items. 开发人员通过开发人员平台进行所有操作,因此拥有一个开发人员名称空间来对相关项目进行分组将是有意义的。 The same logic would seem to make sense for the admin, give them an admin panel under an admin namespace. 对于管理员来说,相同的逻辑似乎很有意义,在admin名称空间下为他们提供一个管理面板。

Now here's the question - I have two namespaces, Admin and Developer - if, a developer can change an app's state from :draft to :pending (for review) and the admin can change the state from pending to :approved/:rejected, where is the recommended place to store apps_controller.rb? 现在的问题是-我有两个命名空间,分别是AdminDeveloper如果开发人员可以将应用程序的状态从:draft更改为:pending(以供审核),而admin可以将状态从暂挂更改为:approved /:rejected,推荐存放apps_controller.rb的地方吗?

There seems to be three ways to handle this and I'm not quite sure which is the most 'correct', either for practical reasons or conventional reasons. 似乎有三种方法可以解决此问题,无论是出于实际原因还是出于常规原因,我都不确定哪种方法最正确。

The three options I can think of are: 我可以想到的三个选项是:

1) In both the Admin & Developer namespace (and populate the store from developer::apps)? 1)在AdminDeveloper命名空间中(并从developer :: apps填充商店)?

2) Only in the Developers namespace 2)仅在Developers名称空间中

3) The apps_controller should not live in either namespace. 3)apps_controller不应存在于任何命名空间中。

No worries, you are in OO world. 不用担心,您在OO世界中。

Two designs: 两种设计:

  1. Make a general AppsController but no routing to it. 制作一个通用的AppsController,但不要路由到它。 Admin and Developer's apps controllers will inherit this, and overwrite when necessary. 管理员和开发人员的应用程序控制器将继承此内容,并在必要时覆盖。

  2. Make a module and get it included in Admin's AppsController and Developer's, overwrite when necessary. 制作一个模块,并将其包含在Admin的AppsController和Developer的模块中,必要时将其覆盖。

It's hard to say which is better without detailed use case, though I would prefer #1 generally. 很难说没有详细的用例哪个更好,尽管我通常更喜欢#1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM