-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add Support filters and search in count_ functions. #52
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,8 +105,15 @@ defmodule ContextFunctionsGenerator do | |
|
||
def generate_function(:count, _name, pluralized_name, module, opts) do | ||
quote do | ||
def unquote(:"count_#{pluralized_name}")(field \\ :id, repo_opts \\ []) do | ||
def unquote(:"count_#{pluralized_name}")(opts \\ [], repo_opts \\ []) do | ||
field = opts[:field] || :id | ||
search = opts[:search] | ||
search_fields = opts[:search_fields] || [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would that be a good one? Leave search_fields as default with all module fields? search_fields = unquote(module).schema(:fields) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't know... First, we would need to take care of excluding fields that are embededd schemas (which are returned by But I also feel that having to define the search fields every time we use the function is not so good. I suggest that we add the Also, should we raise an error if the user dont define this option anywhere and the One more thing here, I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
module_fields = unquote(module).__schema__(:fields)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
filters = opts[:filters] || %{} | ||
|
||
unquote(module) | ||
|> Crudry.Query.search(search, search_fields) | ||
|> Crudry.Query.filter(filters) | ||
|> unquote(get_repo_module(opts)).aggregate(:count, field, repo_opts) | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 things here:
list_
functions as well) or we can add a guardwhen is_atom(field)
andwhen is_map(count_params) or when is_list(count_params)
, keeping the compatibilityThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @gabrielpra1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can create a more specific problem for using typespecs since it's something we're not using yet, taking advantage of the idea of doing this for filters and other functions.
About the rupture change, we can make this adjustment using guard 👍
Until we created a new version removing all deprecated functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, probably we could add typespecs later.
Agreed!