Skip to content

Commit

Permalink
Merge pull request #46 from dxw/refactor-prediction-partial
Browse files Browse the repository at this point in the history
Refactor temperature, UV and pollen predictions
  • Loading branch information
patrickjfl authored Oct 15, 2024
2 parents bb35110 + 014e6bd commit 8aeff51
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 40 deletions.
4 changes: 2 additions & 2 deletions app/components/prediction_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<div class="prediction py-4 <%= name_for_class %> <%= daqi_level_for_class %> border-b border-gray-400">
<div class="summary flex flex-row">
<dt class="name w-1/2">
<%= @prediction.name %>
<%= name_for_label %>
</dt>
<dd class="flex flex-row w-1/2">
<div class=" -mt-1 daqi-indicator <%= daqi_level_for_class %> <%= daqi_indicator_colour %> text-2xl"></div>
<div class=" daqi-label font-bold ml-1 w-4/5"><%= @prediction.daqi_label %></div>
<div class=" daqi-label font-bold ml-1 w-4/5"><%= daqi_level_for_label %></div>
<div class="control"><%= render partial: "shared/icons/chevron_down" %></div>
</dd>
</div>
Expand Down
8 changes: 8 additions & 0 deletions app/components/prediction_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ def name_for_class
@prediction.name.parameterize
end

def name_for_label
@prediction.name
end

def daqi_level_for_class
@prediction.daqi_level.to_s.parameterize
end

def daqi_level_for_label
@prediction.daqi_label
end

def daqi_indicator_colour
case @prediction.daqi_level
when :low
Expand Down
4 changes: 4 additions & 0 deletions app/models/pollen_prediction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def initialize(value)
@value = value
end

def name
"Pollen"
end

def guidance
I18n.t("prediction.guidance.pollen.#{daqi_level}")
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/temperature_prediction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def initialize(min:, max:)
@max = max
end

def name
"Temperature"
end

# :nocov:
def inspect
"#<#{self.class.name} @min=#{min} @max=#{max}>"
Expand Down
4 changes: 4 additions & 0 deletions app/models/uv_prediction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def initialize(value)
@value = value
end

def name
"Ultravoilet rays (UV)"
end

def guidance
I18n.t("prediction.guidance.uv.#{daqi_level}")
end
Expand Down
41 changes: 3 additions & 38 deletions app/views/styled_forecasts/_predictions.html.erb
Original file line number Diff line number Diff line change
@@ -1,40 +1,5 @@
<dl class="predictions p-4">
<div class="prediction py-4 uv border-b border-gray-400">
<div class="summary flex flex-row">
<dt class="w-1/2">Ultravoilet rays (UV)</dd>
<dd class="flex flex-row w-1/2">
<div class="daqi-indicator -mt-1 text-green-600 text-2xl"></div>
<div class="daqi-label font-bold ml-1 w-4/5">Low</div>
<div class="control"><%= render partial: "shared/icons/chevron_down" %></div>
</dd>
</div>
<div class="details hidden">
</div>
</div>
<div class="prediction py-4 pollen border-b border-gray-400">
<div class="summary flex flex-row">
<dt class="w-1/2">Pollen</dd>
<dd class="flex flex-row w-1/2">
<div class=" -mt-1 daqi-indicator text-amber-300 text-2xl"></div>
<div class=" daqi-label font-bold ml-1 w-4/5">Moderate</div>
<div class="control"><%= render partial: "shared/icons/chevron_down" %></div>
</dd>
</div>
<div class="details visible bg-amber-50 p-4 mt-2">
<%= render partial: "details" %>
</div>
</div>
<div class="prediction py-4 temperature border-b border-gray-400">
<div class="summary flex flex-row">
<dt class="w-1/2">Temperature</dd>
<dd class="flex flex-row w-1/2">
<div class="daqi-indicator -mt-1 text-green-600 text-2xl"></div>
<div class="daqi-label font-bold ml-1 w-4/5">Low</div>
<div class="control"><%= render partial: "shared/icons/chevron_down" %></div>
</dd>
</div>
<div class="details hidden">
</div>
</div>
</div>
<%= render(PredictionComponent.new(prediction: @forecasts.first.uv)) %>
<%= render(PredictionComponent.new(prediction: @forecasts.first.pollen)) %>
<%= render "temperature_prediction", prediction: @forecasts.first.temperature %>
</dl>
9 changes: 9 additions & 0 deletions app/views/styled_forecasts/_temperature_prediction.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="prediction py-4 temperature border-b border-gray-400">
<div class="summary flex flex-row">
<dt class="w-1/2"> <%= prediction.name %> </dt>
<dd class="flex flex-row w-1/2">
<div class="font-bold"><%= "#{prediction.min.round}°C - #{prediction.max.round}" %>°C</div>
</dd>
</div>
</div>

18 changes: 18 additions & 0 deletions spec/feature_steps/forecast_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ def and_i_see_predicted_temperature_for_each_day
expect_prediction(day: :day_after_tomorrow, category: :temperature, value: "27-31°C")
end

def and_i_see_predicted_uv_level_v2
expect_prediction_v2(category: "ultravoilet-rays-uv", value: "Low")
end

def and_i_see_predicted_pollen_level_v2
expect_prediction_v2(category: :pollen, value: "Low")
end

def and_i_see_predicted_temperature_level_v2
expect_prediction_v2(category: :temperature, value: "-5°C - 4°C")
end

def expect_prediction(day:, category:, value:)
within(prediction_category(category)) do
within("td[data-date='#{date(day)}']") do
Expand All @@ -96,6 +108,12 @@ def expect_prediction(day:, category:, value:)
end
end

def expect_prediction_v2(category:, value:)
within(".#{category}") do
find("dd", text: value)
end
end

def expect_air_pollution_prediction(day:, value:)
within("div[data-date='#{date(day)}']") do
expect(page).to have_content(content_for_air_pollution(value))
Expand Down
3 changes: 3 additions & 0 deletions spec/features/visitors/view_styled_forecasts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
when_i_select_view_forecasts_v2
then_i_see_the_forecasts_page_v2
and_i_see_predicted_air_pollution_status_for_each_day_v2
and_i_see_predicted_uv_level_v2
and_i_see_predicted_pollen_level_v2
and_i_see_predicted_temperature_level_v2
end
end

0 comments on commit 8aeff51

Please sign in to comment.