Skip to content

Instantly share code, notes, and snippets.

@shankardevy
Last active July 8, 2017 11:22
Show Gist options
  • Save shankardevy/1ec16af7881263d2fccc831b7db4b2cc to your computer and use it in GitHub Desktop.
Save shankardevy/1ec16af7881263d2fccc831b7db4b2cc to your computer and use it in GitHub Desktop.
defmodule Mango.Sales.LineItem do
use Ecto.Schema
import Ecto.Changeset
alias Mango.Catalog
alias Mango.Sales.LineItem
embedded_schema do
field :product_id, :integer
field :product_name, :string
field :pack_size, :string
field :quantity, :integer
field :unit_price, :decimal
field :total, :decimal
end
@doc false
def changeset(%LineItem{} = line_item, attrs) do
line_item
|> cast(attrs, [:product_id, :product_name, :pack_size, :quantity, :unit_price, :total])
|> set_product_details
|> set_total
|> validate_required([:product_id, :product_name, :pack_size, :quantity, :unit_price])
end
defp set_product_details(changeset) do
case get_change(changeset, :product_id) do
nil -> changeset
product_id ->
product = Catalog.get_product!(product_id)
changeset
|> put_change(:product_name, product.name)
|> put_change(:unit_price, product.price)
|> put_change(:pack_size, product.pack_size)
end
end
defp set_total(changeset) do
quantity = get_field(changeset, :quantity) |> Decimal.new
unit_price = get_field(changeset, :unit_price)
changeset
|> put_change(:total, Decimal.mult(unit_price, quantity))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment