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.
72 73 74 75 76 |
# File 'lib/temporal_scopes/has_temporal_scopes.rb', line 72 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.
85 86 87 |
# File 'lib/temporal_scopes/has_temporal_scopes.rb', line 85 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.
94 95 96 |
# File 'lib/temporal_scopes/has_temporal_scopes.rb', line 94 def with_past without_temporal_condition end |
#without_temporal_condition ⇒ ActiveRecord::Relation
Removes temporal conditions from the query.
60 61 62 63 64 |
# File 'lib/temporal_scopes/has_temporal_scopes.rb', line 60 def without_temporal_condition relation = unscope(where: [:valid_from, :valid_to]) relation.where_values.delete_if { |query| query.to_sql.include?("\"valid_from\"") || query.to_sql.include?("\"valid_to\"") } if relation && relation.where_values relation end |