futurism 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e784a06ccd2401b46d5a8a0cffd44e194e3a69db8c10439a7bb1eb0a7cfcf3d8
4
- data.tar.gz: 763001a24e053f55745e7560ce0fe446256328a01c758efaa4a3eb65424cd40e
3
+ metadata.gz: b49e6ebab4db767a2f7aaa4fbc6500ed4103787ea3abefc0489ef6a1536c4fec
4
+ data.tar.gz: 066ab614d937281fe22e5a6d17b8873370c29649815be1b966e2bc22e7cf4ed0
5
5
  SHA512:
6
- metadata.gz: 1486c1e9d54f3e380c675f1fb5f79ec9dc4753446209dae14bbef9a15600f1635d81b0fd75415067019efd5c4b3df6110785da951ef4cc5076d5b9ae13febffc
7
- data.tar.gz: d68ab7a3c7909206e92644b2d7b3e227ddf2e4d5d0c7d787bffbc9766dc48115bb785bd923aecf5dc64244a74dd419bc930148968ff72c827f3bf21d9c199031
6
+ metadata.gz: 638f1eb1bb3fb7304024282419ae2f3baa92a8e9cafe60d31ece08ba837925279005ce905b4acec2c68a6673f430b6b29fec8f4ee9fee5232539c2ea827f54ba
7
+ data.tar.gz: 5d9a0f5d66ee06ea62b6875b4cf19bab6a22c0b0f272ddacfbe2ad0f5d8f06b4a288c32a6863f5d9c4e65de4b9df3799343fff5a5086a06597e9b23a66c9ab53
@@ -3,6 +3,8 @@ require "action_cable"
3
3
  require "cable_ready"
4
4
  require "futurism/engine"
5
5
  require "futurism/message_verifier"
6
+ require "futurism/resolver/controller"
7
+ require "futurism/resolver/controller/renderer"
6
8
  require "futurism/channel"
7
9
  require "futurism/helpers"
8
10
 
@@ -16,18 +16,20 @@ module Futurism
16
16
  end
17
17
 
18
18
  def receive(data)
19
- resources = data.fetch_values("signed_params", "sgids", "signed_controllers") { |_key| Array.new(data["signed_params"].length, nil) }.transpose
19
+ resources = data.fetch_values("signed_params", "sgids", "signed_controllers", "urls") { |_key| Array.new(data["signed_params"].length, nil) }.transpose
20
20
 
21
- resources.each do |signed_params, sgid, signed_controller|
21
+ resources.each do |signed_params, sgid, signed_controller, url|
22
22
  selector = "[data-signed-params='#{signed_params}']"
23
23
  selector << "[data-sgid='#{sgid}']" if sgid.present?
24
24
 
25
- controller_lookup = ControllerLookup.from(signed_string: signed_controller)
26
- controller_lookup.setup_env!(connection: connection)
27
- controller = controller_lookup.controller
25
+ controller = Resolver::Controller.from(signed_string: signed_controller)
26
+ renderer = Resolver::Controller::Renderer.for(controller: controller,
27
+ connection: connection,
28
+ url: url,
29
+ params: @params)
28
30
 
29
31
  resource = lookup_resource(signed_params: signed_params, sgid: sgid)
30
- html = controller.render(resource)
32
+ html = renderer.render(resource)
31
33
 
32
34
  cable_ready[stream_name].outer_html(
33
35
  selector: selector,
@@ -47,41 +49,5 @@ module Futurism
47
49
  .verify(signed_params)
48
50
  .deep_transform_values { |value| value.is_a?(String) && value.start_with?("gid://") ? GlobalID::Locator.locate(value) : value }
49
51
  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
86
52
  end
87
53
  end
@@ -16,18 +16,20 @@ module Futurism
16
16
  end
17
17
 
18
18
  def receive(data)
19
- resources = data.fetch_values("signed_params", "sgids", "signed_controllers") { |_key| Array.new(data["signed_params"].length, nil) }.transpose
19
+ resources = data.fetch_values("signed_params", "sgids", "signed_controllers", "urls") { |_key| Array.new(data["signed_params"].length, nil) }.transpose
20
20
 
21
- resources.each do |signed_params, sgid, signed_controller|
21
+ resources.each do |signed_params, sgid, signed_controller, url|
22
22
  selector = "[data-signed-params='#{signed_params}']"
23
23
  selector << "[data-sgid='#{sgid}']" if sgid.present?
24
24
 
25
- controller_lookup = ControllerLookup.from(signed_string: signed_controller)
26
- controller_lookup.setup(connection: connection)
27
- controller = controller_lookup.controller
25
+ controller = Resolver::Controller.from(signed_string: signed_controller)
26
+ renderer = Resolver::Controller::Renderer.for(controller: controller,
27
+ connection: connection,
28
+ url: url,
29
+ params: @params)
28
30
 
29
31
  resource = lookup_resource(signed_params: signed_params, sgid: sgid)
30
- html = controller.render(resource)
32
+ html = renderer.render(resource)
31
33
 
32
34
  cable_ready[stream_name].outer_html(
33
35
  selector: selector,
@@ -47,41 +49,5 @@ module Futurism
47
49
  .verify(signed_params)
48
50
  .deep_transform_values { |value| value.is_a?(String) && value.start_with?("gid://") ? GlobalID::Locator.locate(value) : value }
49
51
  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
86
52
  end
87
53
  end
@@ -3,11 +3,10 @@ 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
- render options
6
+ return render(**options)
7
7
  else
8
- render records_or_string, options
8
+ return render(records_or_string, **options)
9
9
  end
10
- return
11
10
  end
12
11
 
13
12
  placeholder = capture(&block)
@@ -44,13 +43,15 @@ module Futurism
44
43
  # wraps functionality for rendering a futurism element
45
44
  class Element
46
45
  include ActionView::Helpers
46
+ include Futurism::MessageVerifier
47
47
 
48
- attr_reader :extends, :placeholder, :html_options, :data_attributes, :model, :options, :eager
48
+ attr_reader :extends, :placeholder, :html_options, :data_attributes, :model, :options, :eager, :controller
49
49
 
50
50
  def initialize(extends:, placeholder:, options:)
51
51
  @extends = extends
52
52
  @placeholder = placeholder
53
53
  @eager = options.delete(:eager)
54
+ @controller = options.delete(:controller)
54
55
  @html_options = options.delete(:html_options) || {}
55
56
  @data_attributes = html_options.fetch(:data, {}).except(:sgid, :signed_params)
56
57
  @model = options.delete(:model)
@@ -61,7 +62,8 @@ module Futurism
61
62
  data_attributes.merge({
62
63
  signed_params: signed_params,
63
64
  sgid: model && model.to_sgid.to_s,
64
- eager: eager.presence
65
+ eager: eager.presence,
66
+ signed_controller: signed_controller
65
67
  })
66
68
  end
67
69
 
@@ -87,7 +89,13 @@ module Futurism
87
89
  private
88
90
 
89
91
  def signed_params
90
- Rails.application.message_verifier("futurism").generate(transformed_options)
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)
91
99
  end
92
100
  end
93
101
  end
@@ -1,6 +1,8 @@
1
1
  module Futurism
2
2
  module MessageVerifier
3
- private
3
+ def self.message_verifier
4
+ @message_verifier ||= Rails.application.message_verifier("futurism")
5
+ end
4
6
 
5
7
  def message_verifier
6
8
  @message_verifier ||= Rails.application.message_verifier("futurism")
@@ -0,0 +1,21 @@
1
+ module Futurism
2
+ module Resolver
3
+ class Controller
4
+ def self.from(signed_string:)
5
+ if signed_string.present?
6
+ Futurism::MessageVerifier
7
+ .message_verifier
8
+ .verify(signed_string)
9
+ .to_s
10
+ .safe_constantize
11
+ else
12
+ default_controller
13
+ end
14
+ end
15
+
16
+ def self.default_controller
17
+ Futurism.default_controller || ::ApplicationController
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,57 @@
1
+ module Futurism
2
+ module Resolver
3
+ class Controller
4
+ class Renderer
5
+ def self.for(controller:, connection:, url:, params:)
6
+ new(controller: controller, connection: connection, url: url, params: params).renderer
7
+ end
8
+
9
+ def initialize(controller:, connection:, url:, params:)
10
+ @controller = controller
11
+ @connection = connection
12
+ @url = url || ""
13
+ @params = params || {}
14
+
15
+ setup_env!
16
+ end
17
+
18
+ def renderer
19
+ @renderer ||= controller.renderer
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :controller, :connection, :url, :params
25
+ attr_writer :renderer
26
+
27
+ def setup_env!
28
+ if url.present?
29
+ uri = URI.parse(url)
30
+ path = ActionDispatch::Journey::Router::Utils.normalize_path(uri.path)
31
+ query_hash = Rack::Utils.parse_nested_query(uri.query)
32
+
33
+ path_params = Rails.application.routes.recognize_path(path)
34
+
35
+ self.renderer =
36
+ renderer.new(
37
+ "rack.request.query_hash" => query_hash,
38
+ "rack.request.query_string" => uri.query,
39
+ "ORIGINAL_SCRIPT_NAME" => "",
40
+ "ORIGINAL_FULLPATH" => path,
41
+ Rack::SCRIPT_NAME => "",
42
+ Rack::PATH_INFO => path,
43
+ Rack::REQUEST_PATH => path,
44
+ Rack::QUERY_STRING => uri.query,
45
+ ActionDispatch::Http::Parameters::PARAMETERS_KEY => params.symbolize_keys.merge(path_params).merge(query_hash)
46
+ )
47
+ end
48
+
49
+ # Copy connection env to renderer to fix some RACK related issues from gems like
50
+ # Warden or Devise
51
+ new_env = connection.env.merge(renderer.instance_variable_get(:@env))
52
+ renderer.instance_variable_set(:@env, new_env)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,57 @@
1
+ module Futurism
2
+ module Resolver
3
+ class Controller
4
+ class Renderer
5
+ def self.for(controller:, connection:, url:, params:)
6
+ new(controller: controller, connection: connection, url: url, params: params).renderer
7
+ end
8
+
9
+ def initialize(controller:, connection:, url:, params:)
10
+ @controller = controller
11
+ @connection = connection
12
+ @url = url || ""
13
+ @params = params || {}
14
+
15
+ setup_env!
16
+ end
17
+
18
+ def renderer
19
+ @renderer ||= controller.renderer
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :controller, :connection, :url, :params
25
+ attr_writer :renderer
26
+
27
+ def setup_env!
28
+ if url.present?
29
+ uri = URI.parse(url)
30
+ path = ActionDispatch::Journey::Router::Utils.normalize_path(uri.path)
31
+ query_hash = Rack::Utils.parse_nested_query(uri.query)
32
+
33
+ path_params = Rails.application.routes.recognize_path(path)
34
+
35
+ self.renderer =
36
+ renderer.new(
37
+ "rack.request.query_hash" => query_hash,
38
+ "rack.request.query_string" => uri.query,
39
+ "ORIGINAL_SCRIPT_NAME" => "",
40
+ "ORIGINAL_FULLPATH" => path,
41
+ Rack::SCRIPT_NAME => "",
42
+ Rack::PATH_INFO => path,
43
+ Rack::REQUEST_PATH => path,
44
+ Rack::QUERY_STRING => uri.query,
45
+ ActionDispatch::Http::Parameters::PARAMETERS_KEY => path_params.reverse_merge(path_params)
46
+ )
47
+ end
48
+
49
+ # Copy connection env to renderer to fix some RACK related issues from gems like
50
+ # Warden or Devise
51
+ new_env = connection.env.merge(renderer.instance_variable_get(:@env))
52
+ renderer.instance_variable_set(:@env, new_env)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module Futurism
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
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.6.0
4
+ version: 0.7.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-12 00:00:00.000000000 Z
11
+ date: 2020-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal
@@ -108,20 +108,6 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: spy
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: '0'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: rack
127
113
  requirement: !ruby/object:Gem::Requirement
@@ -184,6 +170,9 @@ files:
184
170
  - lib/futurism/helpers.rb
185
171
  - lib/futurism/helpers.rb~
186
172
  - lib/futurism/message_verifier.rb
173
+ - lib/futurism/resolver/controller.rb
174
+ - lib/futurism/resolver/controller/renderer.rb
175
+ - lib/futurism/resolver/controller/renderer.rb~
187
176
  - lib/futurism/shims/deep_transform_values.rb
188
177
  - lib/futurism/version.rb
189
178
  - lib/tasks/futurism_tasks.rake