Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Backpex.Fields.HasMany #554

Merged
merged 32 commits into from
Sep 13, 2024
Merged

Refactor Backpex.Fields.HasMany #554

merged 32 commits into from
Sep 13, 2024

Conversation

Flo0807
Copy link
Collaborator

@Flo0807 Flo0807 commented Aug 30, 2024

  • Refactor Backpex.Field.HasMany to derive everything from the form
  • Add before_changeset callback to fields
  • Validate users to contain at least one post association (demo application)
  • Refactor some functions in resource.ex (see upgrade guide)

@Flo0807 Flo0807 marked this pull request as draft August 30, 2024 05:57
@Flo0807 Flo0807 self-assigned this Aug 30, 2024
@Flo0807 Flo0807 changed the title Refactor HasMany Refactor Backpex.Fields.HasMany Aug 30, 2024
@impl Backpex.Field
def before_changeset(changeset, attrs, _metadata, repo, field, assigns) do
{field_name, field_options} = field
validate_live_resource(field_name, field_options)
Copy link
Member

Choose a reason for hiding this comment

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

Things like this should be done way more generic and for every field / filter / etc. (in the future).

https://github.com/dashbitco/nimble_options

Maybe even on compile time.

Comment on lines +86 to +94
defp validate_posts(changeset) do
posts = get_field(changeset, :posts) || []

if length(posts) < 1 do
add_error(changeset, :posts, "must have at least one post")
else
changeset
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Just an idea: We could provide a generic version of this via Backpex.Ecto.ChangesetHelpers.validate_assoc_length or something like that (in the future).

lib/backpex/fields/has_many.ex Outdated Show resolved Hide resolved
@Flo0807 Flo0807 added the breaking-change A breaking change label Sep 13, 2024
@Flo0807 Flo0807 marked this pull request as ready for review September 13, 2024 10:04
@Flo0807 Flo0807 merged commit 306c6f6 into develop Sep 13, 2024
6 checks passed
@Flo0807 Flo0807 deleted the feature/refactor-has-many branch September 13, 2024 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change A breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants