Skip to content

Commit

Permalink
Refactor helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith authored and begedin committed Oct 12, 2016
1 parent ffb5609 commit 376c0fa
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 59 deletions.
42 changes: 2 additions & 40 deletions lib/code_corps/helpers/filter_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,57 +1,19 @@
defmodule CodeCorps.FilterHelpers do
use CodeCorps.Web, :model

import CodeCorps.StringHelpers, only: [coalesce_id_string: 1, coalesce_string: 1]

def id_filter(query, id_list) do
ids = id_list |> coalesce_id_string
query |> where([object], object.id in ^ids)
end

def limit_filter(query, count) do
query |> limit(^count)
end

def number_as_id_filter(query, number) do
query |> where([object], object.number == ^number)
end

def organization_filter(query, organization_id) do
query |> where([object], object.organization_id == ^organization_id)
end

def project_filter(query, project_id) do
query |> where([object], object.project_id == ^project_id)
end

def role_filter(query, roles_list) do
roles = roles_list |> coalesce_string
query |> where([object], object.role in ^roles)
end

def task_filter(query, task_id) do
query |> where([object], object.task_id == ^task_id)
end

def task_status_filter(query, status) do
query |> where([object], object.status == ^status)
end

def task_type_filter(query, task_type_list) do
task_types = task_type_list |> coalesce_string
query |> where([object], object.task_type in ^task_types)
end

def title_filter(query, title) do
query |> where([object], ilike(object.title, ^"%#{title}%"))
end

defp coalesce_id_string(string) do
string
|> String.split(",")
|> Enum.map(&String.to_integer(&1))
end

defp coalesce_string(string) do
string
|> String.split(",")
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.ControllerHelpers do
defmodule CodeCorps.StringHelpers do
def coalesce_id_string(string) do
string
|> String.split(",")
Expand Down
6 changes: 1 addition & 5 deletions web/models/comment.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule CodeCorps.Comment do
use CodeCorps.Web, :model
import CodeCorps.ModelHelpers

alias CodeCorps.MarkdownRenderer

schema "comments" do
Expand Down Expand Up @@ -31,8 +31,4 @@ defmodule CodeCorps.Comment do
|> assoc_constraint(:task)
|> assoc_constraint(:user)
end

def index_filters(query, params) do
query |> task_filter(params)
end
end
13 changes: 1 addition & 12 deletions web/models/model_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule CodeCorps.ModelHelpers do
use CodeCorps.Web, :model

import CodeCorps.ControllerHelpers
import CodeCorps.StringHelpers, only: [coalesce_id_string: 1, coalesce_string: 1]

def generate_slug(changeset, value_key, slug_key) do
case changeset do
Expand Down Expand Up @@ -35,11 +35,6 @@ defmodule CodeCorps.ModelHelpers do
end
def number_as_id_filter(query, _), do: query

def organization_filter(query, %{"organization_id" => organization_id}) do
query |> where([object], object.organization_id == ^organization_id)
end
def organization_filter(query, _), do: query

def task_type_filter(query, %{"task_type" => task_type_list}) do
task_types = task_type_list |> coalesce_string
query |> where([object], object.task_type in ^task_types)
Expand All @@ -61,12 +56,6 @@ defmodule CodeCorps.ModelHelpers do
end
def project_filter(query, _), do: query

def role_filter(query, %{"role" => roles}) do
roles = roles |> coalesce_string
query |> where([object], object.role in ^roles)
end
def role_filter(query, _), do: query

def title_filter(query, %{"query" => title}) do
query |> where([object], ilike(object.title, ^"%#{title}%"))
end
Expand Down
1 change: 0 additions & 1 deletion web/models/organization_membership.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule CodeCorps.OrganizationMembership do
"""

use CodeCorps.Web, :model
import CodeCorps.ModelHelpers

schema "organization_memberships" do
field :role, :string
Expand Down

0 comments on commit 376c0fa

Please sign in to comment.