An open API service indexing awesome lists of open source software.

https://github.com/railsjazz/lazy_value

Rails Lazy value loader
https://github.com/railsjazz/lazy_value

lazy-evaluation ruby-on-rails

Last synced: 2 months ago
JSON representation

Rails Lazy value loader

Awesome Lists containing this project

README

        

# Lazy Value

[![RailsJazz](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/my_other.svg?raw=true)](https://www.railsjazz.com)

![RailsLiveReload](docs/lazy_value.gif)

Introducing a versatile value loader for your Ruby on Rails views, designed to optimize parallel loading of information on a page. This solution is perfect for scenarios where you need to display multiple stats, each taking 1-2 seconds to compute, thus causing a delay in page loading time.

Functioning similarly to lazy Turbo frames, this value loader has no reliance on Turbo and eliminates the need for creating new actions to load data. Make your page loading experience smoother and more efficient with this practical solution.

## Main Benefits

1. Just 1 line of code
2. No reliance on JavaScript dependencies, such as Turbo
3. Easy to implement with a "Plug & Play" approach
4. Compatible with Turbo Frames

## Essential Information & Constraints

1. Currently compatible with ERB only
2. Each lazy value should be atomic and not rely on any external value or variable initialized in the controller/view
3. Limit usage to one lazy_value_tag per line
4. Carefully follow the provided format when adding this block; refer to the README for guidance

## Usage

It's strongly suggested to configure initializer (can be generated with `rails g lazy_value initializer`). You can put your credentials/secrets/env variable. It's needed to use the same encryption key between deploys or server instances.

```ruby
LazyValue.setup do |config|
config.salt = ENV["LAZY_VALUE_SALT"].presence || SecureRandom.random_bytes(ActiveSupport::MessageEncryptor.key_len)
config.key = ENV["LAZY_VALUE_KEY"].presence || SecureRandom.hex(32)
end
```

And now you can use `lazy_value_tag` helper in your views:

```erb



Number 2


<%= lazy_value_tag do %>

<%= Project.pending.count %>%


<% end %>




Number 3



<%= lazy_value_tag { User.active.count } %>




```

It also works with partials:

```erb


<%= lazy_value_tag do %>
Random 5 users
<%= render "/home/users", users: User.limit(5).order("random()") %>
<% end %>

```

And even compatible with Turbo.

```erb

This content is from lazy loaded turbo frame.

<%= lazy_value_tag do %>

<%= rand(1000) %>


<% end %>

<%= lazy_value_tag do %>
<%= "I'm lazy loaded from the turbo frame" %>
<% end %>

```

## Installation

Add this line to your application's Gemfile:

```ruby
gem "lazy_value"
```

And then execute:
```bash
$ bundle
```

And that is it. Start using it.

## How it works

1. We call `lazy_value_tag` in the view
2. We save location from where it was called (with `caller_locations.first`), file + line number.
3. We encrypt this info using `ActiveSupport::MessageEncryptor`, creating span with spinner, and JS snippet that will call `/lazy_value/show?payload=`
4. In the controller we decrypt our data and reading ERB file and detecting our snippet
5. Depending on the block syntax we evaluate ERB or Ruby.
6. We return from the controller HTML that will replace snippen on the page.

## Testing

`bin/rails test:system`.

## TODO

- websockets options vs http
- pass variables
- change to modern JS?
- use POST? (if payload might be too big)

## Contributing

You are welcome to contribute.

[](https://opensource-heroes.com/r/railsjazz/lazy_value)

## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

[](https://www.railsjazz.com/?utm_source=github&utm_medium=bottom&utm_campaign=lazy_value)