live_cable 0.3.0 → 0.5.0

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.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +739 -0
  3. data/README.md +101 -56
  4. data/app/assets/javascript/controllers/live_controller.js +61 -7
  5. data/app/assets/javascript/live_cable.js +2 -0
  6. data/app/assets/javascript/loading.js +61 -9
  7. data/app/assets/javascript/subscriptions.js +389 -42
  8. data/app/channels/live_channel.rb +51 -29
  9. data/app/helpers/live_cable_helper.rb +45 -2
  10. data/lib/live_cable/action_cable_connection.rb +4 -1
  11. data/lib/live_cable/arel_predications.rb +15 -0
  12. data/lib/live_cable/component/broadcasting.rb +47 -8
  13. data/lib/live_cable/component/dispatching.rb +37 -0
  14. data/lib/live_cable/component/events.rb +2 -1
  15. data/lib/live_cable/component/lifecycle.rb +1 -1
  16. data/lib/live_cable/component/reactive_variables.rb +33 -5
  17. data/lib/live_cable/component/rendering.rb +54 -17
  18. data/lib/live_cable/component/streaming.rb +35 -4
  19. data/lib/live_cable/component.rb +4 -3
  20. data/lib/live_cable/connection/broadcasting.rb +56 -12
  21. data/lib/live_cable/connection/component_management.rb +12 -8
  22. data/lib/live_cable/connection/error_handling.rb +28 -7
  23. data/lib/live_cable/connection/messaging.rb +54 -51
  24. data/lib/live_cable/connection/state_management.rb +13 -16
  25. data/lib/live_cable/connection.rb +33 -0
  26. data/lib/live_cable/container.rb +17 -11
  27. data/lib/live_cable/defaults_signer.rb +155 -0
  28. data/lib/live_cable/delegation/array.rb +27 -1
  29. data/lib/live_cable/delegation/hash.rb +60 -1
  30. data/lib/live_cable/delegation/methods.rb +25 -1
  31. data/lib/live_cable/delegation/model.rb +17 -0
  32. data/lib/live_cable/delegator.rb +68 -12
  33. data/lib/live_cable/dispatch.rb +54 -0
  34. data/lib/live_cable/engine.rb +8 -1
  35. data/lib/live_cable/forbidden.rb +7 -0
  36. data/lib/live_cable/identity_digest.rb +27 -0
  37. data/lib/live_cable/model_observer.rb +82 -0
  38. data/lib/live_cable/observer_tracking.rb +20 -2
  39. data/lib/live_cable/predicate_builder.rb +31 -0
  40. data/lib/live_cable/record_ids.rb +37 -0
  41. data/lib/live_cable/render_context.rb +134 -8
  42. data/lib/live_cable/rendering/controller_renderer.rb +30 -0
  43. data/lib/live_cable/rendering/dependency_visitor.rb +101 -24
  44. data/lib/live_cable/rendering/handler.rb +1 -1
  45. data/lib/live_cable/rendering/method_analyzer.rb +18 -4
  46. data/lib/live_cable/rendering/method_collector.rb +13 -5
  47. data/lib/live_cable/rendering/partial.rb +28 -12
  48. data/lib/live_cable/rendering/partial_renderer.rb +55 -38
  49. data/lib/live_cable/rendering/render_result.rb +5 -0
  50. data/lib/live_cable/rendering/renderer.rb +35 -15
  51. data/lib/live_cable/rendering/view_buffer.rb +22 -0
  52. data/lib/live_cable/testing/render_state.rb +6 -4
  53. data/lib/live_cable/testing/test_channel.rb +6 -0
  54. data/lib/live_cable/testing/test_component.rb +5 -5
  55. data/lib/live_cable/testing.rb +13 -7
  56. data/lib/live_cable/version.rb +1 -1
  57. data/lib/live_cable.rb +32 -3
  58. metadata +19 -9
@@ -30,22 +30,28 @@ module LiveCable
30
30
  result
31
31
  end
32
32
 
33
- # Returns children from the previous context that came from parts which were
34
- # skipped (not rendered) in this render cycle. These children should not be
35
- # destroyed — their part simply didn't re-evaluate.
33
+ # Carried children stay out of #children, which broadcast_changeset reads as
34
+ # "rendered this cycle": a child there gets no render of its own that cycle.
36
35
  #
