Browse files

core: revert m_category:id_to_name/2.

Added doc explaining the use for id_to_name/2 with a name as input (thanks Arjan).
  • Loading branch information...
1 parent 6ae5cc5 commit 98a4f9184efea014b711583a886cdad9310a6dbc @kaos kaos committed Nov 13, 2012
Showing with 9 additions and 1 deletion.
  1. +9 −1 src/models/m_category.erl
View
10 src/models/m_category.erl
@@ -224,9 +224,17 @@ name_to_id_check(Name, Context) ->
{ok, Id} = name_to_id(Name, Context),
Id.
+%% @doc Return the name for a given category.
+%%
+%% If the category does not have a unique name will result in undefined.
+%% If the lookup is made by name, the name is checked for existence,
+%% and if not found, results in undefined.
+-spec id_to_name(Id::integer(), #context{}) -> atom() | undefined;
+ (Name, #context{}) -> atom() | undefined when Name :: atom() | binary() | list().
id_to_name(Name, Context) when is_atom(Name); is_binary(Name); is_list(Name) ->
F = fun() ->
- z_convert:to_atom(Name)
+ Nm = z_db:q1("select r.name from rsc r join category c on r.id = c.id where r.name = $1", [Name], Context),
+ z_convert:to_atom(Nm)
end,
z_depcache:memo(F, {category_id_to_name, Name}, ?WEEK, [category], Context);
id_to_name(Id, Context) when is_integer(Id) ->

1 comment on commit 98a4f91

@kaos
Zotonic member

Help!

I couldn't get edoc to properly show the spec for this function (that is, to include both versions in the docs). It only shows the first spec, so I opted for the "standard" variant, leaving the alternative in the desc only... :/

Please sign in to comment.