Class: RailsuiCharts::Filters

Inherits:
Object
  • Object
show all
Defined in:
lib/railsui_charts/filters.rb

Overview

Resolves request params into the window every chart on a page shares.

Filters belong above the charts, not inside them: one row scopes the whole view so every chart re-renders against the same slice. A per-card date picker invites two cards to disagree about what "this week" means.

filters = RailsuiCharts::Filters.new(params)
filters.range           # => Mon, 06 Aug..Mon, 12 Aug
filters.interval        # => :day
filters.previous_range  # => the preceding 7 days, when comparing

Defined Under Namespace

Classes: Preset

Constant Summary collapse

PRESETS =
[
  Preset.new(key: "24h", label: "Last 24 hours", length: 24, interval: :hour, unit: :hour),
  Preset.new(key: "7d", label: "Last 7 days", length: 7, interval: :day, unit: :day),
  Preset.new(key: "30d", label: "Last 30 days", length: 30, interval: :day, unit: :day),
  Preset.new(key: "90d", label: "Last 90 days", length: 13, interval: :week, unit: :week),
  Preset.new(key: "12m", label: "Last 12 months", length: 12, interval: :month, unit: :month),
  Preset.new(key: "mtd", label: "Month to date", length: nil, interval: :day, unit: :day)
].freeze
DEFAULT_PRESET =
"7d"
INTERVAL_LABELS =
{ hour: "Hourly", day: "Daily", week: "Weekly", month: "Monthly" }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, today: nil) ⇒ Filters

Returns a new instance of Filters.



32
33
34
35
36
37
38
39
40
# File 'lib/railsui_charts/filters.rb', line 32

def initialize(params = {}, today: nil)
  params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params.to_h
  params = params.transform_keys(&:to_s)

  @today = today || current_date
  @preset = find_preset(params["range"])
  @interval = coerce_interval(params["interval"])
  @compare = compare_param?(params["compare"])
end

Instance Attribute Details

#presetObject (readonly)

Returns the value of attribute preset.



30
31
32
# File 'lib/railsui_charts/filters.rb', line 30

def preset
  @preset
end

Instance Method Details

#compare?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/railsui_charts/filters.rb', line 50

def compare?
  @compare
end

#intervalObject



46
47
48
# File 'lib/railsui_charts/filters.rb', line 46

def interval
  @interval || @preset.interval
end

#interval_labelObject



79
80
81
# File 'lib/railsui_charts/filters.rb', line 79

def interval_label
  INTERVAL_LABELS.fetch(interval)
end

#intervalsObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/railsui_charts/filters.rb', line 64

def intervals
  available = case @preset.unit
              when :hour then %i[hour day]
              when :month then %i[month]
              when :week then %i[week month]
              else %i[day week month]
              end

  available.map { |key| [INTERVAL_LABELS.fetch(key), key.to_s] }
end

#labelObject



75
76
77
# File 'lib/railsui_charts/filters.rb', line 75

def label
  @preset.label
end

#presetsObject



60
61
62
# File 'lib/railsui_charts/filters.rb', line 60

def presets
  PRESETS
end

#previous_rangeObject



54
55
56
57
58
# File 'lib/railsui_charts/filters.rb', line 54

def previous_range
  return unless compare?

  Interval.preceding(range, interval: interval)
end

#rangeObject



42
43
44
# File 'lib/railsui_charts/filters.rb', line 42

def range
  @range ||= @preset.key == "mtd" ? (@today.beginning_of_month..@today) : trailing_range
end

#summaryObject



83
84
85
86
87
# File 'lib/railsui_charts/filters.rb', line 83

def summary
  parts = [label, interval_label]
  parts << "Compared to previous period" if compare?
  parts
end

#to_paramsObject



89
90
91
# File 'lib/railsui_charts/filters.rb', line 89

def to_params
  { range: @preset.key, interval: interval.to_s, compare: compare? ? "1" : "0" }
end