37
- # @param previous_context [RenderContext]
36
+ # @param previous [RenderContext]
37
+ def inherit_skipped(previous)
38
+ @children_by_part.reverse_merge!(previous.children_by_part)
39
+ end
40
+
38
41
  # @return [Array<LiveCable::Component>]
39
- def preserved_children_from(previous_context)
40
- previous_context.children_by_part.each_with_object([]) do |(part, part_children), preserved|
41
- preserved.concat(part_children) unless @children_by_part.key?(part)
42
- end
42
+ def owned_children
43
+ children_by_part.values.flatten.uniq(&:object_id)
43
44
  end
44
45
 
45
46
  def root?
46
47
  root.nil?
47
48
  end
48
49
 
50
+ # @return [LiveCable::Component] the component whose frame carries this render
51
+ def root_component
52
+ root? ? component : root.component
53
+ end
54
+
49
55
  def clear
50
56
  @children = nil
51
57
  @component = nil
@@ -78,9 +84,129 @@ module LiveCable
78
84
  component.live_connection
79
85
  end
80
86
 
87
+ # @param delegator [LiveCable::Delegator]
88
+ # @return [Object] the value the template sees
89
+ def unwrap(delegator)
90
+ value = delegator.__getobj__
91
+ locations = (tracked_values[value] ||= [])
92
+
93
+ # `todos.first` builds a new wrapper on every call; one per set of observers is enough
94
+ unless locations.any? { |root, _path| root.same_live_cable_observers?(delegator) }
95
+ locations << [delegator, []]
96
+ unindexed << delegator
97
+ end
98
+
99
+ value
100
+ end
101
+
102
+ # @param value [Array, Hash] a plain collection a component method returned
103
+ # @return [Array, Hash] the collection with no Delegator left in it
104
+ def unwrap_nested(value)
105
+ unwrapped_nested[value] ||= Delegator.unwrap(value) { |delegator| unwrap(delegator) }
106
+ end
107
+
108
+ # Maps a prop back to tracked values, so a child's changes still mark this
109
+ # component's variables, even through a collection built in the template.
110
+ def track(value)
111
+ return value unless Delegator.supported?(value)
112
+
113
+ while (delegator = unindexed.shift)
114
+ index_nested(delegator, delegator.__getobj__, [])
115
+ end
116
+
117
+ locations = tracked_values[value]
118
+
119
+ # A child given a variable's own value shares its wrapper, so changes on either side re-render both
120
+ stored, *aliases = stored_wrappers[value]
121
+ if stored
122
+ (aliases + locations.to_a.map(&:first)).each { |source| source.share_live_cable_observers_with(stored) }
123
+ return stored
124
+ end
125
+
126
+ if locations&.one?
127
+ delegator, path = locations.first
128
+ return path.reduce(delegator) { |nested, key| nested[key] }
129
+ end
130
+
131
+ sources = locations ? locations.map(&:first) : sources_within(value).keys
132
+ return value if sources.empty?
133
+
134
+ Delegator.new(value).tap do |wrapped|
135
+ sources.each { |source| source.share_live_cable_observers_with(wrapped) }
136
+ end
137
+ end
138
+
139
+ # A finished context is kept until the next render; drop what it unwrapped.
140
+ def forget_tracked_values
141
+ @tracked_values = @unindexed = @unwrapped_nested = @stored_wrappers = nil
142
+ end
143
+
81
144
  private
82
145
 
83
146
  # @return [LiveCable::RenderContext, nil]
84
147
  attr_reader :root
