Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date range filter #37

Merged
merged 15 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/controllers/lab_tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ def index
@recordables = policy_scope(LabTest)
.select(:recordable_id, :created_at)
.where(user_id: @chosen_user_id)
.in_date_range(params[:start_date], params[:end_date])
.order(:created_at)
.group(:recordable_id, :created_at)

@biomarkers = policy_scope(Biomarker)
.includes(:reference_ranges, :lab_tests)
.where(lab_tests: { user_id: @chosen_user_id })
Expand Down Expand Up @@ -42,7 +44,7 @@ def create

ActiveRecord::Base.transaction do
@health_record = HealthRecord.new(
user: set_user,
user: lab_test_user,
notes: lab_test_params[:notes]
)

Expand Down Expand Up @@ -115,7 +117,7 @@ def save_health_record_with_lab_test
@health_record.save && @lab_test.save
end

def set_user
def lab_test_user
if current_user.full_access_roles_can? && lab_test_params[:user_id].present?
User.find(lab_test_params[:user_id])
else
Expand All @@ -124,7 +126,8 @@ def set_user
end

def build_lab_test
@lab_test = current_user.lab_tests.build(lab_test_params)
user = lab_test_user
@lab_test = user.lab_tests.build(lab_test_params)
end

# Only allow a list of trusted parameters through.
Expand Down
16 changes: 16 additions & 0 deletions app/models/lab_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ class LabTest < ApplicationRecord
message: :must_be_nonnegative_and_numeric
}

scope :in_date_range, lambda { |start_date, end_date|
scope = all
scope = scope.where(created_at: Date.parse(start_date).beginning_of_day..) if start_date.present?
scope = scope.where(created_at: ..Date.parse(end_date).end_of_day) if end_date.present?
scope
}

scope :with_date, lambda { |date|
where(created_at: date.all_day)
}

# Single ordering scope with consistent secondary ordering
scope :order_by_date, lambda {
order(created_at: :asc, id: :asc)
}

class Status
NORMAL = :normal
HIGH = :high
Expand Down
25 changes: 25 additions & 0 deletions app/views/lab_tests/_date_filter.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="mb-6 flex justify-between items-center">
<%= form_with(url: lab_tests_path, method: :get, class: "inline-flex items-center") do |f| %>
<div class="inline-flex items-center">
<span class="mr-3 text-sm font-medium"><%= t('lab_tests.lab_test_date_filter.from') %></span>
<%= f.date_field :start_date,
value: params[:start_date] || Rails.application.config.x.lab_test_date_range[:default_start_date].call,
min: Rails.application.config.x.lab_test_date_range[:min_date],
max: Rails.application.config.x.lab_test_date_range[:max_date],
onchange: "this.nextElementSibling.nextElementSibling.min=this.value",
class: "rounded-md border border-gray-300 py-2 px-3 mr-6",
placeholder: "mm/dd/yyyy" %>

<span class="mr-3 text-sm font-medium"><%= t('lab_tests.lab_test_date_filter.to') %></span>
<%= f.date_field :end_date,
value: params[:end_date] || Rails.application.config.x.lab_test_date_range[:default_end_date].call,
min: params[:start_date] || Rails.application.config.x.lab_test_date_range[:min_date],
max: Rails.application.config.x.lab_test_date_range[:max_date],
class: "rounded-md border border-gray-300 py-2 px-3 mr-6",
placeholder: "mm/dd/yyyy" %>

