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 9 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
12 changes: 7 additions & 5 deletions app/controllers/lab_tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class LabTestsController < ApplicationController
# GET /lab_tests or /lab_tests.json
def index
authorize LabTest

@recordables = policy_scope(LabTest)
aksafan marked this conversation as resolved.
Show resolved Hide resolved
.select(:recordable_id, :created_at)
.where(user_id: @chosen_user_id)
.order(:created_at)
.group(:recordable_id, :created_at)
.in_date_range(params[:start_date], params[:end_date])
aksafan marked this conversation as resolved.
Show resolved Hide resolved
.latest_records
.ordered_by_date

@biomarkers = policy_scope(Biomarker)
.includes(:reference_ranges, :lab_tests)
.where(lab_tests: { user_id: @chosen_user_id })
Expand All @@ -33,12 +35,12 @@ def new

# GET /lab_tests/1/edit
def edit
authorize @lab_test
# authorize @lab_test
aksafan marked this conversation as resolved.
Show resolved Hide resolved
end

# POST /lab_tests or /lab_tests.json
def create
authorize @lab_test
# authorize @lab_test
aksafan marked this conversation as resolved.
Show resolved Hide resolved

ActiveRecord::Base.transaction do
@health_record = HealthRecord.new(
Expand Down
29 changes: 29 additions & 0 deletions app/models/lab_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ 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 :by_date, lambda { |date|
aksafan marked this conversation as resolved.
Show resolved Hide resolved
where(created_at: date.all_day)
}

# Show all records, ordered by newest first
scope :latest_records, lambda {
aksafan marked this conversation as resolved.
Show resolved Hide resolved
order(created_at: :asc)
}

# Changed to descending order to match the requirement
scope :ordered_by_date, lambda {
aksafan marked this conversation as resolved.
Show resolved Hide resolved
order(created_at: :asc, id: :asc)
}

# here code with group by tests by day (...DISTINCT ON)
aksafan marked this conversation as resolved.
Show resolved Hide resolved
# scope :latest_records, lambda {
# records = all.group_by { |record| record.created_at.to_date }
# .transform_values { |group| group.max_by(&:created_at) }
# .values
# where(id: records)
# }

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('.from') %></span>
<%= f.date_field :start_date,
value: params[:start_date] || Rails.application.config.x.dates[:default_start_date].call,
min: Rails.application.config.x.dates[:min_date],
max: Rails.application.config.x.dates[: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" %>
aksafan marked this conversation as resolved.
Show resolved Hide resolved

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

<%= f.submit t('.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">
<div class="flex flex-col items-center justify-center p-4">
<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">
<h3 class="text-lg font-medium text-gray-900 mb-1 relative z-10">
<%= t('.no_data_title') %>
aksafan marked this conversation as resolved.
Show resolved Hide resolved
</h3>

<% if params[:start_date].present? && params[:end_date].present? %>
<p class="text-gray-500 relative z-10">
<%= t('.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 relative z-10">
<%= t('.no_data_message_no_dates') %>
</p>
<% end %>
</div>
</div>
18 changes: 12 additions & 6 deletions app/views/lab_tests/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<section>
<header class="my-2 flex flex-center justify-between items-center">
<h1 class="text-center text-2xl my-3">Lab Tests</h1>
<%= link_to "New Lab Test",
<header class="my-2 flex flex-center justify-between items-center">
<h1 class="text-center text-2xl my-3">Lab Tests</h1>
<%= link_to "New Lab Test",
new_lab_test_path,
method: :get,
class: "create-button",
data: { turbo_frame: "_top" }
%>
</header>

<%= render "index_table", recordables: @recordables, biomarkers: @biomarkers %>
</header>
<div class="tw-container tw-mx-auto">
<%= render 'date_filter' %>
</div>
<% if @recordables.present? %>
<%= render "index_table", recordables: @recordables, biomarkers: @biomarkers %>
aksafan marked this conversation as resolved.
Show resolved Hide resolved
<% else %>
<%= render 'no_data_placeholder' %>
aksafan marked this conversation as resolved.
Show resolved Hide resolved
<% 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

# Date range settings for lab tests
config.x.dates = {
aksafan marked this conversation as resolved.
Show resolved Hide resolved
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."
date_filter:
from: "From"
to: "To"
filter: "Filter"
no_data_placeholder:
no_data_title: "No Lab Tests Found"
aksafan marked this conversation as resolved.
Show resolved Hide resolved
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