From 2b8bbf023d6d013633b761d15c42dfa880a98a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radan=20Skori=C4=87?= Date: Tue, 12 Mar 2024 09:15:14 +0000 Subject: [PATCH] Add article on customizing rails console setup --- ...024-03-12-customize-rails-console.markdown | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 _posts/2024-03-12-customize-rails-console.markdown diff --git a/_posts/2024-03-12-customize-rails-console.markdown b/_posts/2024-03-12-customize-rails-console.markdown new file mode 100644 index 0000000..6741a43 --- /dev/null +++ b/_posts/2024-03-12-customize-rails-console.markdown @@ -0,0 +1,44 @@ +--- +layout: post +title: "How to customize Rails console setup without modifying the project" +date: 2024-03-12 +categories: articles +tags: rails console setup +--- + +When working on projects with other developers you might (like me) find yourself wanting to customize the project console in a way that's not useful as a default but it is useful for you. And ideally, I don't want to modify the project, these are just my configurations. I deserve some privacy. + +My friend [Nikola Topalović](https://github.com/topalovic){:target="\_blank"} shared with me a setup that makes this possible in an elegant way. + +## Set it all up in an initializer + +It starts with a custom initializer where you make the changes you want, for example, it might look like this: + +```ruby +Rails.application.console do + puts "Loading custom initializer #{__FILE__}" + + Rails.logger.level = 0 + puts 'Enabled SQL logs' + + ApplicationController.allow_forgery_protection = false + puts "Disable CSRF to enable app.post calls" + + puts "DJOTD: #{DAD_JOKES.sample}" +end +``` +{: file='config/initializers/me.rb'} + + +The message printing the location of the initializer file is there for that inevitable moment you will forget that you set this up and will be wondering why your console is different. + +## Make git ignore the initializer + +The least disruptive way is to navigate to `.git/info/exclude` + and add a line excluding your custom initializer: +``` +config/initializers/me.rb +``` +{: file='.git/info/exclude'} + +And that's it, git will ignore it, you have your own console modifications and you didn't have to modify anything in the project.