简体   繁体   中英

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. Neither depend on each other, The idea is that each is just a detail. Rails, ember and html are all just details.

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.

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.

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. 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.

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. Here is the presentation http://www.ustream.tv/recorded/35107339/highlight/377037

I haven't seen it in practice though, but I would like to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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