Last active
December 10, 2015 05:35
-
-
Save cseeger/ddd862ef96ead12a1218 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for @changeset, @action, fn f -> %> | |
<%= if @changeset.action do %> | |
<p> Something is broken. Check the following: </p> | |
<ul> | |
<%= for {attr, message} <- f.errors do %> | |
<li> | |
<%= humanize(attr) %> | |
| |
<%= message %> | |
</li> | |
<% end %> | |
</ul> | |
<% end %> | |
<%= label f, :name %> | |
<%= text_input f, :name %> | |
<%= label f, :description %> | |
<%= text_input f, :description %> | |
<%= label f, :seo_slug %> | |
<%= text_input f, :seo_slug %> | |
<%= inputs_for f, :variants, fn v -> %> | |
<%= label v, :price %> | |
<%= text_input v, :price %> | |
<%= label v, :sale_price %> | |
<%= text_input v, :sale_price %> | |
<%= label v, :available %> | |
<%= text_input v, :available %> | |
<%= label v, :sku %> | |
<%= text_input v, :sku %> | |
<%= if v.model.id do %> | |
<%= label v, :delete, "Delete?" %> | |
<%= checkbox v, :delete %> | |
<% end %> | |
<%= inputs_for v, :variant_images, [multipart: true], fn i -> %> | |
<%= label i, :image %> | |
<%= file_input i, :image %> | |
<% end %> | |
<% end %> | |
<%= submit "Submit" %> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[debug] Processing by Dumaine.ProductController.create/2 | |
Parameters: %{"_csrf_token" => "Oz4ZSmsZJjEUBhtkCzQGGBQEICF7NgAAvhIrYzVGYahVfN0+bTdXCg==", "_utf8" => "✓", "product" => %{"description" => "All natural!", "name" => "Belt", "seo_slug" => "belt", "variants" => %{"0" => %{"available" => "5", "price" => "123", "sale_price" => "123", "sku" => "abc-123", "variant_images" => %{"0" => %{"image" => "approved.jpg"}}}, "1" => %{"available" => "2", "price" => "44", "sale_price" => "44", "sku" => "abc-111", "variant_images" => %{"0" => %{"image" => "dangerous.jpeg"}}}}}} | |
Pipelines: [:browser] | |
[debug] BEGIN [] OK query=0.3ms | |
[debug] INSERT INTO "products" ("description", "inserted_at", "name", "seo_slug", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" ["All natural!", {{2015, 12, 10}, {5, 24, 34, 0}}, "Belt", "belt", {{2015, 12, 10}, {5, 24, 34, 0}}] OK query=0.4ms | |
[debug] INSERT INTO "variants" ("available", "inserted_at", "price", "product_id", "sale_price", "sku", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [5, {{2015, 12, 10}, {5, 24, 34, 0}}, 123, 15, 123, "abc-123", {{2015, 12, 10}, {5, 24, 34, 0}}] OK query=0.3ms | |
[debug] INSERT INTO "variants" ("available", "inserted_at", "price", "product_id", "sale_price", "sku", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [2, {{2015, 12, 10}, {5, 24, 34, 0}}, 44, 15, 44, "abc-111", {{2015, 12, 10}, {5, 24, 34, 0}}] OK query=0.4ms | |
[debug] COMMIT [] OK query=6.1ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Dumaine.Product do | |
use Dumaine.Web, :model | |
schema "products" do | |
field :name, :string | |
field :description, :string | |
field :seo_slug, :string | |
has_many :variants, Dumaine.Variant, on_delete: :fetch_and_delete | |
timestamps | |
end | |
@required_fields ~w(name description seo_slug variants) | |
@optional_fields ~w() | |
@doc """ | |
Creates a changeset based on the `model` and `params`. | |
If no params are provided, an invalid changeset is returned | |
with no validation performed. | |
""" | |
def changeset(model, params \\ :empty) do | |
model | |
|> cast(params, @required_fields, @optional_fields) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def new(conn, _params) do | |
changeset = Product.changeset( | |
%Product{variants: [ | |
%Dumaine.Variant{variant_images: [%Dumaine.VariantImage{}] }, | |
%Dumaine.Variant{variant_images: [%Dumaine.VariantImage{}] } | |
] | |
} | |
) | |
render(conn, "new.html", changeset: changeset) | |
end | |
def create(conn, %{"product" => product_params}) do | |
changeset = Product.changeset(%Product{}, product_params) | |
case Repo.insert(changeset) do | |
{:ok, product} -> | |
# Product.changeset(product, product_params) | |
# |> Repo.update | |
conn | |
|> put_flash(:info, "Product created successfully.") | |
|> redirect(to: product_path(conn, :index)) | |
{:error, changeset} -> | |
render(conn, "new.html", changeset: changeset) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Dumaine.Variant do | |
use Dumaine.Web, :model | |
schema "variants" do | |
field :price, :integer | |
field :sale_price, :integer | |
field :available, :integer | |
field :sku, :string | |
field :delete, :boolean, virtual: true | |
belongs_to :product, Dumaine.Product | |
has_many :variant_images, Dumaine.VariantImage, on_delete: :fetch_and_delete | |
timestamps | |
end | |
@required_fields ~w(price sale_price available sku) | |
@optional_fields ~w(delete) | |
@doc """ | |
Creates a changeset based on the `model` and `params`. | |
If no params are provided, an invalid changeset is returned | |
with no validation performed. | |
""" | |
def changeset(model, params \\ :empty) do | |
model | |
|> cast(params, @required_fields, @optional_fields) | |
|> mark_for_deletion() | |
end | |
defp mark_for_deletion(changeset) do | |
if get_change(changeset, :delete) do | |
%{changeset | action: :delete} | |
else | |
changeset | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Dumaine.VariantImage do | |
use Dumaine.Web, :model | |
use Arc.Ecto.Model | |
schema "variant_images" do | |
field :attachment, Dumaine.Attachment.Type | |
field :delete, :boolean, virtual: true | |
belongs_to :variant, Dumaine.Variant | |
timestamps | |
end | |
@required_fields ~w() | |
@optional_fields ~w(delete) | |
@required_file_fields ~w() | |
@optional_file_fields ~w(attachment) | |
@doc """ | |
Creates a changeset based on the `model` and `params`. | |
If no params are provided, an invalid changeset is returned | |
with no validation performed. | |
""" | |
def changeset(model, params \\ :empty) do | |
model | |
|> cast(params, @required_fields, @optional_fields) | |
|> mark_for_deletion() | |
end | |
def attachment_changeset(model, params \\ :empty) do | |
model | |
|> cast_attachments(params, @required_file_fields, @optional_file_fields) | |
|> mark_for_deletion() | |
end | |
defp mark_for_deletion(changeset) do | |
if get_change(changeset, :delete) do | |
%{changeset | action: :delete} | |
else | |
changeset | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment