Creating And Customizing Rails Generators & Templates — Ruby On Rails Guides
Since Rails three.Zero, generators are constructed on top of Thor. Thor gives powerful choices for parsing and a fantastic API for manipulating information. As an illustration, let’s build a generator that creates an initializer file named initializer.rb inside config/initializers.
create_file is a method supplied by Thor::Actions. Documentation for create_file and other Thor strategies may be found in Thor’s documentation
Our new generator is kind of simple: it inherits from Rails::Generators::Base and has one method definition. When a generator is invoked, every public method within the generator is executed sequentially in the order that it is defined. Finally, we invoke the create_file method that may create a file on the given vacation spot with the given content. If you’re accustomed to the Rails Software Templates API, you may really feel proper at residence with the brand new generators API.
Earlier than we go on, let’s see our model new generator description:
We will see that by invoking the description of this new generator (don’t forget to delete the old generator file):
So as to know what a generator template means, let’s create the file lib/generators/initializer/templates/initializer.rb with the next content material:
And now let’s change the generator to repeat this template when batman slim fit t shirt 60 invoked:
And let’s execute our generator:
The examples above put recordsdata under the application’s lib because mentioned listing belongs to $LOAD_PATH.
Earlier than we customise our workflow, let’s first see what our scaffold seems like:
Looking at this output, it’s easy to know how generators work in Rails three.0 and above. The scaffold generator doesn’t actually generate anything, it just invokes others to do the work. This allows us to add/substitute/take away any of those invocations. For example, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. Since each generator has a single duty, they are easy to reuse, avoiding code duplication.
If we wish to keep away from producing the default app/property/stylesheets/scaffolds.scss file when scaffolding a new useful resource we will disable scaffold_stylesheet:
After that, we will delete each the templates listing and the source_root class method name from our new generator, as a result of we are not going to wish them. Add the tactic under, so our generator seems like the next:
We can check out our new generator by making a helper for products:
Now, when the helper generator is invoked and TestUnit is configured as the check framework, it will attempt to invoke both Rails::TestUnitGenerator and TestUnit::MyHelperGenerator. Since none of these are defined, we will inform our generator to invoke TestUnit::Generators::HelperGenerator instead, which is defined since it’s a Rails generator. To try this, we simply want so as to add:
In the step above we simply wished so as to add a line to the generated helper, with out adding any further functionality. There’s a less complicated manner to do this, and it is by replacing the templates of already present generators, in that case Rails::Generators::HelperGenerator.
In Rails 3.Zero and above, generators don’t just look in the supply root for templates, in addition they seek for templates in different paths. And one in all them is lib/templates. Since we wish to customize Rails::Generators::HelperGenerator, we can do this by simply making a template copy inside lib/templates/rails/helper with the identify helper.rb. So let’s create that file with the following content material:
Scaffold templates in Rails frequently use ERB tags; these tags need to be escaped so that the generated output is legitimate ERB code.
For instance, the next escaped ERB tag could be wanted in the template (observe the additional %)…
…to generate the next output:
Now that you’ve seen how generators can be used inside an application, do you know they can be used to generate purposes too This sort of generator is referred as a “template”. That is a quick overview of the Templates API. For detailed documentation see the Rails Software Templates information.
Within the above template we specify that the application depends on the rspec-rails and cucumber-rails gem so these two can be added to the test group within the Gemfile. Then we pose a question to the person about whether or not they would like to put in Devise. If the consumer replies “y” or “yes” to this query, then the template will add Devise to batman slim fit t shirt 60 the Gemfile outside of any group after which runs the devise:set up generator. This template then takes the customers input and runs the devise generator, with the user’s answer from the final query being handed to this generator.
Templates don’t must be saved on the local system, the -m option also helps on-line templates:
While the ultimate part of this guide would not cover how to generate the most superior template identified to man, it would take you through the strategies available at your disposal so that you could develop it yourself. These similar methods are also out there for generators.
9 Including Command Line Arguments
Rails generators will be easily modified to just accept custom command line arguments. This performance comes from Thor:
Now our generator may be invoked as follows:
The command line arguments are accessed through the choices methodology contained in the generator class. e.g:
10 Generator strategies
Methods provided by Thor are not covered this information and might be batman slim fit t shirt 60 present in Thor’s documentation
– :group – The group within the Gemfile where this gem should go.
– :model – The version string of the gem you need to make use of. Can also be specified because the second argument to the tactic.
– :git – The URL to the git repository for this gem.
Injects a block of code into an outlined place in your file.
Replaces textual content inside a file.
Regular Expressions can be utilized to make this technique more precise. You may as well use append_file and prepend_file in the same manner to position code in the beginning and end of a file respectively.
– :env – Specify an environment for this configuration choice. If you wish to make use of this selection with the block syntax the beneficial syntax is as follows:
This technique also takes a block, anticipated to return a string:
– :env – Specifies the setting in which to run this rake task.
– :sudo – Whether or to not run this process utilizing sudo. Defaults to false.
Output the contents of a file in the template’s supply_path, normally a README.
Please contribute should you see any typos or factual errors. To get started, you’ll be able to read our documentation contributions section.
You may also find incomplete content, or stuff that is not updated. Please do add any missing documentation for master. Be sure that to test Edge Guides first to confirm if the problems are already fastened or not on the master branch. Check the Ruby on Rails Guides Guidelines for type and conventions.