Class: Reports::FillDates

Inherits:
Object
  • Object
show all
Defined in:
app/commands/reports/fill_dates.rb

Overview

::Reports::FillDates.call to populate the dw_dates table with date records.

Class Method Summary collapse

Class Method Details

.callObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/commands/reports/fill_dates.rb', line 6

def self.call
  start_date = Date.new(2015, 1, 1)
  end_date = Date.new(2030, 1, 1)

  records = []

  (start_date..end_date).each do |date|
    year = date.year
    month = date.month

    month_abbreviation = date.strftime("%b")
    month_name = date.strftime("%B")
    day = date.day
    day_of_year = date.yday
    weekday_name = date.strftime("%A")
    calendar_week = date.cweek
    formatted_date = date.strftime("%m/%d/%Y")
    quarter = ((month - 1) / 3) + 1
    year_quarter = "#{year}_Q#{quarter}"
    year_month = "#{year}_#{month_name}"
    weekend = [6, 7].include?(date.cwday) # ISO: 6=Sat, 7=Sun
    holiday = %w[0101 0704 1225 1226].include?(date.strftime("%m%d"))

    week_start_date = date - (date.cwday - 1)
    week_end_date = date + (7 - date.cwday)
    month_start_date = date.beginning_of_month
    month_end_date = date.end_of_month

    records << {
      id: date,
      year: year,
      month: month,
      month_abbreviation: month_abbreviation,
      month_name: month_name,
      day: day,
      day_of_year: day_of_year,
      weekday_name: weekday_name,
      calendar_week: calendar_week,
      formatted_date: formatted_date,
      quarter: quarter,
      year_quarter: year_quarter,
      year_month: year_month,
      weekend: weekend,
      holiday: holiday,
      week_start_date: week_start_date,
      week_end_date: week_end_date,
      month_start_date: month_start_date,
      month_end_date: month_end_date,
      created_at: Time.current,
      updated_at: Time.current
    }
  end

  # rubocop:disable Rails/SkipsModelValidations
  DwDate.insert_all!(records)
  # rubocop:enable Rails/SkipsModelValidations
  puts "✅ Inserted #{records.size} records into dw_dates"
end