148
+
149
+ # @return [Hash{Object => Array<Array(LiveCable::Delegator, Array)>}] by identity
150
+ def tracked_values
151
+ @tracked_values ||= {}.compare_by_identity
152
+ end
153
+
154
+ # @return [Array<LiveCable::Delegator>]
155
+ def unindexed
156
+ @unindexed ||= []
157
+ end
158
+
159
+ # @return [Hash{Object => Array<LiveCable::Delegator>}] by identity, each variable's value to its wrappers
160
+ def stored_wrappers
161
+ @stored_wrappers ||= begin
162
+ variables = component.all_reactive_variables | component.shared_variables
163
+
164
+ variables.each_with_object({}.compare_by_identity) do |variable, wrappers|
165
+ value = component.public_send(variable)
166
+ (wrappers[value.__getobj__] ||= []) << value if value.is_a?(Delegator)
167
+ end
168
+ end
169
+ end
170
+
171
+ # @return [Hash{Object => Object}] by identity
172
+ def unwrapped_nested
173
+ @unwrapped_nested ||= {}.compare_by_identity
174
+ end
175
+
176
+ def index_nested(delegator, value, path, seen = {}.compare_by_identity)
177
+ nested_entries(value).each do |key, child|
178
+ next if !Delegator.supported?(child) || seen.key?(child)
179
+
180
+ seen[child] = true
181
+ (tracked_values[child] ||= []) << [delegator, path + [key]]
182
+ index_nested(delegator, child, path + [key], seen)
183
+ end
184
+ end
185
+
186
+ # @return [Hash{LiveCable::Delegator => true}] by identity
187
+ def sources_within(value, sources = {}.compare_by_identity, seen = {}.compare_by_identity)
188
+ nested_entries(value).map(&:last).each do |child|
189
+ next if !Delegator.supported?(child) || seen.key?(child)
190
+
191
+ seen[child] = true
192
+ locations = tracked_values[child]
193
+
194
+ if locations
195
+ locations.each { |location| sources[location.first] = true }
196
+ else
197
+ sources_within(child, sources, seen)
198
+ end
199
+ end
200
+
201
+ sources
202
+ end
203
+
204
+ def nested_entries(value)
205
+ case value
206
+ when Hash then value.each_pair
207
+ when Array then value.each_with_index.map { |child, index| [index, child] }
208
+ else []
209
+ end
210
+ end
85
211
  end
86
212
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveCable
4
+ module Rendering
5
+ # Builds controllers as ActionController::Renderer#render does, but on one
6
+ # request and response it keeps, so a connection builds those only once.
7
+ class ControllerRenderer < ActionController::Renderer
8
+ # @param controller_class [Class]
9
+ # @return [ControllerRenderer]
10
+ def self.from_defaults(controller_class)
11
+ self.for(controller_class, nil, controller_class.renderer.defaults)
12
+ end
13
+
14
+ def initialize(...)
15
+ super
16
+ @request = ActionDispatch::Request.new(env_for_request)
17
+ @request.routes = controller._routes
18
+ @response = controller.make_response!(@request)
19
+ end
20
+
21
+ # @return [ActionController::Base]
22
+ def build_controller
23
+ controller.new.tap do |instance|
24
+ instance.set_request!(@request)
25
+ instance.set_response!(@response)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,45 +1,86 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'prism'
4
+
3
5
  module LiveCable
4
6
  module Rendering
5
7
  class DependencyVisitor < Prism::Visitor
8
+ # @return [Array<Symbol>]
9
+ attr_reader :variable_calls
10
+
6
11
  # @return [Array<Symbol>]
7
12
  attr_reader :local_reads
8
13
 
9
14
  # @return [Array<Symbol>]
10
15
  attr_reader :local_writes
11
16
 
17
+ # Reads of top-level locals the part hasn't surely assigned before them
18
+ # @return [Array<Symbol>]
19
+ attr_reader :exposed_local_reads
20
+
12
21
  # @return [Set<Symbol>]
13
22
  attr_reader :component_method_calls
14
23
 
15
24
  def initialize
16
25
  super
26
+ @variable_calls = []
17
27
  @local_reads = []
18
28
  @local_writes = []
29
+ @exposed_local_reads = []
30
+ @assigned = []
19
31
  @component_method_calls = Set.new
32
+ @scope_depth = 0
33
+ end
34
+
35
+ def visit_block_node(node) = nested_scope { super }
36
+
37
+ def visit_lambda_node(node) = nested_scope { super }
38
+
39
+ # Prism never resolves a name past a def, so nothing inside one counts as top level
40
+ def visit_def_node(node) = nested_scope { super }
41
+
42
+ # An assignment only covers the reads after it in its own statement list
43
+ def visit_statements_node(node)
44
+ assigned = @assigned
45
+ node.body.each do |statement|
46
+ visit(statement)
47
+ @assigned |= [statement.name] if statement.is_a?(Prism::LocalVariableWriteNode) && top_level?(statement)
48
+ end
49
+ ensure
50
+ @assigned = assigned
20
51
  end
21
52
 
