How can I pre-process .rb files in Rails? - ruby-on-rails

I wrote a Ruby language pre-processor that I also want to tie in to Rails.
Accomplishing this isn't exactly clear from my research. I want the same effect of having .rb.erb or .rb.haml but on files that are not templates(models and controllers, for example). It seems that typical pre-processing is specific to the asset pipeline.
http://guides.rubyonrails.org/v3.2.17/asset_pipeline.html#preprocessing
Does anyone know where I could start in writing a pre-processor for code that is not part of the asset pipeline?
EDIT: I want to do something to the following effect:
Write Ruby code in a custom syntax.
Each file with the custom syntax has an extra extension to denote custom syntax. For example, the model page.rb would be page.rb.custom.
When Rails runs(or when Spring reloads the app), code that has a .custom file extension is preprocessed(or precompiled, rather) into pure Ruby and is executed.

Related

30Bees/Presta 1.6 shop translation in frontController

I am trying to add a few translations to the frontend of our module. When the translations are in the .tpl files they do get rendered. However no translation fields get shown in the backend my code for the .tpl files is:
{l s="Text" mod="myModule"}
I also do need to do some translating in the FrontControllers (mainly Error handling and feedback for serverside validation).
In the AdminController I simply use $this->l('Text'); which works. However, in the FrontController this is not available. I've checked the ControllerCore and FrontControllerCore, l() is not defined in those and only available in AdminController.
Can anyone give me a detailed explanation of what I need doing? All my research on the web always points to $this->l() being the thing to use...
When using translations in tpl files you need to use single quotes not double quotes.
{l s='Text' mod='myModule'}
As for front controllers... well if you're using custom module controllers as in controllers that extend ModuleFrontController you can use
$this->module->l('Text');
And if you're not using those controllers then... start using them.
Some things might be different since thirtybees is a fork of PrestaShop but I guess translation mechanism is the same.

how does preprocessor engine runs when asset files are compiled in rails?

Let's say I have a file called foobar.js.erb.coffee.
I'm confused how this file is interpreted when rails application is compiled. My understanding is like following:
1) Coffeescript preprocessor engine interprets from coffeescript to ruby(erb).
2) ERB preprocessor engine converts ruby to javscript.
Am I understanding this correctly?
For example, foobar.js.erb.coffee
The extension of the file will be composed of two parts: the format (foobar.js) followed by the handler (.erb.coffee).
A handler is a preprocessor for the template, or a templating language. There are a number of handlers built in, and many more can be added by using extra gems.
The order of conversion is from right to left.
In your case, the CoffeeScript engine try to convert CoffeeScript into JavaScript (Error may occur because of existing ERB may cause CoffeeScript has invalid syntax) and then the ERB handlers will replace all the Ruby code to what the output value should be.
I always put .erb at last for these kind of situation. For example, main.css.scss.erb or app.js.es6.erb.

Rails Custom ActionForm Helper

I do not like the current date helper for action form and would like to replace it with something like the bootstrap datetime control.
I need to know the steps to write a custom actionform helper. I need this because I am looking to make this a modular solution for date/time control that I can use across projects -- and share with the world too, of course.
I am relatively new to ruby/rails and the whole DSL thing... so please try to explain a little of the magic to a long time c/java programmer.

Rails overriding Mini-Test templates

I am trying to override the generator templates here, but am having no luck.
I have configured mini-test to use the spec syntax, and it generates the spec template but not my custom one. Also it calls the generated file for example *controller_test.rb even though it contains the spec syntax.
Here is my structure:
OK it turns out that the mini-test folder should be mini_test

In Ruby, how can I inspect the class generated by a .html.erb template?

When doing J2EE development, I find it handy for debugging to view the Java classes that are generated by the JSP compiler.
How can I do the equivalent in Ruby? Since it is all in memory, it won't generate a file that I can view. I believe it's the ERB module that generates the corresponding object for a template, so how can I actually view the object? Can I drop a debugger statement somewhere and use rdb? Is there some configuration value I can tell it to dump the object definition? I'm using rails, in case that makes a difference.
I don't think rails generates a class for your view. It basically calls eval after processing the file. Or do you mean inspecting the erb object while it's parsing your template?
If it's the latter you can find erb.rb in lib\ruby\1.9.1 I'd imagine you could just drop a debugger statement throughout that file.
I always make a habit of adding the following to my views (layout) which allows me to inspect or debug the parameters being used by the view in question.
<%= debug(params) %>
This will format all the parameters in yaml and display them in a Hash format.
Have a look at the method in the source code to get a better understanding. SOURCE
There are some differences compared with the Java way due to language differences.
Most template libraries for Ruby follow these steps when compiling/optimizing:
The template is compiled into Ruby source code -- not a class but a long procedure that appends to a string buffer while traversing the logic of the original template.
This ruby code is evaluated in order to be bound for later reference, preferably within a method body. This way, it is only parsed once by the interpreter.
The method (or other context) containing the logic of the parsed template is invoked to render it.
Anyway, the compiled template code therefore looks a lot like a much noisier version of your original template, and will generally not help you debugging, unless you're debugging the template language itself.
Anyone interested in template language implementation might enjoy a look around the Tilt code (use different template languages with the same rendering interface and optimization), and Temple (a great template language meta-implementation).

Resources