File tree Expand file tree Collapse file tree 3 files changed +41
-5
lines changed Expand file tree Collapse file tree 3 files changed +41
-5
lines changed Original file line number Diff line number Diff line change @@ -545,8 +545,16 @@ defmodule Mongo.Collection do
545
545
|> Enum . map ( fn { name , opts } -> { name , opts [ :default ] } end )
546
546
|> Enum . filter ( fn { _name , fun } -> is_function ( fun ) end )
547
547
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
550
558
end
551
559
end
552
560
@@ -669,6 +677,17 @@ defmodule Mongo.Collection do
669
677
updated_at = @ timestamps [ :updated_at ]
670
678
Collection . timestamps ( struct , updated_at , @ attributes [ updated_at ] )
671
679
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
672
691
end
673
692
674
693
quote do
Original file line number Diff line number Diff line change @@ -2,11 +2,12 @@ defmodule Mongodb.Mixfile do
2
2
use Mix.Project
3
3
4
4
@ source_url "https://github.com/zookzook/elixir-mongodb-driver"
5
+ @ version "0.9.2"
5
6
6
7
def project ( ) do
7
8
[
8
9
app: :mongodb_driver ,
9
- version: "0.9.2" ,
10
+ version: @ version ,
10
11
elixirc_paths: elixirc_paths ( Mix . env ( ) ) ,
11
12
elixir: "~> 1.8" ,
12
13
name: "mongodb-driver" ,
@@ -53,7 +54,7 @@ defmodule Mongodb.Mixfile do
53
54
] ,
54
55
main: "readme" ,
55
56
source_url: @ source_url ,
56
- source_ref: " #{ project ( ) [ : version] } " ,
57
+ source_ref: @ version ,
57
58
formatters: [ "html" ]
58
59
]
59
60
end
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ defmodule Collections.SimpleTest do
38
38
collection "cards" do
39
39
attribute :title , String . t ( ) , default: "new title"
40
40
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 )
42
42
end
43
43
44
44
def insert_one ( % Card { } = card , top ) do
@@ -53,6 +53,22 @@ defmodule Collections.SimpleTest do
53
53
|> Mongo . find_one ( @ collection , % { @ id => id } )
54
54
|> load ( )
55
55
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
56
72
end
57
73
58
74
test "load and dump" , _c do
You can’t perform that action at this time.
0 commit comments