Skip to content

Commit 9b2f0dc

Browse files
committed
fix sorting events in rom integration
1 parent 476e709 commit 9b2f0dc

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

contrib/ruby_event_store-rom/lib/ruby_event_store/rom/relations/events.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,36 @@ def by_event_type(types)
3434
where(event_type: types)
3535
end
3636

37-
def newer_than(time)
38-
where { |r| r.events[:created_at] > time.localtime }
37+
def newer_than(time, time_sort_by)
38+
if time_sort_by == :as_of
39+
where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) > time.localtime }
40+
else
41+
where { |r| r.events[:created_at] > time.localtime }
42+
end
3943
end
4044

41-
def newer_than_or_equal(time)
42-
where { |r| r.events[:created_at] >= time.localtime }
45+
def newer_than_or_equal(time, time_sort_by)
46+
if time_sort_by == :as_of
47+
where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) >= time.localtime }
48+
else
49+
where { |r| r.events[:created_at] >= time.localtime }
50+
end
4351
end
4452

45-
def older_than(time)
46-
where { |r| r.events[:created_at] < time.localtime }
53+
def older_than(time, time_sort_by)
54+
if time_sort_by == :as_of
55+
where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) < time.localtime }
56+
else
57+
where { |r| r.events[:created_at] < time.localtime }
58+
end
4759
end
4860

49-
def older_than_or_equal(time)
50-
where { |r| r.events[:created_at] <= time.localtime }
61+
def older_than_or_equal(time, time_sort_by)
62+
if time_sort_by == :as_of
63+
where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) <= time.localtime }
64+
else
65+
where { |r| r.events[:created_at] <= time.localtime }
66+
end
5167
end
5268

5369
DIRECTION_MAP = { forward: %i[asc > <], backward: %i[desc < >] }.freeze

contrib/ruby_event_store-rom/lib/ruby_event_store/rom/relations/stream_entries.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,36 @@ def max_position(stream)
3434
by_stream(stream).select(:position).order(Sequel.desc(:position)).first
3535
end
3636

37-
def newer_than(time)
38-
join_events.where { |r| r.events[:created_at] > time.localtime }
37+
def newer_than(time, time_sort_by)
38+
if time_sort_by == :as_of
39+
join_events.where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) > time.localtime }
40+
else
41+
join_events.where { |r| r.events[:created_at] > time.localtime }
42+
end
3943
end
4044

41-
def newer_than_or_equal(time)
42-
join_events.where { |r| r.events[:created_at] >= time.localtime }
45+
def newer_than_or_equal(time, time_sort_by)
46+
if time_sort_by == :as_of
47+
join_events.where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) >= time.localtime }
48+
else
49+
join_events.where { |r| r.events[:created_at] >= time.localtime }
50+
end
4351
end
4452

45-
def older_than(time)
46-
join_events.where { |r| r.events[:created_at] < time.localtime }
53+
def older_than(time, time_sort_by)
54+
if time_sort_by == :as_of
55+
join_events.where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) < time.localtime }
56+
else
57+
join_events.where { |r| r.events[:created_at] < time.localtime }
58+
end
4759
end
4860

49-
def older_than_or_equal(time)
50-
join_events.where { |r| r.events[:created_at] <= time.localtime }
61+
def older_than_or_equal(time, time_sort_by)
62+
if time_sort_by == :as_of
63+
join_events.where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) <= time.localtime }
64+
else
65+
join_events.where { |r| r.events[:created_at] <= time.localtime }
66+
end
5167
end
5268

5369
DIRECTION_MAP = { forward: %i[asc > <], backward: %i[desc < >] }.freeze

contrib/ruby_event_store-rom/lib/ruby_event_store/rom/repositories/events.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ def read_scope(specification)
110110

111111
query = query.by_event_id(specification.with_ids) if specification.with_ids
112112
query = query.by_event_type(specification.with_types) if specification.with_types?
113-
query = query.older_than(specification.older_than) if specification.older_than
114-
query = query.older_than_or_equal(specification.older_than_or_equal) if specification.older_than_or_equal
115-
query = query.newer_than(specification.newer_than) if specification.newer_than
116-
query = query.newer_than_or_equal(specification.newer_than_or_equal) if specification.newer_than_or_equal
113+
query = query.older_than(specification.older_than, specification.time_sort_by) if specification.older_than
114+
query = query.older_than_or_equal(specification.older_than_or_equal, specification.time_sort_by) if specification.older_than_or_equal
115+
query = query.newer_than(specification.newer_than, specification.time_sort_by) if specification.newer_than
116+
query = query.newer_than_or_equal(specification.newer_than_or_equal, specification.time_sort_by) if specification.newer_than_or_equal
117117
query
118118
end
119119

0 commit comments

Comments
 (0)