-
Notifications
You must be signed in to change notification settings - Fork 86
Add active filter #1323
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 active filter #1323
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 |
---|---|---|
|
@@ -33,16 +33,16 @@ defmodule CodeCorps.Messages.ConversationQuery do | |
|
||
|
||
@doc ~S""" | ||
Narrows down a `CodeCorps.Conversation` query to return only those records | ||
considered to have a specific status. | ||
Filters `CodeCorps.Conversation` record queries to return only those | ||
considered to be active. | ||
|
||
The status of `active` means that only those records are included which either | ||
- belong to a `CodeCorps.Message` initiated by user | ||
- belong to a `CodeCorps.Message` initiated by admin, with at least a single | ||
reply in the form of a `CodeCorps.ConversationPart` | ||
Active conversations belong either: | ||
- to a `CodeCorps.Message` initiated by user | ||
- to a `CodeCorps.Message` initiated by an admin, with at least a single | ||
conversation part | ||
""" | ||
@spec status_filter(Queryable.t, map) :: Queryable.t | ||
def status_filter(queryable, %{"status" => "active"}) do | ||
@spec active_filter(Queryable.t, map) :: Queryable.t | ||
def active_filter(queryable, %{"active" => true}) do | ||
prefiltered_ids = queryable |> select([c], c.id) |> Repo.all | ||
|
||
Conversation | ||
|
@@ -53,5 +53,15 @@ defmodule CodeCorps.Messages.ConversationQuery do | |
|> having([_c, m, _cp], m.initiated_by == "user") | ||
|> or_having([c, m, cp], m.initiated_by == "admin" and count(cp.id) > 0) | ||
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. In response to your question above, this is the query that does it (specifically the |
||
end | ||
def status_filter(query, %{}), do: query | ||
def active_filter(query, %{}), do: query | ||
|
||
@doc ~S""" | ||
Filters `CodeCorps.Conversation` record queries by their status. | ||
""" | ||
@spec status_filter(Queryable.t, map) :: Queryable.t | ||
def status_filter(queryable, %{"status" => status}) do | ||
queryable | ||
|> where([c], c.status == ^status) | ||
end | ||
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. This still needs a test |
||
def status_filter(query, _), do: query | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule CodeCorps.Messages.ConversationQueryTest do | ||
use CodeCorps.DbAccessCase | ||
|
||
alias CodeCorps.{ | ||
Conversation, | ||
Messages.ConversationQuery, | ||
Repo | ||
} | ||
|
||
describe "status_filter/2" do | ||
test "filters by status" do | ||
open_conversation = insert(:conversation, status: "open") | ||
_closed_conversation = insert(:conversation, status: "closed") | ||
|
||
[result] = | ||
Conversation | ||
|> ConversationQuery.status_filter(%{"status" => "open"}) | ||
|> Repo.all() | ||
|
||
assert result.id == open_conversation.id | ||
end | ||
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.
@joshsmith is this how active filter should work? What specifically filters out inactive records?