Skip to content

Commit

Permalink
fix: Allow users to customize the singular name of a resource for whe…
Browse files Browse the repository at this point in the history
…n naming is hard (#169)
  • Loading branch information
staylorwr authored Mar 31, 2023
1 parent c53119b commit d1075bb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 1 addition & 6 deletions lib/ex_teal/resource.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule ExTeal.Resource do
@type params :: map()

alias Phoenix.Naming

@moduledoc """
When used, includes all aspects of the functionality required to
manage the resource.
Expand Down Expand Up @@ -114,12 +112,9 @@ defmodule ExTeal.Resource do
end

def to_json(resource, conn) do
singular =
resource.title() |> Inflex.underscore() |> Naming.humanize() |> Inflex.singularize()

%{
title: resource.title(),
singular: singular,
singular: resource.singular_title(),
group: resource.nav_group(conn),
uri: resource.uri(),
hidden: resource.hide_from_nav(),
Expand Down
11 changes: 11 additions & 0 deletions lib/ex_teal/resource/model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ defmodule ExTeal.Resource.Model do
"""
@callback title() :: String.t()

@doc """
Returns the singularized version of the title to display on forms.
"""
@callback singular_title() :: String.t()

@doc """
Returns the uri to display in the side bar.
Expand Down Expand Up @@ -76,13 +81,18 @@ defmodule ExTeal.Resource.Model do
@behaviour ExTeal.Resource.Model

alias ExTeal.Resource.Model
alias Phoenix.Naming

@inferred_model Model.model_from_resource(__MODULE__)
@inferred_title Model.title_from_resource(__MODULE__)
@inferred_uri Model.uri_from_resource(__MODULE__)

def model, do: @inferred_model
def title, do: @inferred_title

def singular_title,
do: title() |> Inflex.underscore() |> Naming.humanize() |> Inflex.singularize()

def uri, do: @inferred_uri
def title_for_schema(schema), do: Model.title_for_schema_from_struct(schema)
def subtitle_for_schema(schema), do: nil
Expand All @@ -94,6 +104,7 @@ defmodule ExTeal.Resource.Model do

defoverridable model: 0,
title: 0,
singular_title: 0,
uri: 0,
title_for_schema: 1,
subtitle_for_schema: 1,
Expand Down
4 changes: 3 additions & 1 deletion test/ex_teal/resource_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defmodule ExTeal.ResourceTest do
use ExTeal.Resource

def nav_group(_), do: "Admin"

def singular_title, do: "Foo"
end

defmodule ExTealTest.TestResource do
Expand Down Expand Up @@ -54,7 +56,7 @@ defmodule ExTeal.ResourceTest do
},
%{
title: "Groups",
singular: "Group",
singular: "Foo",
uri: "groups",
group: "Admin",
hidden: false,
Expand Down

0 comments on commit d1075bb

Please sign in to comment.