Plug in additional functionallity with an engine - best practice?

Hi, I would like to plug in certain functionality with an engine and I have done it in different ways in different projects and would really like to understand how you are approaching this problem. An engine provides additional controller, views, models - how do you plug in links to this additional controllers in existing views.

I have an ‘organizations’ engine that provides

# in the 'organization' engine
class Organization < ApplicationRecord
end

There are controllers, models, views

One client needs to have a relationship (parent-children) between the organizations. This is not needed by other clients. Naturally we create an engine - 'organizations_relations" that provides the model, controllers, views for creating and managing relations and we would pack this only for this client.

# in the 'organizations_relations' engine
class OrganizationRelation < ApplicationRecord
end 
...
# and models, views, controllers. 

But now that there is the ‘organizations_relations’ engine we should put an extra link in the organizations/show.html.erb that points to /organizations_relations.

But organizations/show.html.erb is in the ‘organizations’ gem that knows nothing about the ‘organizations_relations’.

How do you plug in the link to /organizations_relations from the ‘organizations_engines’ into the organizations/show.html.erb available in ‘organizations’ engine.

  1. Do not do it and put the link somewhere else, but not on the organizations/show.html.erb
  2. Use deface gem or similar mechanism for extending views
  3. Rewrite the organizations/show.html.erb in the ‘organizations_relations’ engine
  4. Rewrite the organizations/show.html.erb in the host app.
  5. Something different?
  6. Do not do it at all because it is the wrong approach - engines are not meant to be extended…

Thanks

1 Like

What about a hook?

Create a blank file called _links.html.erb in the “organizations” engine and have show.html.erb render that partial. “organization_relations” engine then overrides that partial with your link.

This way your “organizations” engine is giving a official place for another engine or the application to hook in extra links related to the organization.

The only problem with this approach is if you had more than once engine that needed to add links. If that was the case maybe bring it into Ruby code. Have some Ruby code that keeps a list of registered links. By default this list is empty but other engines and application code can add to this list.

Yes. Hooks are an option. As you mention you get to the point where there is only onе engine that could extend you.