Rails Engines Example This shows how to use engines for namespacing within a "operator" Rails application. View original post. Table of con…
Rails Engines Example This shows how to use engines for namespacing within a "operator" Rails application. View original post. Table of con…
This shows how to use engines for namespacing within a "operator" Rails application. View original post.
At TaskRabbit, we have gone through a few iterations on how we make our app(s). In the beginning, there was the monolithic Rails app in the standard way with 100+ models and their many corresponding controllers and views. Then we moved to several apps with their own logic and often using the big one via API. Our newest project is a single "app" made up of several Rails engines. We have found that this strikes a great balance between the (initial) straightforwardness of the single Rails app and the modularity of the more service-oriented architecture.
We've talked about this approach with a few people and they often ask very specific questions about the tactics used to make this happen, so let's go through it here and via a
sample application.back to top
Rails Engines is basically a whole Rails app that lives in the container of another one. Put another way, as the docs note: an app itself is basically just an engine at the root level. Over the years, we've seen engines as parts of gems such as devise or rails_admin. These example show the power of engines by providing a large set of relatively self-contained functionality "mounted" into an app.
At some point, there was a talk that suggested the approach of putting my our functionality into engines and that the Rails team seemed to be devoting more and more time to make them a first class citizen. Our friends at Pivotal Labs were talking about it a lot, too. Sometimes good and sometimes not so good.
We'd seen an app balloon and get out of control before, leading us to try and find better ways of modularization. It was fun and somewhat liberating to say "Make a new app!" when there was a new problem domain to tackle. We also used it as a way to handle our growing organization. We could ask Team A to work on App A and know that they could run faster by understanding the scope was limited to that. As a side-note and in retrospect, we probably let organizational factors affect architecture way more than appropriate.
Lots of things were great about this scenario. The teams had freedom to explore new approaches and we learned a lot. App B could upgrade Rack (or whatever) because it depended on the crazy thing that App A depended on. App C had the terrible native code-dependent gem and we only had to put that on the App C servers. Memory usage was kept lower, allowing us to run more background workers and unicorn threads.
But things got rough in coordinating across these apps. It wasn't just the data access. We made APIs and allowed any app to have read-only access to the platform app's database. This allowed things go much faster by preventing creation of many GET endpoints and possible points of failure. The main issue in coordinating releases that spanned apps is that they just went slower than if it was one codebase. There was also interminable bumping of gem versions to get shared code to all the apps. Integration testing the whole experience was also very rough.
So it's a simple one, but the main advantage that we've seen in the engine model is that it is one codebase and git repo. A single pull request has everything related to that feature. It rolls out atomically. Gems can be bumped once and our internal gems aren't bumped at all as they live unbuilt in a gems folder in the app itself. We still get most of the modularization that multiple apps had. For example, the User model in the payments engine has all the stuff about balances and the one in the profile engine doesn't know anything about all that and it's various helper methods.
The issue with gem upgrades and odd server configurations does continue to exist in the engine model and is mostly fine in the many app model. The gem one is tough and we just try to stay on top of upgrading to the newest things and overall reducing dependencies. The specs will also run slower in the engine app, but you'll have better integration testing. I'll go over a little bit about we've tackled server configurations and memory further down.
It's very tempting when green-fielding a project to just revert back to the good-old-days of the original app. Man, that was so nice back before the (too) fat models and tangled views and combinatorics of 4 years of iterating screwed things up. And we've learned a lot since then too, right? Especially about saying no to all those combinatorics and also using decorators and service objects and using APIs. Maybe.
What we do know is that you can feel that way again even a year into an app. Inside any given engine, you have the scope of a much smaller project. Some engines may grow larger and you'll start to use those tools to keep things under control. Some will (correctly) have limited scope and feel like a simple app in which you understand everything that is happening. For example, decorators are great tool and they came in handy in our big app and larger engines. However, we've found in an a targeted engine that only serves its one purpose, it feels like there is room in that model to have some things that would have been decorated in a larger app. This is because it doesn't have all that other junk in it. Only this engine's junk :-)
We've seen a few different ways to use engines in a Rails project. A few examples are below. The basic variables are what is in the "operator" (root) app and what kind of app we're making (API driven or not).
The first engine we've recommend making to people is the admin engine. In the first app, we made the mistake of putting admin functionality in the "normal" pages. It was very enticing. We had that form already for the user to edit it. Just by changing the permissions, we could allow the admin to edit it, too. Forms are cheap and admins want extra fields. And more info. And basically a different UI.
So we can made an engine basically just like rails_admin did and gave it's own layout and views and JS and models and controllers, etc. Overall, we started treating our hardworking admins like we should: a customer with their own needs and dedicated experience.
The structure looked something like this...
app
assets
controllers
models
user.rb
post.rb
views
layouts
admin
app
assets
controllers
models
admin
user.rb
post.rb
views
layouts
config
db
migrate
gems
specWhen we had this all mixed into one interface and set of models, at least a third of the code in a model like Post or User would be admin-specific actions. With this approach, we can give the admins a better, targeted experience and keep that code in admin-land.
Throughout these engine discussions, the question of sharing code and/or inheriting from objects will keep coming up. Specifically, for the admin scenario, we say do whatever works for you and on a case by case basis. In the above approach, we would probably tend to have `Admin::Post '/', as: app.gem_name end end
So as expected, the operator app has no routes of it's own and it's all handled by the engines. I'll add little more about the `BootInquirer` in a bit. It is just a helper class that knows all the engines. This means that the code is functionally something more like this:
```ruby
RailsEnginesExample::Application.routes.draw do
mount Admin::Engine => '/', as: 'admin'
mount Account::Engine => '/', as: 'account'
mount Content::Engine => '/', as: 'content'
mount Marketing::Engine => '/', as: 'marketing'
end
…
ruby
describe Content::Post do
fixtures :users
it "should be associated with a user" do
user = users(:willy)
post = Content::Post.new(content: "words")
post.user = user
post.save.should == true
user.posts.count.should == 1
end
endThis is a problem because of classes expecting to be a certain type. You'd get this error:
Failures:
1) Content::Post should be associated with a user
Failure/Error: post.user = user
ActiveRecord::AssociationTypeMismatch:
Content::User(#70346317272500) expected, got Account::User(#70346295701620)So the user has to be an instance of the Content::User and not an Account::User class. We use a
helper
to say what the classes are as well as switch between them. So this test will use the correct classes:
describe Content::Post do
fixtures :users
it "should be associated with a user" do
user = fixture(:users, :willy, Content)
post = Content::Post.new(content: "words")
post.user = user
post.save.should == true
user.posts.count.should == 1
end
endThe same sort of thing could be done with FactoryGirl too. Often, we end up just using the ids more than we would in a normal test suite. The important thing to note is to just do whatever you feel gives you the best coverage with the most return on investment for your time.
You may have noticed the BootInquirer class mentioned earlier. This is a class that know about all the engines in the system.
APPS = {
'a' => 'account',
'c' => 'content',
'm' => 'marketing',
'z' => 'admin'
}It is called from three places.
# Gemfile
gemspec path: "apps/shared"
BootInquirer.each_active_app do |app|
gemspec path: "apps/#{app.gem_name}"
end
# application.rb
require_relative "../lib/boot_inquirer"
BootInquirer.each_active_app do |app|
require app.gem_name
end
# routes.rb
BootInquirer.each_active_app do |app|
mount app.engine => '/', as: app.gem_name
end
…
$ ENGINE_BOOT=am bundle exec rails c
=> will boot the account and marketing engines - but not content, admin, etc.
$ ENGINE_BOOT=-m bundle exec rails c
=> will boot all engines except marketing
…
ruby
ResqueBus.dispatch('profile') do
subscribe 'post_rated' do |attributes|
profile = Profile::Document.find_by(user_id: attributes['author_id'])
profile.post_ratings_total = attributes['total_ratings']
profile.post_rating_average = attributes['new_average']
profile.save!
end
endOr in the way that we p
No open issues yet, or sync has not completed.