Tidy up your Rails code with `inquiry`

This article was originally published on Rails Designer—The first professionally-designed UI components library for Rails.

This short article is less product-engineering/UI focused, but does help you as a developer write tidier code. For Rails Desi…


This content originally appeared on DEV Community and was authored by Rails Designer

This article was originally published on Rails Designer—The first professionally-designed UI components library for Rails.

This short article is less product-engineering/UI focused, but does help you as a developer write tidier code. For Rails Designer v1 I refactored some code in the components. One the of the improvements I made was using inquiry more.

I mentioned this ActiveSupport extension in this this article about lesser known Rails helpers too, but I thought it worthy of its own article.

So previously a component might looked like this:

class DropdownComponent
  def initialize(theme: "light")
    @theme = theme
  end

  erb_template <<~ERB
    <%= tag.div "content", class: class_names("block, {"bg-white": light_theme?, "bg-black": dark_theme?}) %>
  ERB

  private

   def light_theme? = @theme == "light"
   def dark_theme? = @theme == "dark"
end

I personally like a (internal) API like this; it helps with reading the code instead of doing mental gymnastics needed to parse the variable (@theme) and its value ("light" or "dark").

So I was pretty happy with this code, but an annoyance arose when more themes would be added. So instead of writing separate methods for each option, I reached for inquiry.

The above component was rewritten like this:

class DropdownComponent
  def initialize(theme: "light")
    @theme = theme.inquiry
  end

  erb_template <<~ERB
    <%= tag.div "content", class: class_names("block, {"bg-white": @theme.light?, "bg-black": @theme.dark?}) %>
  ERB
end

One could argue @theme.light? reads worse than light_theme?, but I feel like its an improvement overall:

  • less lines of code in the class;
  • using a first-party helper from Rails/ActiveSupport;
  • no mental overhead on what to name the extra method(s).

As I pointed out in the article above, inquiry can also be used on arrays: ["pending", "active"].inquiry.pending? # => true.

This is one of those helpers that once you know about it, you see use for it everywhere.


This content originally appeared on DEV Community and was authored by Rails Designer


Print Share Comment Cite Upload Translate Updates
APA

Rails Designer | Sciencx (2024-08-27T13:00:00+00:00) Tidy up your Rails code with `inquiry`. Retrieved from https://www.scien.cx/2024/08/27/tidy-up-your-rails-code-with-inquiry/

MLA
" » Tidy up your Rails code with `inquiry`." Rails Designer | Sciencx - Tuesday August 27, 2024, https://www.scien.cx/2024/08/27/tidy-up-your-rails-code-with-inquiry/
HARVARD
Rails Designer | Sciencx Tuesday August 27, 2024 » Tidy up your Rails code with `inquiry`., viewed ,<https://www.scien.cx/2024/08/27/tidy-up-your-rails-code-with-inquiry/>
VANCOUVER
Rails Designer | Sciencx - » Tidy up your Rails code with `inquiry`. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/27/tidy-up-your-rails-code-with-inquiry/
CHICAGO
" » Tidy up your Rails code with `inquiry`." Rails Designer | Sciencx - Accessed . https://www.scien.cx/2024/08/27/tidy-up-your-rails-code-with-inquiry/
IEEE
" » Tidy up your Rails code with `inquiry`." Rails Designer | Sciencx [Online]. Available: https://www.scien.cx/2024/08/27/tidy-up-your-rails-code-with-inquiry/. [Accessed: ]
rf:citation
» Tidy up your Rails code with `inquiry` | Rails Designer | Sciencx | https://www.scien.cx/2024/08/27/tidy-up-your-rails-code-with-inquiry/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.