The easiest solution would have been to make ajax calls to the server with the parameters get the results. But we wanted a UI that reacts immediately so needed to have the code loaded in the browser.
So this a solution I come up with, in summary:
- Ruby Models load and use those formulas using V8
- The formulas are served to the front end as partials
Here is a rundown of what I did step by step. I will use a simple interest formula in this example.
Storing the formulas
I needed the formulas to be accessible as partials so I added them in the view folder:
1 2 3
Using the formulas in my Ruby models
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Loading the formulas in the front-end
I couldn’t find a way of injecting the JS code into the assets pipeline, so instead I just loaded in a partial that gets loaded in the application layout.
<%= render 'formulas/index' %>
1 2 3 4 5 6 7 8 9 10
Using the formulas in the front end
Once the formulas are loaded as shown above they can be used as:
So this is a way of sharing code between the back and the front end. If you how how could be improved or of a cleaner way altogether I would love to hear about it, maybe some DSL that can be used in both places.