Last active
August 19, 2016 14:39
-
-
Save snewcomer/ae221f8f33c813b5ae930772281ce09e to your computer and use it in GitHub Desktop.
Update Belongs To relationship
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 CodeCorps.Post do | |
use CodeCorps.Web, :model | |
alias CodeCorps.MarkdownRenderer | |
schema "posts" do | |
field :body, :string | |
field :markdown, :string | |
field :number, :integer | |
field :post_type, :string | |
field :status, :string, default: "open" | |
field :title, :string | |
belongs_to :project, CodeCorps.Project | |
belongs_to :user, CodeCorps.User | |
timestamps() | |
end | |
def changeset(struct, params \\ %{}) do | |
struct | |
|> cast(params, [:title, :markdown, :post_type, :status]) | |
|> validate_required([:title, :markdown, :post_type, :status]) | |
|> validate_inclusion(:post_type, post_types) | |
|> validate_inclusion(:status, statuses) | |
|> MarkdownRenderer.render_markdown_to_html(:markdown, :body) | |
end | |
def create_changeset(struct, params) do | |
struct | |
|> changeset(params) | |
|> cast(params, [:project_id, :user_id]) | |
|> validate_required([:project_id, :user_id]) | |
|> assoc_constraint(:project) | |
|> assoc_constraint(:user) | |
end | |
defp post_types do | |
~w{ idea issue task } | |
end | |
defp statuses do | |
~w{ open closed } | |
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 update(conn, %{"id" => id, "data" => data = %{"type" => "post", "attributes" => _post_params}}) do | |
post = | |
Post | |
|> preload([:project]) | |
|> Repo.get!(id) | |
changeset = Post.create_changeset(post, Params.to_attributes(data)) | |
case Repo.update(changeset) do | |
{:ok, post} -> | |
render(conn, "show.json-api", data: post) | |
{:error, changeset} -> | |
conn | |
|> put_status(:unprocessable_entity) | |
|> render(CodeCorps.ChangesetView, "error.json-api", 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
test "updates related data and renders chosen resource when data is valid", %{conn: conn} do | |
user = insert_user() | |
project = insert_project() | |
project_two = insert_project() | |
post = insert_post(%{project_id: project.id, user_id: user.id}) | |
conn = put conn, post_path(conn, :update, post), %{ | |
"meta" => %{}, | |
"data" => %{ | |
"type" => "post", | |
"id" => post.id, | |
"attributes" => @valid_attrs, | |
"relationships" => relationships(user, project_two) | |
} | |
} | |
data = json_response(conn, 200)["data"] | |
assert data["type"] == "post" | |
assert data["id"] == post.id |> Integer.to_string | |
# FAILURE | |
assert data["relationships"]["project"]["data"]["id"] == project_two.id |> Integer.to_string | |
assert data["relationships"]["project"]["data"]["type"] == "project" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment