Skip to content

Accept a module attribute as the private: option of a localized route - #26

Merged
kipcole9 merged 2 commits into
elixir-localize:mainfrom
rubas:fix/route-attr-options
Sep 27, 2026
Merged

kipcole9 merged 2 commits into
elixir-localize:mainfrom
rubas:fix/route-attr-options

Conversation

@rubas

@rubas rubas commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Before

A localized route whose private: option is a module attribute:

@page_private %{section: :docs}

localize [:en, :fr] do
  get "/#{locale}/sections/:page", PageController, :show, private: @page_private
end

The router does not compile:

** (FunctionClauseError) no function clause matching in Localize.Routes.put_value/3
    # 1
    {:@, [line: 79, column: 70], [{:page_private, [line: 79, column: 71], nil}]}
    # 2
    :localize_locale
    lib/localize/routes.ex:701: Localize.Routes.put_value/3

Problem

  • localize/1 adds the locale to the private: (or assigns:) option, but only when that option is a literal map.
  • A module attribute, or any other expression, reaches the macro as AST, and no clause of put_value/3 matches it.

After

The same route compiles. A request to /fr/sections/1 has both values in conn.private:

%{section: :docs, localize_locale: :fr}
Details

Cause. Localize.Routes.put_value/3 has a clause for nil and one for a literal map ({:%{}, meta, key_values}), nothing else.

Fix. A third clause wraps any other expression in Map.put/3, so the map is built at runtime, when the attribute has its value. The two existing clauses stay, so a literal map still merges at compile time.

Tests. test/support/router.ex gets a route with private: @page_private; test/routes_test.exs checks that /fr/sections/1 has section: :docs and the locale fr in conn.private. Without the fix the test router does not compile.

Checks. mix test (268 passed), mix format --check-formatted, mix compile --warnings-as-errors, mix credo --strict (one existing finding, unchanged).


AI disclosure: AI models helped us find this issue, write the change, and review it.

@kipcole9
kipcole9 merged commit 867b165 into elixir-localize:main Sep 27, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants