Skip to content

Commit

Permalink
Fix: Add support for rendering multiple scopes by passing string as n…
Browse files Browse the repository at this point in the history
…ames

Resolves: hanami#252
  • Loading branch information
swilgosz committed Dec 7, 2023
1 parent 595ce78 commit 200f91b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/hanami/view/scope_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def scope_class(name = nil, rendering:)
elsif name.is_a?(Class)
name
else
View.cache.fetch_or_store(:scope_class, rendering.config) do
View.cache.fetch_or_store("scope_class: #{name}", rendering.config) do
resolve_scope_class(name: name, rendering: rendering)
end
end
Expand All @@ -40,7 +40,7 @@ def scope_class(name = nil, rendering:)
def resolve_scope_class(name:, rendering:)
name = rendering.inflector.camelize(name.to_s)

namespace = rendering.config.scope_namespace
namespace = rendering.config.scope_namespace || Object

# Give autoloaders a chance to act
begin
Expand Down
26 changes: 26 additions & 0 deletions spec/integration/scope_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,31 @@

expect(scope).to be_an_instance_of scope_class
end

context 'when multiple scopes are rendered in the same view' do

let(:builder) { described_class.new }

it 'allows to build scopes with different classes' do
FirstScopeClass = Class.new(Hanami::View::Scope)
SecondScopeClass = Class.new(Hanami::View::Scope)

view = Class.new(Hanami::View) do
config.paths = SPEC_ROOT.join("__ignore__")
config.template = "__ignore__"

config.scope_class = FirstScopeClass
end.new

scope = view.rendering.scope({})
expect(scope).to be_an_instance_of FirstScopeClass

scope = view.rendering.scope('SecondScopeClass', {})
expect(scope).to be_an_instance_of SecondScopeClass

scope = view.rendering.scope('FirstScopeClass', {})
expect(scope).to be_an_instance_of FirstScopeClass
end
end
end
end

0 comments on commit 200f91b

Please sign in to comment.