Skip to content

Commit

Permalink
Updates for the lucky 0.17 release (#98)
Browse files Browse the repository at this point in the history
* Updates for the lucky 0.17 release

* remove mention of using memoize all over for now. We will add it in later.
  • Loading branch information
jwoertink authored and paulcsmith committed Aug 14, 2019
1 parent 11815b0 commit 97f575c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/actions/guides/getting-started/concepts.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ class Guides::GettingStarted::Concepts < GuideAction
The classes used by [Carbon](#{Guides::Emails::SendingEmailsWithCarbon.path}) along with the email templates are placed in here.
### `forms` folder
### `operations` folder
Forms for saving database records or interacting with HTTP forms.
Operations for saving database records or interacting with HTTP forms.
### `js` folder
Expand Down
4 changes: 2 additions & 2 deletions src/actions/guides/http_and_routing/before_after_actions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Guides::HttpAndRouting::BeforeAfterActions < GuideAction
before require_admin
get "/admin/users" do
text "List of users"
plain_text "List of users"
end
private def require_admin
Expand Down Expand Up @@ -70,7 +70,7 @@ class Guides::HttpAndRouting::BeforeAfterActions < GuideAction
include LogRequest
get "/dashboard" do
text "The dashboard"
plain_text "The dashboard"
end
end
```
Expand Down
2 changes: 1 addition & 1 deletion src/actions/guides/http_and_routing/link_generation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Guides::HttpAndRouting::LinkGeneration < GuideAction
# Normally you would use `nested_route`
# We'll use `get` here to make the example more clear
get "projects/:project_id/users" do
text "Users"
plain_text "Users"
end
end
```
Expand Down
9 changes: 5 additions & 4 deletions src/actions/guides/http_and_routing/request_and_response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class Guides::HttpAndRouting::RequestAndResponse < GuideAction
route do
remote_ip = headers["X-Forwarded-For"]?
if remote_ip
text "The remote IP is \#{remote_ip}"
plain_text "The remote IP is \#{remote_ip}"
else
text "No remote IP found"
plain_text "No remote IP found"
end
end
end
Expand All @@ -62,8 +62,9 @@ class Guides::HttpAndRouting::RequestAndResponse < GuideAction
* `render` - render a Lucky::HTMLPage
* `redirect` - redirect the request to another location
* `text` - respond with plain text
* `plain_text` - respond with plain text
* `json` - return a json response
* `xml` - return an xml response
* `head` - return a head response with a 204 status
* `file` - return a file for download
Expand All @@ -73,7 +74,7 @@ class Guides::HttpAndRouting::RequestAndResponse < GuideAction
# Run some fancy background job
if plain?
# plain text request, return some plain text
text "Job sent for processing"
plain_text "Job sent for processing"
else
# Respond with HEAD 201
head 201
Expand Down
65 changes: 45 additions & 20 deletions src/actions/guides/http_and_routing/routing_and_params.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ class Guides::HttpAndRouting::RoutingAndParams < GuideAction
To save some typing, Lucky automatically infers a default route path from the name of the action class,
if the name ends with a known [RESTful action (see below)](##{ANCHOR_AUTOMATICALLY_GENERATE_RESTFUL_ROUTES}).
For example, an action named `Item::Show` will by default respond to `get "/item/:item_id"`, a HTTP GET request
For example, an action nemed `Item::Show` will by default respond to `get "/item/:item_id"`, a HTTP GET request
for a specific item, and have the requested item_id available as #{:item_id}.
To see what a simple action looks like, let's generate an index action for showing users with
`lucky gen.action.browser Users::Index`.
```crystal
# src/actions/users/index.cr
class Users::Index < BrowserAction
get "/users/:user_id" do
# `text` sends plain/text to the client
text "Rendering something in Users::Index"
# `plain_text` sends plain/text to the client
plain_text "Rendering something in Users::Index"
end
end
```
Routes can be defined for specific request types by using the `get`, `put`, `post`, `patch`, `trace`, and `delete` macros.
If you need access to still different methods like `options`, you can use the `match` macro.
```crystal
Expand All @@ -51,8 +53,6 @@ class Guides::HttpAndRouting::RoutingAndParams < GuideAction
end
```
> Note that `lucky gen.action.browser` is used to create actions that should be
shown in a browser. Whereas `lucky gen.action.api` is used for actions meant
to be used for an API (e.g. JSON).
Expand Down Expand Up @@ -97,11 +97,11 @@ class Guides::HttpAndRouting::RoutingAndParams < GuideAction
# src/actions/users/show.cr
class Users::Show < BrowserAction
get "/users/:some_user_id" do
text "Requested user id: \#{some_user_id}"
plain_text "Requested user id: \#{some_user_id}"
end
end
```
Here, the string from the request path will be returned by the `some_user_id` method. So in this example if
`/users/123-foo` is requested `some_user_id` would return a text response of `Requested user id: 123-foo`.
Expand Down Expand Up @@ -144,10 +144,9 @@ class Guides::HttpAndRouting::RoutingAndParams < GuideAction
# From the name,
# "Users" is the resource, and
# "Show" is the RESTful action.
route do # The infered route is: get "/users/:user_id"
text "A request was made for the user_id: \#{user_id}"
plain_text "A request was made for the user_id: \#{user_id}"
end
end
```
Expand All @@ -166,10 +165,9 @@ class Guides::HttpAndRouting::RoutingAndParams < GuideAction
# "Projects" is the parent resource
# "Users" is the nested resource
# "Index" is the RESTful action
nested_route do # The infered route is: get "/projects/:project_id/users"
text "Render list of users in project \#{project_id}"
plain_text "Render list of users in project \#{project_id}"
end
end
```
Expand All @@ -186,10 +184,9 @@ class Guides::HttpAndRouting::RoutingAndParams < GuideAction
class Admin::Projects::Index < BrowserAction
# From the name,
# anything before the resource (`Projects`) will be used as a namespace (`Admin`).
route do # The infered route is: get "/admin/projects"
text "Render list of projects"
plain_text "Render list of projects"
end
end
```
Expand Down Expand Up @@ -247,6 +244,34 @@ class Guides::HttpAndRouting::RoutingAndParams < GuideAction
> The `fallback` should always contain a `Lucky::RouteNotFoundError` error. This is to throw a 404 when an asset, or some other file is not found.
## Memoization
As your application gets larger, you may need to write helper methods that run expensive
calculations, or queries. Calling these methods multiple times can lead to performance issues.
To mitigate these, you can use the `memoize` macro.
```crystal
class Reports::Show < BrowserAction
get "/report" do
small_number = calculate_numbers
big_number = calculate_numbers + 1000
render ShowPage, small_number: small_number, big_number: big_number
end
memoize def calculate_numbers
# This is ran only the first time it's called
ReportQuery.new.fetch_numbers_for_today(Time.utc)
end
end
```
There are a few caveats to the `memoize` macro. It will not memoize `false`, or `nil` return values.
If your method returns a "falsey" value, then it will be ran each time. Another thing to note is
you can't memoize a method that takes an argument. This is due to the dynamic nature of the
arguments.
[Learn more about memoization](https://en.wikipedia.org/wiki/Memoization).
## 404 errors
By default Lucky will respond with a 404 when neither a route nor a static
Expand Down Expand Up @@ -280,7 +305,7 @@ class Guides::HttpAndRouting::RoutingAndParams < GuideAction
param page : Int32 = 1
route do
text "All users starting on page \#{page}"
plain_text "All users starting on page \#{page}"
end
end
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Guides::HttpAndRouting::SessionsAndCookies < GuideAction
session.set(:name, "Sally")
session.get(:name) # Will return "Sally"
session.get("person") # oops! An exception is raised because this key doesn't exist
text "Cookies!"
plain_text "Cookies!"
end
end
```
Expand Down
2 changes: 1 addition & 1 deletion src/models/lucky_cli_version.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ module LuckyCliVersion
end

def current_version : String
"0.16.0"
"0.17.0"
end
end

0 comments on commit 97f575c

Please sign in to comment.