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

Add Support filters and search in count_ functions. #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/context_functions_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

2 things here:

  1. What do you think about adding typespecs?
  2. This is a breaking change, since the first parameter is changing. So there's two options here, accept the breaking change and start planning the next major version (since there are plans to also change the list_ functions as well) or we can add a guard when is_atom(field) and when is_map(count_params) or when is_list(count_params), keeping the compatibility

Copy link
Member

@jotaviobiondo jotaviobiondo Jan 31, 2022

Choose a reason for hiding this comment

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

Copy link
Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

  1. Okay, probably we could add typespecs later.

  2. Agreed!

field = opts[:field] || :id
search = opts[:search]
search_fields = opts[:search_fields] || []
Copy link
Author

Choose a reason for hiding this comment

The 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)

Copy link
Member

Choose a reason for hiding this comment

The 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 __schema__(:fields).
Also, a search in all fields would include, timestamps, id's, etc. and I don't see this being useful. Most of the cases we define which fields should be searchable.

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 search_fields as an option of the generate_function, and do something like opts[:search_fields] || unquote(generate_opts)[:search_fields].

Also, should we raise an error if the user dont define this option anywhere and the search parameter is sent?

One more thing here, I think the :search_fields option should come from the second parameter (renaming repo_opts to opts and opts to count_params or something`)

Copy link
Author

Choose a reason for hiding this comment

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

  • Yes, maybe the generation of this new function is a good idea, we could use it in the search function, today it uses the same logic.
module_fields = unquote(module).__schema__(:fields)
  • I don't think we need to return an error here, as the search_term attribute in the search function can be nill or an empty string.

  • Hmm, I don't know, it looks so good this way, I wouldn't understand why all the attributes specified as opt, and this one doesn't.

Copy link
Member

Choose a reason for hiding this comment

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

  1. Nice! I think changing the search_ it would be nice too. (Just remember to not do a breaking change, there I think we need to default to all fields in the schema)

  2. My idea is when the search_term is not null (nor empty) AND the search fields are not defined

  3. It's because generally speaking, opts in Elixir always refer to options that modifies the behaviour of the function somehow. It's usually the last argument in the function (search_fields would modify that behaviour). The first arguments are generally the parameters that are uses to calculate something (this case the first argument would have filters and search, used to count the number of records of a schema)

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
Expand Down
12 changes: 10 additions & 2 deletions lib/crudry_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,16 @@ defmodule Crudry.Context do
|> Repo.all()
end

def count_my_schemas(field \\\\ :id) do
Repo.aggregate(MySchema, :count, field)
def count_my_schemas(opts \\\\ []) do
field = opts[:field] || :id
search = opts[:search]
search_fields = opts[:search_fields] || []
filters = opts[:filters] || %{}

MySchema
|> Crudry.Query.search(search, search_fields)
|> Crudry.Query.filter(filters)
|> Repo.aggregate(:count, field)
end

## Create functions
Expand Down
34 changes: 25 additions & 9 deletions test/crudry_context_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ defmodule CrudryContextTest do
alias Crudry.Repo
alias Crudry.{Post, User}

@user %{username: "Chuck Norris"}
@user2 %{username: "Will Smith"}
@user %{username: "Chuck Norris", age: 81}
@user2 %{username: "Will Smith", age: 53}
@user3 %{username: "Sylvester Stallone"}
@user4 %{username: "Will Ferrell", age: 54}
@post %{title: "Chuck Norris threw a grenade and killed 50 people, then it exploded."}

setup do
Expand All @@ -25,8 +26,9 @@ defmodule CrudryContextTest do
assert {:ok, %{} = user1} = Repo.insert(User.changeset(%User{}, @user))
assert {:ok, %{} = user2} = Repo.insert(User.changeset(%User{}, @user2))
assert {:ok, %{} = user3} = Repo.insert(User.changeset(%User{}, @user3))
assert {:ok, %{} = user4} = Repo.insert(User.changeset(%User{}, @user4))
assert {:ok, %{} = post} = Repo.insert(Post.changeset(%Post{}, %{title: "title", user_id: user1.id}))
%{user1: user1, user2: user2, user3: user3, post: post}
%{user1: user1, user2: user2, user3: user3, user4: user4, post: post}
end

test "create/1" do
Expand All @@ -39,12 +41,12 @@ defmodule CrudryContextTest do
assert %Crudry.User{username: ^username} = UserContext.create_user!(@user)
end

test "list/0", %{user1: user1, user2: user2, user3: user3} do
assert UserContext.list_users() == [user1, user2, user3]
test "list/0", %{user1: user1, user2: user2, user3: user3, user4: user4} do
assert UserContext.list_users() == [user1, user2, user3, user4]
end

test "list_with_assocs/1", %{user1: user1, user2: user2, user3: user3} do
assert UserContext.list_users_with_assocs(:posts) == Repo.preload([user1, user2, user3], :posts)
test "list_with_assocs/1", %{user1: user1, user2: user2, user3: user3, user4: user4} do
assert UserContext.list_users_with_assocs(:posts) == Repo.preload([user1, user2, user3, user4], :posts)
end

test "list/1", %{user1: user1} do
Expand All @@ -64,8 +66,22 @@ defmodule CrudryContextTest do
assert UserContext.filter_users(%{username: @user3.username}) == [user3]
end

test "count/1" do
assert UserContext.count_users(:id) == 3
test "count/1", %{user2: user2} do
assert UserContext.count_users() == 4
assert UserContext.count_users(field: :age) == 3

opts_search = [search: "Will", search_fields: [:username]]
assert UserContext.count_users(opts_search) == 2

opts_filters = [filters: %{id: user2.id}]
assert UserContext.count_users(opts_filters) == 1

opts_search_with_filters = [
filters: %{age: user2.age},
search: "Will",
search_fields: [:username],
]
assert UserContext.count_users(opts_search_with_filters) == 1
end

test "exists?/1", %{user1: user1} do
Expand Down