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

Configurable DateTime types #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

narrowtux
Copy link

This PR adds the possibility to configure the DateTime module used to generate utc_now() datetimes and the types of all datetime fields in the user schema.

config.exs

config :coherence,  
  datetime_module: DateTime

user.ex

#...
  schema "users" do
    coherence_schema(datetime: :utc_datetime)
  end

@norbajunior
Copy link

hey @narrowtux thanks for your PR. I'll take a look as soon as possible.

@norbajunior norbajunior self-assigned this Jun 18, 2019
@caironoleto
Copy link

@narrowtux thanks you for your PR. We'll take a look ASAP.

@caironoleto
Copy link

@narrowtux also, I think it's missing some tests, especially related to Schema.

@@ -254,7 +254,8 @@ defmodule Coherence.Schema do

Returns a changeset ready for Repo.update
"""
def lock(user, locked_at \\ NaiveDateTime.utc_now()) do
def lock(user, locked_at \\ nil) do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use Config.datetime_module().utc_now() as a default param?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that default parameters are decided at compile time - then all locked users would have the same locked_at date.

Copy link
Author

@narrowtux narrowtux Jun 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I got this wrong - this suggests that the expression is only evaluated when needed: https://elixir-lang.org/getting-started/modules-and-functions.html#default-arguments

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, look:

defmodule MarsRover do
  def time(foo \\ NaiveDateTime.utc_now()) do
    foo
  end
end

iex [1] > MarsRover.time
~N[2019-06-20 13:58:51.149503]
iex [2] > MarsRover.time
~N[2019-06-20 13:58:52.923541]
iex [3] >

Again, thank you! <3

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.

3 participants