1

I have a Rails (4+) untypical usecase where one controller action needs to use the logic of another controllers action...

This question is related: Rails: call another controller action from a controller But its answers address only the case of redirection to another controller or delegating rendering to another controller, but I need to use some data transforming logic from the other controller and get the transformed results back to the calling controller...

Preliminaries: My Rails application depends on several local Rails Engines, loaded in the main applications Gemfile with:

gem 'my_engine', :path => '../my_engine'

Those Engines implement an input-data transforming REST service with MVC and routes. They use some of the models of the main application (for configuration), extend a base controller of the main app to have the same REST authorization like the main application etc...

Use Case: Now my new use case needs me to use those engines controller actions inside of a main applications controller, to transform input data with those engines in a serial manner and return the transformed result for every execution back to the calling controller.

Pseudocode:

In main application: app/controllers/api/main_app_controller.rb:

class Api::MainAppController < ApplicationController

def index
   result = params['data']
   my_engines.each do |my_engine|
      result = my_engine.execute_engine_controller_action(input)
   end          
   render result
end

end

How do I have to change my Engines, to fulfill the new use-case and how do I execute those engines controller actions in a serial manner from a main applications controller?

Community
  • 1
  • 1
Stilzk1n
  • 165
  • 8

1 Answers1

1

Answer to myself:

You should out-source the data transforming logic from the Engine's controller into a helper of your Engine or in:

lib/my_engine/my_engine.rb

Then call this logic in the controller of the Engine and for the new use-case also in the main applications controller.

Stilzk1n
  • 165
  • 8