Skip to content

Instantly share code, notes, and snippets.

@emwalker
Last active July 10, 2016 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emwalker/9387160b41e4bc6e6d93bdae9a41406c to your computer and use it in GitHub Desktop.
Save emwalker/9387160b41e4bc6e6d93bdae9a41406c to your computer and use it in GitHub Desktop.
defmodule Digraffe.Collection do
use Digraffe.Web, :model
alias Digraffe.Settings
schema "collections" do
field :title, :string
belongs_to :owner, Settings
timestamps
end
@required_fields ~w(title owner_id)
@optional_fields ~w()
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end
1) test allows setting of the selected collection (Digraffe.SettingsTest)
test/models/settings_test.exs:12
Assertion with == failed
code: collection.id() == updated.selected_collection().id()
lhs: "fb444450-5562-4356-90fc-858401a2987e"
rhs: "8c20c15c-d970-4230-bfb3-f80fc35f1119"
stacktrace:
test/models/settings_test.exs:23
Finished in 0.6 seconds (0.5s on load, 0.03s on tests)
65 tests, 1 failure, 64 skipped
Randomized with seed 854580
defmodule Digraffe.Settings do
use Digraffe.Web, :model
alias Digraffe.{Repo, Settings}
schema "settings" do
field :name, :string
field :provider, :string
field :provider_id, :string
field :avatar_url, :string
has_many :collections, Digraffe.Collection, foreign_key: :owner_id
has_one :selected_collection, Digraffe.Collection, foreign_key: :id
timestamps
end
@required_fields ~w(name provider provider_id avatar_url)
@optional_fields ~w()
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> cast_assoc(:selected_collection)
|> unique_constraint(:provider_id)
end
def get_or_create(user) when is_map(user) do
case get_settings(user) do
nil -> changeset(%Settings{}, user) |> Repo.insert!
model -> model
end
|> Repo.preload(:selected_collection)
end
def get_or_create(nil), do: nil
defp get_settings(%{} = user) do
case Repo.get_by(Settings, user) do
nil -> nil
settings -> Repo.preload(settings, :selected_collection)
end
end
def small_avatar_url(settings) do
~s(#{settings.avatar_url}&s=40)
end
end
defmodule Digraffe.SettingsTest do
use Digraffe.ModelCase
import Digraffe.Factory
alias Digraffe.{Settings, Collection}
test "#small_avatar_url is added" do
settings = %Settings{avatar_url: "http://avatar/url?v=1"}
assert "http://avatar/url?v=1&s=40" == Settings.small_avatar_url(settings)
end
@tag wip: true
test "allows setting of the selected collection" do
settings = create(:settings) |> Repo.preload(:selected_collection)
refute settings.selected_collection
collection = create(:collection)
changeset = settings
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:selected_collection, collection)
assert changeset.valid?
updated = Repo.update!(changeset) |> Repo.preload(:selected_collection)
assert collection.title == updated.selected_collection.title
# This assertion is failing
assert collection.id == updated.selected_collection.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment