Module: Attio::Concerns::TimeFilterable
- Defined in:
- lib/attio/concerns/time_filterable.rb
Overview
Provides time-based filtering methods for any model Include this module to add time filtering capabilities
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#age_in_days ⇒ Integer
Get the age of the record in days.
-
#created_in?(period) ⇒ Boolean
Check if this record was created in a specific period.
-
#new?(days = 7) ⇒ Boolean
Check if record is new (created recently).
-
#old?(days = 365) ⇒ Boolean
Check if record is old.
-
#updated_in?(period) ⇒ Boolean
Check if this record was updated in a specific period.
Instance Method Details
#age_in_days ⇒ Integer
Get the age of the record in days
130 131 132 133 134 |
# File 'lib/attio/concerns/time_filterable.rb', line 130 def age_in_days return nil unless respond_to?(:created_at) && created_at created = created_at.is_a?(String) ? Time.parse(created_at) : created_at ((Time.now - created) / (24 * 60 * 60)).round end |
#created_in?(period) ⇒ Boolean
Check if this record was created in a specific period
113 114 115 116 117 |
# File 'lib/attio/concerns/time_filterable.rb', line 113 def created_in?(period) return false unless respond_to?(:created_at) && created_at date = created_at.is_a?(String) ? Time.parse(created_at) : created_at period.includes?(date) end |
#new?(days = 7) ⇒ Boolean
Check if record is new (created recently)
139 140 141 142 |
# File 'lib/attio/concerns/time_filterable.rb', line 139 def new?(days = 7) age = age_in_days age && age <= days end |
#old?(days = 365) ⇒ Boolean
Check if record is old
147 148 149 150 |
# File 'lib/attio/concerns/time_filterable.rb', line 147 def old?(days = 365) age = age_in_days age && age > days end |
#updated_in?(period) ⇒ Boolean
Check if this record was updated in a specific period
122 123 124 125 126 |
# File 'lib/attio/concerns/time_filterable.rb', line 122 def updated_in?(period) return false unless respond_to?(:updated_at) && updated_at date = updated_at.is_a?(String) ? Time.parse(updated_at) : updated_at period.includes?(date) end |