22
- # Track all local variable reads
53
+ # Ruby evaluates the collection first, then assigns the index before each pass of the body
54
+ def visit_for_node(node)
55
+ assigned = @assigned
56
+ visit(node.collection)
57
+ visit(node.index)
58
+ @assigned |= [node.index.name] if node.index.is_a?(Prism::LocalVariableTargetNode) && top_level?(node.index)
59
+ visit(node.statements) if node.statements
60
+ ensure
61
+ @assigned = assigned
62
+ end
63
+
64
+ # Track reads of top-level locals
23
65
  # @param node [Prism::LocalVariableReadNode]
24
66
  # @return [void]
25
67
  def visit_local_variable_read_node(node)
26
- @local_reads |= [node.name]
68
+ read(node) if top_level?(node)
27
69
  super
28
70
  end
29
71
 
30
- # Track variable method calls (e.g., `foo` without parens)
31
- # Also track component.method_name calls
72
+ # Track argument-less receiverless calls (e.g., `foo`, `foo()` or the value
73
+ # of a `foo:` hash shorthand) and calls on component or self (e.g.,
74
+ # `component.foo`), which read the component too
32
75
  # @param node [Prism::CallNode]
33
76
  # @return [void]
34
77
  def visit_call_node(node)
35
- # Track variable calls (implicit self)
36
- @local_reads |= [node.name] if node.variable_call?
37
-
38
- # Track component.method_name calls
39
- if component_receiver?(node.receiver)
40
- @component_method_calls << node.name
78
+ if node.variable_call? || bare_call?(node) || explicit_component_receiver?(node.receiver)
79
+ @variable_calls |= [node.name]
41
80
  end
42
81
 
82
+ @component_method_calls << node.name if component_receiver?(node.receiver)
83
+
43
84
  super
44
85
  end
45
86
 
@@ -47,7 +88,15 @@ module LiveCable
47
88
  # @param node [Prism::LocalVariableWriteNode]
48
89
  # @return [void]
49
90
  def visit_local_variable_write_node(node)
50
- @local_writes |= [node.name]
91
+ @local_writes |= [node.name] if top_level?(node)
92
+ super
93
+ end
94
+
95
+ # Track multiple assignment, rescue and pattern targets
96
+ # @param node [Prism::LocalVariableTargetNode]
97
+ # @return [void]
98
+ def visit_local_variable_target_node(node)
99
+ @local_writes |= [node.name] if top_level?(node)
51
100
  super
52
101
  end
53
102
 
@@ -55,8 +104,7 @@ module LiveCable
55
104
  # @param node [Prism::LocalVariableOperatorWriteNode]
56
105
  # @return [void]
57
106
  def visit_local_variable_operator_write_node(node)
58
- @local_writes |= [node.name]
59
- @local_reads |= [node.name] # Reads before writing
107
+ read_and_write(node)
60
108
  super
61
109
  end
62
110
 
@@ -64,8 +112,7 @@ module LiveCable
64
112
  # @param node [Prism::LocalVariableAndWriteNode]
65
113
  # @return [void]
66
114
  def visit_local_variable_and_write_node(node)
67
- @local_writes |= [node.name]
68
- @local_reads |= [node.name]
115
+ read_and_write(node)
69
116
  super
70
117
  end
71
118
 
@@ -73,24 +120,54 @@ module LiveCable
73
120
  # @param node [Prism::LocalVariableOrWriteNode]
74
121
  # @return [void]
75
122
  def visit_local_variable_or_write_node(node)
76
- @local_writes |= [node.name]
77
- @local_reads |= [node.name]
123
+ read_and_write(node)
78
124
  super
79
125
  end
80
126
 
81
127
  private
82
128
 
83
- # Check if receiver is a reference to 'component'
129
+ def nested_scope
130
+ @scope_depth += 1
131
+ yield
132
+ ensure
133
+ @scope_depth -= 1
134
+ end
135
+
136
+ # Prism's depth is how many scopes up it found the local
137
+ def top_level?(node)
138
+ node.depth == @scope_depth
139
+ end
140
+
141
+ def read(node)
142
+ @local_reads |= [node.name]
143
+ @exposed_local_reads |= [node.name] unless @assigned.include?(node.name)
144
+ end
145
+
146
+ def read_and_write(node)
147
+ return unless top_level?(node)
148
+
149
+ read(node)
150
+ @local_writes |= [node.name]
151
+ end
152
+
153
+ # @param node [Prism::CallNode]
154
+ # @return [Boolean]
155
+ def bare_call?(node)
156
+ node.receiver.nil? && node.arguments.nil? && node.block.nil?
157
+ end
158
+
84
159
  # @param receiver [Prism::Node, nil]
85
160
  # @return [Boolean]
86
161
  def component_receiver?(receiver)
87
- # No receiver, calling something lke live_id or any component method
88
- return true if receiver.nil?
89
-
90
- # Ignore these because they're not component methods, just output methods
91
- return false if receiver.try(:name) == :@output_buffer
162
+ receiver.nil? || explicit_component_receiver?(receiver)
163
+ end
92
164
 
93
- # If there is a receiver, it must be a component method call or local variable read
165
+ # Part code runs on the renderer, which hands unknown names to the component,
166
+ # so `self.foo` reads the component just as `component.foo` does
167
+ # @param receiver [Prism::Node, nil]
168
+ # @return [Boolean]
169
+ def explicit_component_receiver?(receiver)
170
+ return true if receiver.is_a?(Prism::SelfNode)
94
171
  return false unless receiver.try(:name) == :component
95
172
 
96
173
  receiver.is_a?(Prism::CallNode) || receiver.is_a?(Prism::LocalVariableReadNode)
@@ -7,7 +7,7 @@ module LiveCable
7
7
  parse_result = ::Herb.parse(source, track_whitespace: true)
8
8
  ast = parse_result.value
9
9
 
10
- engine = Renderer.new
10
+ engine = Renderer.new(template.short_identifier, template.virtual_path)
11
11
  compiler = Compiler.new(engine)
12
12
  ast.accept(compiler)
13
13
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'prism'
4
+
3
5
  module LiveCable
4
6
  module Rendering
5
7
  # Analyzes component methods to track their dependencies on reactive variables
@@ -19,11 +21,15 @@ module LiveCable
19
21
  def analyze_all_methods
20
22
  return dependencies if analyzed
21
23
 
22
- # Get the source file for this component
23
- source_location = Object.const_source_location(component_class.name)
24
+ # Classes built with Class.new have no source file to analyze; see analyzable?
25
+ class_name = component_class.name
26
+ file_path = class_name && Object.const_source_location(class_name)&.first
24
27
 
25
- file_path = source_location[0]
26
- return {} unless File.exist?(file_path)
28
+ unless file_path && File.exist?(file_path)
29
+ @analyzed = true
30
+ @analyzable = false
31
+ return dependencies
32
+ end
27
33
 
28
34
  # Parse the entire file once
29
35
  source_code = File.read(file_path)
@@ -35,9 +41,17 @@ module LiveCable
35
41
 
36
42
  @dependencies = collector.dependencies
37
43
  @analyzed = true
44
+ @analyzable = true
38
45
  dependencies
39
46
  end
40
47
 
48
+ # @return [Boolean] false when the class has no source file, so method
49
+ # dependencies are unknown and callers must assume any change matters
50
+ def analyzable?
51
+ analyze_all_methods unless analyzed
52
+ @analyzable
53
+ end
54
+
41
55
  # Get dependencies for a specific method (analyzes all if not done yet)
42
56
  # @param method_name [Symbol] The method name
43
57
  # @return [Hash, nil] { methods: Set, reactive_vars: Set }
@@ -15,7 +15,7 @@ module LiveCable
15
15
 
16
16
  @component_class = component_class
17
17
  @dependencies = {}
18
- @reactive_vars = component_class.all_reactive_variables
18
+ @reactive_vars = component_class.all_reactive_variables | component_class.shared_variables
19
19
  end
20
20
 
21
21
  # Visit each method definition
@@ -24,6 +24,8 @@ module LiveCable
24
24
  def visit_def_node(node)
25
25
  method_name = node.name
26
26
 
27
+ return if node.receiver
28
+
27
29
  # Skip private/internal methods
28
30
  return super if %i[render render_in].include?(method_name)
29
31
 
@@ -31,14 +33,20 @@ module LiveCable
31
33
  visitor = MethodDependencyVisitor.new(component_class, reactive_vars)
32
34
  visitor.visit(node)
33
35
 
