简体   繁体   English

创建Rails宝石

[英]Creating Rails Gems

I want to build my application in a modular way: For example I might have: 我想以模块化方式构建我的应用程序:例如,我可能有:

Users->has_and_belongs_to_many->Projects
Users->has_and_belongs_to_many->Tasks
Projects->has_many->Tasks
Tasks->belongs_to->Project (1 task belongs to 1 project.)

Note : Projects AND tasks can have many users. 注意 :项目和任务可以包含许多用户。

The way I was thinking of building this is by doing: 我想要构建这个的方式是:

  • User = Gem 用户=宝石
  • Projects = Gem 项目=宝石
  • Tasks = Gem 任务=宝石

Each can return either json (for ember) or html. 每个都可以返回json(for ember)或html。 Neither depend on each other, The idea is that each is just a detail. 两者都没有相互依赖,这个想法是每个都只是一个细节。 Rails, ember and html are all just details. Rails,ember和html都只是细节。

How could I go about this and at the end of the day hook them together like a puzzle? 我怎么能这样做,并在一天结束时像拼图一样把它们连在一起? or is this even feasible? 或者这甚至可行吗?

Right now I have the user piece of this concept almost done. 现在,我已经完成了这个概念的用户部分。 It contains models, tests, controllers and views. 它包含模型,测试,控制器和视图。 Can I bundle that as a gem or should I only bundle the controller and the models? 我可以将其捆绑为宝石,还是仅将控制器和模型捆绑在一起? This "User" gem would make use of friendlyId gem to make the urls look nice, infact all the "gems" (project, tasks and users) will depend on this gem. 这个“用户”宝石将使用friendlyId gem来使网址看起来不错,实际上所有“宝石”(项目,任务和用户)都将依赖于这个宝石。

What would you recommend to do to have the kind of modularity I want? 你有什么建议去做我想要的模块化? is it good practice? 这是好习惯吗? bad practice? 不好的做法?

I believe use of Rails engines is the way to go. 我相信使用Rails引擎可行的方法。

Each Engine can be an app in itself. 每个引擎本身都可以是一个应用程序。

For instance Devise - gem that adds a lot of authentication functionality into your app is a Rails Engine. 例如, Devise - 在你的应用程序中添加了许多身份验证功能的gem是一个Rails引擎。 It provides its own controllers, helpers, mailers and views that you can use from your app. 它提供了您自己的控制器,帮助器,邮件程序和视图,您可以从您的应用程序中使用它们。

I personally was working in projects that were using Engines to plugin i18n web backends (admin page that allows entering and saving translations to Redis) into many apps and to plugin users' bug reports and Question&Answer functionality into several existing projects. 我个人正在使用Engines来插入i18n网络后端(允许输入和保存翻译到Redis的管理页面)到许多应用程序中,并将用户的错误报告和问答功能插入到几个现有项目中。

Each engine is being included in a project as a gem, it can consist of anything that normal app does - controllers, views, models, assets, other gems... 每个引擎都作为宝石包含在项目中,它可以包含普通应用程序所做的任何事情 - 控制器,视图,模型,资产,其他宝石......

I personally haven't heard of any success stories with such approach (but I don't know if anyone tried though), so if you have a solid concept in mind maybe it will work for you. 我个人没有听说过这种方法有任何成功的故事(但我不知道是否有人试过),所以如果你有一个坚实的概念,也许它会对你有用。

Pivotal Labs made a presentation (Euruko 2013 in Athens) and they showed a rails engines approach similar to what you are describing. Pivotal Labs做了一个演讲(Euruko 2013在雅典),他们展示了类似于你所描述的轨道引擎方法。 Here is the presentation http://www.ustream.tv/recorded/35107339/highlight/377037 这是演示文稿http://www.ustream.tv/recorded/35107339/highlight/377037

I haven't seen it in practice though, but I would like to. 虽然我没有在实践中看到它,但我想。

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

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