Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove view_context #643

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,16 @@ def await(task)
end

# Renders the view and returns the buffer. The default buffer is a mutable String.
def call(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: nil, &block)
__final_call__(buffer, context: context, view_context: view_context, parent: parent, &block).tap do
def call(...)
__final_call__(...).tap do
self.class.rendered_at_least_once!
end
end

# @api private
def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: nil, &block)
def __final_call__(buffer = +"", context: Phlex::Context.new, parent: nil, &block)
@_buffer = buffer
@_context = context
@_view_context = view_context
@_parent = parent

block ||= @_content_block
Expand Down Expand Up @@ -223,10 +222,10 @@ def flush
def render(renderable, &block)
case renderable
when Phlex::SGML
renderable.call(@_buffer, context: @_context, view_context: @_view_context, parent: self, &block)
renderable.call(@_buffer, context: @_context, parent: self, &block)
when Class
if renderable < Phlex::SGML
renderable.new.call(@_buffer, context: @_context, view_context: @_view_context, parent: self, &block)
renderable.new.call(@_buffer, context: @_context, parent: self, &block)
end
when Enumerable
renderable.each { |r| render(r, &block) }
Expand Down
6 changes: 1 addition & 5 deletions lib/phlex/testing/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ def render(view, &block)
view = view.new
end

view.call(view_context: view_context, &block)
end

def view_context
nil
view.call(&block)
end
end
end
12 changes: 0 additions & 12 deletions test/phlex/view/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ def view_template
end
end

with "`render?` method returning false when the view context is true" do
view do
def view_template
plain "Hi"
end

def render?
!@_view_context
end
end
end

with "a view that yields an object" do
view do
def view_template
Expand Down
17 changes: 13 additions & 4 deletions test/phlex/view/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,29 @@ def view_template
view do
def view_template
h1 { "Before" }
render @_view_context.previewer do
render @_view_context.component
render @_context.view_context.previewer do
render @_context.view_context.component
end
h1 { "After" }
end
end

let(:context) do
Phlex::Context.new.tap do |context|
class << context
attr_accessor :view_context
end
context.view_context = self
end
end

it "should contain the full capture" do
expect(example.call(view_context: self)).to be == %(<h1>Before</h1><iframe srcdoc="&lt;h1&gt;Hello&lt;/h1&gt;&lt;h1&gt;World&lt;/h1&gt;"></iframe><h1>After</h1>)
expect(example.call(context: context)).to be == %(<h1>Before</h1><iframe srcdoc="&lt;h1&gt;Hello&lt;/h1&gt;&lt;h1&gt;World&lt;/h1&gt;"></iframe><h1>After</h1>)
end

it "should contain the full capture if the buffer is provided" do
my_buffer = +""
example.call(my_buffer, view_context: self)
example.call(my_buffer, context: context)
expect(my_buffer).to be == %(<h1>Before</h1><iframe srcdoc="&lt;h1&gt;Hello&lt;/h1&gt;&lt;h1&gt;World&lt;/h1&gt;"></iframe><h1>After</h1>)
end
end
Expand Down