lazy_value 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +20 -0
- data/README.md +112 -0
- data/Rakefile +7 -0
- data/app/controllers/lazy_value/application_controller.rb +39 -0
- data/app/helpers/lazy_value/application_helper.rb +59 -0
- data/config/routes.rb +7 -0
- data/lib/lazy_value/engine.rb +11 -0
- data/lib/lazy_value/railtie.rb +4 -0
- data/lib/lazy_value/routes.rb +7 -0
- data/lib/lazy_value/version.rb +3 -0
- data/lib/lazy_value.rb +15 -0
- data/lib/tasks/lazy_value_tasks.rake +4 -0
- metadata +14 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39c7e1e0a2dc4c49d36a057f49ffb054a527ae09880e8967067e4ce5b864e2b5
|
4
|
+
data.tar.gz: ad44eab22f9afcd3198a957cc41f0f810d43066b7e1b3b6fcbef3da1981d837d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32e9bdc1463df76ecd671811f876cfacd1d3961275f8506a6ca9b0fdfdd68a0fd2774b3d078a951d67ff8050216587712e2843955761e06f7eed31a7f4d51189
|
7
|
+
data.tar.gz: 5594961af3dbfb7b83f3266024f2c92e031ccd3c84973f27e015e1ce805febec3970e79ea09b7de1ef59964bded77791cb2fac7581a1af3b6b08ff4c04f6397f
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2023 Igor Kasyanchuk
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# Lazy Value
|
2
|
+
|
3
|
+
[![RailsJazz](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/my_other.svg?raw=true)](https://www.railsjazz.com)
|
4
|
+
[![Listed on OpenSource-Heroes.com](https://opensource-heroes.com/badge-v1.svg)](https://opensource-heroes.com/o/railsjazz)
|
5
|
+
|
6
|
+
![RailsLiveReload](docs/lazy_value.gif)
|
7
|
+
|
8
|
+
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.
|
9
|
+
|
10
|
+
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.
|
11
|
+
|
12
|
+
## Main Benefits
|
13
|
+
|
14
|
+
1. Just 1 line of code
|
15
|
+
2. No reliance on JavaScript dependencies, such as Turbo
|
16
|
+
3. Easy to implement with a "Plug & Play" approach
|
17
|
+
4. Compatible with Turbo Frames
|
18
|
+
|
19
|
+
## Essential Information & Constraints
|
20
|
+
|
21
|
+
1. Currently compatible with ERB only
|
22
|
+
2. Each lazy value should be atomic and not rely on any external value or variable initialized in the controller/view
|
23
|
+
3. Limit usage to one lazy_value_tag per line
|
24
|
+
4. Carefully follow the provided format when adding this block; refer to the README for guidance
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
```erb
|
29
|
+
<div class="column is-one-quarter">
|
30
|
+
<div class="box has-text-centered">
|
31
|
+
<h2 class="subtitle">Number 2</h2>
|
32
|
+
<%= lazy_value_tag do %>
|
33
|
+
<p class="title"><%= Project.pending.count %>%</p>
|
34
|
+
<% end %>
|
35
|
+
</div>
|
36
|
+
</div>
|
37
|
+
<div class="column is-one-quarter">
|
38
|
+
<div class="box has-text-centered">
|
39
|
+
<h2 class="subtitle">Number 3</h2>
|
40
|
+
<p class="title">
|
41
|
+
<%= lazy_value_tag { User.active.count } %>
|
42
|
+
</p>
|
43
|
+
</div>
|
44
|
+
</div>
|
45
|
+
```
|
46
|
+
|
47
|
+
It also works with partials:
|
48
|
+
|
49
|
+
```erb
|
50
|
+
<div class="box">
|
51
|
+
<%= lazy_value_tag do %>
|
52
|
+
<strong>Random 5 users</strong>
|
53
|
+
<%= render "/home/users", users: User.limit(5).order("random()") %>
|
54
|
+
<% end %>
|
55
|
+
</div>
|
56
|
+
```
|
57
|
+
|
58
|
+
And even compatible with Turbo.
|
59
|
+
|
60
|
+
```erb
|
61
|
+
<turbo-frame id="messages" target="_top">
|
62
|
+
This content is from lazy loaded turbo frame.
|
63
|
+
|
64
|
+
<%= lazy_value_tag do %>
|
65
|
+
<h3 class="title"><%= rand(1000) %></h3>
|
66
|
+
<% end %>
|
67
|
+
|
68
|
+
<%= lazy_value_tag do %>
|
69
|
+
<%= "I'm lazy loaded from the turbo frame" %>
|
70
|
+
<% end %>
|
71
|
+
</turbo-frame>
|
72
|
+
```
|
73
|
+
|
74
|
+
## Installation
|
75
|
+
|
76
|
+
Add this line to your application's Gemfile:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
gem "lazy_value"
|
80
|
+
```
|
81
|
+
|
82
|
+
And then execute:
|
83
|
+
```bash
|
84
|
+
$ bundle
|
85
|
+
```
|
86
|
+
|
87
|
+
And that is it. Start using it.
|
88
|
+
|
89
|
+
## How it works
|
90
|
+
|
91
|
+
1. We call `lazy_value_tag` in the view
|
92
|
+
2. We save location from where it was called (with `caller_locations.first`), file + line number.
|
93
|
+
3. We encrypt this info using `ActiveSupport::MessageEncryptor`, creating span with spinner, and JS snippet that will call `/lazy_value/show?payload=`
|
94
|
+
4. In the controller we decrypt our data and reading ERB file and detecting our snippet
|
95
|
+
5. Depending on the block syntax we evaluate ERB or Ruby.
|
96
|
+
6. We return from the controller HTML that will replace snippen on the page.
|
97
|
+
|
98
|
+
## Testing
|
99
|
+
|
100
|
+
`bin/rails test:system`.
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
You are welcome to contribute.
|
105
|
+
|
106
|
+
## License
|
107
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
108
|
+
|
109
|
+
|
110
|
+
[<img src="https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true"
|
111
|
+
/>](https://www.railsjazz.com/?utm_source=github&utm_medium=bottom&utm_campaign=rails_live_reload)
|
112
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module LazyValue
|
2
|
+
class ApplicationController < ActionController::Base
|
3
|
+
REGEXP = /<%= lazy_value_tag(?:.*?)do %>(.*?)<% end %>|<%= lazy_value_tag { (.+?) } %>/m
|
4
|
+
ERROR_MESSAGE = "Incorrect usage. Try to use \"lazy_value_tag do ... end\" or \"lazy_value_tag { ... }\""
|
5
|
+
|
6
|
+
def show
|
7
|
+
matches = code.match(REGEXP)
|
8
|
+
if matches.present? && matches[1].present?
|
9
|
+
render plain: ActionController::Base.render(inline: matches[1])
|
10
|
+
elsif matches.present? && matches[2].present?
|
11
|
+
render plain: eval(matches[2])
|
12
|
+
else
|
13
|
+
render plain: ERROR_MESSAGE
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def payload
|
20
|
+
Base64.urlsafe_decode64(params[:payload])
|
21
|
+
end
|
22
|
+
|
23
|
+
def code
|
24
|
+
data = JSON.parse(LazyValue.cryptography.decrypt_and_verify(payload))
|
25
|
+
# some security checks
|
26
|
+
raise "Incorrect file path" if data["path"] !~ /^#{Rails.root}/ || data["path"].include?("..")
|
27
|
+
# I want to return only first found lazy_value_tag snippet
|
28
|
+
snippet = File.read(data["path"]).lines[data["lineno"]-1..-1]
|
29
|
+
result = []
|
30
|
+
times = 0
|
31
|
+
snippet.map do |line|
|
32
|
+
times += 1 if line =~ /<%= lazy_value_tag/
|
33
|
+
break if times > 1
|
34
|
+
result << line
|
35
|
+
end
|
36
|
+
result.join
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module LazyValue
|
2
|
+
module ApplicationHelper
|
3
|
+
SPINNER = <<~SVG
|
4
|
+
<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><style>.spinner_OSmW{transform-origin:center;animation:spinner_T6mA .75s step-end infinite}@keyframes spinner_T6mA{8.3%{transform:rotate(30deg)}16.6%{transform:rotate(60deg)}25%{transform:rotate(90deg)}33.3%{transform:rotate(120deg)}41.6%{transform:rotate(150deg)}50%{transform:rotate(180deg)}58.3%{transform:rotate(210deg)}66.6%{transform:rotate(240deg)}75%{transform:rotate(270deg)}83.3%{transform:rotate(300deg)}91.6%{transform:rotate(330deg)}100%{transform:rotate(360deg)}}</style><g class="spinner_OSmW"><rect x="11" y="1" width="2" height="5" opacity=".14"/><rect x="11" y="1" width="2" height="5" transform="rotate(30 12 12)" opacity=".29"/><rect x="11" y="1" width="2" height="5" transform="rotate(60 12 12)" opacity=".43"/><rect x="11" y="1" width="2" height="5" transform="rotate(90 12 12)" opacity=".57"/><rect x="11" y="1" width="2" height="5" transform="rotate(120 12 12)" opacity=".71"/><rect x="11" y="1" width="2" height="5" transform="rotate(150 12 12)" opacity=".86"/><rect x="11" y="1" width="2" height="5" transform="rotate(180 12 12)"/></g></svg>
|
5
|
+
SVG
|
6
|
+
|
7
|
+
def lazy_value_tag(*args, &block)
|
8
|
+
element_id = "lazy_value_#{SecureRandom.hex(8)}"
|
9
|
+
kaller = caller_locations.first
|
10
|
+
|
11
|
+
options = {}
|
12
|
+
args.each { |e| options.merge!(e) if e.is_a?(Hash) }
|
13
|
+
options[:class] = [options[:class], "lazy-value-tag"].compact_blank.join(" ")
|
14
|
+
options[:id] = element_id
|
15
|
+
|
16
|
+
data = LazyValue.cryptography.encrypt_and_sign(
|
17
|
+
{
|
18
|
+
path: kaller.path,
|
19
|
+
lineno: kaller.lineno
|
20
|
+
}.to_json
|
21
|
+
)
|
22
|
+
|
23
|
+
content_tag(:span, raw(SPINNER), options) + raw(js_code(data, element_id))
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def js_code(data, element_id)
|
28
|
+
<<~html
|
29
|
+
<script>
|
30
|
+
var remoteServerURL = "/lazy_value/show?payload=#{Base64.urlsafe_encode64(data)}"
|
31
|
+
|
32
|
+
function fetchRemoteContent#{element_id}() {
|
33
|
+
var xhr = new XMLHttpRequest();
|
34
|
+
|
35
|
+
xhr.onreadystatechange = function() {
|
36
|
+
if (xhr.readyState === XMLHttpRequest.DONE) {
|
37
|
+
if (xhr.status >= 200 && xhr.status < 400) {
|
38
|
+
var data = xhr.responseText; // Use JSON.parse(xhr.responseText) if the response is in JSON format
|
39
|
+
var contentContainer = document.getElementById('#{element_id}');
|
40
|
+
contentContainer.nextElementSibling.remove();
|
41
|
+
contentContainer.outerHTML = data;
|
42
|
+
//contentContainer.innerHTML = data;
|
43
|
+
} else {
|
44
|
+
console.error('Error fetching remote content:', xhr.status);
|
45
|
+
}
|
46
|
+
}
|
47
|
+
};
|
48
|
+
|
49
|
+
xhr.open('GET', remoteServerURL, true);
|
50
|
+
xhr.send();
|
51
|
+
}
|
52
|
+
|
53
|
+
fetchRemoteContent#{element_id}();
|
54
|
+
</script>
|
55
|
+
html
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
data/config/routes.rb
ADDED
data/lib/lazy_value.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "lazy_value/version"
|
2
|
+
require "lazy_value/routes"
|
3
|
+
require "lazy_value/engine"
|
4
|
+
|
5
|
+
module LazyValue
|
6
|
+
mattr_accessor :cryptography
|
7
|
+
|
8
|
+
@@cryptography = begin
|
9
|
+
len = ActiveSupport::MessageEncryptor.key_len
|
10
|
+
salt = SecureRandom.random_bytes(len)
|
11
|
+
key = ActiveSupport::KeyGenerator.new(SecureRandom.hex(32)).generate_key(salt, len)
|
12
|
+
ActiveSupport::MessageEncryptor.new(key)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_value
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kasyanchuk
|
@@ -130,7 +130,19 @@ email:
|
|
130
130
|
executables: []
|
131
131
|
extensions: []
|
132
132
|
extra_rdoc_files: []
|
133
|
-
files:
|
133
|
+
files:
|
134
|
+
- MIT-LICENSE
|
135
|
+
- README.md
|
136
|
+
- Rakefile
|
137
|
+
- app/controllers/lazy_value/application_controller.rb
|
138
|
+
- app/helpers/lazy_value/application_helper.rb
|
139
|
+
- config/routes.rb
|
140
|
+
- lib/lazy_value.rb
|
141
|
+
- lib/lazy_value/engine.rb
|
142
|
+
- lib/lazy_value/railtie.rb
|
143
|
+
- lib/lazy_value/routes.rb
|
144
|
+
- lib/lazy_value/version.rb
|
145
|
+
- lib/tasks/lazy_value_tasks.rake
|
134
146
|
homepage: https://github.com/railsjazz.com/lazy_value
|
135
147
|
licenses:
|
136
148
|
- MIT
|