Skip to content

Commit

Permalink
fix (View): properly handle inline blocks
Browse files Browse the repository at this point in the history
Fixes #74
  • Loading branch information
vladfaust committed Apr 9, 2019
1 parent a68a134 commit 9e60b18
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
25 changes: 25 additions & 0 deletions spec/onyx-rest/view_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ class Spec::View

json foo: @foo, bar: @bar

json content_type: "application/json-a" do
builder do
field "foo", @foo
field "bar", @bar
end
end

json @foo.chars.map { |c| c.upcase.to_s }, content_type: "application/json-b"

text "foo: #{@foo}, bar: #{@bar}"

xml do
Expand Down Expand Up @@ -59,6 +68,22 @@ describe Onyx::HTTP::View do
end
end

describe "#render_to_application_json_a" do
it do
io = IO::Memory.new
view.render_to_application_json_a(io)
io.to_s.should eq %Q[{"foo":"baz","bar":42}]
end
end

describe "#render_to_application_json_b" do
it do
io = IO::Memory.new
view.render_to_application_json_b(io)
io.to_s.should eq %Q{["B","A","Z"]}
end
end

describe "#render_to_text_plain" do
it do
io = IO::Memory.new
Expand Down
20 changes: 13 additions & 7 deletions src/onyx-http/view.cr
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module Onyx::HTTP::View
# end
# ```
macro text(value, content_type = "text/plain", accept = {"text/plain"})
define_type_renderer(render_to_{{content_type.split("/").join("_").id}}, {{content_type}}, {{accept}}) do
define_type_renderer(render_to_{{content_type.split("/").map { |s| s.underscore.gsub(/-/, "_") }.join("_").id}}, {{content_type}}, {{accept}}) do
io << ({{value}})
end
end
Expand All @@ -125,7 +125,7 @@ module Onyx::HTTP::View
# end
# ```
macro template(template, content_type = "text/html", accept = {"text/html"})
define_type_renderer(render_to_{{content_type.split("/").join("_").id}}, {{content_type}}, {{accept}}) do
define_type_renderer(render_to_{{content_type.split("/").map { |s| s.underscore.gsub(/-/, "_") }.join("_").id}}, {{content_type}}, {{accept}}) do
Kilt.embed("#{__DIR__}/#{{{template}}}", io)
end
end
Expand All @@ -141,7 +141,7 @@ module Onyx::HTTP::View
# ({{object}}).to_json(builder)
# end
# ```
macro json(object, content_type = "application/json", accept = {"application/json"})
macro json(object, content_type = "application/json", accept = {"application/json"}, &block)
{% unless @type.methods.find { |m| m.name.stringify == "to_json" } %}
def to_json(io : IO)
({{object}}).to_json(io)
Expand All @@ -152,8 +152,14 @@ module Onyx::HTTP::View
end
{% end %}

define_type_renderer(render_to_{{content_type.split("/").join("_").id}}, {{content_type}}, {{accept}}) do
({{object}}).to_json(io)
define_type_renderer(render_to_{{content_type.split("/").map { |s| s.underscore.gsub(/-/, "_") }.join("_").id}}, {{content_type}}, {{accept}}) do
{% if block %}
{{object}} do |{{block.args.join(",").id}}|
{{block.body}}
end.to_json(io)
{% else %}
({{object}}).to_json(io)
{% end %}
end
end

Expand Down Expand Up @@ -197,7 +203,7 @@ module Onyx::HTTP::View
end
{% end %}

define_type_renderer(render_to_{{content_type.split("/").join("_").id}}, {{content_type}}, {{accept}}) do
define_type_renderer(render_to_{{content_type.split("/").map { |s| s.underscore.gsub(/-/, "_") }.join("_").id}}, {{content_type}}, {{accept}}) do
to_json(io)
end
end
Expand Down Expand Up @@ -251,7 +257,7 @@ module Onyx::HTTP::View
end
end

define_type_renderer(render_to_{{content_type.split("/").join("_").id}}, {{content_type}}, {{accept}}) do
define_type_renderer(render_to_{{content_type.split("/").map { |s| s.underscore.gsub(/-/, "_") }.join("_").id}}, {{content_type}}, {{accept}}) do
to_xml(io)
end
end
Expand Down

0 comments on commit 9e60b18

Please sign in to comment.