turbo_reflex 0.0.25 → 0.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c28abca449b5630362e4a5926b270c4c8075a1ef989434c0899355d96390b388
4
- data.tar.gz: 6a767145f9a32ab240feac42051f0d2be4c4d395202641b4e8caaf3cabe9c242
3
+ metadata.gz: e1ef38965d0ef18dbbc67316bed5aaffcb50da54e603528b0b72088378e4daff
4
+ data.tar.gz: d1938187e80bdb62e1f2ab46bb14f17fd130eafeb24592df905052d4c26897ab
5
5
  SHA512:
6
- metadata.gz: 36568b96f6cfd94e119adec06a2624c12a042447cdbbd5973de82ed8b629135a30ef033f23384da49084fa76a76a3ccc5422e3e0ec5267269cba693f7fcbce1d
7
- data.tar.gz: 58efc30fb5c49b605b76a5d8998cd6f6d69b4288bad4d3a3b5b1085e26236f0162e282be402e34837e1e33ec78f405cb2cb5034279ee8bf9cb6fdd70a157c02c
6
+ metadata.gz: 86a168a615001267eed78ffc6b1fc7ff6032d2ef7b4e4d730e2a0ed9f0b13976a5972def9eb831340618560c62021175386aa438d133a5824e10d1063a85896b
7
+ data.tar.gz: e6438f83d9584b47d04f0a152dbd1ff9c886828847b66995d6d72d5c5448f162650584b3f9cde9d3710e88d8e2651a240b2e3d2b894fdf0377c8afb9d420dacc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- turbo_reflex (0.0.25)
4
+ turbo_reflex (0.0.27)
5
5
  rails (>= 6.1)
6
6
  turbo-rails (>= 1.1)
7
7
  turbo_ready (>= 0.1)
@@ -190,7 +190,7 @@ GEM
190
190
  zeitwerk (~> 2.5)
191
191
  rainbow (3.1.1)
192
192
  rake (13.0.6)
193
- regexp_parser (2.6.0)
193
+ regexp_parser (2.6.1)
194
194
  rexml (3.2.5)
195
195
  rouge (4.0.0)
196
196
  rubocop (1.39.0)
@@ -205,7 +205,7 @@ GEM
205
205
  unicode-display_width (>= 1.4.0, < 3.0)
206
206
  rubocop-ast (1.23.0)
207
207
  parser (>= 3.1.1.0)
208
- rubocop-performance (1.15.0)
208
+ rubocop-performance (1.15.1)
209
209
  rubocop (>= 1.7.0, < 2.0)
210
210
  rubocop-ast (>= 0.4.0)
211
211
  ruby-progressbar (1.11.0)
@@ -222,11 +222,10 @@ GEM
222
222
  actionpack (>= 5.2)
223
223
  activesupport (>= 5.2)
224
224
  sprockets (>= 3.0.0)
225
- sqlite3 (1.5.3-aarch64-linux)
226
- sqlite3 (1.5.3-arm64-darwin)
227
- standard (1.18.0)
225
+ sqlite3 (1.5.4-arm64-darwin)
226
+ standard (1.18.1)
228
227
  rubocop (= 1.39.0)
229
- rubocop-performance (= 1.15.0)
228
+ rubocop-performance (= 1.15.1)
230
229
  standardrb (1.0.1)
231
230
  standard
232
231
  tailwindcss-rails (2.0.21-aarch64-linux)
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  </h1>
9
9
  <p align="center">
10
10
  <a href="http://blog.codinghorror.com/the-best-code-is-no-code-at-all/">
11
- <img alt="Lines of Code" src="https://img.shields.io/badge/loc-1228-47d299.svg" />
11
+ <img alt="Lines of Code" src="https://img.shields.io/badge/loc-1249-47d299.svg" />
12
12
  </a>
13
13
  <a href="https://codeclimate.com/github/hopsoft/turbo_reflex/maintainability">
14
14
  <img src="https://api.codeclimate.com/v1/badges/fe1162a742fe83a4fdfd/maintainability" />
@@ -67,7 +67,7 @@ class TurboReflex::Base
67
67
  attr_reader :controller, :turbo_streams
68
68
  alias_method :streams, :turbo_streams
69
69
 
