futurism 0.5.4 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -0
- data/lib/futurism.rb +2 -1
- data/lib/futurism/channel.rb +50 -10
- data/lib/futurism/channel.rb~ +50 -10
- data/lib/futurism/helpers.rb +14 -5
- data/lib/futurism/message_verifier.rb +9 -0
- data/lib/futurism/version.rb +1 -1
- metadata +3 -3
- data/app/assets/config/futurism_manifest.js +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e784a06ccd2401b46d5a8a0cffd44e194e3a69db8c10439a7bb1eb0a7cfcf3d8
|
4
|
+
data.tar.gz: 763001a24e053f55745e7560ce0fe446256328a01c758efaa4a3eb65424cd40e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1486c1e9d54f3e380c675f1fb5f79ec9dc4753446209dae14bbef9a15600f1635d81b0fd75415067019efd5c4b3df6110785da951ef4cc5076d5b9ae13febffc
|
7
|
+
data.tar.gz: d68ab7a3c7909206e92644b2d7b3e227ddf2e4d5d0c7d787bffbc9766dc48115bb785bd923aecf5dc64244a74dd419bc930148968ff72c827f3bf21d9c199031
|
data/README.md
CHANGED
@@ -124,6 +124,22 @@ Collection rendering is also possible:
|
|
124
124
|
<% end %>
|
125
125
|
```
|
126
126
|
|
127
|
+
#### Specifying Controller to Render
|
128
|
+
|
129
|
+
You can also pass in the controller that will be used to render the partial.
|
130
|
+
|
131
|
+
```erb
|
132
|
+
<%= futurize partial: "items/card", collection: @cards, controller: MyController, extends: :div do %>
|
133
|
+
<div class="spinner"></div>
|
134
|
+
<% end %>
|
135
|
+
```
|
136
|
+
|
137
|
+
By default (i.e. not passing in a value), futurize will use `ApplicationController`, but you may override by setting the Futurism default controller in an initializer, for example `config/initializers/futurism.rb`.
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
Futurism.default_controller = MyController
|
141
|
+
```
|
142
|
+
|
127
143
|
### HTML Options
|
128
144
|
|
129
145
|
You can pass a hash of attribute/value pairs which will be mixed into the HTML markup for the placeholder element. This is important for layouts that require elements to have dimensionality. For example, many scripts calculate size based on element height and width. This option ensures that your elements have integrity, even if they are gone before you see them.
|
@@ -241,6 +257,39 @@ to your environments.
|
|
241
257
|
|
242
258
|
## Contributing
|
243
259
|
|
260
|
+
### Get local environment setup
|
261
|
+
|
262
|
+
Below are a set of instructions that may help you get a local development environment working
|
263
|
+
|
264
|
+
```shell
|
265
|
+
# Get the gem/npm package source locally
|
266
|
+
git clone futurism
|
267
|
+
cd futurism/javascript
|
268
|
+
yarn install # install all of the npm package's dependencies
|
269
|
+
yarn link # set the local machine's futurism npm package's lookup to this local path
|
270
|
+
|
271
|
+
# Setup a sample project, use the information below directly or use your own project
|
272
|
+
git clone https://github.com/leastbad/stimulus_reflex_harness.git
|
273
|
+
cd stimulus_reflex_harness
|
274
|
+
git checkout futurism
|
275
|
+
# Edit Gemfile to point point to local gem (e.g. `gem "futurism", path: "../futurism"`)
|
276
|
+
# yarn link @minthesize/futurism
|
277
|
+
|
278
|
+
|
279
|
+
# Do your work, Submit PR, Profit!
|
280
|
+
|
281
|
+
|
282
|
+
# To stop using your local version of futurism
|
283
|
+
# change your Gemfile back to the published (e.g. `gem "futurism"`)
|
284
|
+
cd path/to/futurism/javascript
|
285
|
+
# Stop using the local npm package
|
286
|
+
yarn unlink
|
287
|
+
|
288
|
+
# Instruct your project to reinstall the published version of the npm package
|
289
|
+
cd path/to/project
|
290
|
+
yarn install --force
|
291
|
+
```
|
292
|
+
|
244
293
|
## License
|
245
294
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
246
295
|
|
data/lib/futurism.rb
CHANGED
@@ -2,6 +2,7 @@ require "rails"
|
|
2
2
|
require "action_cable"
|
3
3
|
require "cable_ready"
|
4
4
|
require "futurism/engine"
|
5
|
+
require "futurism/message_verifier"
|
5
6
|
require "futurism/channel"
|
6
7
|
require "futurism/helpers"
|
7
8
|
|
@@ -10,7 +11,7 @@ module Futurism
|
|
10
11
|
|
11
12
|
autoload :Helpers, "futurism/helpers"
|
12
13
|
|
13
|
-
mattr_accessor :skip_in_test
|
14
|
+
mattr_accessor :skip_in_test, :default_controller
|
14
15
|
|
15
16
|
ActiveSupport.on_load(:action_view) {
|
16
17
|
include Futurism::Helpers
|
data/lib/futurism/channel.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Futurism
|
2
2
|
class Channel < ActionCable::Channel::Base
|
3
3
|
include CableReady::Broadcaster
|
4
|
+
include Futurism::MessageVerifier
|
4
5
|
|
5
6
|
def stream_name
|
6
7
|
ids = connection.identifiers.map { |identifier| send(identifier).try(:id) || send(identifier) }
|
@@ -15,17 +16,22 @@ module Futurism
|
|
15
16
|
end
|
16
17
|
|
17
18
|
def receive(data)
|
18
|
-
resources = data.fetch_values("signed_params", "sgids") { |
|
19
|
+
resources = data.fetch_values("signed_params", "sgids", "signed_controllers") { |_key| Array.new(data["signed_params"].length, nil) }.transpose
|
19
20
|
|
20
|
-
|
21
|
-
ApplicationController.renderer.instance_variable_set(:@env, new_env)
|
22
|
-
|
23
|
-
resources.each do |signed_params, sgid|
|
21
|
+
resources.each do |signed_params, sgid, signed_controller|
|
24
22
|
selector = "[data-signed-params='#{signed_params}']"
|
25
23
|
selector << "[data-sgid='#{sgid}']" if sgid.present?
|
24
|
+
|
25
|
+
controller_lookup = ControllerLookup.from(signed_string: signed_controller)
|
26
|
+
controller_lookup.setup_env!(connection: connection)
|
27
|
+
controller = controller_lookup.controller
|
28
|
+
|
29
|
+
resource = lookup_resource(signed_params: signed_params, sgid: sgid)
|
30
|
+
html = controller.render(resource)
|
31
|
+
|
26
32
|
cable_ready[stream_name].outer_html(
|
27
33
|
selector: selector,
|
28
|
-
html:
|
34
|
+
html: html
|
29
35
|
)
|
30
36
|
end
|
31
37
|
|
@@ -34,14 +40,48 @@ module Futurism
|
|
34
40
|
|
35
41
|
private
|
36
42
|
|
37
|
-
def
|
43
|
+
def lookup_resource(signed_params:, sgid:)
|
38
44
|
return GlobalID::Locator.locate_signed(sgid) if sgid.present?
|
39
45
|
|
40
|
-
|
41
|
-
.application
|
42
|
-
.message_verifier("futurism")
|
46
|
+
message_verifier
|
43
47
|
.verify(signed_params)
|
44
48
|
.deep_transform_values { |value| value.is_a?(String) && value.start_with?("gid://") ? GlobalID::Locator.locate(value) : value }
|
45
49
|
end
|
50
|
+
|
51
|
+
class ControllerLookup
|
52
|
+
include Futurism::MessageVerifier
|
53
|
+
|
54
|
+
def self.from(signed_string:)
|
55
|
+
new(signed_string)
|
56
|
+
end
|
57
|
+
|
58
|
+
def initialize(signed_string)
|
59
|
+
@signed_string = signed_string
|
60
|
+
end
|
61
|
+
|
62
|
+
def controller
|
63
|
+
if signed_string.present?
|
64
|
+
message_verifier
|
65
|
+
.verify(signed_string)
|
66
|
+
.to_s
|
67
|
+
.safe_constantize
|
68
|
+
else
|
69
|
+
default_controller
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def setup_env!(connection:)
|
74
|
+
new_env = connection.env.merge(controller.renderer.instance_variable_get(:@env))
|
75
|
+
controller.renderer.instance_variable_set(:@env, new_env)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
attr_reader :signed_string
|
81
|
+
|
82
|
+
def default_controller
|
83
|
+
Futurism.default_controller || ::ApplicationController
|
84
|
+
end
|
85
|
+
end
|
46
86
|
end
|
47
87
|
end
|
data/lib/futurism/channel.rb~
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Futurism
|
2
2
|
class Channel < ActionCable::Channel::Base
|
3
3
|
include CableReady::Broadcaster
|
4
|
+
include Futurism::MessageVerifier
|
4
5
|
|
5
6
|
def stream_name
|
6
7
|
ids = connection.identifiers.map { |identifier| send(identifier).try(:id) || send(identifier) }
|
@@ -15,17 +16,22 @@ module Futurism
|
|
15
16
|
end
|
16
17
|
|
17
18
|
def receive(data)
|
18
|
-
resources = data.fetch_values("signed_params", "sgids") { |
|
19
|
+
resources = data.fetch_values("signed_params", "sgids", "signed_controllers") { |_key| Array.new(data["signed_params"].length, nil) }.transpose
|
19
20
|
|
20
|
-
|
21
|
-
ApplicationController.renderer.instance_variable_set(:@env, new_env)
|
22
|
-
|
23
|
-
resources.each do |signed_params, sgid|
|
21
|
+
resources.each do |signed_params, sgid, signed_controller|
|
24
22
|
selector = "[data-signed-params='#{signed_params}']"
|
25
23
|
selector << "[data-sgid='#{sgid}']" if sgid.present?
|
24
|
+
|
25
|
+
controller_lookup = ControllerLookup.from(signed_string: signed_controller)
|
26
|
+
controller_lookup.setup(connection: connection)
|
27
|
+
controller = controller_lookup.controller
|
28
|
+
|
29
|
+
resource = lookup_resource(signed_params: signed_params, sgid: sgid)
|
30
|
+
html = controller.render(resource)
|
31
|
+
|
26
32
|
cable_ready[stream_name].outer_html(
|
27
33
|
selector: selector,
|
28
|
-
html:
|
34
|
+
html: html
|
29
35
|
)
|
30
36
|
end
|
31
37
|
|
@@ -34,14 +40,48 @@ module Futurism
|
|
34
40
|
|
35
41
|
private
|
36
42
|
|
37
|
-
def
|
43
|
+
def lookup_resource(signed_params:, sgid:)
|
38
44
|
return GlobalID::Locator.locate_signed(sgid) if sgid.present?
|
39
45
|
|
40
|
-
|
41
|
-
.application
|
42
|
-
.message_verifier("futurism")
|
46
|
+
message_verifier
|
43
47
|
.verify(signed_params)
|
44
48
|
.deep_transform_values { |value| value.is_a?(String) && value.start_with?("gid://") ? GlobalID::Locator.locate(value) : value }
|
45
49
|
end
|
50
|
+
|
51
|
+
class ControllerLookup
|
52
|
+
include Futurism::MessageVerifier
|
53
|
+
|
54
|
+
def self.from(signed_string:)
|
55
|
+
new(signed_string)
|
56
|
+
end
|
57
|
+
|
58
|
+
def initialize(signed_string)
|
59
|
+
@signed_string = signed_string
|
60
|
+
end
|
61
|
+
|
62
|
+
def controller
|
63
|
+
if signed_string.present?
|
64
|
+
message_verifier
|
65
|
+
.verify(signed_string)
|
66
|
+
.to_s
|
67
|
+
.safe_constantize
|
68
|
+
else
|
69
|
+
default_controller
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def setup(connection:)
|
74
|
+
new_env = connection.env.merge(controller.renderer.instance_variable_get(:@env))
|
75
|
+
controller.renderer.instance_variable_set(:@env, new_env)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
attr_reader :signed_string
|
81
|
+
|
82
|
+
def default_controller
|
83
|
+
Futurism.default_controller || ::ApplicationController
|
84
|
+
end
|
85
|
+
end
|
46
86
|
end
|
47
87
|
end
|
data/lib/futurism/helpers.rb
CHANGED
@@ -3,9 +3,9 @@ module Futurism
|
|
3
3
|
def futurize(records_or_string = nil, extends:, **options, &block)
|
4
4
|
if Rails.env.test? && Futurism.skip_in_test
|
5
5
|
if records_or_string.nil?
|
6
|
-
return render
|
6
|
+
return render(**options)
|
7
7
|
else
|
8
|
-
return render
|
8
|
+
return render(records_or_string, **options)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -43,13 +43,15 @@ module Futurism
|
|
43
43
|
# wraps functionality for rendering a futurism element
|
44
44
|
class Element
|
45
45
|
include ActionView::Helpers
|
46
|
+
include Futurism::MessageVerifier
|
46
47
|
|
47
|
-
attr_reader :extends, :placeholder, :html_options, :data_attributes, :model, :options, :eager
|
48
|
+
attr_reader :extends, :placeholder, :html_options, :data_attributes, :model, :options, :eager, :controller
|
48
49
|
|
49
50
|
def initialize(extends:, placeholder:, options:)
|
50
51
|
@extends = extends
|
51
52
|
@placeholder = placeholder
|
52
53
|
@eager = options.delete(:eager)
|
54
|
+
@controller = options.delete(:controller)
|
53
55
|
@html_options = options.delete(:html_options) || {}
|
54
56
|
@data_attributes = html_options.fetch(:data, {}).except(:sgid, :signed_params)
|
55
57
|
@model = options.delete(:model)
|
@@ -60,7 +62,8 @@ module Futurism
|
|
60
62
|
data_attributes.merge({
|
61
63
|
signed_params: signed_params,
|
62
64
|
sgid: model && model.to_sgid.to_s,
|
63
|
-
eager: eager.presence
|
65
|
+
eager: eager.presence,
|
66
|
+
signed_controller: signed_controller
|
64
67
|
})
|
65
68
|
end
|
66
69
|
|
@@ -86,7 +89,13 @@ module Futurism
|
|
86
89
|
private
|
87
90
|
|
88
91
|
def signed_params
|
89
|
-
|
92
|
+
message_verifier.generate(transformed_options)
|
93
|
+
end
|
94
|
+
|
95
|
+
def signed_controller
|
96
|
+
return unless controller.present?
|
97
|
+
|
98
|
+
message_verifier.generate(controller.to_s)
|
90
99
|
end
|
91
100
|
end
|
92
101
|
end
|
data/lib/futurism/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: futurism
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julian Rubisch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: appraisal
|
@@ -175,7 +175,6 @@ files:
|
|
175
175
|
- MIT-LICENSE
|
176
176
|
- README.md
|
177
177
|
- Rakefile
|
178
|
-
- app/assets/config/futurism_manifest.js
|
179
178
|
- config/routes.rb
|
180
179
|
- lib/futurism.rb
|
181
180
|
- lib/futurism.rb~
|
@@ -184,6 +183,7 @@ files:
|
|
184
183
|
- lib/futurism/engine.rb
|
185
184
|
- lib/futurism/helpers.rb
|
186
185
|
- lib/futurism/helpers.rb~
|
186
|
+
- lib/futurism/message_verifier.rb
|
187
187
|
- lib/futurism/shims/deep_transform_values.rb
|
188
188
|
- lib/futurism/version.rb
|
189
189
|
- lib/tasks/futurism_tasks.rake
|
File without changes
|