Skip to content

Commit

Permalink
fix warnings on Elixir 1.19-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Dec 11, 2024
1 parent 3b4f773 commit 0f8cf82
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ jobs:
- elixir: 1.14.5
otp: 25.3.2.9

- elixir: 1.15.x
otp: 25.x

- elixir: 1.17.3
otp: 27.1
otp: 27.2
lint: true
installer: true

Expand Down
4 changes: 2 additions & 2 deletions guides/contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ Next we can create the template at `lib/hello_web/controllers/cart_html/show.htm
<.back navigate={~p"/products"}>Back to products</.back>
```

We started by showing the empty cart message if our preloaded `cart.items` is empty. If we have items, we use the `simple_form` component provided by our `HelloWeb.CoreComponents` to take our cart changeset that we assigned in the `CartController.show/2` action and create a form which maps to our cart controller `update/2` action. Within the form, we use the [`inputs_for`](`Phoenix.Component.inputs_for/1`) component to render inputs for the nested cart items. This will allow us to map item inputs back together when the form is submitted. Next, we display a number input for the item quantity and label it with the product title. We finish the item form by converting the item price to string. We haven't written the `ShoppingCart.total_item_price/1` function yet, but again we employed the idea of clear, descriptive public interfaces for our contexts. After rendering inputs for all the cart items, we show an "update cart" submit button, along with the total price of the entire cart. This is accomplished with another new `ShoppingCart.total_cart_price/1` function which we'll implement in a moment. Finally, we added a `back` component to go back to our products page.
We started by showing the empty cart message if our preloaded `cart.items` is empty. If we have items, we use the `simple_form` component provided by our `HelloWeb.CoreComponents` to take our cart changeset that we assigned in the `CartController.show/2` action and create a form which maps to our cart controller `update/2` action. Within the form, we use the [`inputs_for`](https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#inputs_for/1) component to render inputs for the nested cart items. This will allow us to map item inputs back together when the form is submitted. Next, we display a number input for the item quantity and label it with the product title. We finish the item form by converting the item price to string. We haven't written the `ShoppingCart.total_item_price/1` function yet, but again we employed the idea of clear, descriptive public interfaces for our contexts. After rendering inputs for all the cart items, we show an "update cart" submit button, along with the total price of the entire cart. This is accomplished with another new `ShoppingCart.total_cart_price/1` function which we'll implement in a moment. Finally, we added a `back` component to go back to our products page.

We're almost ready to try out our cart page, but first we need to implement our new currency calculation functions. Open up your shopping cart context at `lib/hello/shopping_cart.ex` and add these new functions:

Expand Down Expand Up @@ -1027,7 +1027,7 @@ Head back over to your shopping cart context in `lib/hello/shopping_cart.ex` and
end
```

We started much like how our out-of-the-box code started – we take the cart struct and cast the user input to a cart changeset, except this time we use `Ecto.Changeset.cast_assoc/3` to cast the nested item data into `CartItem` changesets. Remember the [`<.inputs_for />`](`Phoenix.Component.inputs_for/1`) call in our cart form template? That hidden ID data is what allows Ecto's `cast_assoc` to map item data back to existing item associations in the cart. Next we use `Ecto.Multi.new/0`, which you may not have seen before. Ecto's `Multi` is a feature that allows lazily defining a chain of named operations to eventually execute inside a database transaction. Each operation in the multi chain receives the values from the previous steps and executes until a failed step is encountered. When an operation fails, the transaction is rolled back and an error is returned, otherwise the transaction is committed.
We started much like how our out-of-the-box code started – we take the cart struct and cast the user input to a cart changeset, except this time we use `Ecto.Changeset.cast_assoc/3` to cast the nested item data into `CartItem` changesets. Remember the [`<.inputs_for />`](https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#inputs_for/1) call in our cart form template? That hidden ID data is what allows Ecto's `cast_assoc` to map item data back to existing item associations in the cart. Next we use `Ecto.Multi.new/0`, which you may not have seen before. Ecto's `Multi` is a feature that allows lazily defining a chain of named operations to eventually execute inside a database transaction. Each operation in the multi chain receives the values from the previous steps and executes until a failed step is encountered. When an operation fails, the transaction is rolled back and an error is returned, otherwise the transaction is committed.

For our multi operations, we start by issuing an update of our cart, which we named `:cart`. After the cart update is issued, we perform a multi `delete_all` operation, which takes the updated cart and applies our zero-quantity logic. We prune any items in the cart with zero quantity by returning an ecto query that finds all cart items for this cart with an empty quantity. Calling `Repo.transaction/1` with our multi will execute the operations in a new transaction and we return the success or failure result to the caller just like the original function.

Expand Down
27 changes: 20 additions & 7 deletions lib/phoenix/naming.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,26 @@ defmodule Phoenix.Naming do
"""
@spec unsuffix(String.t, String.t) :: String.t
def unsuffix(value, suffix) do
string = to_string(value)
suffix_size = byte_size(suffix)
prefix_size = byte_size(string) - suffix_size
case string do
<<prefix::binary-size(prefix_size), ^suffix::binary>> -> prefix
_ -> string
# TODO: remove conditional once we require Elixir 1.15 for existing apps
if Version.match?(System.version(), ">= 1.15.0") do
def unsuffix(value, suffix) do
string = to_string(value)
suffix_size = byte_size(suffix)
prefix_size = byte_size(string) - suffix_size
case string do
<<prefix::binary-size(^prefix_size), ^suffix::binary>> -> prefix
_ -> string
end
end
else
def unsuffix(value, suffix) do
string = to_string(value)
suffix_size = byte_size(suffix)
prefix_size = byte_size(string) - suffix_size
case string do
<<prefix::binary-size(prefix_size), ^suffix::binary>> -> prefix
_ -> string
end
end
end

Expand Down
11 changes: 8 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ defmodule Phoenix.MixProject do
elixir: @elixir_requirement,
deps: deps(),
package: package(),
preferred_cli_env: [docs: :docs],
consolidate_protocols: Mix.env() != :test,
xref: [
exclude: [
Expand All @@ -47,6 +46,12 @@ defmodule Phoenix.MixProject do
]
end

def cli do
[
preferred_envs: [docs: :docs]
]
end

defp elixirc_paths(:docs), do: ["lib", "installer/lib"]
defp elixirc_paths(_), do: ["lib"]

Expand Down Expand Up @@ -124,12 +129,12 @@ defmodule Phoenix.MixProject do
main: "overview",
logo: "logo.png",
extra_section: "GUIDES",
assets: "guides/assets",
assets: %{"guides/assets" => "assets"},
formatters: ["html", "epub"],
groups_for_modules: groups_for_modules(),
extras: extras(),
groups_for_extras: groups_for_extras(),
groups_for_functions: [
groups_for_docs: [
Reflection: &(&1[:type] == :reflection)
],
skip_undefined_reference_warnings_on: ["CHANGELOG.md"]
Expand Down

0 comments on commit 0f8cf82

Please sign in to comment.