Skip to content

Commit

Permalink
Add end_color liquid filter
Browse files Browse the repository at this point in the history
It simplifies templates.

Note that the content of the cell has been inverted to get rid of the 'Ends'/'Ended' prefix. It's a small thing, but it complexifies things a lot !
  • Loading branch information
marcwrobel committed Jan 2, 2023
1 parent 824ce76 commit 860fb14
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
20 changes: 7 additions & 13 deletions _includes/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,16 @@
{%- elsif type == "timeago" %}
<td>{{ value | timeago }}</td>
{%- elsif type == "end-date" %}
{%- if value == true %}
<td class="bg-green-000">Yes</td>
<td class="{{ row[field] | end_color }}">
{%- if value == true %}}
Yes
{%- elsif value == false %}
<td class="bg-red-000">No</td>
No
{%- else %}
{%- assign row_date = row[field] | date: '%s' %}
{%- assign site_date = site.time | date: '%s' %}
{%- assign days_between = row_date | minus: site_date | divided_by: 86400 %}
{%- if days_between < 0 %}
<td class="bg-red-000">Ended {{ row[field] | timeago }} <div>({{ row[field] | date_to_string }})</div></td>
{%- elsif days_between < 120 %}
<td class="bg-yellow-200">Ends {{ row[field] | timeago }} <div>({{ row[field] | date_to_string }})</div></td>
{%- else %}
<td class="bg-green-000">Ends {{ row[field] | timeago }} <div>({{ row[field] | date_to_string }})</div></td>
{%- endif %}
{{ row[field] | date_to_string }}
<div>({{ row[field] | timeago }})</div>
{%- endif %}
</td>
{%- else %}
<td>{{ row[field] }}</td>
{%- endif %}
Expand Down
40 changes: 40 additions & 0 deletions _plugins/end-of-life-filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@ def remove_first_element(html, element)
def drop_zero_patch(input)
input.end_with?(".0") ? input[0, input.length - 2] : input
end

# Compute the number of days from now to the given date.
#
# Usage (assuming now is '2023-01-01'):
# {{ '2023-01-10' | days_from_now }} => 9
# {{ '2023-01-01' | days_from_now }} => 0
# {{ '2022-12-31' | days_from_now }} => -1
def days_from_now(from)
from_timestamp = Date.parse(from.to_s).to_time.to_i
to_timestamp = Date.today.to_time.to_i
return (from_timestamp - to_timestamp) / (60 * 60 * 24)
end

# Compute the color according to the given number of days until the end.
#
# Usage:
# {{ true | end_color }} => bg-green-000
# {{ false | end_color }} => bg-red-000
# {{ '2025-01-01' | days_until_end: '2023-01-01' | end_color }} => bg-green-000
# {{ '2023-01-02' | days_until_end: '2023-01-01' | end_color }} => bg-yellow-200
# {{ '2021-01-01' | days_until_end: '2023-01-01' | end_color }} => bg-red-000
# {{ '2025-01-01' | end_color }} => bg-green-000
def end_color(input)
if input == true
return 'bg-green-000'
elsif input == false
return 'bg-red-000'
elsif input.is_a? Integer
if input < 0
return 'bg-red-000'
elsif input < 120
return 'bg-yellow-200'
else
return 'bg-green-000'
end
else
# Assuming it's a date
return end_color(days_from_now(input))
end
end
end

Liquid::Template.register_filter(EndOfLifeFilter)

0 comments on commit 860fb14

Please sign in to comment.