Skip to content

Commit 0db61da

Browse files
committed
fix: use the same timestamps in new/0 function
1 parent 59aa841 commit 0db61da

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

lib/mongo/collection.ex

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,16 @@ defmodule Mongo.Collection do
545545
|> Enum.map(fn {name, opts} -> {name, opts[:default]} end)
546546
|> Enum.filter(fn {_name, fun} -> is_function(fun) end)
547547

548-
def new() do
549-
%__MODULE__{unquote_splicing(Collection.struct_args(args))}
548+
case @timestamps != [] do
549+
true ->
550+
def new() do
551+
new_timestamps(%__MODULE__{unquote_splicing(Collection.struct_args(args))})
552+
end
553+
554+
false ->
555+
def new() do
556+
%__MODULE__{unquote_splicing(Collection.struct_args(args))}
557+
end
550558
end
551559
end
552560

@@ -669,6 +677,17 @@ defmodule Mongo.Collection do
669677
updated_at = @timestamps[:updated_at]
670678
Collection.timestamps(struct, updated_at, @attributes[updated_at])
671679
end
680+
681+
def new_timestamps(struct) do
682+
inserted_at = @timestamps[:inserted_at]
683+
opts = @attributes[inserted_at]
684+
ts = opts[:default].()
685+
updated_at = @timestamps[:updated_at]
686+
687+
struct
688+
|> Map.put(inserted_at, ts)
689+
|> Map.put(updated_at, ts)
690+
end
672691
end
673692

674693
quote do

mix.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ defmodule Mongodb.Mixfile do
22
use Mix.Project
33

44
@source_url "https://github.com/zookzook/elixir-mongodb-driver"
5+
@version "0.9.2"
56

67
def project() do
78
[
89
app: :mongodb_driver,
9-
version: "0.9.2",
10+
version: @version,
1011
elixirc_paths: elixirc_paths(Mix.env()),
1112
elixir: "~> 1.8",
1213
name: "mongodb-driver",
@@ -53,7 +54,7 @@ defmodule Mongodb.Mixfile do
5354
],
5455
main: "readme",
5556
source_url: @source_url,
56-
source_ref: "#{project()[:version]}",
57+
source_ref: @version,
5758
formatters: ["html"]
5859
]
5960
end

test/collections/simple_test.exs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defmodule Collections.SimpleTest do
3838
collection "cards" do
3939
attribute :title, String.t(), default: "new title"
4040
embeds_one(:label, Label, default: &Label.new/0)
41-
timestamps(inserted_at: :created, updated_at: :modified)
41+
timestamps(inserted_at: :created, updated_at: :modified, default: &Card.ts/0)
4242
end
4343

4444
def insert_one(%Card{} = card, top) do
@@ -53,6 +53,22 @@ defmodule Collections.SimpleTest do
5353
|> Mongo.find_one(@collection, %{@id => id})
5454
|> load()
5555
end
56+
57+
def ts() do
58+
Process.sleep(100)
59+
DateTime.utc_now()
60+
end
61+
end
62+
63+
test "timestamps", _c do
64+
alias Collections.SimpleTest.Card
65+
alias Collections.SimpleTest.Label
66+
67+
new_card = Card.new()
68+
map_card = Card.dump(new_card)
69+
70+
ts = Map.get(new_card, :created)
71+
assert %{created: ^ts, modified: ^ts} = map_card
5672
end
5773

5874
test "load and dump", _c do

0 commit comments

Comments
 (0)