Module: Attio::Concerns::TimeFilterable::ClassMethods
- Defined in:
- lib/attio/concerns/time_filterable.rb
Instance Method Summary collapse
-
#activity_metrics(period, **opts) ⇒ Hash
Get activity metrics for a period.
-
#created_in_month(year, month, **opts) ⇒ Array
Get records created in a specific month.
-
#created_in_quarter(year, quarter, **opts) ⇒ Array
Get records created in a specific quarter.
-
#created_in_year(year, **opts) ⇒ Array
Get records created in a specific year.
-
#created_this_month(**opts) ⇒ Array
Get records created this month.
-
#created_this_year(**opts) ⇒ Array
Get records created this year.
-
#created_year_to_date(**opts) ⇒ Array
Get records created year to date.
-
#in_period(period, date_field: :created_at, **opts) ⇒ Array
Filter records by a time period for a specific date field.
-
#recently_created(days = 7, **opts) ⇒ Array
Get records created in the last N days.
-
#recently_updated(days = 7, **opts) ⇒ Array
Get records updated in the last N days.
Instance Method Details
#activity_metrics(period, **opts) ⇒ Hash
Get activity metrics for a period
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/attio/concerns/time_filterable.rb', line 95 def activity_metrics(period, **opts) created = in_period(period, date_field: :created_at, **opts) updated = in_period(period, date_field: :updated_at, **opts) { period: period.label, created_count: created.size, updated_count: updated.size, total_activity: (created + updated).uniq.size } end |
#created_in_month(year, month, **opts) ⇒ Array
Get records created in a specific month
73 74 75 |
# File 'lib/attio/concerns/time_filterable.rb', line 73 def created_in_month(year, month, **opts) in_period(Util::TimePeriod.month(year, month), date_field: :created_at, **opts) end |
#created_in_quarter(year, quarter, **opts) ⇒ Array
Get records created in a specific quarter
81 82 83 |
# File 'lib/attio/concerns/time_filterable.rb', line 81 def created_in_quarter(year, quarter, **opts) in_period(Util::TimePeriod.quarter(year, quarter), date_field: :created_at, **opts) end |
#created_in_year(year, **opts) ⇒ Array
Get records created in a specific year
88 89 90 |
# File 'lib/attio/concerns/time_filterable.rb', line 88 def created_in_year(year, **opts) in_period(Util::TimePeriod.year(year), date_field: :created_at, **opts) end |
#created_this_month(**opts) ⇒ Array
Get records created this month
59 60 61 |
# File 'lib/attio/concerns/time_filterable.rb', line 59 def created_this_month(**opts) in_period(Util::TimePeriod.current_month, date_field: :created_at, **opts) end |
#created_this_year(**opts) ⇒ Array
Get records created this year
53 54 55 |
# File 'lib/attio/concerns/time_filterable.rb', line 53 def created_this_year(**opts) in_period(Util::TimePeriod.current_year, date_field: :created_at, **opts) end |
#created_year_to_date(**opts) ⇒ Array
Get records created year to date
65 66 67 |
# File 'lib/attio/concerns/time_filterable.rb', line 65 def created_year_to_date(**opts) in_period(Util::TimePeriod.year_to_date, date_field: :created_at, **opts) end |
#in_period(period, date_field: :created_at, **opts) ⇒ Array
Filter records by a time period for a specific date field
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/attio/concerns/time_filterable.rb', line 19 def in_period(period, date_field: :created_at, **opts) all(**opts).select do |record| # Try accessor method first, then bracket notation date_value = if record.respond_to?(date_field) record.send(date_field) else record[date_field] end if date_value parsed_date = date_value.is_a?(String) ? Time.parse(date_value) : date_value period.includes?(parsed_date) else false end end end |
#recently_created(days = 7, **opts) ⇒ Array
Get records created in the last N days
40 41 42 |
# File 'lib/attio/concerns/time_filterable.rb', line 40 def recently_created(days = 7, **opts) in_period(Util::TimePeriod.last_days(days), date_field: :created_at, **opts) end |
#recently_updated(days = 7, **opts) ⇒ Array
Get records updated in the last N days
47 48 49 |
# File 'lib/attio/concerns/time_filterable.rb', line 47 def recently_updated(days = 7, **opts) in_period(Util::TimePeriod.last_days(days), date_field: :updated_at, **opts) end |