From caa3651870c9a7c0f2e1761cef6fc0f2040fa41c Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Sat, 8 Oct 2016 17:03:16 -0700 Subject: [PATCH] Refactor helpers --- .../helpers/{filter_helpers.ex => query.ex} | 7 +++---- lib/code_corps/helpers/slug.ex | 15 +++++++++++++++ .../helpers/{string_helpers.ex => string.ex} | 2 +- web/controllers/organization_controller.ex | 3 +-- .../organization_membership_controller.ex | 2 +- web/controllers/project_category_controller.ex | 2 +- web/controllers/project_skill_controller.ex | 2 +- web/controllers/role_skill_controller.ex | 2 +- web/controllers/user_category_controller.ex | 2 +- web/controllers/user_controller.ex | 2 +- web/controllers/user_role_controller.ex | 2 +- web/controllers/user_skill_controller.ex | 2 +- web/models/category.ex | 3 ++- web/models/model_helpers.ex | 14 +------------- web/models/organization.ex | 2 +- web/models/project.ex | 2 +- web/models/slugged_route.ex | 3 ++- 17 files changed, 35 insertions(+), 32 deletions(-) rename lib/code_corps/helpers/{filter_helpers.ex => query.ex} (71%) create mode 100644 lib/code_corps/helpers/slug.ex rename lib/code_corps/helpers/{string_helpers.ex => string.ex} (83%) diff --git a/lib/code_corps/helpers/filter_helpers.ex b/lib/code_corps/helpers/query.ex similarity index 71% rename from lib/code_corps/helpers/filter_helpers.ex rename to lib/code_corps/helpers/query.ex index 35a37a757..ed7534c68 100644 --- a/lib/code_corps/helpers/filter_helpers.ex +++ b/lib/code_corps/helpers/query.ex @@ -1,7 +1,6 @@ -defmodule CodeCorps.FilterHelpers do - use CodeCorps.Web, :model - - import CodeCorps.StringHelpers, only: [coalesce_id_string: 1, coalesce_string: 1] +defmodule CodeCorps.Helpers.Query do + import CodeCorps.Helpers.String, only: [coalesce_id_string: 1, coalesce_string: 1] + import Ecto.Query, only: [where: 3] def id_filter(query, id_list) do ids = id_list |> coalesce_id_string diff --git a/lib/code_corps/helpers/slug.ex b/lib/code_corps/helpers/slug.ex new file mode 100644 index 000000000..eeb02ec66 --- /dev/null +++ b/lib/code_corps/helpers/slug.ex @@ -0,0 +1,15 @@ +defmodule CodeCorps.Helpers.Slug do + alias Ecto.Changeset + + def generate_slug(changeset, value_key, slug_key) do + case changeset do + %Changeset{valid?: true, changes: changes} -> + case Map.fetch(changes, value_key) do + {:ok, value} -> Changeset.put_change(changeset, slug_key, Inflex.parameterize(value)) + _ -> changeset + end + _ -> + changeset + end + end +end diff --git a/lib/code_corps/helpers/string_helpers.ex b/lib/code_corps/helpers/string.ex similarity index 83% rename from lib/code_corps/helpers/string_helpers.ex rename to lib/code_corps/helpers/string.ex index 382085a5d..244386dc7 100644 --- a/lib/code_corps/helpers/string_helpers.ex +++ b/lib/code_corps/helpers/string.ex @@ -1,4 +1,4 @@ -defmodule CodeCorps.StringHelpers do +defmodule CodeCorps.Helpers.String do def coalesce_id_string(string) do string |> String.split(",") diff --git a/web/controllers/organization_controller.ex b/web/controllers/organization_controller.ex index 47dda1eb3..d78bee821 100644 --- a/web/controllers/organization_controller.ex +++ b/web/controllers/organization_controller.ex @@ -2,9 +2,8 @@ defmodule CodeCorps.OrganizationController do use CodeCorps.Web, :controller use JaResource - import CodeCorps.FilterHelpers, only: [id_filter: 2] + import CodeCorps.Helpers.Query, only: [id_filter: 2] - alias CodeCorps.Organization alias CodeCorps.Organization plug :load_and_authorize_resource, model: Organization, only: [:create, :update] diff --git a/web/controllers/organization_membership_controller.ex b/web/controllers/organization_membership_controller.ex index 7c4f21ce4..8a1ed0512 100644 --- a/web/controllers/organization_membership_controller.ex +++ b/web/controllers/organization_membership_controller.ex @@ -2,7 +2,7 @@ defmodule CodeCorps.OrganizationMembershipController do use CodeCorps.Web, :controller use JaResource - import CodeCorps.FilterHelpers, only: [id_filter: 2, organization_filter: 2, role_filter: 2] + import CodeCorps.Helpers.Query, only: [id_filter: 2, organization_filter: 2, role_filter: 2] alias CodeCorps.OrganizationMembership diff --git a/web/controllers/project_category_controller.ex b/web/controllers/project_category_controller.ex index 806fd445f..62dfe2360 100644 --- a/web/controllers/project_category_controller.ex +++ b/web/controllers/project_category_controller.ex @@ -4,7 +4,7 @@ defmodule CodeCorps.ProjectCategoryController do alias CodeCorps.ProjectCategory - import CodeCorps.FilterHelpers, only: [id_filter: 2] + import CodeCorps.Helpers.Query, only: [id_filter: 2] plug :load_resource, model: ProjectCategory, only: [:show], preload: [:project, :category] plug :load_and_authorize_changeset, model: ProjectCategory, only: [:create] diff --git a/web/controllers/project_skill_controller.ex b/web/controllers/project_skill_controller.ex index 6f11d3f6b..75343ea69 100644 --- a/web/controllers/project_skill_controller.ex +++ b/web/controllers/project_skill_controller.ex @@ -2,7 +2,7 @@ defmodule CodeCorps.ProjectSkillController do use CodeCorps.Web, :controller use JaResource - import CodeCorps.FilterHelpers, only: [id_filter: 2] + import CodeCorps.Helpers.Query, only: [id_filter: 2] alias CodeCorps.ProjectSkill diff --git a/web/controllers/role_skill_controller.ex b/web/controllers/role_skill_controller.ex index 039da7eac..222533a0a 100644 --- a/web/controllers/role_skill_controller.ex +++ b/web/controllers/role_skill_controller.ex @@ -5,7 +5,7 @@ defmodule CodeCorps.RoleSkillController do alias CodeCorps.RoleSkill alias JaSerializer.Params - import CodeCorps.FilterHelpers, only: [id_filter: 2] + import CodeCorps.Helpers.Query, only: [id_filter: 2] plug :load_and_authorize_resource, model: RoleSkill, only: [:create, :delete] plug :scrub_params, "data" when action in [:create] diff --git a/web/controllers/user_category_controller.ex b/web/controllers/user_category_controller.ex index 8b4b785fe..80fe347fb 100644 --- a/web/controllers/user_category_controller.ex +++ b/web/controllers/user_category_controller.ex @@ -2,7 +2,7 @@ defmodule CodeCorps.UserCategoryController do use CodeCorps.Web, :controller use JaResource - import CodeCorps.FilterHelpers, only: [id_filter: 2] + import CodeCorps.Helpers.Query, only: [id_filter: 2] alias CodeCorps.UserCategory diff --git a/web/controllers/user_controller.ex b/web/controllers/user_controller.ex index 3a381e9c8..fa65947c3 100644 --- a/web/controllers/user_controller.ex +++ b/web/controllers/user_controller.ex @@ -3,7 +3,7 @@ defmodule CodeCorps.UserController do use JaResource - import CodeCorps.FilterHelpers, only: [id_filter: 2] + import CodeCorps.Helpers.Query, only: [id_filter: 2] alias CodeCorps.User alias JaSerializer.Params diff --git a/web/controllers/user_role_controller.ex b/web/controllers/user_role_controller.ex index c24f7a5dd..901988b6e 100644 --- a/web/controllers/user_role_controller.ex +++ b/web/controllers/user_role_controller.ex @@ -2,7 +2,7 @@ defmodule CodeCorps.UserRoleController do use CodeCorps.Web, :controller use JaResource - import CodeCorps.FilterHelpers, only: [id_filter: 2] + import CodeCorps.Helpers.Query, only: [id_filter: 2] alias CodeCorps.UserRole diff --git a/web/controllers/user_skill_controller.ex b/web/controllers/user_skill_controller.ex index 13314a0f5..5c51625c6 100644 --- a/web/controllers/user_skill_controller.ex +++ b/web/controllers/user_skill_controller.ex @@ -2,7 +2,7 @@ defmodule CodeCorps.UserSkillController do use CodeCorps.Web, :controller use JaResource - import CodeCorps.FilterHelpers, only: [id_filter: 2] + import CodeCorps.Helpers.Query, only: [id_filter: 2] alias CodeCorps.UserSkill diff --git a/web/models/category.ex b/web/models/category.ex index 937ebc7bf..3330dc682 100644 --- a/web/models/category.ex +++ b/web/models/category.ex @@ -4,7 +4,8 @@ defmodule CodeCorps.Category do """ use CodeCorps.Web, :model - import CodeCorps.ModelHelpers + + import CodeCorps.Helpers.Slug schema "categories" do field :name, :string diff --git a/web/models/model_helpers.ex b/web/models/model_helpers.ex index c14cce638..902a74266 100644 --- a/web/models/model_helpers.ex +++ b/web/models/model_helpers.ex @@ -1,19 +1,7 @@ defmodule CodeCorps.ModelHelpers do use CodeCorps.Web, :model - import CodeCorps.StringHelpers, only: [coalesce_id_string: 1, coalesce_string: 1] - - def generate_slug(changeset, value_key, slug_key) do - case changeset do - %Ecto.Changeset{valid?: true, changes: changes} -> - case Map.fetch(changes, value_key) do - {:ok, value} -> put_change(changeset, slug_key, Inflex.parameterize(value)) - _ -> changeset - end - _ -> - changeset - end - end + import CodeCorps.Helpers.String, only: [coalesce_id_string: 1, coalesce_string: 1] # filters diff --git a/web/models/organization.ex b/web/models/organization.ex index 94ad8265b..1ff89c2fd 100644 --- a/web/models/organization.ex +++ b/web/models/organization.ex @@ -6,7 +6,7 @@ defmodule CodeCorps.Organization do use Arc.Ecto.Schema use CodeCorps.Web, :model import CodeCorps.Base64ImageUploader - import CodeCorps.ModelHelpers + import CodeCorps.Helpers.Slug import CodeCorps.Validators.SlugValidator alias CodeCorps.SluggedRoute diff --git a/web/models/project.ex b/web/models/project.ex index 7aece0696..8bf117d0a 100644 --- a/web/models/project.ex +++ b/web/models/project.ex @@ -6,7 +6,7 @@ defmodule CodeCorps.Project do use Arc.Ecto.Schema use CodeCorps.Web, :model import CodeCorps.Base64ImageUploader - import CodeCorps.ModelHelpers + import CodeCorps.Helpers.Slug import CodeCorps.Validators.SlugValidator alias CodeCorps.MarkdownRenderer diff --git a/web/models/slugged_route.ex b/web/models/slugged_route.ex index bf248305e..5466f9033 100644 --- a/web/models/slugged_route.ex +++ b/web/models/slugged_route.ex @@ -5,7 +5,8 @@ defmodule CodeCorps.SluggedRoute do """ use CodeCorps.Web, :model - import CodeCorps.ModelHelpers + + import CodeCorps.Helpers.Slug import CodeCorps.Validators.SlugValidator schema "slugged_routes" do