module TemporalScopes::HasTemporalScopes::ClassMethods
The following class methods and scopes are added to `ActiveRecord::Base` classes that have been called `has_temporal_scopes` on.
Public Instance Methods
now()
click to toggle source
Filters for only current objects.
This is the default scope.
@return [ActiveRecord::Relation] only current objects.
# File lib/temporal_scopes/has_temporal_scopes.rb, line 74 def now without_temporal_condition .where(arel_table[:valid_from].eq(nil).or(arel_table[:valid_from].lteq(Time.zone.now))) .where(arel_table[:valid_to].eq(nil).or(arel_table[:valid_to].gteq(Time.zone.now))) end
past()
click to toggle source
Filters for only past objects.
@return [ActiveRecord::Relation] only past objects.
@example Getting only archived articles by an author.
author.articles.past
# File lib/temporal_scopes/has_temporal_scopes.rb, line 87 def past without_temporal_condition.where('valid_to < ?', Time.zone.now) end
with_past()
click to toggle source
Removes the filters such that past and present objects are returned.
@return [ActiveRecord::Relation] past and current objects.
# File lib/temporal_scopes/has_temporal_scopes.rb, line 96 def with_past without_temporal_condition end
without_temporal_condition()
click to toggle source
Removes temporal conditions from the query.
@example
Article.where(valid_to: 10.days.ago..1.day.ago) # => [] (due to default scope.) Article.without_temporal_condition.where(valid_to: 10.days.ago..1.day.ago) # returns the desired articles.
@return [ActiveRecord::Relation] the relation without temporal conditions on `valid_from` and `valid_to`.
# File lib/temporal_scopes/has_temporal_scopes.rb, line 60 def without_temporal_condition relation = rewhere(valid_from: nil, valid_to: nil) relation.where_values.delete_if { |query| query.to_sql.include?("\"valid_from\"") || query.to_sql.include?("\"valid_to\"") } relation end