Rails Tricks
Archive
18 Apr
Array Tricks - Rails Tricks Issue 3
Hi, this is your weekly Rails Trick!
This time, I am bringing a few Active Support extensions on Array.
The first one is the to_sentence conversion method. By default, it converts the array to a comma-separated list of words, with the last element joined by “and”. For example:
You can specify the words_connector and the last_word_connector if you want to use something other than the comma and “and”:
The next extension I want to mention is the ArrayInquirer. By calling inquiry on an array, you can convert it to an ActiveSupport::ArrayInquirer object. This will give you predicate methods on the string-like contents of the array. For example:
As a side note, Rails uses this internally for the variant predicates in Action Dispatch.Active Support also adds a few extra access methods to Array. There is from, which takes a position and returns the tail of the array from that position. There is to, which does the opposite and returns the beginning of the array up until the given position.
There is also the including method, which returns a new array including the elements passed to it. and you can achieve the opposite with excluding, which returns a new array excluding the elements passed to the method. excluding is actually implemented on Enumerable in Ruby, but Active Support reimplements it on Array to make it more performant. It is also worth mentioning that excluding is aliased as without.
There are also methods to access, second, third, fourth and the fifth elements of an array. And there is fourty_two, which has an interesting history. Back in the day, we had these helpers up till 10 (if my memory serves me well), and some people were complaining about them, saying they are bloating Active Support. As a response to the criticism, fourty_two was added as a kind of joke to access “the reddit”, implicating that it holds the Ultimate Answer to Life, the Universe, and Everything .
That’s it for this week!
11 Apr
Rails Tricks Issue 2: Console Tricks
Hi, this is Greg with the second issue of Rails Tricks!
Thanks to everyone for subscribing!
This week, I want to share a few Rails console tricks I use regularly. The first one is the _ command, which always holds the return value of the last evaluated command in IRB, which is the default for Rails` console. For instance, if you run an Active Record query to find a record, then you realize that you want to keep the result around, you can easily assign it to a variable:
Another thing I regularly do is, to look up the definition of methods or the location of their definition. This comes in handy when you are debugging an issue and can be helpful to see what a method actually does or where it is defined.
Because maybe a gem overrides it, and that causes unexpected behavior. In a Rails console, to see the definition, you can use Object.method(:name).source:
And if you want to verify that all is defined in Active Record and nothing overrides it in your application, you can check the location of the definition:
The source method is actually added by a gem called methodsource, which was part of Rails between 7.1, but it will be removed with that release, so if you still want to be able to see the definition of methods in your console, you will need to add that gem to your Gemfile.The next thing I want to share is the --sandbox option. If you pass this option when you start your console, every database modification will be rolled back on exit.
This can be useful when you are debugging something in production, and you want to make sure you are not messing up something accidentally.
And the final thing is to have some fun! Type IRB.send(:easter_egg, :dancing) or IRB.send(:easter_egg, :logo) in your IRB session and enjoy the result!
You can exit by pressing CTRL+C.
P.S.: I just came across this blog post recently from Jemma about the measure command in IRB: https://jemma.dev/blog/irb-measure.
This week I have a special guest trick:
Hello there, I'm Adrian, the author of Avo, and I'm excited to share a neat trick with you today. In this tutorial, I'll be demonstrating how to attach hooks and business logic to Avo's controllers using a Current model.
You may already be familiar with Rails' Current model, which is used to set the current user, multi-tenancy accounts, or other pieces of information. Typically, before_action is used in your ApplicationController to demonstrate how Current works. However, if you attempt to apply the same changes to your app the action will not work in Avo's context. This is because Avo has its own AplicationController.
So how do we apply this behavior inside Avo? Let me walk you through it.
Firstly, we configure the Current model and create the concern that holds the business logic.
Next, we add this concern to Avo's ApplicationController using Rails' to_prepare hook:
With this setup, our Authentication concern will be applied to the Avo::ApplicationController, and it will be executed within Avo's context.This technique can also be applied to other classes, such as including all helpers inside Avo's field declarations:
By following these steps, you can now use your own extensions in your Avo resources. I hope this short tutorial helps you write better Avo resources and ship code faster.Adrian ✌
That’s it for this week, see you next time!
Greg
04 Apr
Rails Tricks Issue 1
Hi, this is Greg, bringing you the first edition of the Rails Tricks newsletter.
Thank you for subscribing! I hope you will learn a few things over time.
Let me explain what I consider a "Rails Trick". For me, these are the not so well known features of Rails/Ruby.
Let's see our first one.
I am bringing you a view layer trick this week. We will be looking into conditionally rendering links. Rails has a link_to helper to generate links, but there are not such well-known similar helpers. For instance, there is link_to_if, to conditionally render a link:
It takes a condition for the first param, the name for the link as the second, and the options to construct the URL as the third. There is also an optional fourth argument to set HTML options for the link.
This helper has a counterpart, link_to_unless, which is the same, except it creates the link if the condition is false.
And there is link_to_unless_current. It takes the name for the first argument, the options to construct the URL as the second, and the optional HTML options as the third. But it only renders the link if the current request URI is not the same as the URL of the link. It can be handy when you don't want to have a link to the current page in your navigation bar.
As I mentioned, I will also try to share a non-Rails trick every week, and the first one will be a vim trick.
Vim has many lesser-known features, for instance, it has a built-in spell-checker. So if you are writing content in vim as I do, it can help to catch spelling mistakes. To enable it, enter vim set spell into the command bar.
That's it for now, see you next week!