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 376c0fa commit caa3651
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/code_corps/helpers/slug.ex
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.StringHelpers do
defmodule CodeCorps.Helpers.String do
def coalesce_id_string(string) do
string
|> String.split(",")
Expand Down
3 changes: 1 addition & 2 deletions web/controllers/organization_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion web/controllers/organization_membership_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion web/controllers/project_category_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion web/controllers/project_skill_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion web/controllers/role_skill_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion web/controllers/user_category_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion web/controllers/user_role_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion web/controllers/user_skill_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion web/models/category.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 1 addition & 13 deletions web/models/model_helpers.ex
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion web/models/organization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion web/models/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion web/models/slugged_route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit caa3651

Please sign in to comment.