AllFutures
Search…
AllFutures
Rails developers can use the all_futures gem to persist data across multiple requests. It leverages Redis to provide an ephemeral model that behaves just like an ActiveRecord model.
It's perfect for building faceted search interfaces, multi-step forms, real-time input validation and persisting the display state of UI elements.
Try a demo, here: πŸ‘‰ Beast Mode StimulusReflex πŸ‘ˆ

Why use AllFutures?

Many reactive UI concepts are a pain in the ass to implement using the classic Rails request/response pattern, which was created at a time before developers started using Ajax to update portions of a page. ActionController is designed to mutate state in response to form submissions, leading to abuse of the session object and awkward hacks to validate and persist models across multiple requests.
The combination of ActionCable and Turbo Drive creates a persistent Connection that blurs the line between session and request, forcing a new mental model that is poorly served by ActionDispatch and the conventions which drove Rails to success... in 2005.
Moving forward, new tooling is required to take full advantage of reactive possibilities.
AllFutures presents a flexible and lightweight mechanism to refine a model that persists its attributes across multiple updates, and even multiple servers.

Is AllFutures for you?

Do you ever find yourself:
  • building complex search interfaces
  • creating multi-stage data entry processes
  • frustrated by the limitations of classic form submission
  • wanting to save data even if the model is currently invalid
  • reinventing the wheel every time you need field validation
  • needing granular dirty checking and state management for every attribute
If you answered yes to any of the above... you are every Rails developer, and you're not crazy. This functionality has been a blind-spot in the framework for a long time.
Yes, AllFutures is for you.

Key features and advantages

  • A natural fit with StimulusReflex, Stimulus, Turbo Drive and mrujs​
  • No reliance on sessions, so it works across servers
  • Easy to learn, quick to implement
  • Supports model attributes with defaults, arrays and associations
  • Per-attribute dirty checking and state management with rollbacks
  • Model validations, errors and associations
  • No more temporary database tables that need to be purged later

How does AllFutures work?

AllFutures is the fusion of Active Entity and Kredis. It is similar to using ActiveModel::Model, except that it has full support for Attributes, including arrays and nested attributes. AllFutures classes behave like ActiveRecord model instances as much as possible.
1
class Example < AllFutures::Base
2
attribute :name, :string
3
validates :name, presence: true
4
end
5
​
6
example = Example.create
7
example.valid? # false
8
example.errors # @errors=[#<ActiveModel::Error attribute=name, type=blank, options={}>]
Copied!
Unlike an ActiveRecord model, AllFutures instances can persist their attributes even if the attributes are currently invalid. This design allows you to resolve any errors present, even if it takes several distinct operations to do so.
Once the state of your attributes is valid, you can pass the attributes from your AllFutures model right into the constructor of a real ActiveRecord model.
AllFutures v1 persisted the attributes every time you set the value of an attribute using bracket notation. This behavior has been removed. An explicit save operation is now required to persist changes.

Try it now

You can experiment with Beast Mode StimulusReflex, a live demonstration of using AllFutures to drill down into a tabular dataset, right now. πŸ‘ˆ
The Beast Mode codebase ​
​
​
​
is set up as a template repo which I recommend that you clone and experiment with.
The three key files are the CustomerFilter, the Reflex and the Model. You can read the tutorial post behind this example on my blog here.
Assuming you're running at least Ruby 2.7.3, Postgres and have Redis running on your system, you can just run bin/setup to install it, including migrations and the DB seed file.
Did they meet at the gym?
Last modified 6d ago