Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Custom Attribute Name for created_at #871

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/ash_authentication/token_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ defmodule AshAuthentication.TokenResource do
The Ash domain to use to access this resource.
"""
],
created_at_attribute_name: [
type: :atom,
doc: "The name of the `created_at` attribute on this resource.",
default: :created_at
],
expunge_expired_action_name: [
type: :atom,
doc: """
Expand Down
6 changes: 4 additions & 2 deletions lib/ash_authentication/token_resource/transformer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ defmodule AshAuthentication.TokenResource.Transformer do
writable?: true,
public?: true
),
:ok <- validate_extra_data_field(dsl_state),
{:ok, created_at} <-
TokenResource.Info.token_created_at_attribute_name(dsl_state),
{:ok, dsl_state} <-
maybe_build_attribute(dsl_state, :created_at, :utc_datetime_usec,
maybe_build_attribute(dsl_state, created_at, :utc_datetime_usec,
allow_nil?: false,
public?: false,
default: &DateTime.utc_now/0
Expand All @@ -80,7 +83,6 @@ defmodule AshAuthentication.TokenResource.Transformer do
default: &DateTime.utc_now/0,
update_default: &DateTime.utc_now/0
),
:ok <- validate_extra_data_field(dsl_state),
{:ok, expunge_expired_action_name} <- Info.token_expunge_expired_action_name(dsl_state),
{:ok, dsl_state} <-
maybe_build_action(
Expand Down
6 changes: 6 additions & 0 deletions test/ash_authentication/token_resource_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ defmodule AshAuthentication.TokenResourceTest do
end
end

test "uses custom create timestamp" do
attribute = Ash.Resource.Info.attribute(Example.TokenWithCustomCreateTimestamp, :created_at)

assert :inserted_at = attribute.source
end

def build_token do
{:ok, token, claims} =
build_user()
Expand Down
1 change: 1 addition & 0 deletions test/support/example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Example do
resource Example.User
resource Example.UserWithTokenRequired
resource Example.Token
resource Example.TokenWithCustomCreateTimestamp
resource Example.UserIdentity
resource Example.UserWithRegisterMagicLink
end
Expand Down
10 changes: 10 additions & 0 deletions test/support/example/token_with_custom_create_timestamp.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Example.TokenWithCustomCreateTimestamp do
@moduledoc false
use Ash.Resource,
extensions: [AshAuthentication.TokenResource],
domain: Example

attributes do
create_timestamp :inserted_at
end
end