From bab98f9163e29ae8747e637f3bb24a9bc4d4cf82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 8 May 2024 21:47:37 +0200 Subject: [PATCH] Sort data by date when building graphs This doesn't affect the end result because all collections used the same order, but it makes debugging easier. --- app/models/ahoy/data_source.rb | 2 +- spec/models/ahoy/data_source_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/models/ahoy/data_source.rb b/app/models/ahoy/data_source.rb index 1041c159bba2..6ca294365af1 100644 --- a/app/models/ahoy/data_source.rb +++ b/app/models/ahoy/data_source.rb @@ -18,7 +18,7 @@ def add(name, collection) def build data = { x: [] } - dates.each do |date| + dates.sort.each do |date| # Add the key with a valid date format data[:x].push date.strftime("%Y-%m-%d") diff --git a/spec/models/ahoy/data_source_spec.rb b/spec/models/ahoy/data_source_spec.rb index 2766d13911d5..185ba6a11aef 100644 --- a/spec/models/ahoy/data_source_spec.rb +++ b/spec/models/ahoy/data_source_spec.rb @@ -25,5 +25,15 @@ "foo" => [2, 1, 0], "bar" => [1, 0, 2] end + + it "returns data ordered by dates" do + ds = Ahoy::DataSource.new + ds.add "foo", { january_third => 2, january_second => 1 } + ds.add "bar", { january_first => 2, january_second => 1 } + + expect(ds.build).to eq :x => ["2015-01-01", "2015-01-02", "2015-01-03"], + "foo" => [0, 1, 2], + "bar" => [2, 1, 0] + end end end