Is it possible to override the create_migration template in Rails 6? - ruby-on-rails

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

Related

Overriding the default views in RailsAdmin

I am using Ruby on Rails 4.2.1 with RailsAdmin. The gem works excellent, but I have the requirement that the layout of the admin panel and the forms must look different than what is generated by default. For example, the navigation should be horizontal top, the forms should order the fields in two columns.
So far I haven't find a way to copy the views locally and modify them (like in Devise for example). I have tried to replicate the views manually in the respective path under my views folder by copying the original views, but I got problems with the helper methods that are part of RailsAdmin not being accessible from my views.
I dug deeper and found that there is a task copy_views, it was referred to in questions for the older versions of the gem, but if I try to use it now rake rails_admin:copy_views, it is not available anymore.
Am I doing something wrong, or is there another way to do this?
You can create folders in your app
app/views/rails_admin/main for https://github.com/sferik/rails_admin/tree/master/app/views/rails_admin/main
app/views/layouts/rails_admin/ for
https://github.com/sferik/rails_admin/tree/master/app/views/layouts/rails_admin
Put modified files there. It can get a little messy and you will need to update the files if the gem changes.

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.

unnecessary files in grails templates

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.

Change Rails Scaffold Naming Scheme

I'm a happy user of RoR but have one complaint. When I do script/generate scaffold it automatically generates all my files and places them in their proper folders. However, all the different scaffolds I've created name their view files the same.
I have a bunch of index.html.erb view files and when I have them open in my text editor, it's almost impossible to tell what controller they're related to.
I'd like to change the default naming scheme of the scaffold command to name the individual files to contain their view folder name. So, instead of index.html.erb, use index.home.html.
Is there a way to do this, or am I stuck? What solutions to the multiple files with the same name problem have you Rails developers discovered?
Thanks!
You're going to be fighting the Rails' conventions by going down that path and Rails works best when you work with it rather than against it. A core part of the philosophy of Rails is that there are a set of conventions that once learned make it easy to find your way around any Rails application.
Instead of trying to redefine how Rails works, I would recommend taking advantage of the features offered by your text editor or IDE for quickly navigating to the correct view template. For example, the Rails bundle within TextMate on the Mac lets you quickly open the view file associated with a particular model and there's a plugin for Vim that provides an equivalent feature.

Resources