Skip to content

ezscaff

Andreas Dausenau edited this page Jul 14, 2023 · 9 revisions

ezscaff - generator

This generator provides a scaffold like the default rails scaffold generator do, including everything you need like the model, the controller, the views etc. The difference to the default rails scaffold generator is that the generated files are compatible with ez-on-rails views. You will get a scaffold that uses the permission system out of the box, uses the ez-on-rails partials to render its views and get a default render info helper that provides the partials with the necessary information to render all available views.

This generator also adds a default restriction with super admin access to manage the data on backend level. Additionaly it provides the i18n files and specs to test the permission and working models and controllers.

In short, if you use this generator you mostly do not have to do anything to get your model to work. The basics should work out of the box, including the administration backend view to manage your data. However, especially on using relations some additional work is needed. We recommend just to test your scaffold with the provided specs to see what you may need to adjust.

rails generate ez_on_rails:ezscaff Article title:string content:string released:boolean --sharable true

After you did this, you will get a helper file including the render info method that is used by the ez-on-rails partials and controllers to do their work. Consider reading the render info basics to get the info what you can configure here out of the box. You should also read the permission concept to see what you have to do to restrict the access to your scaffold like you want.

Also note that this generator creates a javascript stimulus controller in your app/javascript/controllers directory. This controller contains onChange listeners and field targets for each defined attribute of your scaffold. You can use them to implement custom javascript code.

If anything you need is not provided, feel free to open an issue or contact us and maybe if your idea is good, we will try to add it. You can also just create a pull request.

Also note that you are not limited to use ez-on-rails. The structure that is generated here provides you with the ability to use the default rails controllers, helpers, views, models etc.. Hence if anything does not work, you can also just work in rails like you are familar with. This is because ez-on-rails only uses the rails awesome engine structure that is fully customizable but convention over configuration. Everything you do in your application will overwrite the behavior of the engine.

Options

The first parameters are the same like the default rails scaffold generator expects. Its the name of the resource, followed by the attributes and its types. Additionally you can type the following argument:

  • --sharable true
    • If this option is set to true your model will get the necessary relations to get the ability to be shared to other groups or users using the permission system
    • If this option is set to true the render info method located in the helper will be extended by the necessary attributes to manage the shared access in the administration views
    • The argument is optional and considered to be false as default

The following steps are executed by this generator

1. Call the default scaffold generator of rails

  • This ensures that all files used by the default scaffold are provided, even if they are not changed by this generator

2. Copy the helper, views and the controller

  • This overwrites the previously generated files to be compatible with ez-on-rails
  • However, even if the views use the partials or the controller inherits from the ez-on-rails classes, you also can use the generated files as usual helpers / views / controllers

3. Generate the migration

  • This deletes the previously generated migration and creates a new one having some additional fields needed by the permission system

4. Generate i18n files

  • The i18n files are located in config/locales/<your_model_name>/
  • They are already filled with the attributes and model names, of course using the rails standard
    • Hence, you are able to use fancy rails things like YourModel.model_name.human or YourModel.human_attribute_name(:attribute)

5. Remove unused files

  • The *.html.erb files generated by the defalt scaffold generator are removed
    • This is because ez-on-rails provides views in slim
  • Unnecessary class path files in the model are removed, if your model is placed in a namespace
  • The scaffold.scss file is removed, since it was generated by the default scaffold generator

6. Generate routes

  • The previously generated routes are replaced with additional routes to provide e.g. actions to search or destroy multiple records at once

7. Add permissions to seeds

  • The permission to permit the access to the administration views for your new resource are written to the db/seeds.rb file

8. Generate javascript

  • Creates a stimulus controller to the javascript/controllers folder for the scaffold. This can be used to include custom javascript code.

9. Generate specs and factory

  • A model spec is generated to test that your model will be created having valid attributes
  • A request spec is generated that tests the access restrictions to the management views and tests various tasks like e.g. creating and updating your resource
  • A factory is generated to be able to write ez tests

10. Remove test directory

  • Since ez-on-rails uses rspec and is expected to be only used on fresh installations, the test directory for the standard rails minitests will be removed