-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from dry-rb/decorated-attributes-in-context
Add base Context class with support for decorated attributes
- Loading branch information
Showing
9 changed files
with
236 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Dry | ||
module View | ||
class Context | ||
attr_reader :_options, :_part_builder, :_renderer | ||
|
||
def initialize(part_builder: nil, renderer: nil, **options) | ||
@_part_builder = part_builder | ||
@_renderer = renderer | ||
@_options = options | ||
end | ||
|
||
def bind(part_builder:, renderer:) | ||
self.class.new( | ||
**_options.merge( | ||
part_builder: part_builder, | ||
renderer: renderer, | ||
) | ||
) | ||
end | ||
|
||
def bound? | ||
!!(_part_builder && _renderer) | ||
end | ||
|
||
def self.decorate(*names, **options) | ||
mod = DecoratedAttributes.new(names) do | ||
names.each do |name| | ||
define_method name do | ||
attribute = super() | ||
|
||
return attribute unless bound? || !attribute | ||
|
||
_part_builder.( | ||
name: name, | ||
value: attribute, | ||
renderer: _renderer, | ||
context: self, | ||
**options, | ||
) | ||
end | ||
end | ||
end | ||
|
||
prepend mod | ||
end | ||
|
||
class DecoratedAttributes < Module | ||
def initialize(names, &block) | ||
@names = names | ||
super(&block) | ||
end | ||
|
||
def inspect | ||
%(#<#{self.class.name}#{@names.inspect}>) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
spec/fixtures/integration/context/decorated_attributes.html.slim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
== assets.image_tag("hello.png") | ||
.user | ||
== user.image_tag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require "dry/view/context" | ||
require "dry/view/controller" | ||
require "dry/view/part" | ||
|
||
RSpec.describe "Context" do | ||
it "Provides decorated attributes for use in templates and parts" do | ||
module Test | ||
class Assets | ||
def [](path) | ||
"hashed/path/to/#{path}" | ||
end | ||
end | ||
|
||
class Context < Dry::View::Context | ||
attr_reader :assets | ||
decorate :assets | ||
|
||
def initialize(assets:, **options) | ||
@assets = assets | ||
super | ||
end | ||
end | ||
|
||
module Parts | ||
class Assets < Dry::View::Part | ||
def image_tag(path) | ||
<<~HTML | ||
<img src="#{value[path]}"> | ||
HTML | ||
end | ||
end | ||
|
||
class User < Dry::View::Part | ||
def image_tag | ||
value[:image_url] || context.assets.image_tag("default.png") | ||
end | ||
end | ||
end | ||
end | ||
|
||
vc = Class.new(Dry::View::Controller) do | ||
configure do |config| | ||
config.paths = FIXTURES_PATH.join("integration/context") | ||
config.template = "decorated_attributes" | ||
config.part_namespace = Test::Parts | ||
end | ||
|
||
expose :user | ||
end.new | ||
|
||
context = Test::Context.new(assets: Test::Assets.new) | ||
|
||
output = vc.( | ||
user: {image_url: nil}, | ||
context: context, | ||
).to_s | ||
|
||
expect(output.gsub("\n", "")).to eq <<~HTML.gsub("\n", "") | ||
<img src="hashed/path/to/hello.png"> | ||
<div class="user"> | ||
<img src="hashed/path/to/default.png"> | ||
</div> | ||
HTML | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require "dry/view/context" | ||
require "dry/view/part" | ||
require "dry/view/part_builder" | ||
|
||
RSpec.describe Dry::View::Context do | ||
let(:context_class) { | ||
Class.new(Dry::View::Context) do | ||
attr_reader :assets | ||
|
||
decorate :assets, :invalid_attribute | ||
|
||
def initialize(assets:, **options) | ||
@assets = assets | ||
super | ||
end | ||
end | ||
} | ||
|
||
let(:assets) { double(:assets) } | ||
let(:renderer) { double(:renderer) } | ||
let(:part_builder) { Dry::View::PartBuilder.new } | ||
|
||
it "provides a helpful #inspect on the generated decorated attributes module" do | ||
expect(context_class.ancestors[0].inspect).to eq "#<Dry::View::Context::DecoratedAttributes[:assets, :invalid_attribute]>" | ||
end | ||
|
||
context "unbound" do | ||
subject(:context) { context_class.new(assets: assets) } | ||
|
||
it { is_expected.not_to be_bound } | ||
|
||
it "returns its attributes" do | ||
expect(context.assets).to eql assets | ||
end | ||
|
||
it "raises NoMethodError when an invalid attribute is decorated" do | ||
expect { context.invalid_attribute }.to raise_error(NoMethodError) | ||
end | ||
end | ||
|
||
context "bound" do | ||
subject(:context) { | ||
context_class.new(assets: assets). | ||
bind(renderer: renderer, part_builder: part_builder) | ||
} | ||
|
||
it { is_expected.to be_bound } | ||
|
||
it "returns its assets decorated in view parts" do | ||
expect(context.assets).to be_a Dry::View::Part | ||
expect(context.assets.value).to eql assets | ||
end | ||
|
||
it "raises NoMethodError when an invalid attribute is decorated" do | ||
expect { context.invalid_attribute }.to raise_error(NoMethodError) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters