diff --git a/spec/lucky/action_spec.cr b/spec/lucky/action_spec.cr index ee82fdcfd..0ada48740 100644 --- a/spec/lucky/action_spec.cr +++ b/spec/lucky/action_spec.cr @@ -223,6 +223,17 @@ class Tests::ComponentActionWithCustomContentType < TestAction end end +class Tests::MultiConditionWithEarlyReturn < TestAction + get "/tests/multi_condition_with_early_return" do + data = {check: 1} + if data + return json(data) + end + + raw_json("{}") + end +end + describe Lucky::Action do it "has a url helper" do Lucky::RouteHelper.temp_config(base_uri: "example.com") do @@ -231,6 +242,11 @@ describe Lucky::Action do end end + it "allows for early returns" do + response = Tests::MultiConditionWithEarlyReturn.new(build_context, params).call + response.body.to_s.should eq "{\"check\":1}" + end + describe ".url_without_query_params" do it "returns url without declared non-nil query params" do Lucky::RouteHelper.temp_config(base_uri: "example.com") do diff --git a/src/lucky/routable.cr b/src/lucky/routable.cr index 728a0d142..ca53edb72 100644 --- a/src/lucky/routable.cr +++ b/src/lucky/routable.cr @@ -87,6 +87,12 @@ module Lucky::Routable # :nodoc: macro setup_call_method(body) + # Return a response with `html`, `redirect`, or `json` at the end of your action. + # Ensure all conditionals (like if/else) return a response with html, redirect, json, etc. + private def action_call_body : Lucky::Response + {{ body }} + end + def call # Ensure clients_desired_format is cached by calling it clients_desired_format @@ -96,7 +102,7 @@ module Lucky::Routable %response = if %pipe_result.is_a?(Lucky::Response) %pipe_result else - {{ body }} + action_call_body end %pipe_result = run_after_pipes