Module: TemporalScopes::HasTemporalScopes::ClassMethods
- Defined in:
- lib/temporal_scopes/has_temporal_scopes.rb
Overview
The following class methods and scopes are added to ActiveRecord::Base classes that have been called has_temporal_scopes on.
Instance Method Summary collapse
-
#now ⇒ ActiveRecord::Relation
Filters for only current objects.
-
#past ⇒ ActiveRecord::Relation
Filters for only past objects.
-
#with_past ⇒ ActiveRecord::Relation
Removes the filters such that past and present objects are returned.
-
#without_temporal_condition ⇒ ActiveRecord::Relation
Removes temporal conditions from the query.
Instance Method Details
#now ⇒ ActiveRecord::Relation
Filters for only current objects.
This is the default scope.
74 75 76 77 78 |
# 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 ⇒ ActiveRecord::Relation
Filters for only past objects.
87 88 89 |
# File 'lib/temporal_scopes/has_temporal_scopes.rb', line 87 def past without_temporal_condition.where('valid_to < ?', Time.zone.now) end |
#with_past ⇒ ActiveRecord::Relation
Removes the filters such that past and present objects are returned.
96 97 98 |
# File 'lib/temporal_scopes/has_temporal_scopes.rb', line 96 def with_past without_temporal_condition end |
#without_temporal_condition ⇒ ActiveRecord::Relation
Removes temporal conditions from the query.
60 61 62 63 64 65 66 |
# 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 |