Skip to content

Commit

Permalink
Refactor: Forecast#forecast_for -> Forecast#date
Browse files Browse the repository at this point in the history
This is much clearer, `#forecast_for` was a poor initial
choice!
  • Loading branch information
edavey committed Oct 7, 2024
1 parent 5b146a5 commit 8f057f6
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/lib/forecast_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.build(forecast_representation)
.map do |forecast|
Forecast.new({
obtained_at: obtained_at,
forecast_for: Date.parse(forecast.fetch("forecast_date")),
date: Date.parse(forecast.fetch("forecast_date")),

zone: ForecastZone.new(
id: zone.fetch("zone_id"),
Expand Down
2 changes: 1 addition & 1 deletion app/models/air_quality_alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(forecast)
}

def date
@forecast.forecast_for
@forecast.date
end

def daqi_level
Expand Down
6 changes: 3 additions & 3 deletions app/models/forecast.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Forecast
attr_reader :obtained_at, :forecast_for, :zone, :air_pollution, :uv, :pollen, :temperature
attr_reader :obtained_at, :date, :zone, :air_pollution, :uv, :pollen, :temperature

def initialize(attrs)
@obtained_at = attrs.fetch(:obtained_at)
@forecast_for = attrs.fetch(:forecast_for)
@date = attrs.fetch(:date)
@zone = attrs.fetch(:zone)
@air_pollution = attrs.fetch(:air_pollution)
@uv = attrs.fetch(:uv)
Expand All @@ -25,7 +25,7 @@ def air_quality_alert
def inspect
attr_values = [
"@obtained_at=#{obtained_at}",
"@forecast_for=#{forecast_for}",
"@date=#{date}",
"@zone=#{zone.inspect}",
"@air_pollution=#{air_pollution.inspect}",
"@uv=#{uv.inspect}",
Expand Down
10 changes: 5 additions & 5 deletions app/views/forecasts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
head.with_row do |row|
row.with_cell(text: '')
@forecasts.map { |forecast|
row.with_cell(text: forecast.forecast_for.to_formatted_s(:short))
row.with_cell(text: forecast.date.to_formatted_s(:short))
}

end; end; table.with_body do |body|
Expand All @@ -40,7 +40,7 @@
@forecasts.map { | forecast|
row.with_cell(
text: forecast.air_pollution.daqi_label,
html_attributes: { "data-date" => forecast.forecast_for}
html_attributes: { "data-date" => forecast.date}
)
}
end;
Expand All @@ -49,7 +49,7 @@
@forecasts.map { | forecast|
row.with_cell(
text: "#{forecast.uv.daqi_label} - #{forecast.uv.guidance}",
html_attributes: { "data-date" => forecast.forecast_for}
html_attributes: { "data-date" => forecast.date}
)
}
end;
Expand All @@ -58,7 +58,7 @@
@forecasts.map { | forecast|
row.with_cell(
text: [forecast.pollen.daqi_label, forecast.pollen.guidance].join(" - "),
html_attributes: { "data-date" => forecast.forecast_for}
html_attributes: { "data-date" => forecast.date}
)
}
end;
Expand All @@ -67,7 +67,7 @@
@forecasts.map { | forecast|
row.with_cell(
text: "#{forecast.temperature.min.round}-#{forecast.temperature.max.round}°C",
html_attributes: { "data-date" => forecast.forecast_for}
html_attributes: { "data-date" => forecast.date}
)
}
end;
Expand Down
6 changes: 3 additions & 3 deletions spec/factories/forecasts.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# #<Forecast
# @obtained_at=2024-10-02 15:38:00 +0100
# @forecast_for=2024-10-02
# @date=2024-10-02
# @zone=#<ForecastZone
# @id=29
# @name=Southwark
Expand All @@ -22,7 +22,7 @@
FactoryBot.define do
factory :forecast do
obtained_at { Time.current }
forecast_for { Date.tomorrow }
date { Date.tomorrow }
zone { FactoryBot.build(:forecast_zone) }
air_pollution { FactoryBot.build(:air_pollution_prediction) }
uv { FactoryBot.build(:uv_prediction) }
Expand All @@ -32,7 +32,7 @@
initialize_with {
new(
obtained_at: obtained_at,
forecast_for: forecast_for,
date: date,
zone: zone,
air_pollution: air_pollution,
uv: uv,
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/forecast_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

aggregate_failures do
expect(forecast.obtained_at).to eq(Time.zone.parse("02-10-2024 15:38"))
expect(forecast.forecast_for).to eq(Date.parse("2024-10-02"))
expect(forecast.date).to eq(Date.parse("2024-10-02"))

expect(forecast.zone.name).to eq("Southwark")
expect(forecast.zone.id).to eq(29)
Expand Down
2 changes: 1 addition & 1 deletion spec/models/air_quality_alert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
let(:forecast) do
FactoryBot.build(
:forecast,
forecast_for: Date.tomorrow,
date: Date.tomorrow,
air_pollution: FactoryBot.build(:air_pollution_prediction, :high)
)
end
Expand Down

0 comments on commit 8f057f6

Please sign in to comment.