Skip to content

Commit

Permalink
Sort data by date when building graphs
Browse files Browse the repository at this point in the history
This doesn't affect the end result because all collections used the same
order, but it makes debugging easier.
  • Loading branch information
javierm committed May 8, 2024
1 parent d74d900 commit bab98f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/ahoy/data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
10 changes: 10 additions & 0 deletions spec/models/ahoy/data_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bab98f9

Please sign in to comment.