Introducing Rails Vault: simple to add settings to any Active Record model
When you build advanced UI’s for your Saas there will be a point you need to store certain preferences or appearance settings for a user or workspace, like:
- synced light- and dark-theme;
- expand/collapsed state for navigation items;
- time zone.
And so on. I’ve come across this use cases in every app I built. For a long time I used a set up like described in this article. But ever since I published it and even including a template to get the required code in your app, I got questions if it wasn’t better as a gem.
So after about 10 months, here is: Rails Vault.
Rails Vault is a simple gem to add settings and preferences to any Active Record model. The goal is to keep it simple and light-weight. Let’s check out how it works.
Installation is just three lines:
bundle add rails_vault
rails generate rails_vault:install
rails db:migrate
Then you can create a vault for any Active Record model. The convention is that the vaults are stored in the model’s namespace. So when your run:
rails generate rails_vault:add User::Preferences \
time_zone:string \
datetime_format:string \
hotkeys_disabled:boolean
…it will create the following class:
# app/models/user/preferences.rb
class User::Preferences < RailsVault::Base
vault_attribute :time_zone, :string
vault_attribute :datetime_format, :string
vault_attribute :hotkeys_disabled, :boolean
end
Preferences are created with User.first.create_preferences.
Default values can be set as follows:
# app/models/user/preferences.rb
class User::Preferences < RailsVault::Base
vault_attribute :time_zone, :string, default: "UTC"
vault_attribute :datetime_format, :string, default: "dd-mm-yyyy"
vault_attribute :hotkeys_disabled, :boolean, default: false
end
Reading and updating a value is simple too:
user.preferences.time_zone # => "UTC"
user.preferences.hotkeys_disabled? # => false
user.preferences.update time_zone: "Amsterdam", hotkeys_disabled: true
user.preferences.time_zone # => "Amsterdam"
user.preferences.hotkeys_disabled? # => true
Have ideas or suggestions? Feel free to create a PR. There are also some good first issues that you could pick up.
Want to read me more?
-
Simple Preferences to Any Resource for Rails
Explore a straightforward guide to integrating user preferences in SaaS products, featuring methods like store_attribute for Rails. Learn how to customize your application's user interface with adaptable settings and enhance usability with this in-depth tutorial. -
Store UI State in localStorage with Stimulus
Easily store user preferences in the browser's localStorage using Stimulus. -
Create an Email Drip Campaign Using Rails Vault
Email drip campaigns are a powerful marketing tool for web apps. In this article I give the lowdown how to create one yourself using the Rails Vault gem.
Over to you…
What did you like about this article? Learned something knew? Found something is missing or even broken? 🫣 Let me (and others) know!
Comments are powered by Chirp Form
{{comment}}