Skip to content
Merged
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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ require "css"
class ArticlesPage < Crumble::Page
layout ToHtml::Layout

before do
# Return `true` to continue, `false` for 400, or an Int32 HTTP status code.
ctx.request.headers["X-Auth"]? == "1" ? true : 401
end

view do
css_class ArticleListBox

Expand Down Expand Up @@ -79,6 +84,31 @@ end

- Pass a class to `view(SomeView)` if you prefer a reusable component.
- `layout SomeLayout` can reference an existing layout class, which is just something with a `#to_html(io : IO)` method that yields; when omitted, the view renders bare.
- Use `before { ... }` to short-circuit with `false` (400) or an `Int32` status code.

#### Path matching

Pages can declare URL parameters and nested segments with path-matching macros:

```crystal
class AccountPostDetailsPage < Crumble::Page
root_path "/accounts"
path_param account_id
path_param slug, /[a-z0-9-]+/
nested_path "posts"
nested_path "details"

view do
template do
page = ctx.handler.as(AccountPostDetailsPage)
p { "account_id=#{page.account_id} slug=#{page.slug}" }
end
end
end

AccountPostDetailsPage.uri_path(account_id: 123, slug: "hello-world")
# => /accounts/123/hello-world/posts/details
```

### Resources

Expand Down
56 changes: 56 additions & 0 deletions spec/page/before_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "../spec_helper"

module Crumble::Page::BeforeSpec
class Parent < Crumble::Page
before do
ctx.request.headers["X-OK"]? == "1"
end
end

class DeniedPage < Parent
before do
true
end

before do
403
end
end

class AllowedPage < Parent
view do
template do
p { "Success!" }
end
end
end

describe "DeniedPage" do
it "halts when parent before returns false" do
ctx = Crumble::Server::TestRequestContext.new(resource: DeniedPage.uri_path)
DeniedPage.handle(ctx).should eq(true)
ctx.response.status_code.should eq(400)
end

it "halts with status code when a before returns an Int32" do
headers = HTTP::Headers{"X-OK" => "1"}
ctx = Crumble::Server::TestRequestContext.new(resource: DeniedPage.uri_path, headers: headers)
DeniedPage.handle(ctx).should eq(true)
ctx.response.status_code.should eq(403)
end
end

describe "AllowedPage" do
it "renders when before returns true" do
res = String.build do |io|
headers = HTTP::Headers{"X-OK" => "1"}
ctx = Crumble::Server::TestRequestContext.new(response_io: io, resource: AllowedPage.uri_path, headers: headers)
AllowedPage.handle(ctx).should eq(true)
ctx.response.status_code.should eq(200)
ctx.response.flush
end

res.should contain("Success!")
end
end
end
25 changes: 25 additions & 0 deletions src/page/page.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ abstract class Crumble::Page
"Page"
end

macro before(&blk)
def _before : Bool | Int32
{% if @type.has_method?("_before") %}
{% if @type.methods.map(&.name).includes?("_before") %}
prev = previous_def
{% else %}
prev = super
{% end %}
return prev unless prev == true
{% end %}

{{blk.body}}
end
end

macro view(klass = nil, &blk)
{% raise "Pass a view class or a block, not both" if klass && blk %}
{% unless klass || blk %}{% raise "Provide a view class or block" %}{% end %}
Expand Down Expand Up @@ -66,6 +81,16 @@ abstract class Crumble::Page
return false unless ctx.request.method == "GET"

instance = new(ctx)
if instance.responds_to? :_before
ret_val = instance._before
if ret_val == false
ctx.response.status = :bad_request
return true
elsif ret_val.is_a?(Int32)
ctx.response.status_code = ret_val
return true
end
end
instance.call
true
end
Expand Down