diff --git a/lib/code_corps/helpers/filter_helpers.ex b/lib/code_corps/helpers/filter_helpers.ex index 9ec6860b2..35a37a757 100644 --- a/lib/code_corps/helpers/filter_helpers.ex +++ b/lib/code_corps/helpers/filter_helpers.ex @@ -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 diff --git a/web/controllers/controller_helpers.ex b/lib/code_corps/helpers/string_helpers.ex similarity index 82% rename from web/controllers/controller_helpers.ex rename to lib/code_corps/helpers/string_helpers.ex index a40098404..382085a5d 100644 --- a/web/controllers/controller_helpers.ex +++ b/lib/code_corps/helpers/string_helpers.ex @@ -1,4 +1,4 @@ -defmodule CodeCorps.ControllerHelpers do +defmodule CodeCorps.StringHelpers do def coalesce_id_string(string) do string |> String.split(",") diff --git a/web/models/comment.ex b/web/models/comment.ex index d70eb3d26..f96c33981 100644 --- a/web/models/comment.ex +++ b/web/models/comment.ex @@ -1,6 +1,6 @@ defmodule CodeCorps.Comment do use CodeCorps.Web, :model - import CodeCorps.ModelHelpers + alias CodeCorps.MarkdownRenderer schema "comments" do @@ -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 diff --git a/web/models/model_helpers.ex b/web/models/model_helpers.ex index eb9025b1f..c14cce638 100644 --- a/web/models/model_helpers.ex +++ b/web/models/model_helpers.ex @@ -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 @@ -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) @@ -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 diff --git a/web/models/organization_membership.ex b/web/models/organization_membership.ex index 76793479c..581adab05 100644 --- a/web/models/organization_membership.ex +++ b/web/models/organization_membership.ex @@ -4,7 +4,6 @@ defmodule CodeCorps.OrganizationMembership do """ use CodeCorps.Web, :model - import CodeCorps.ModelHelpers schema "organization_memberships" do field :role, :string