aboutsummaryrefslogtreecommitdiff
path: root/app/models/client.rb
blob: 2bcf670ab51e883bbe475c83954b5c86e755caa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Client < ApplicationRecord
  has_many :visits
  has_many :household_members

  accepts_nested_attributes_for :household_members, allow_destroy: true, reject_if: :all_blank

  def last_visit
    v = visits.last
    return '-' if v.nil?

    v.created_at.to_date.to_fs(:long)
  end

  def visit_history
    vs = visits
          .includes(:user)
          .order(created_at: :desc)

    Hash[
      vs.group_by { |visit| visit.created_at.year }.map{ |y, items|
        [y, items.group_by { |v| v.created_at.strftime('%B') } ]
      }
    ]
  end

  def name
    [first_name, last_name].join(' ')
  end
end