Skip to content

Commit

Permalink
Add a model for the statistics block
Browse files Browse the repository at this point in the history
It's unclear exactly how the data from the CSV files will be used to
display the graphs, so for the moment the unmodified rows are just being
returned.
  • Loading branch information
leenagupte committed Oct 9, 2024
1 parent 5994ccf commit 6bf9e3d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/models/block/statistics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "csv"

module Block
class Statistics < Block::Base
STATISTICS_DATA_PATH = "lib/data/landing_page_content_items/statistics".freeze

def title
data["title"]
end

def x_axis_label
data["x_axis_label"]
end

def y_axis_label
data["y_axis_label"]
end

def row_header
CSV.read(csv_file_path, headers: true).headers
end

def rows
CSV.read(csv_file_path, headers: true).map(&:to_h)
end

def data_source_link
data["data_source_link"]
end

private

def csv_file_path
@csv_file_path ||= Rails.root.join("#{STATISTICS_DATA_PATH}/#{data['csv_file']}")
end
end
end
60 changes: 60 additions & 0 deletions spec/models/block/statistics_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
RSpec.describe Block::Statistics do
let(:blocks_hash) do
{
"type" => "statistics",
"title" => "Chart to visually represent data",
"x_axis_label" => "X Axis",
"y_axis_label" => "Y Axis",
"csv_file" => "data_one.csv",
"data_source_link_text" => "Data source",
"data_source_link" => "https://www.example.com",
}
end

before do
stub_const("Block::ChartLayout::STATISTICS_DATA_PATH", "spec/fixtures/landing_page_statistics_data")
end

describe "statistics content" do
describe "#title" do
it "gets statistics title" do
expect(described_class.new(blocks_hash).title).to eq("Chart to visually represent data")
end
end

describe "#x_axis_label" do
it "gets the label for the x-axix" do
expect(described_class.new(blocks_hash).x_axis_label).to eq("X Axis")
end
end

describe "#y_axis_label" do
it "gets the label for the y-axis" do
expect(described_class.new(blocks_hash).y_axis_label).to eq("Y Axis")
end
end
end

describe "chart data" do
describe "#rows" do
it "returns the row data for the chart" do
expected_rows = [
{ "Date" => "2024-01-01", "value" => "10", "variable" => "variable_name" },
{ "Date" => "2024-02-01", "value" => "11", "variable" => "variable_name" },
{ "Date" => "2024-03-01", "value" => "12", "variable" => "variable_name" },
{ "Date" => "2024-04-01", "value" => "13", "variable" => "variable_name" },
{ "Date" => "2024-05-01", "value" => "14", "variable" => "variable_name" },
{ "Date" => "2024-06-01", "value" => "15", "variable" => "variable_name" },
]

expect(described_class.new(blocks_hash).rows).to eq(expected_rows)
end
end

describe "#data_source_link" do
it "returns the link to the external data source" do
expect(described_class.new(blocks_hash).data_source_link).to eq("https://www.example.com")
end
end
end
end

0 comments on commit 6bf9e3d

Please sign in to comment.