-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add article on customizing rails console setup
- Loading branch information
1 parent
d9e455e
commit 2b8bbf0
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |