unnecessary files in grails templates - grails

I created some templates for the auto-generation code by grails install-templates, but some unnecessary files like form.gsp are still created. How can I remove them? I cannot find a config file to modify and create only my templates. Can anyone help me?

grails install-templates installs a copy of the Grails templates to src/templates for you to modify. If you don't care about form.gsp, then just ignore it. The templates aren't packaged into your WAR, they are simply used by the Grails generate-* commands. If you feel it's necessary to remove it, just don't run grails install-templates again. The command only needs to be done once.

Grails builds up the list of templates that it needs to generate from by combining the default set of templates in GRAILS_HOME with the ones installed in your app, with the one in your app taking precedence in the case of a name clash. So if you delete a template from your app's src/templates then Grails will simply use the built-in version of that template instead. I presume this is deliberate don't-repeat-yourself behaviour, to allow you to selectively override some of the templates but stick with the defaults for others without having to have a duplicate copy of the ones you haven't modified.
So I don't think you can prevent the form.gsp from being created when you generate-views, but if your modified scaffolding templates don't reference it then it does no harm being there and you can just ignore it. You could create an empty version in your src/templates
<%-- This file intentionally left blank --%>

When you execute the command "grails install-templates" it generates a folder src/templates/scaffolding. Inside this folder are the files that will be generated automatically when you execute o comando generete-all/view/controller.

Related

Is it possible to override the create_migration template in Rails 6?

I tried placing my custom file in multiple locations inside the lib folder with no success.
I found a PR from 2017 to reactivate this behavior but wasn't merged.
https://github.com/rails/rails/pull/13972
I had a look at
https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb#L17
and it looks like it's not implemented, but I don't know if just this file should change.
I´ve found resources on how to do it in older rails versions, but is there any way to override the Rails templates in Rails 6?
From this line of the documentation:
Since we want to customize Rails::Generators::HelperGenerator, we can do that by simply making a template copy inside lib/templates/rails/helper with the name helper.rb.
In the case for migrations, the template names should be specified as follows (here are the default templates for reference):
lib/templates/active_record/migration/create_table_migration.rb.tt
lib/templates/active_record/migration/migration.rb.tt

How can I change the default path to the `rails` script in my project, or remove that dependency?

In another question (Why does the Rails command force a help message for the new command?), I found that Rails needs the rails script to be in the script folder, in the root of my project, in order for it to be properly detected as an existing Rails projects, and allow me to use the various rails commands other then new.
I did this because I felt that the more popular moniker for including executable content in a repository to highlight available use cases is by using the name scripts. At least the pluralism in English should be appreciated!
Is there anyway to change which folder the main Rails executable looks for the project-included one?
I actually think it's a bit silly to include this rails executable in the project, and can be redundant. Maybe it's for customization, but I feel that could better be done in the configuration, environment, other .rb files. So also, could this just be removed somehow, and still have a functioning project through varied use of the main rails command.

How to replace a Template using Deface

I'm new to rails and I've set up a standard spree e commerce solution. I now want to customize the default templates and styles. According to the spree documentation [1], the best way to do so is using Deface. Unfortunately, I can't figure out the location of the existing views/layouts.
For example, while the document suggests:
For example, to override the main layout, create the file YOUR_SITE_OR_EXTENSION/app/views/spree/layouts/spree_application.html.erb
my app/views folder doesn't contain a folder called spree
Can anybody tell me, how to change the view templates?
Thank you.
[1] https://guides.spreecommerce.com/developer/view.html
You have to actually create that directory and every directory along the way until you reach the file you wish to override. If you run these commands from your root directory you should be good to go.
mkdir app/views/spree
mkdir app/views/spree/layouts
touch app/views/spree/layouts/application.html.erb
Then copy the content from here: https://github.com/spree/spree/blob/master/frontend/app/views/spree/layouts/spree_application.html.erb into the file you've just created.
If you restart your web server you should be able to make changes to the local application.html.erb file and see them locally.
Any time you want to override a template completely, you can find the template you need to override on Github: https://github.com/spree/spree and recreate the necessary files/directories. Spree will look for templates in on localhost before the gem, so as long as you override the right template in the right directory, you can customize any view you like.
Alternatively, you can try running the rails generate spree:frontend:copy_views command to pull all the views you need from gems, and anything that is missing can be added manually, in strict accordance with the gems folder hierarchy. For example, in gems is parsl in the following spree_backend/views/spree/admin/users/_lifetime_stats.html.erb. To enter the desired code, I have to make a folder on the following path - the name of the project/app/views/spree/admin/users/_lifetime_stats.html.the erb and all the changes I will make here will fall into the main view code. This is an alternative to Deface::override

Replace the default templates that Grails generates

So, i am getting started with Grails and i just started using install-templates! And it is a pretty cool stuff.
I learned that i should modify the installed templates before using generate-all, generate-views or generate-controllers.
But what i couldn't figure out (and also i couldn't find in google) is how to make the changes i have made to the template permanent. A want to make the changes i have made to be used for all the projects i create from now on.
In a nutshell, How can i change the default templates Grails generates?
You have to modify the scaffolding-X.X.X.zip at grails-X-X-X\plugins (the place you installed it) so the generate goes for all the projects.
Go to src/templates/scaffolding and edit the files.

using slimrb from command line

I am using slim for a project outside rails. I see that slimrb gives options to compile the templates to html from command line.
1) However is there a way to compile a complete view directory to html.
2) Also is it possible to watch for changes and, continuously update the generated html, like what compass does for .scss files.
There are actually two complete solutions to this problem! Both are quite easy to implement:
The Slim Guard plugin
Every Ruby developer should know about Guard. It's the easiest way to automatically transpile files.
There are actually two Slim guards on GitHub, and unfortunately the main one is older and not as good. I would recommend using indrekj/guard-sim (the one I linked to).
The RubyMine IDE by JetBrains
Truly exceptional software, RubyMine is heaven for Rubyists. It has built-in file watchers that you can configure on a per-project basis for transpiling all your typical assets. It's simple to set up a watcher for the Slim files using the slimrb program, and you're provided with a slew of customization options.
In RubyMine, you can import/export file watcher configurations; I host a few here, including one for Slim.
1) To compile all files in a directory from slim to html cd into it and type slimrb * and then hit tab to expand * into all filenames in that directory. Since it is a view directory there should be no other files then *.slim
2) Slim itself doesn't imlement this, but it shouldn't be too hard to write a script which listens for filesystem events and triggers based on which file was changed the slimrb executable.

Resources