70
- delegate :dom_id, :render, to: :"controller.view_context"
70
+ delegate :dom_id, to: :"controller.view_context"
71
71
  delegate(
72
72
  :controller_action_prevented?,
73
73
  :render_response,
@@ -86,6 +86,22 @@ class TurboReflex::Base
86
86
  "##{dom_id(...)}"
87
87
  end
88
88
 
89
+ # Same method signature as ActionView::Rendering#render (i.e. controller.view_context.render)
90
+ def render(options = {}, locals = {}, &block)
91
+ return controller.view_context.render(options, locals, &block) unless options.is_a?(Hash)
92
+
93
+ options = options.symbolize_keys
94
+
95
+ ivars = options[:assigns]&.each_with_object({}) do |(key, value), memo|
96
+ memo[key] = controller.instance_variable_get("@#{key}")
97
+ controller.instance_variable_set "@#{key}", value
98
+ end
99
+
100
+ controller.view_context.render(options.except(:assigns), locals, &block)
101
+ ensure
102
+ ivars&.each { |key, value| controller.instance_variable_set "@#{key}", value }
103
+ end
104
+
89
105
  def morph(selector, html)
90
106
  turbo_streams << turbo_stream.invoke("morph", args: [html], selector: selector)
91
107
  end
@@ -5,13 +5,26 @@ require_relative "version"
5
5
  require_relative "base"
6
6
  require_relative "controller_pack"
7
7
 
8
- class TurboReflex::Engine < ::Rails::Engine
9
- config.turbo_reflex = ActiveSupport::OrderedOptions.new
10
- initializer "turbo_reflex.configuration" do
11
- Mime::Type.register "text/vnd.turbo-reflex.html", :turbo_reflex
8
+ module TurboReflex
9
+ def self.config
10
+ Rails.application.config.turbo_reflex
11
+ end
12
+
13
+ class Engine < ::Rails::Engine
14
+ config.turbo_reflex = ActiveSupport::OrderedOptions.new
15
+ config.turbo_reflex[:max_cookie_size] = ActionDispatch::Cookies::MAX_COOKIE_SIZE / 3
16
+ config.turbo_reflex[:validate_client_token] = true
17
+
18
+ # must opt-in to state overrides
19
+ config.turbo_reflex[:apply_client_state_overrides] = false
20
+ config.turbo_reflex[:apply_server_state_overrides] = false
21
+
22
+ initializer "turbo_reflex.configuration" do
23
+ Mime::Type.register "text/vnd.turbo-reflex.html", :turbo_reflex
12
24
 
13
- config.to_prepare do |app|
14
- ::ActionController::Base.send :include, TurboReflex::Controller
25
+ ActiveSupport.on_load(:action_controller_base) do
26
+ include TurboReflex::Controller
27
+ end
15
28
  end
16
29
  end
17
30
  end
@@ -195,6 +195,7 @@ class TurboReflex::Runner
195
195
  end
196
196
 
197
197
  def valid_client_token?
198
+ return true unless Rails.configuration.turbo_reflex.validate_client_token
198
199
  return false unless client_token.present?
199
200
  return false unless message_verifier.valid_message?(client_token)
200
201
  unmasked_client_token = message_verifier.verify(client_token)
@@ -18,12 +18,6 @@ class TurboReflex::StateManager
18
18
  include ActiveModel::Dirty
19
19
 
20
20
  class << self
21
- attr_writer :cookie_max_bytesize
22
-
23
- def cookie_max_bytesize
24
- @cookie_max_bytesize ||= 2.kilobytes
25
- end
26
-
27
21
  def state_override_blocks
28
22
  @state_overrides ||= {}
29
23
  end
@@ -69,25 +63,29 @@ class TurboReflex::StateManager
69
63
  header_state_hash = {}
70
64
 
71
65
  # Apply server state overrides (i.e. state stored in databases like Redis, Postgres, etc...)
72
- begin
73
- state_override_block = self.class.state_override_block(runner.controller)
74
- if state_override_block
75
- server_state_hash = runner.controller.instance_eval(&state_override_block).with_indifferent_access
76
- server_state_hash.each { |key, val| self[key] = val }
66
+ if TurboReflex.config.apply_server_state_overrides
67
+ begin
68
+ state_override_block = self.class.state_override_block(runner.controller)
69
+ if state_override_block
70
+ server_state_hash = runner.controller.instance_eval(&state_override_block).with_indifferent_access
71
+ server_state_hash.each { |key, val| self[key] = val }
72
+ end
73
+ rescue => error
74
+ Rails.logger.error "Failed to apply `state_override_block` configured in #{runner.controller.class.name} to TurboReflex::State! #{error.message}"
77
75
  end
78
- rescue => error
79
- Rails.logger.error "Failed to apply `state_override_block` configured in #{runner.controller.class.name} to TurboReflex::State! #{error.message}"
80
76
  end
81
77
 
82
- # Merge client state into server state (i.e. optimistic state)
78
+ # Apply client state overrides (i.e. optimistic state)
83
79
  # NOTE: Client state HTTP headers are only sent if/when state has changed on the client (only the changes are sent).
84
80
  # This prevents race conditions (state mismatch) caused when frame and XHR requests emit immediately
85
81
  # before the <meta id="turbo-reflex"> has been updated with the latest state from the server.
86
- begin
87
- header_state_hash = TurboReflex::State.deserialize_base64(header).with_indifferent_access
88
- header_state_hash.each { |key, val| self[key] = val }
89
- rescue => error
90
- Rails.logger.error "Failed to apply client state from HTTP headers to TurboReflex::State! #{error.message}"
82
+ if TurboReflex.config.apply_client_state_overrides
83
+ begin
84
+ header_state_hash = TurboReflex::State.deserialize_base64(header).with_indifferent_access
85
+ header_state_hash.each { |key, val| self[key] = val }
86
+ rescue => error
87
+ Rails.logger.error "Failed to apply client state from HTTP headers to TurboReflex::State! #{error.message}"
88
+ end
91
89
  end
92
90
 
93
91
  @cookie_data = cookie_state_hash
@@ -130,7 +128,7 @@ class TurboReflex::StateManager
130
128
  def ordinal_payload
131
129
  provisional_state.clear
132
130
  state.shrink!
133
- state.prune! max_bytesize: TurboReflex::StateManager.cookie_max_bytesize
131
+ state.prune! max_bytesize: TurboReflex.config.max_cookie_size
134
132
  state.ordinal_payload
135
133
  end
136
134
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TurboReflex
4
- VERSION = "0.0.25"
4
+ VERSION = "0.0.27"
5
5
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turbo_reflex",
3
- "version": "0.0.23",
3
+ "version": "0.0.26",
4
4
  "description": "Reflexes for Turbo Frames that help you build robust reactive applications",
5
5
  "main": "app/javascript/index.js",
6
6
  "repository": "https://github.com/hopsoft/turbo_reflex",