<%= f.submit t('lab_tests.lab_test_date_filter.filter'),
class: "primary-button" %>
</div>
<% end %>
</div>
42 changes: 42 additions & 0 deletions app/views/lab_tests/_no_data_placeholder.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<div class="flex flex-col items-center justify-center w-full p-4">
<div class="flex justify-center w-full">
<svg fill="#000" height="50px" width="50px" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 512.001 512.001" xml:space="preserve">
<g id="SVGRepo_bgCarrier" stroke-width="0"></g>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
<g id="SVGRepo_iconCarrier">
<g>
<g>
<path d="M363.801,0.001H54.872C24.615,0.001,0,24.616,0,54.874v402.254c0,30.256,24.615,54.872,54.872,54.872h55.403 c5.073,0,9.186-4.113,9.186-9.186c0-5.073-4.113-9.186-9.186-9.186H54.872c-20.126,0-36.5-16.373-36.5-36.5V54.874 c0-20.126,16.373-36.501,36.5-36.501h308.929c5.074,0,9.186-4.113,9.186-9.186C372.987,4.114,368.876,0.001,363.801,0.001z"></path>
</g>
</g>
<g>
<g>
<path d="M457.128,0.002h-56.581c-5.074,0-9.186,4.113-9.186,9.186c0,5.073,4.111,9.185,9.185,9.185h56.581 c20.128,0,36.501,16.375,36.501,36.5v402.254c0,20.126-16.373,36.5-36.501,36.5H147.021c-5.073,0-9.186,4.113-9.186,9.186 c0,5.073,4.113,9.186,9.186,9.186h310.107c30.257,0,54.873-24.615,54.873-54.872V54.874 C512.001,24.617,487.385,0.002,457.128,0.002z"></path>
</g>
</g>
<g>
<g>
<path d="M320.286,256l53.998-53.998c3.587-3.588,3.587-9.404,0-12.99l-51.296-51.294c-3.586-3.588-9.403-3.588-12.99,0 l-53.998,53.996l-53.998-53.996c-3.588-3.588-9.404-3.588-12.99,0l-51.294,51.294c-3.588,3.588-3.588,9.404,0,12.99L191.714,256 l-53.998,53.998c-3.588,3.588-3.588,9.403,0,12.99l51.294,51.294c1.722,1.723,4.059,2.691,6.495,2.691 c2.436,0,4.773-0.968,6.495-2.691l53.998-53.998l53.998,53.998c1.722,1.723,4.059,2.691,6.495,2.691 c2.436,0,4.773-0.968,6.495-2.691l51.296-51.294c3.587-3.587,3.587-9.404,0-12.99L320.286,256z M316.492,354.796l-53.998-53.998 c-1.793-1.794-4.145-2.691-6.495-2.691c-2.35,0-4.702,0.897-6.495,2.691l-53.998,53.998l-38.304-38.304l53.998-53.996 c1.723-1.723,2.691-4.059,2.691-6.495c0-2.436-0.968-4.772-2.691-6.495l-53.998-53.998l38.304-38.304l53.998,53.998 c3.588,3.588,9.404,3.588,12.99,0l53.998-53.998l38.304,38.304l-53.998,53.998c-1.723,1.723-2.691,4.059-2.691,6.495 c0,2.436,0.968,4.772,2.691,6.495l53.998,53.996L316.492,354.796z"></path>
</g>
</g>
</g>
</svg>
</div>
<div class="flex flex-col items-center justify-center max-w-md mx-auto text-center p-4">
<h3 class="font-medium text-gray-900 mb-2 text-lg sm:text-xl">
<%= t('lab_tests.lab_test_no_data_placeholder.no_data_title') %>
</h3>

<% if params[:start_date].present? && params[:end_date].present? %>
<p class="text-gray-500 text-sm sm:text-base">
<%= t('lab_tests.lab_test_no_data_placeholder.no_data_message_with_dates',
start_date: Date.parse(params[:start_date]).strftime("%B %d, %Y"),
end_date: Date.parse(params[:end_date]).strftime("%B %d, %Y")) %>
</p>
<% else %>
<p class="text-gray-500 text-sm sm:text-base">
<%= t('lab_tests.lab_test_no_data_placeholder.no_data_message_no_dates') %>
</p>
<% end %>
</div>
</div>
10 changes: 8 additions & 2 deletions app/views/lab_tests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
data: { turbo_frame: "_top" }
%>
</header>

<%= render "index_table", recordables: @recordables, biomarkers: @biomarkers %>
<div class="tw-container tw-mx-auto">
<%= render 'date_filter' %>
</div>
<% if @recordables.present? %>
<%= render "index_table", recordables: @recordables, biomarkers: @biomarkers %>
<% else %>
<%= render 'no_data_placeholder' %>
<% end %>
</section>
8 changes: 8 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,13 @@ class Application < Rails::Application
# Fixes broken tailwind.css build for CI
# @see https://github.com/rails/tailwindcss-rails/issues/153#issuecomment-1225895063
config.assets.css_compressor = nil

# Lab test date range settings
config.x.lab_test_date_range = {
min_date: '1900-01-01',
max_date: '2100-01-01',
default_start_date: -> { Time.zone.today.beginning_of_year },
default_end_date: -> { Time.zone.today }
}
end
end
8 changes: 8 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ en:
success: "Lab test was successfully updated."
destroy:
success: "Lab test was successfully removed."
lab_test_date_filter:
from: "From"
to: "To"
filter: "Filter"
lab_test_no_data_placeholder:
no_data_title: "No Lab Tests Found"
no_data_message_with_dates: "No lab tests were found between %{start_date} and %{end_date}"
no_data_message_no_dates: "No lab tests have been recorded yet"
admin:
users:
update:
Expand Down
Loading