I am unsure of what the best way of doing this is, or if my thinking is fundamentally flawed.
I have a controller that creates a Plain Old Ruby Object (PORO) presenter on its #index
action:
# app/controllers/my_controller.rb
class MyController < ApplicationController
def index
@my_presenter = MyPresenter.new(
filter_1: params[:filter_1],
filter_2: params[:filter_2])
end
end
And the presenter which uses data from multiple models:
# app/presenters/my_presenter.rb
class MyPresenter
def initialize(filter_1: nil, filter_2: nil)
@my_data = MyModel.my_scope(filter_1) # scoping in the my_model.rb file
@my_other_data = MyOtherModel.my_scope(filter_1, filter_2)
end
def show(view)
# Somehow call the my_function.js from here and return the result to the view
end
end
And, finally, the View that defers to the presenter:
<!-- app/views/my_controller/index.html.erb -->
<h1>The view</h1>
<%= @my_presenter.show(this) %>
I want the #show
action of the presenter to call some JavaScript (D3 code for visualisation), and return the result to the View:
// app/assests/javascripts.js
function my_function(data) {
// D3 visualisation to go here...
return null
}
What is the best way to accomplish this?