all_futures 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +126 -0
- data/Rakefile +4 -0
- data/SUMMARY.md +6 -0
- data/bin/console +7 -0
- data/bin/loc +3 -0
- data/bin/setup +8 -0
- data/bin/standardize +4 -0
- data/lib/all_futures/all_futures.rb +28 -0
- data/lib/all_futures/config.rb +14 -0
- data/lib/all_futures/version.rb +3 -0
- data/lib/all_futures.rb +5 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 58b9a9722498f608ed23c7fabc86843449339927c7bc788be45af8141ff4f7f2
|
4
|
+
data.tar.gz: ee1f665dd243d05454b4d6f2d0d462fd3632d6a64d78f3cbfcbad354e0d3b083
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 37c7801e9181a793516c0fc69617af28c4f9f3cfdf414ce3506cd4f3c2fbbdd0b5d891464231845c48f7abeb18dd903cdb81bf57957ed2577eb7711b45a7fbd2
|
7
|
+
data.tar.gz: 383a254d75030587bf4bfef93e1cee9738ff7f96a21e91c89f5d6461d9639d77ff1fe42daa9a7ea974dbfa393a73c8d3cd9e964c498f024f75a7c0b66c70f566
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 leastbad
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# All Futures
|
2
|
+
|
3
|
+
The [all\_futures](https://github.com/leastbad/all_futures) gem offers Rails developers a way to **gather attributes** on an unsaved model **across multiple requests**. It's perfect for [StimulusReflex](https://docs.stimulusreflex.com/) users that are building faceted search interfaces, as well as [Optimism](https://optimism.leastbad.com/) users looking to implement real-time, per-attribute validation schemes.
|
4
|
+
|
5
|
+
Try a demo, here: 👉 [Beast Mode StimulusReflex](https://beastmode.leastbad.com/) 👈
|
6
|
+
|
7
|
+
[![GitHub stars](https://img.shields.io/github/stars/leastbad/all_futures?style=social)](https://github.com/leastbad/all_futures) [![GitHub forks](https://img.shields.io/github/forks/leastbad/all_futures?style=social)](https://github.com/leastbad/all_futures) [![Twitter follow](https://img.shields.io/twitter/follow/theleastbad?style=social)](https://twitter.com/theleastbad) [![Discord](https://img.shields.io/discord/681373845323513862)](https://discord.gg/GnweR3)
|
8
|
+
|
9
|
+
## Why use All Futures?
|
10
|
+
|
11
|
+
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.
|
12
|
+
|
13
|
+
All Futures presents a flexible and lightweight mechanism to refine a model that persists its attributes across multiple updates, and even multiple servers.
|
14
|
+
|
15
|
+
## Is All Futures for you?
|
16
|
+
|
17
|
+
Do you ever find yourself:
|
18
|
+
|
19
|
+
* building complex search interfaces
|
20
|
+
* creating multi-stage data entry processes
|
21
|
+
* frustrated by the limitations of classic form submission
|
22
|
+
* wanting to save data even if the model is currently invalid
|
23
|
+
* reinventing the wheel every time you need field validation
|
24
|
+
|
25
|
+
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.
|
26
|
+
|
27
|
+
Yes, All Futures is for **you**.
|
28
|
+
|
29
|
+
## Key features and advantages
|
30
|
+
|
31
|
+
* A natural fit with [StimulusReflex](https://docs.stimulusreflex.com/) and [Stimulus](https://stimulus.hotwire.dev/)
|
32
|
+
* No reliance on sessions, so it works across servers
|
33
|
+
* Easy to learn, quick to implement
|
34
|
+
* Supports model attributes with defaults, arrays and dirty checking
|
35
|
+
* Model validations and errors
|
36
|
+
* No need to mess around with temporary records
|
37
|
+
|
38
|
+
## How does All Futures work?
|
39
|
+
|
40
|
+
First, set up an All Futures class that defines some [attributes](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html#method-i-attribute). Your class will inherit from `Possibility`, which is aptly-named.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
class ExampleModel < Possibility
|
44
|
+
attribute :name, :string
|
45
|
+
attribute :age, :integer, default: 21
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Then create an instance and assign it to an instance variable in the controller responsible for your initial page load:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class ExampleController < ApplicationController
|
53
|
+
def index
|
54
|
+
@af = ExampleModel.new
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
Emit the instance id as a data attribute on every element which can update your model:
|
60
|
+
|
61
|
+
```text
|
62
|
+
Name: <input type="text" data-af="<%= @af.id %>" data-reflex="input->Example#name" /><br/>
|
63
|
+
Age: <input type="text" data-af="<%= @af.id %>" data-reflex="input->Example#age" placeholder="<%= @id.age %>" />
|
64
|
+
```
|
65
|
+
|
66
|
+
Since all attributes are gathered and sent to the server during a Reflex operation, it's easy to retrieve the instance id from the Reflex element accessor and use it to call up the correct All Futures object and make changes to it:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class ExampleReflex < ApplicationReflex
|
70
|
+
def name
|
71
|
+
model = ExampleModel.find(element.dataset.af)
|
72
|
+
model[:name] = element.value
|
73
|
+
end
|
74
|
+
|
75
|
+
def age
|
76
|
+
model = ExampleModel.find(element.dataset.af)
|
77
|
+
model[:age] = element.value
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
The current state of the attributes is persisted every time you set the value of an attribute using bracket notation. You can use standard setter assignments, but the model state will not be persisted until you manually call `save`:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
model[:name] = "Helen" # saved
|
86
|
+
model.name = "Helen" # not saved
|
87
|
+
model.save # saved
|
88
|
+
```
|
89
|
+
|
90
|
+
{% hint style="warning" %}
|
91
|
+
All Futures class attributes are persisted in Redis via the excellent [Kredis](https://github.com/rails/kredis) gem, which must be set up and running in your project before you can use All Futures.
|
92
|
+
{% endhint %}
|
93
|
+
|
94
|
+
All Futures is based on [Active Entity](https://github.com/jasl/activeentity). It is similar to using [ActiveModel::Model](https://api.rubyonrails.org/classes/ActiveModel/Model.html), except that it has full support for [Attributes](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html#method-i-attribute), including arrays and nested attributes. All Futures classes behave like ActiveModel classes, so you can inspect `valid?` and the `errors` accessor.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
class ExampleModel < Possibility
|
98
|
+
attribute :name, :string
|
99
|
+
validates :name, presence: true
|
100
|
+
end
|
101
|
+
|
102
|
+
model = ExampleModel.new
|
103
|
+
model.valid? # false
|
104
|
+
model.errors # @errors=[#<ActiveModel::Error attribute=name, type=blank, options={}>]
|
105
|
+
```
|
106
|
+
|
107
|
+
{% hint style="info" %}
|
108
|
+
Unlike an ActiveRecord model, All Futures 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.
|
109
|
+
{% endhint %}
|
110
|
+
|
111
|
+
{% hint style="success" %}
|
112
|
+
Once the state of your attributes is valid, you can pass the `attributes` from your All Futures model right into the constructor of a real ActiveRecord model. It should work perfectly.
|
113
|
+
{% endhint %}
|
114
|
+
|
115
|
+
## Try it now
|
116
|
+
|
117
|
+
You can experiment with [Beast Mode StimulusReflex](https://beastmode.leastbad.com/), a live demonstration of using All Futures to drill down into a tabular dataset, [**right now**](https://beastmode.leastbad.com/). 👈
|
118
|
+
|
119
|
+
The Beast Mode [codebase](https://github.com/leastbad/beast_mode) [![GitHub stars](https://img.shields.io/github/stars/leastbad/beast_mode?style=social)](https://github.com/leastbad/beast_mode) [![GitHub forks](https://img.shields.io/github/forks/leastbad/beast_mode?style=social)](https://github.com/leastbad/beast_mode) is set up as a template repo which I recommend that you clone and experiment with.
|
120
|
+
|
121
|
+
The two key files are the [Filter](https://github.com/leastbad/beast_mode/blob/master/app/filters/customer_filter.rb) and the [Reflex](https://github.com/leastbad/beast_mode/blob/master/app/reflexes/customers_reflex.rb). You can read the tutorial post behind this example on my blog [here](https://leastbad.com/beast-mode/).
|
122
|
+
|
123
|
+
Assuming you're running 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.
|
124
|
+
|
125
|
+
{% embed url="https://www.youtube.com/watch?v=Fbo21aWFbhQ" caption="Did they meet at the gym?" %}
|
126
|
+
|
data/Rakefile
ADDED
data/SUMMARY.md
ADDED
data/bin/console
ADDED
data/bin/loc
ADDED
data/bin/setup
ADDED
data/bin/standardize
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
class AllFutures < ActiveEntity::Base
|
2
|
+
attr_accessor :id
|
3
|
+
|
4
|
+
def initialize(attributes={})
|
5
|
+
super
|
6
|
+
unless @id
|
7
|
+
@id = SecureRandom.uuid
|
8
|
+
save
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def []=(attr_name, value)
|
13
|
+
super
|
14
|
+
save
|
15
|
+
end
|
16
|
+
|
17
|
+
def save
|
18
|
+
Kredis.json("#{self.class.name}:#{@id}").value = self.attributes.to_json
|
19
|
+
changes_applied
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.find(id)
|
23
|
+
raise ArgumentError unless id
|
24
|
+
json = Kredis.json("#{name}:#{id}").value
|
25
|
+
raise ActiveRecord::RecordNotFound unless json
|
26
|
+
new json.merge(id: id)
|
27
|
+
end
|
28
|
+
end
|
data/lib/all_futures.rb
ADDED
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: all_futures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- leastbad
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: kredis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activeentity
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '6.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: magic_frozen_string_literal
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.2.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-nav
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '13.0'
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 13.0.3
|
93
|
+
type: :development
|
94
|
+
prerelease: false
|
95
|
+
version_requirements: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - "~>"
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '13.0'
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 13.0.3
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: standardrb
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '1.0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '1.0'
|
117
|
+
description:
|
118
|
+
email:
|
119
|
+
- hello@leastbad.com
|
120
|
+
executables: []
|
121
|
+
extensions: []
|
122
|
+
extra_rdoc_files: []
|
123
|
+
files:
|
124
|
+
- Gemfile
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- SUMMARY.md
|
129
|
+
- bin/console
|
130
|
+
- bin/loc
|
131
|
+
- bin/setup
|
132
|
+
- bin/standardize
|
133
|
+
- lib/all_futures.rb
|
134
|
+
- lib/all_futures/all_futures.rb
|
135
|
+
- lib/all_futures/config.rb
|
136
|
+
- lib/all_futures/version.rb
|
137
|
+
homepage: https://allfutures.leastbad.com/
|
138
|
+
licenses:
|
139
|
+
- MIT
|
140
|
+
metadata:
|
141
|
+
source_code_uri: https://github.com/leastbad/all_futures
|
142
|
+
documentation_uri: https://allfutures.leastbad.com/
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubygems_version: 3.1.6
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: A Redis-backed virtual ActiveModel, full of possibilities.
|
162
|
+
test_files: []
|