34
- @dependencies[method_name] = {
35
- methods: visitor.method_calls,
36
- reactive_vars: visitor.reactive_var_reads,
37
- }
36
+ # Nested and sibling classes in the same file share this table, so a
37
+ # clashing name may only add dependencies, never replace them.
38
+ entry = @dependencies[method_name] ||= { methods: Set.new, reactive_vars: Set.new }
39
+ entry[:methods].merge(visitor.method_calls)
40
+ entry[:reactive_vars].merge(visitor.reactive_var_reads)
38
41
 
39
42
  super
40
43
  end
41
44
 
45
+ # Methods in `class << self` are never instance methods a template calls
46
+ # @param node [Prism::SingletonClassNode]
47
+ # @return [void]
48
+ def visit_singleton_class_node(node); end
49
+
42
50
  private
43
51
 
44
52
  # @return [Class]
@@ -5,15 +5,25 @@ require_relative 'partial_renderer'
5
5
  module LiveCable
6
6
  module Rendering
7
7
  class Partial
8
- def initialize(parts, metadata)
8
+ def initialize(parts, metadata, identifier, virtual_path = nil)
9
9
  @parts = parts
10
10
  @metadata = metadata
11
+ @identifier = identifier
12
+ @virtual_path = virtual_path
11
13
  @renderer_class = build_renderer_class
12
14
  end
13
15
 
14
16
  # @return [PartialRenderer]
15
17
  def for_component(component, view_context)
16
- renderer_class.new(component, parts, view_context)
18
+ renderer_class.new(component, parts, view_context, virtual_path)
19
+ end
20
+
21
+ # ActionView calls this when a template is rendered as a partial or a plain
22
+ # template; only Component#render_in can render a Partial.
23
+ def to_s
24
+ raise LiveCable::Error,
25
+ "#{identifier} is a component template and can only be rendered by its component. " \
26
+ 'Render the component with live(...) or render(component), or make the partial a .html.erb template.'
17
27
  end
18
28
 
19
29
  private
@@ -24,6 +34,12 @@ module LiveCable
24
34
  # @return [Array]
25
35
  attr_reader :metadata
26
36
 
37
+ # @return [String]
38
+ attr_reader :identifier
39
+
40
+ # @return [String, nil]
41
+ attr_reader :virtual_path
42
+
27
43
  # @return [Class<PartialRenderer>]
28
44
  attr_reader :renderer_class
29
45
 
@@ -46,31 +62,31 @@ module LiveCable
46
62
  component_dependencies = part_metadata[:component_dependencies]
47
63
  component_method_calls = part_metadata[:component_method_calls] || []
48
64
  local_dependencies = part_metadata[:local_dependencies]
65
+ exposed_local_reads = part_metadata[:exposed_local_reads]
49
66
  defines_locals = part_metadata[:defines_locals]
50
67
  local_check_code = part_metadata[:local_check_code]
51
68
 
52
- # Code blocks always execute (they define locals that other parts need)
53
- # Expression blocks can be skipped if dependencies haven't changed
54
- skip_check = if type == :code
69
+ # A part whose top-level local a later part reads always runs
70
+ skip_check = if type == :code || part_metadata[:feeds_later_parts]
55
71
  ''
56
72
  else
57
73
  <<~SKIP_CHECK
58
74
  return nil if should_skip_part?(
59
- changes,
75
+ __live_changes,
60
76
  #{component_dependencies.inspect},
61
77
  #{component_method_calls.inspect},
62
- #{local_dependencies.inspect}
78
+ #{exposed_local_reads.inspect}
63
79
  )
64
80
  SKIP_CHECK
65
81
  end
66
82
 
67
- # Initialize local variables from previous parts so that operator
68
- # assignments (||=, &&=, +=) work correctly. Without this, Ruby
69
- # treats them as fresh nil locals instead of resolving via method_missing.
70
- local_init_code = local_dependencies.map { |dep| "#{dep} = locals[:#{dep}]" }.join("\n")
83
+ # Earlier parts' locals become Ruby locals in this part's method, as
84
+ # they would be in one ERB method body.
85
+ local_init_code = local_dependencies.map { |dep| "#{dep} = @locals[:#{dep}]" }.join("\n")
71
86
 
87
+ # Part code runs inside this method, so its parameter must not shadow a name the template uses.
72
88
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
73
- def render_part_#{index}(changes)
89
+ def render_part_#{index}(__live_changes)
74
90
  #{skip_check}
75
91
  # Mark locals defined by this part as dirty
76
92
  mark_locals_dirty(#{defines_locals.inspect})