Skip to content

Conversation

@joshsmith
Copy link
Contributor

@joshsmith joshsmith commented Nov 29, 2017

What's in this PR?

Improves the project creation process by allowing nested skills/categories.

Copy link
Contributor

@begedin begedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the potential simplification we're getting out of this. Seeing some inconsistencies, though.

has_many :stripe_connect_subscriptions, through: [:stripe_connect_plan, :stripe_connect_subscriptions]

many_to_many :categories, CodeCorps.Category, join_through: CodeCorps.ProjectCategory
many_to_many :skills, CodeCorps.Skill, join_through: CodeCorps.ProjectSkill
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably wanna do an on_delete: :delete_all here. Default is :nothing according to the docs.

If this works out, I wouldn't mind doing it with tasks and users where applicable to.


defp do_associate_skills(changeset, _) do
changeset |> put_assoc(:skills, [])
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could save us a clause, and a do_associate helper, by using these:

defp associate_categories(changeset, %{"categories_ids" => ids}) when is_binary(ids) do
  categories =
    ids |> Enum.join(",") |> coalesce_id_string() |> find_categories()
  changeset |> put_assoc(:categories, categories)
end
defp associate_categories(changeset, _), do: changeset |> put_assoc(:categories, [])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I'm seeing some inconsistencies with coalesce_ids string and some of our tests.
If we're supposed to be getting a list here, instead of a csv idsstring, then the first clause would guard with

when is_list(ids) when length(ids) > 0 do

"categories_ids" => [category.id],
"cloudinary_public_id" => "foo123",
"description" => "Description",
"skills_ids" => [skill.id],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using coalesce_id_string above. Wouldn't that mean we're supposed to be sending csv ids here, instead of a list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because this is how JaSerializer modifies the data.


defp do_associate_categories(changeset, _) do
changeset |> put_assoc(:categories, [])
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm reasonably sure you could still rewrite this

defp associate_categories(changeset, %{"categories_ids" => []}) do
  changeset |> put_assoc(:categories, [])
end
defp associate_categories(changeset, %{"categories_ids" => ids}) do
  categories = ids |> Enum.map(&String.to_integer/1) |> find_categories()
  changeset |> put_assoc(:categories, categories)
end
defp associate_categories(changeset, _) do
  changeset |> put_assoc(:categories, [])
end

as this

defp associate_categories(changeset, %{"categories_ids" => ids}) when is_list(ids) when length(ids) > 0 do
  categories = ids |> Enum.map(&String.to_integer/1) |> find_categories()
  changeset |> put_assoc(:categories, categories)
end
defp associate_categories(changeset, _) do
  changeset |> put_assoc(:categories, [])
end

Heck, since you're just using an enum map, you don't even need the when length(ids) > 0 part.

end
defp associate_categories(changeset, _) do
changeset |> put_assoc(:categories, [])
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Same thing applies to associate_skills, of course. 😬

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nvm, it's there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, node version manager, it's there.

@joshsmith joshsmith force-pushed the improve-project-creation branch from 64ef63e to d23e230 Compare November 30, 2017 07:50
- Update views, controllers, changesets
- Add missing required portions to changeset
- Fix JSON API helpers to allow list relationships
- Clean up JSON API helpers
@joshsmith joshsmith force-pushed the improve-project-creation branch from d23e230 to d0ea4b5 Compare November 30, 2017 07:58
Copy link
Contributor

@begedin begedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good 👍

@joshsmith joshsmith merged commit 350065c into develop Nov 30, 2017
@joshsmith joshsmith deleted the improve-project-creation branch November 30, 2017 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants