Kredis (Keyed Redis) is a recent addition to the Rails developer's toolkit. It strives to simplify storing and accessing structured data on Redis.
In this first part of a two-part series, we'll start by going into how Kredis works. We'll then run through an example use case for storing ephemeral UI state using a bespoke Redis key.
Let's get started!
An Introduction to Kredis for Rails
Kredis is a Railtie that provides convenient wrappers to streamline its use in three ways:
- Ruby-esque API: For example, collection types like
Kredis.list
orKredis.set
emulate native Ruby types (and their respective API) as much as possible. - Typings: Especially handy for collections, Kredis can handle type casting the elements from/to standard data types (e.g.,
datetime
,json
). - ActiveRecord DSL: Probably the library's biggest asset, it allows you to easily connect any Redis data structure with a specific model instance.
Here's an example from the README:
Kredis' major benefit is the ease it provides to store ephemeral information associated with a certain record, but independent of the session. Typically, when you need to persist data in Rails, you have a few options — of which the two most common ones are:
- ActiveRecord: In most cases, this requires adding a column or otherwise patching your data model. A migration is needed, plus the optional backfilling of old records.
- Session: The default key/value store of every Rails app and requires no or little setup. The downside is that data stored in it doesn't survive a login/logout cycle.
Kredis brings a third option to the table. Little setup is required, apart from invoking the DSL in the model. But unless your Redis instance goes down, your data is stored across sessions, and even devices. So a good use case for Kredis is uncritical information that you want to share across device borders, e.g., in a web app and a companion mobile app.
Case Study: Persist and Restore a Collapsed/Expanded UI State Using Kredis
A typical instance of a good use case for Kredis is when persisting UI state, such as:
- Sidebar open/closed state
- Tree view open/closed state
- Accordion collapsed/expanded state
- Custom dashboard layout
- How many lines of a data table to display
Exemplarily, we will take a look at how to manage the collapsed/expanded state of a <details>
element.
Let's start out with a fresh Rails app, add kredis
to the bundle, and run its installer:
Note: This will create a Redis configuration file in config/redis/shared.yml
.
For the rest of this article, I will assume that you have a local running Redis instance. On macOS with Homebrew, this is as easy as running:
Please consult the official "Getting Started" guide for information on how to install Redis on your operating system.
User Authentication
We are going to use a User
model as the entity to store UI state information. To avoid bikeshedding here, let's just use what Devise provides out of the box:
We then create an example user in the Rails console:
Our Example App: An Online Store
To illustrate how Kredis can help persist the state of a complex tree structure, let's pretend we are running an online department store. To this end, we will scaffold Department
and Product
models. We include a self join from department to department, to create a two-level nested structure:
We have to permit null parents, of course, to allow for our tree structure roots:
Our Department
and Product
models are defined as such:
Finally, we use faker to generate some seed data:
Scaffolding a Storefront
We'll create a very simple HomeController
that will act as our shop's storefront.
We perform a self join on the departments' children to retrieve only those which actually have subdepartments (or, in other words, are our tree's roots):
In the index view, we set up a nested tree view using two levels of <details>
elements for our departments:
Right now we have a tree view of departments with intentionally silly product names that we can explore by opening and closing:
We'd like to persist the disclosure state of the individual categories, which we will tend to next.
Persisting UI State of Categories in Kredis
Here is what we are going to do, step by step:
-
Add a
kredis_set
calledopen_department_ids
to theUser
model. The reason we are using a set here is that it doesn't allow duplicates, so we can safely add and remove our departments. -
Create a
UIStateController
that will receive the following params:- the
department_id
- the
open
state of that department
It will then add or remove this department to the
kredis_set
for the currently logged-in user. - the
-
Create a Stimulus controller which will listen for the toggle event on the details element and send over the respective payload.
Let's get into it!
Adding said Kredis data structure to the User
model is as easy as calling kredis_set
and passing an identifier:
Next, we generate a UIStateController
to receive the UI state updates. Note that we have to configure the generated route to be a patch
endpoint:
Our first encounter with Kredis' API is in the controller. We can see that it tries to conform to Ruby developers' expectations as closely as possible, so you can add to the set using <<
, and delete using remove
.
What's happening here is that we toggle the presence of a specific department_id
in the set based on the open
param being handed over from the client. To complete the picture, we must write some client-side code to transmit these UI state changes.
We are going to use @rails/request.js
to perform the actions, so we have to pin it:
In a new Stimulus controller that we'll attach to a specific <details>
element, we append the department ID and its open
state to a FormData
object, and submit it:
We edit our view code as proposed, and listen for the toggle
event of each <details>
element to trigger the UI state updates:
Rehydrate the DOM Manually
The only component missing to go full circle is rehydrating our DOM to the desired state once the user refreshes the page. We do this manually by adding the open
attribute to the <details>
node (if its department ID is present in the Kredis set):
Finally, here's the result. Note that the open/closed state of individual tree nodes is preserved over 2 levels:
Up Next: A Generalized User-local Container for UI State
In the first part of this two-part series, we introduced Kredis and explored how to persist and restore a collapsed/expanded UI state with Kredis.
We used the example of an online department store to highlight how Kredis can persist a complex tree structure's state, before finally manually rehydrating the DOM.
However, this does mean that we have to invent a lot of Kredis keys. Next time, we'll dive into how we can address this with a generalized user-local container for UI state.
Until then, happy coding!
P.S. If you'd like to read Ruby Magic posts as soon as they get off the press, subscribe to our Ruby Magic newsletter and never miss a single post!