Simplify Your Rails Schema Navigation with Interactive FZF

Introduction: Navigating a large Rails schema can be a daunting task, especially when numerous tables are defined. In this article, we explore a simple yet effective approach to streamline this process using the FZF command-line fuzzy finder. By leveraging FZF along with a custom Bash script, we can effortlessly navigate our Rails schema, easing the location and inspection of specific tables.

Step-by-Step Guide:

1. Creating the Script:

First, create a Bash script named preview_table.sh. This script will extract and display the content of the selected table from our Rails schema file.

#!/bin/bash

# Extracts the name of the selected table
table_name="$1"

# Checks if a table name is provided
if [[ -z "$table_name" ]]; then
   echo "No table name provided; exiting."
   exit 1
fi

# Displays the content of the selected table
awk -v table="$table_name" '$0 ~ "create_table \"" table "\""{flag=1} flag {print} /end/ {flag=0}' db/schema.rb

2. Making the Script Executable:

Before using the script, it needs to be made executable. Open your terminal and navigate to the directory containing the script. Then, run the following command:

chmod +x preview_table.sh

3. Configuring the Shell Function:

Next, define a shell function in your shell configuration file to integrate FZF with our custom script. This function, named sf, allows for interactive searching and selecting a table from our Rails schema.

# Defines the search function
function sf() {
  local table_name
  table_name=$(grep -oP 'create_table "\K[^"]+' db/schema.rb | fzf --preview="~/bin/./preview_table.sh {}" --preview-window=right:70%:wrap --prompt='Select Table: ')
  echo "Selected: $table_name"
  if [[ -n "$table_name" ]]; then
    awk -v tn="$table_name" '$0 ~ "create_table \""tn"\""{p=1} p==1; /end/ {p=0}' db/schema.rb
  fi
}

Usage:

To use this functionality, simply open your terminal within your Rails project directory and execute the sf function. You’ll be presented with a searchable interface where you can type the name of the table you’re interested in. Once selected, the script will display the content of the chosen table in the preview window.

Conclusion:

With the combination of FZF and our custom Bash script, navigating your Rails schema has never been easier. This streamlined approach allows you to quickly locate and inspect specific tables, enhancing your development workflow and productivity.

Give it a try in your Rails project and experience the convenience firsthand!

5 Likes

Would be interesting to share this as a program via some package manager. Maybe a bit overkill, but hey, why not hahaha

Very useful! I’ve been just using whatever editor’s search functionality, but this is pretty cool.

1 Like

Thank you for the encouragement and the great suggestion! It inspired me to package the tool as a gem, and I’m excited to share that it’s now available on RubyGems. You can find it at railsfinder gem. I hope it makes searching through Rails schema files even easier. Feedback is always welcome! :slightly_smiling_face:

2 Likes

Sent a star your way! Will try it out!

1 Like

@jpstudioweb on a macOS system, whatd be the best location to be save the script?

I’m currently without macOS installed to test, but on a macOS system, you can store the script anywhere you prefer. To simplify, you can adjust the path to the script in this line within the function to reflect the path where the script is located on your system:

table_name=$(grep -oP 'create_table "\K[^"]+' db/schema.rb | fzf --preview="~/your_path_here/preview_table.sh {}" --preview-window=right:70%:wrap --prompt='Select Table: ')

Replace ~/your_path_here/preview_table.sh with the actual path to your script. This way, you don’t need to modify your PATH environment variable, and you can still access the script functionality directly.

Additionally, if you’d like an even easier way to navigate your Rails schema, I’ve created a gem that offers similar functionality to the script. You can find it at railsfinder gem. It’s a handy command-line tool that allows you to search and display table definitions right from your terminal. Give it a try and let me know what you think!

1 Like